feat(cli+templateengine+deploymanager): resolve follow-ups #4/#5/#6/#8 — CLI ergonomics + structured deploy validation error
Closes the four remaining items in the 2026-06-24 template-inheritance/CLI follow-up tracker. #4 — CLI `instance set-bindings` can now set DataSourceReferenceOverride. `--bindings` accepts an optional 3rd element per entry: [attributeName, dataConnectionId, dataSourceReferenceOverride]. A string sets the override; a JSON null or an omitted 3rd element leaves it unset (template default). TryParseBindings accepts 2- or 3-element entries and rejects a non-string/non-null 3rd element or 4+ elements with a clean error. Previously the CLI sent the override as null and silently wiped any existing one (only a raw POST /management could set it). #5 — `template update` is partial, not full-replace (fixed server-side so all clients benefit). UpdateTemplateAsync now uses leave-unchanged semantics: a null description keeps the stored value (pass "" to clear); a null parentTemplateId keeps the existing parent. Parent stays immutable — a non-null differing value is still rejected — but omitting --parent-id is now a no-op instead of failing every derived-template update. #6 — compact `template list`/`get` table output + `--detail`. Table output is now id/name/description/parent/derived + member counts (#attrs/#alarms/ #scripts/#comps/#nativeAlarms) via TemplateTableProjection, fed through a new optional tableProjector seam on CommandHelpers. `--detail` restores the full dump. JSON output is left untouched (always full) so machine consumers are unaffected — the projector only runs on the table path. #8 — structured deploy-time validation error. New ValidationResult.SummarizeErrors() (Commons) returns a grouped, capped summary: leading total count, one line per ValidationCategory, and a per-module rollup (canonical name up to its last dot) with counts + "... and N more module(s)" caps. DeploymentService uses it for the "Pre-deployment validation failed" message and logs the full per-entry list via LogWarning. Replaces the flat semicolon-joined dump that became a wall of text for instances with 50-194 unbound attributes. Tests: +8 Commons (SummarizeErrors), +8 CLI (4 binding 3-element / 4 table projection), +2 net TemplateEngine (partial-update). Affected suites green: Commons 587, CLI 341, TemplateEngine 447, DeploymentManager 101, ManagementService 230, CentralUI 866; full solution builds 0/0. Docs: Component-DeploymentManager.md "Validation Error Reporting"; CLI README (set-bindings 3-element form, template update leave-unchanged, list/get --detail); UpdateTemplateCommand doc; known-issues tracker #4/#5/#6/#8 resolved (all 8 items now closed).
This commit is contained in:
@@ -1,9 +1,9 @@
|
|||||||
# Follow-up tracker — template-inheritance UI gaps + CLI/validation footguns (2026-06-24 session)
|
# Follow-up tracker — template-inheritance UI gaps + CLI/validation footguns (2026-06-24 session)
|
||||||
|
|
||||||
**Status:** PARTIALLY RESOLVED · **Found:** 2026-06-24 · **Context:** live ops session on `wonder-app-vd03` (CvdReactor / Z28061 / Z28061Sim) — renaming the template, adding the LeakTest module, and adding MoveInType to the MESReceiver children.
|
**Status:** RESOLVED · **Found:** 2026-06-24 · **Context:** live ops session on `wonder-app-vd03` (CvdReactor / Z28061 / Z28061Sim) — renaming the template, adding the LeakTest module, and adding MoveInType to the MESReceiver children.
|
||||||
**Components:** Central UI (#9), Template Engine (#1), CLI (#19), Configuration Database (#17)
|
**Components:** Central UI (#9), Template Engine (#1), CLI (#19), Configuration Database (#17), Deployment Manager (#2)
|
||||||
|
|
||||||
**Resolved:** #3 (collision detector) and #7 (sandbox compile surface) on branch `fix/followups-3-7`; #1 + #2 (inherited-member propagation & resync) on branch `fix/followups-1-2` (2026-06-24). Open: #4, #5, #6, #8.
|
**Resolved:** #3 (collision detector) and #7 (sandbox compile surface) on branch `fix/followups-3-7`; #1 + #2 (inherited-member propagation & resync) on branch `fix/followups-1-2`; #4 + #5 + #6 + #8 (CLI ergonomics + structured deploy validation error) on branch `fix/followups-4-5-6-8` (all 2026-06-24). All items resolved.
|
||||||
|
|
||||||
Issues are listed worst-first. Severities are author estimates. None caused data loss; the runtime/flattened config and deployed instances are correct.
|
Issues are listed worst-first. Severities are author estimates. None caused data loss; the runtime/flattened config and deployed instances are correct.
|
||||||
|
|
||||||
@@ -56,7 +56,17 @@ Issues are listed worst-first. Severities are author estimates. None caused data
|
|||||||
---
|
---
|
||||||
|
|
||||||
## 4. CLI `instance set-bindings` cannot set `DataSourceReferenceOverride`
|
## 4. CLI `instance set-bindings` cannot set `DataSourceReferenceOverride`
|
||||||
**Severity:** Medium · **Components:** CLI (#19)
|
**Severity:** Medium · **Components:** CLI (#19) · **✅ RESOLVED 2026-06-24 (branch `fix/followups-4-5-6-8`)**
|
||||||
|
|
||||||
|
**Fix:** `--bindings` now accepts an optional **third element** per entry —
|
||||||
|
`[attributeName, dataConnectionId, dataSourceReferenceOverride]` — so the CLI can set the
|
||||||
|
per-instance reference override that the wire contract (`ConnectionBinding`) already
|
||||||
|
carried. A string sets it; a JSON `null` or an omitted third element leaves it unset
|
||||||
|
(template default). `TryParseBindings` accepts 2- or 3-element entries and rejects a
|
||||||
|
non-string/non-null third element or 4+ elements with a clean validation error. The
|
||||||
|
`--bindings` help and CLI README now document the full-replace behaviour (omitting the
|
||||||
|
override on a re-bind clears any previously-set one). Covered by
|
||||||
|
`InstanceArgumentParsingTests` (three-element / explicit-null / wrong-type / four-element).
|
||||||
|
|
||||||
**Symptom:** `instance set-bindings --bindings` only accepts `[attributeName, dataConnectionId]` pairs (`InstanceCommands.cs` → `ConnectionBinding(name, connId)` 2-arg). The override is sent as `null`, and because `SetConnectionBindingsAsync` upserts `DataSourceReferenceOverride = b.DataSourceReferenceOverride` (`InstanceService.cs:340`), using the CLI on an attribute that already has an override would **wipe** it.
|
**Symptom:** `instance set-bindings --bindings` only accepts `[attributeName, dataConnectionId]` pairs (`InstanceCommands.cs` → `ConnectionBinding(name, connId)` 2-arg). The override is sent as `null`, and because `SetConnectionBindingsAsync` upserts `DataSourceReferenceOverride = b.DataSourceReferenceOverride` (`InstanceService.cs:340`), using the CLI on an attribute that already has an override would **wipe** it.
|
||||||
|
|
||||||
@@ -67,7 +77,19 @@ Issues are listed worst-first. Severities are author estimates. None caused data
|
|||||||
---
|
---
|
||||||
|
|
||||||
## 5. CLI `template update` is full-replace, not partial
|
## 5. CLI `template update` is full-replace, not partial
|
||||||
**Severity:** Low · **Components:** CLI (#19), Template Engine (#1)
|
**Severity:** Low · **Components:** CLI (#19), Template Engine (#1) · **✅ RESOLVED 2026-06-24 (branch `fix/followups-4-5-6-8`)**
|
||||||
|
|
||||||
|
**Fix:** `TemplateService.UpdateTemplateAsync` now uses **leave-unchanged** semantics for
|
||||||
|
optional fields (fixed server-side, so every client benefits): a `null` description keeps
|
||||||
|
the stored value (pass `""` to explicitly clear it), and a `null` `parentTemplateId` keeps
|
||||||
|
the existing parent. The parent remains immutable — a non-null value that differs from the
|
||||||
|
current parent is still rejected — but omitting it (the CLI default) is now a no-op instead
|
||||||
|
of tripping the immutability guard, which previously made `template update` fail on any
|
||||||
|
derived template unless `--parent-id` was re-passed. CLI `--description`/`--parent-id` help,
|
||||||
|
the `UpdateTemplateCommand` doc, and the CLI README document the semantics. Tests:
|
||||||
|
`UpdateTemplate_OmittedParentAndDescription_LeavesUnchanged`,
|
||||||
|
`UpdateTemplate_EmptyDescription_ClearsIt` (the prior `UpdateTemplate_ClearParent_Fails`
|
||||||
|
was repurposed, since a null parent now means leave-unchanged rather than clear-and-fail).
|
||||||
|
|
||||||
**Symptom:** omitting `--description` on `template update` overwrites the stored description to NULL (`TemplateService.cs:124-125` assigns Name+Description unconditionally). Renaming a template silently drops its description unless you re-pass it.
|
**Symptom:** omitting `--description` on `template update` overwrites the stored description to NULL (`TemplateService.cs:124-125` assigns Name+Description unconditionally). Renaming a template silently drops its description unless you re-pass it.
|
||||||
|
|
||||||
@@ -76,7 +98,16 @@ Issues are listed worst-first. Severities are author estimates. None caused data
|
|||||||
---
|
---
|
||||||
|
|
||||||
## 6. (Minor) CLI `template list`/`get` table output dumps every attribute
|
## 6. (Minor) CLI `template list`/`get` table output dumps every attribute
|
||||||
**Severity:** Low · **Components:** CLI (#19)
|
**Severity:** Low · **Components:** CLI (#19) · **✅ RESOLVED 2026-06-24 (branch `fix/followups-4-5-6-8`)**
|
||||||
|
|
||||||
|
**Fix:** `template list`/`get` **table** output is now a compact projection — id / name /
|
||||||
|
description / parentTemplateId / isDerived plus member **counts** (`#attrs`, `#alarms`,
|
||||||
|
`#scripts`, `#comps`, `#nativeAlarms`) — via a new `TemplateTableProjection.ProjectSummary`
|
||||||
|
fed through an optional `tableProjector` seam on `CommandHelpers.ExecuteCommandAsync`/
|
||||||
|
`HandleResponse`. A `--detail` flag restores the full table dump. **JSON output is
|
||||||
|
deliberately left untouched** (always the full payload) so machine consumers are unaffected
|
||||||
|
— the projector only runs on the table path. Covered by `TemplateTableProjectionTests`
|
||||||
|
(array/object projection, counts, non-JSON passthrough, size-shrink sanity check).
|
||||||
|
|
||||||
**Symptom:** `--format table template list` emitted ~171 KB (the full attribute set per template inline), unusable in a terminal. `--format json` is fine.
|
**Symptom:** `--format table template list` emitted ~171 KB (the full attribute set per template inline), unusable in a terminal. `--format json` is fine.
|
||||||
|
|
||||||
@@ -102,7 +133,17 @@ Issues are listed worst-first. Severities are author estimates. None caused data
|
|||||||
---
|
---
|
||||||
|
|
||||||
## 8. Deploy-time unbound-binding validation returns one giant semicolon-joined error string
|
## 8. Deploy-time unbound-binding validation returns one giant semicolon-joined error string
|
||||||
**Severity:** Low · **Components:** Template Engine (#1), Deployment Manager (#2)
|
**Severity:** Low · **Components:** Template Engine (#1), Deployment Manager (#2) · **✅ RESOLVED 2026-06-24 (branch `fix/followups-4-5-6-8`)**
|
||||||
|
|
||||||
|
**Fix:** new `ValidationResult.SummarizeErrors()` (Commons) returns a grouped, capped
|
||||||
|
summary: a leading total count, one line per `ValidationCategory`, and within a category a
|
||||||
|
per-**module** rollup (canonical name up to its last dot) with counts and a `… and N more
|
||||||
|
module(s)` cap. `DeploymentService` now uses it for the `Pre-deployment validation failed:`
|
||||||
|
message and logs the full per-entry list via `LogWarning` so nothing is lost. Entity-less
|
||||||
|
findings (e.g. script-compile errors) fall back to a capped message list. Documented in
|
||||||
|
`Component-DeploymentManager.md` → "Validation Error Reporting". Covered by
|
||||||
|
`ValidationResultSummaryTests` (count header, module rollup, breadth cap, root grouping,
|
||||||
|
message fallback, mixed categories).
|
||||||
|
|
||||||
**Symptom:** Deploying an instance whose data-sourced attributes aren't all bound fails with a single error that concatenates one clause per attribute: `Pre-deployment validation failed: Attribute 'LeftReactorSide.LeakTest.DeltaVac' has a data source reference but no connection binding; Attribute 'LeftReactorSide.LeakTest.ResultType' has …; …`. For 50–194 unbound attrs (e.g. Z28062's unbound LeakTest members) it's a wall of text that's hard to scan in a CLI/UI toast.
|
**Symptom:** Deploying an instance whose data-sourced attributes aren't all bound fails with a single error that concatenates one clause per attribute: `Pre-deployment validation failed: Attribute 'LeftReactorSide.LeakTest.DeltaVac' has a data source reference but no connection binding; Attribute 'LeftReactorSide.LeakTest.ResultType' has …; …`. For 50–194 unbound attrs (e.g. Z28062's unbound LeakTest members) it's a wall of text that's hard to scan in a CLI/UI toast.
|
||||||
|
|
||||||
|
|||||||
@@ -64,6 +64,19 @@ flowchart TD
|
|||||||
class step4 start
|
class step4 start
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Validation Error Reporting
|
||||||
|
|
||||||
|
When step 2 fails, the returned error is a **grouped, capped summary** rather than a flat
|
||||||
|
semicolon-joined dump (followup #8). `ValidationResult.SummarizeErrors()` (Commons) leads
|
||||||
|
with the total error count, then lists one line per `ValidationCategory`; within a
|
||||||
|
category, entity-scoped findings (notably the unbound connection-binding case, which can
|
||||||
|
produce 50–194 entries for a richly data-sourced instance) are rolled up by **module** —
|
||||||
|
the attribute's canonical name up to its last dot — with per-module counts, and the breadth
|
||||||
|
is capped with a `… and N more module(s)` suffix. The complete per-entry list remains on
|
||||||
|
`ValidationResult.Errors` and is written to the deploy log (`LogWarning`) so operators can
|
||||||
|
still see every clause when needed. This keeps the UI/CLI failure toast scannable while
|
||||||
|
preserving full detail for diagnosis.
|
||||||
|
|
||||||
## Deployment Identity & Idempotency
|
## Deployment Identity & Idempotency
|
||||||
|
|
||||||
- Every deployment is assigned a unique **deployment ID** and includes the flattened configuration's **revision hash** (from the Template Engine).
|
- Every deployment is assigned a unique **deployment ID** and includes the flattened configuration's **revision hash** (from the Template Engine).
|
||||||
|
|||||||
@@ -30,6 +30,13 @@ internal static class CommandHelpers
|
|||||||
/// (<see cref="IsAuthorizationFailure"/>) is preserved on the error path either way,
|
/// (<see cref="IsAuthorizationFailure"/>) is preserved on the error path either way,
|
||||||
/// closing CLI-017's regression.
|
/// closing CLI-017's regression.
|
||||||
/// </param>
|
/// </param>
|
||||||
|
/// <param name="tableProjector">
|
||||||
|
/// Optional transform applied to the success JSON body <em>only</em> when the resolved
|
||||||
|
/// format is <c>table</c>. Lets a command render a compact table projection (e.g.
|
||||||
|
/// <c>template list</c> dropping per-template attribute dumps, followup #6) while
|
||||||
|
/// leaving JSON output untouched for machine consumers. Ignored when
|
||||||
|
/// <paramref name="onSuccess"/> is supplied.
|
||||||
|
/// </param>
|
||||||
/// <returns>A task that resolves to the process exit code (0 = success, 1 = error, 2 = authorization failure).</returns>
|
/// <returns>A task that resolves to the process exit code (0 = success, 1 = error, 2 = authorization failure).</returns>
|
||||||
internal static async Task<int> ExecuteCommandAsync(
|
internal static async Task<int> ExecuteCommandAsync(
|
||||||
ParseResult result,
|
ParseResult result,
|
||||||
@@ -39,7 +46,8 @@ internal static class CommandHelpers
|
|||||||
Option<string> passwordOption,
|
Option<string> passwordOption,
|
||||||
object command,
|
object command,
|
||||||
TimeSpan? timeout = null,
|
TimeSpan? timeout = null,
|
||||||
Func<string, int>? onSuccess = null)
|
Func<string, int>? onSuccess = null,
|
||||||
|
Func<string, string>? tableProjector = null)
|
||||||
{
|
{
|
||||||
var config = CliConfig.Load();
|
var config = CliConfig.Load();
|
||||||
var format = ResolveFormat(result, formatOption, config);
|
var format = ResolveFormat(result, formatOption, config);
|
||||||
@@ -98,7 +106,7 @@ internal static class CommandHelpers
|
|||||||
return IsAuthorizationFailure(response) ? 2 : 1;
|
return IsAuthorizationFailure(response) ? 2 : 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return HandleResponse(response, format);
|
return HandleResponse(response, format, tableProjector);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -158,8 +166,12 @@ internal static class CommandHelpers
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="response">Response received from the management API.</param>
|
/// <param name="response">Response received from the management API.</param>
|
||||||
/// <param name="format">Output format (<c>json</c> or <c>table</c>).</param>
|
/// <param name="format">Output format (<c>json</c> or <c>table</c>).</param>
|
||||||
|
/// <param name="tableProjector">
|
||||||
|
/// Optional transform applied to the JSON body before table rendering only — JSON
|
||||||
|
/// output is never altered. See <see cref="ExecuteCommandAsync"/>.
|
||||||
|
/// </param>
|
||||||
/// <returns>The process exit code (0 = success, 1 = error, 2 = authorization failure).</returns>
|
/// <returns>The process exit code (0 = success, 1 = error, 2 = authorization failure).</returns>
|
||||||
internal static int HandleResponse(ManagementResponse response, string format)
|
internal static int HandleResponse(ManagementResponse response, string format, Func<string, string>? tableProjector = null)
|
||||||
{
|
{
|
||||||
if (response.JsonData != null)
|
if (response.JsonData != null)
|
||||||
{
|
{
|
||||||
@@ -173,7 +185,11 @@ internal static class CommandHelpers
|
|||||||
|
|
||||||
if (string.Equals(format, "table", StringComparison.OrdinalIgnoreCase))
|
if (string.Equals(format, "table", StringComparison.OrdinalIgnoreCase))
|
||||||
{
|
{
|
||||||
WriteAsTable(response.JsonData);
|
// A table projector compacts the body for terminal display (e.g. dropping
|
||||||
|
// per-template attribute dumps). JSON output stays full/untouched so
|
||||||
|
// machine consumers keep the complete payload.
|
||||||
|
var body = tableProjector != null ? tableProjector(response.JsonData) : response.JsonData;
|
||||||
|
WriteAsTable(body);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -54,7 +54,18 @@ public static class InstanceCommands
|
|||||||
private static Command BuildSetBindings(Option<string> urlOption, Option<string> formatOption, Option<string> usernameOption, Option<string> passwordOption)
|
private static Command BuildSetBindings(Option<string> urlOption, Option<string> formatOption, Option<string> usernameOption, Option<string> passwordOption)
|
||||||
{
|
{
|
||||||
var idOption = new Option<int>("--id") { Description = "Instance ID", Required = true };
|
var idOption = new Option<int>("--id") { Description = "Instance ID", Required = true };
|
||||||
var bindingsOption = new Option<string>("--bindings") { Description = "JSON array of [attributeName, dataConnectionId] pairs", Required = true };
|
var bindingsOption = new Option<string>("--bindings")
|
||||||
|
{
|
||||||
|
Description = "JSON array of binding entries. Each entry is either " +
|
||||||
|
"[attributeName, dataConnectionId] or " +
|
||||||
|
"[attributeName, dataConnectionId, dataSourceReferenceOverride] " +
|
||||||
|
"(the 3rd element overrides the attribute's data-source reference; " +
|
||||||
|
"pass null or omit it to use the template default). " +
|
||||||
|
"NOTE: this REPLACES all bindings for the instance — include the " +
|
||||||
|
"override on every entry that needs one, or omitting it clears any " +
|
||||||
|
"previously-set override.",
|
||||||
|
Required = true
|
||||||
|
};
|
||||||
|
|
||||||
var cmd = new Command("set-bindings") { Description = "Set data connection bindings for an instance" };
|
var cmd = new Command("set-bindings") { Description = "Set data connection bindings for an instance" };
|
||||||
cmd.Add(idOption);
|
cmd.Add(idOption);
|
||||||
@@ -76,11 +87,16 @@ public static class InstanceCommands
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Parses the <c>--bindings</c> argument — a JSON array of
|
/// Parses the <c>--bindings</c> argument — a JSON array of binding entries — into a
|
||||||
/// <c>[attributeName, dataConnectionId]</c> pairs — into a typed list.
|
/// typed list. Each entry is either a two-element
|
||||||
/// Returns <c>false</c> with a descriptive <paramref name="error"/> instead of
|
/// <c>[attributeName, dataConnectionId]</c> pair or a three-element
|
||||||
/// throwing when the JSON is malformed, a pair has the wrong arity, or an element
|
/// <c>[attributeName, dataConnectionId, dataSourceReferenceOverride]</c> triple. The
|
||||||
/// has the wrong type.
|
/// optional third element carries the per-instance data-source reference override
|
||||||
|
/// (<see cref="ConnectionBinding.DataSourceReferenceOverride"/>); a JSON
|
||||||
|
/// <c>null</c> (or an omitted third element) leaves it unset so the template default
|
||||||
|
/// applies. Returns <c>false</c> with a descriptive <paramref name="error"/> instead
|
||||||
|
/// of throwing when the JSON is malformed, an entry has the wrong arity, or an
|
||||||
|
/// element has the wrong type.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="json">The JSON string to parse.</param>
|
/// <param name="json">The JSON string to parse.</param>
|
||||||
/// <param name="bindings">The parsed bindings list, or null if parsing fails.</param>
|
/// <param name="bindings">The parsed bindings list, or null if parsing fails.</param>
|
||||||
@@ -99,16 +115,19 @@ public static class InstanceCommands
|
|||||||
.Deserialize<List<List<System.Text.Json.JsonElement>>>(json);
|
.Deserialize<List<List<System.Text.Json.JsonElement>>>(json);
|
||||||
if (pairs == null)
|
if (pairs == null)
|
||||||
{
|
{
|
||||||
error = "Bindings JSON must be a non-null array of [attributeName, dataConnectionId] pairs.";
|
error = "Bindings JSON must be a non-null array of "
|
||||||
|
+ "[attributeName, dataConnectionId] or "
|
||||||
|
+ "[attributeName, dataConnectionId, dataSourceReferenceOverride] entries.";
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
var result = new List<ConnectionBinding>(pairs.Count);
|
var result = new List<ConnectionBinding>(pairs.Count);
|
||||||
foreach (var pair in pairs)
|
foreach (var pair in pairs)
|
||||||
{
|
{
|
||||||
if (pair.Count != 2)
|
if (pair.Count is not (2 or 3))
|
||||||
{
|
{
|
||||||
error = "Each binding must be a [attributeName, dataConnectionId] pair of exactly two elements.";
|
error = "Each binding must be a [attributeName, dataConnectionId] pair, "
|
||||||
|
+ "optionally with a third dataSourceReferenceOverride element.";
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (pair[0].ValueKind != System.Text.Json.JsonValueKind.String)
|
if (pair[0].ValueKind != System.Text.Json.JsonValueKind.String)
|
||||||
@@ -122,7 +141,24 @@ public static class InstanceCommands
|
|||||||
error = "The second element of each binding (dataConnectionId) must be an integer.";
|
error = "The second element of each binding (dataConnectionId) must be an integer.";
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
result.Add(new ConnectionBinding(pair[0].GetString()!, connectionId));
|
|
||||||
|
string? referenceOverride = null;
|
||||||
|
if (pair.Count == 3)
|
||||||
|
{
|
||||||
|
var third = pair[2];
|
||||||
|
if (third.ValueKind == System.Text.Json.JsonValueKind.String)
|
||||||
|
{
|
||||||
|
referenceOverride = third.GetString();
|
||||||
|
}
|
||||||
|
else if (third.ValueKind != System.Text.Json.JsonValueKind.Null)
|
||||||
|
{
|
||||||
|
error = "The third element of each binding (dataSourceReferenceOverride) "
|
||||||
|
+ "must be a string or null.";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
result.Add(new ConnectionBinding(pair[0].GetString()!, connectionId, referenceOverride));
|
||||||
}
|
}
|
||||||
|
|
||||||
bindings = result;
|
bindings = result;
|
||||||
|
|||||||
@@ -54,11 +54,19 @@ public static class TemplateCommands
|
|||||||
|
|
||||||
private static Command BuildList(Option<string> urlOption, Option<string> formatOption, Option<string> usernameOption, Option<string> passwordOption)
|
private static Command BuildList(Option<string> urlOption, Option<string> formatOption, Option<string> usernameOption, Option<string> passwordOption)
|
||||||
{
|
{
|
||||||
var cmd = new Command("list") { Description = "List all templates" };
|
var detailOption = new Option<bool>("--detail")
|
||||||
|
{
|
||||||
|
Description = "Include full template definitions (all attributes/alarms/scripts) in table output. "
|
||||||
|
+ "Without it, table output is a compact summary (counts only). JSON output is always full."
|
||||||
|
};
|
||||||
|
var cmd = new Command("list") { Description = "List all templates (compact table summary; use --detail for the full dump)" };
|
||||||
|
cmd.Add(detailOption);
|
||||||
cmd.SetAction(async (ParseResult result) =>
|
cmd.SetAction(async (ParseResult result) =>
|
||||||
{
|
{
|
||||||
|
var detail = result.GetValue(detailOption);
|
||||||
return await CommandHelpers.ExecuteCommandAsync(
|
return await CommandHelpers.ExecuteCommandAsync(
|
||||||
result, urlOption, formatOption, usernameOption, passwordOption, new ListTemplatesCommand());
|
result, urlOption, formatOption, usernameOption, passwordOption, new ListTemplatesCommand(),
|
||||||
|
tableProjector: detail ? null : TemplateTableProjection.ProjectSummary);
|
||||||
});
|
});
|
||||||
return cmd;
|
return cmd;
|
||||||
}
|
}
|
||||||
@@ -66,13 +74,21 @@ public static class TemplateCommands
|
|||||||
private static Command BuildGet(Option<string> urlOption, Option<string> formatOption, Option<string> usernameOption, Option<string> passwordOption)
|
private static Command BuildGet(Option<string> urlOption, Option<string> formatOption, Option<string> usernameOption, Option<string> passwordOption)
|
||||||
{
|
{
|
||||||
var idOption = new Option<int>("--id") { Description = "Template ID", Required = true };
|
var idOption = new Option<int>("--id") { Description = "Template ID", Required = true };
|
||||||
var cmd = new Command("get") { Description = "Get a template by ID" };
|
var detailOption = new Option<bool>("--detail")
|
||||||
|
{
|
||||||
|
Description = "Include full template definitions (all attributes/alarms/scripts) in table output. "
|
||||||
|
+ "Without it, table output is a compact summary (counts only). JSON output is always full."
|
||||||
|
};
|
||||||
|
var cmd = new Command("get") { Description = "Get a template by ID (compact table summary; use --detail for the full dump)" };
|
||||||
cmd.Add(idOption);
|
cmd.Add(idOption);
|
||||||
|
cmd.Add(detailOption);
|
||||||
cmd.SetAction(async (ParseResult result) =>
|
cmd.SetAction(async (ParseResult result) =>
|
||||||
{
|
{
|
||||||
var id = result.GetValue(idOption);
|
var id = result.GetValue(idOption);
|
||||||
|
var detail = result.GetValue(detailOption);
|
||||||
return await CommandHelpers.ExecuteCommandAsync(
|
return await CommandHelpers.ExecuteCommandAsync(
|
||||||
result, urlOption, formatOption, usernameOption, passwordOption, new GetTemplateCommand(id));
|
result, urlOption, formatOption, usernameOption, passwordOption, new GetTemplateCommand(id),
|
||||||
|
tableProjector: detail ? null : TemplateTableProjection.ProjectSummary);
|
||||||
});
|
});
|
||||||
return cmd;
|
return cmd;
|
||||||
}
|
}
|
||||||
@@ -103,10 +119,10 @@ public static class TemplateCommands
|
|||||||
{
|
{
|
||||||
var idOption = new Option<int>("--id") { Description = "Template ID", Required = true };
|
var idOption = new Option<int>("--id") { Description = "Template ID", Required = true };
|
||||||
var nameOption = new Option<string>("--name") { Description = "Template name", Required = true };
|
var nameOption = new Option<string>("--name") { Description = "Template name", Required = true };
|
||||||
var descOption = new Option<string?>("--description") { Description = "Template description" };
|
var descOption = new Option<string?>("--description") { Description = "Template description. Omit to leave unchanged; pass an empty string (\"\") to clear it." };
|
||||||
var parentOption = new Option<int?>("--parent-id") { Description = "Parent template ID" };
|
var parentOption = new Option<int?>("--parent-id") { Description = "Parent template ID. Immutable after creation; omit to leave unchanged." };
|
||||||
|
|
||||||
var cmd = new Command("update") { Description = "Update a template" };
|
var cmd = new Command("update") { Description = "Update a template (omitted optional fields are left unchanged)" };
|
||||||
cmd.Add(idOption);
|
cmd.Add(idOption);
|
||||||
cmd.Add(nameOption);
|
cmd.Add(nameOption);
|
||||||
cmd.Add(descOption);
|
cmd.Add(descOption);
|
||||||
|
|||||||
@@ -0,0 +1,112 @@
|
|||||||
|
using System.Text.Json;
|
||||||
|
using System.Text.Json.Nodes;
|
||||||
|
|
||||||
|
namespace ZB.MOM.WW.ScadaBridge.CLI.Commands;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Compact table projection for <c>template list</c> / <c>template get</c> (followup #6).
|
||||||
|
/// The management API returns full <c>Template</c> entities — every attribute, alarm,
|
||||||
|
/// script, and composition inline — which the generic table renderer dumps as one giant
|
||||||
|
/// cell per template (~171 KB for a real catalogue, unusable in a terminal). This
|
||||||
|
/// projector reduces each template to id / name / description / parent / derived plus
|
||||||
|
/// member <em>counts</em>, leaving JSON output untouched (callers pass this only on the
|
||||||
|
/// table path) and the full dump available via the command's <c>--detail</c> flag.
|
||||||
|
/// </summary>
|
||||||
|
internal static class TemplateTableProjection
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Projects a templates JSON response (an array from <c>list</c> or a single object
|
||||||
|
/// from <c>get</c>) to its compact summary form. Returns the input unchanged when it
|
||||||
|
/// is not JSON or not the expected shape, so the generic renderer's own fallbacks
|
||||||
|
/// still apply.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="json">The raw success JSON body from the management API.</param>
|
||||||
|
/// <returns>Compact JSON (same array/object shape) suitable for table rendering.</returns>
|
||||||
|
internal static string ProjectSummary(string json)
|
||||||
|
{
|
||||||
|
JsonDocument doc;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
doc = JsonDocument.Parse(json);
|
||||||
|
}
|
||||||
|
catch (JsonException)
|
||||||
|
{
|
||||||
|
// Not JSON (e.g. a proxy error page) — let the renderer print it verbatim.
|
||||||
|
return json;
|
||||||
|
}
|
||||||
|
|
||||||
|
using (doc)
|
||||||
|
{
|
||||||
|
var root = doc.RootElement;
|
||||||
|
if (root.ValueKind == JsonValueKind.Array)
|
||||||
|
{
|
||||||
|
var arr = new JsonArray();
|
||||||
|
foreach (var item in root.EnumerateArray())
|
||||||
|
arr.Add(ProjectElement(item));
|
||||||
|
return arr.ToJsonString();
|
||||||
|
}
|
||||||
|
if (root.ValueKind == JsonValueKind.Object)
|
||||||
|
{
|
||||||
|
return ProjectElement(root).ToJsonString();
|
||||||
|
}
|
||||||
|
return json;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>Projects a single template object to its compact summary node.</summary>
|
||||||
|
private static JsonNode ProjectElement(JsonElement element)
|
||||||
|
{
|
||||||
|
if (element.ValueKind != JsonValueKind.Object)
|
||||||
|
return JsonValue.Create(element.ToString())!;
|
||||||
|
|
||||||
|
// JsonObject preserves insertion order, fixing the column order for the table.
|
||||||
|
return new JsonObject
|
||||||
|
{
|
||||||
|
["id"] = Int(element, "id"),
|
||||||
|
["name"] = Str(element, "name"),
|
||||||
|
["description"] = Str(element, "description"),
|
||||||
|
["parentTemplateId"] = Int(element, "parentTemplateId"),
|
||||||
|
["isDerived"] = Bool(element, "isDerived"),
|
||||||
|
["#attrs"] = Count(element, "attributes"),
|
||||||
|
["#alarms"] = Count(element, "alarms"),
|
||||||
|
["#scripts"] = Count(element, "scripts"),
|
||||||
|
["#comps"] = Count(element, "compositions"),
|
||||||
|
["#nativeAlarms"] = Count(element, "nativeAlarmSources"),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private static bool TryGetPropertyCI(JsonElement obj, string name, out JsonElement value)
|
||||||
|
{
|
||||||
|
foreach (var prop in obj.EnumerateObject())
|
||||||
|
{
|
||||||
|
if (string.Equals(prop.Name, name, StringComparison.OrdinalIgnoreCase))
|
||||||
|
{
|
||||||
|
value = prop.Value;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
value = default;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static JsonNode? Str(JsonElement obj, string name)
|
||||||
|
=> TryGetPropertyCI(obj, name, out var v) && v.ValueKind == JsonValueKind.String
|
||||||
|
? JsonValue.Create(v.GetString())
|
||||||
|
: null;
|
||||||
|
|
||||||
|
private static JsonNode? Int(JsonElement obj, string name)
|
||||||
|
=> TryGetPropertyCI(obj, name, out var v) && v.ValueKind == JsonValueKind.Number && v.TryGetInt32(out var n)
|
||||||
|
? JsonValue.Create(n)
|
||||||
|
: null;
|
||||||
|
|
||||||
|
private static JsonNode? Bool(JsonElement obj, string name)
|
||||||
|
=> TryGetPropertyCI(obj, name, out var v) && (v.ValueKind == JsonValueKind.True || v.ValueKind == JsonValueKind.False)
|
||||||
|
? JsonValue.Create(v.GetBoolean())
|
||||||
|
: null;
|
||||||
|
|
||||||
|
private static JsonNode Count(JsonElement obj, string name)
|
||||||
|
=> JsonValue.Create(
|
||||||
|
TryGetPropertyCI(obj, name, out var v) && v.ValueKind == JsonValueKind.Array
|
||||||
|
? v.GetArrayLength()
|
||||||
|
: 0);
|
||||||
|
}
|
||||||
@@ -86,23 +86,35 @@ Exit codes:
|
|||||||
|
|
||||||
#### `template list`
|
#### `template list`
|
||||||
|
|
||||||
List all templates with their full attribute, alarm, script, and composition definitions.
|
List all templates. **Table** output (`--format table`) shows a compact summary — id,
|
||||||
|
name, description, parent, and member **counts** (`#attrs`, `#alarms`, `#scripts`,
|
||||||
|
`#comps`, `#nativeAlarms`) — so it stays readable in a terminal. Add `--detail` to dump
|
||||||
|
the full attribute/alarm/script/composition definitions in the table. **JSON** output
|
||||||
|
(`--format json`) is always the full, unmodified payload regardless of `--detail`.
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
scadabridge --url <url> template list
|
scadabridge --url <url> template list # compact table
|
||||||
|
scadabridge --url <url> --format table template list --detail # full table dump
|
||||||
|
scadabridge --url <url> --format json template list # full JSON (always)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
| Option | Required | Description |
|
||||||
|
|--------|----------|-------------|
|
||||||
|
| `--detail` | no | Include full definitions in table output (no effect on JSON) |
|
||||||
|
|
||||||
#### `template get`
|
#### `template get`
|
||||||
|
|
||||||
Get a single template by ID.
|
Get a single template by ID. Like `template list`, table output is a compact summary
|
||||||
|
unless `--detail` is supplied; JSON output is always full.
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
scadabridge --url <url> template get --id <int>
|
scadabridge --url <url> template get --id <int> [--detail]
|
||||||
```
|
```
|
||||||
|
|
||||||
| Option | Required | Description |
|
| Option | Required | Description |
|
||||||
|--------|----------|-------------|
|
|--------|----------|-------------|
|
||||||
| `--id` | yes | Template ID |
|
| `--id` | yes | Template ID |
|
||||||
|
| `--detail` | no | Include full definitions in table output (no effect on JSON) |
|
||||||
|
|
||||||
#### `template create`
|
#### `template create`
|
||||||
|
|
||||||
@@ -120,9 +132,11 @@ scadabridge --url <url> template create --name <string> [--description <string>]
|
|||||||
|
|
||||||
#### `template update`
|
#### `template update`
|
||||||
|
|
||||||
Update an existing template. An update **replaces** the whole entity — every required
|
Update an existing template. Optional fields use **leave-unchanged** semantics: omitting
|
||||||
field below must be supplied with the value it should have after the update, even if
|
`--description` or `--parent-id` keeps the stored value rather than wiping it. To
|
||||||
it is unchanged.
|
explicitly clear the description, pass an empty string (`--description ""`). The parent
|
||||||
|
template is immutable after creation — omit `--parent-id` (or pass the current value);
|
||||||
|
passing a different value is rejected.
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
scadabridge --url <url> template update --id <int> --name <string> [--description <string>] [--parent-id <int>]
|
scadabridge --url <url> template update --id <int> --name <string> [--description <string>] [--parent-id <int>]
|
||||||
@@ -132,8 +146,8 @@ scadabridge --url <url> template update --id <int> --name <string> [--descriptio
|
|||||||
|--------|----------|-------------|
|
|--------|----------|-------------|
|
||||||
| `--id` | yes | Template ID |
|
| `--id` | yes | Template ID |
|
||||||
| `--name` | yes | Template name |
|
| `--name` | yes | Template name |
|
||||||
| `--description` | no | Updated description |
|
| `--description` | no | Updated description. Omit to leave unchanged; pass `""` to clear. |
|
||||||
| `--parent-id` | no | Updated parent template ID |
|
| `--parent-id` | no | Immutable; omit to leave unchanged. |
|
||||||
|
|
||||||
#### `template delete`
|
#### `template delete`
|
||||||
|
|
||||||
@@ -544,7 +558,22 @@ scadabridge --url <url> instance set-bindings --id <int> --bindings <json>
|
|||||||
| Option | Required | Description |
|
| Option | Required | Description |
|
||||||
|--------|----------|-------------|
|
|--------|----------|-------------|
|
||||||
| `--id` | yes | Instance ID |
|
| `--id` | yes | Instance ID |
|
||||||
| `--bindings` | yes | JSON array of `[attributeName, dataConnectionId]` pairs (e.g. `[["Speed",7],["Temperature",7]]`) |
|
| `--bindings` | yes | JSON array of binding entries, each either `[attributeName, dataConnectionId]` or `[attributeName, dataConnectionId, dataSourceReferenceOverride]` |
|
||||||
|
|
||||||
|
The optional **third element** sets the per-instance data-source reference override
|
||||||
|
(the OPC UA node id / protocol address used in place of the template's
|
||||||
|
`DataSourceReference` for that attribute). Pass a string to set it, or `null` / omit
|
||||||
|
it to use the template default:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
# Bind Speed with an address override; bind Mode using the template default reference.
|
||||||
|
scadabridge --url <url> instance set-bindings --id 42 \
|
||||||
|
--bindings '[["Speed",7,"ns=2;s=Reactor.Speed"],["Mode",7]]'
|
||||||
|
```
|
||||||
|
|
||||||
|
> **Note:** this command **replaces** all bindings for the instance. Include the
|
||||||
|
> override on every entry that needs one — omitting the third element clears any
|
||||||
|
> previously-set override for that attribute.
|
||||||
|
|
||||||
#### `instance set-overrides`
|
#### `instance set-overrides`
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,13 @@ namespace ZB.MOM.WW.ScadaBridge.Commons.Messages.Management;
|
|||||||
public record ListTemplatesCommand;
|
public record ListTemplatesCommand;
|
||||||
public record GetTemplateCommand(int TemplateId);
|
public record GetTemplateCommand(int TemplateId);
|
||||||
public record CreateTemplateCommand(string Name, string? Description, int? ParentTemplateId);
|
public record CreateTemplateCommand(string Name, string? Description, int? ParentTemplateId);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Updates a template. Optional fields use leave-unchanged semantics (followup #5):
|
||||||
|
/// a <c>null</c> <see cref="Description"/> keeps the stored description (pass an empty
|
||||||
|
/// string to clear it), and a <c>null</c> <see cref="ParentTemplateId"/> keeps the
|
||||||
|
/// existing parent (the parent is immutable; a non-null value that differs is rejected).
|
||||||
|
/// </summary>
|
||||||
public record UpdateTemplateCommand(int TemplateId, string Name, string? Description, int? ParentTemplateId);
|
public record UpdateTemplateCommand(int TemplateId, string Name, string? Description, int? ParentTemplateId);
|
||||||
public record DeleteTemplateCommand(int TemplateId);
|
public record DeleteTemplateCommand(int TemplateId);
|
||||||
public record ValidateTemplateCommand(int TemplateId);
|
public record ValidateTemplateCommand(int TemplateId);
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
using System.Text;
|
||||||
|
|
||||||
namespace ZB.MOM.WW.ScadaBridge.Commons.Types.Flattening;
|
namespace ZB.MOM.WW.ScadaBridge.Commons.Types.Flattening;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -12,6 +14,98 @@ public sealed record ValidationResult
|
|||||||
/// <summary>Non-blocking validation warnings.</summary>
|
/// <summary>Non-blocking validation warnings.</summary>
|
||||||
public IReadOnlyList<ValidationEntry> Warnings { get; init; } = [];
|
public IReadOnlyList<ValidationEntry> Warnings { get; init; } = [];
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Produces a compact, human-readable summary of the validation errors instead of a
|
||||||
|
/// flat semicolon-joined dump (followup #8). The old behaviour concatenated one clause
|
||||||
|
/// per error — for an instance with 50–194 unbound attributes that is a wall of text
|
||||||
|
/// unreadable in a CLI/UI toast. This groups errors by <see cref="ValidationCategory"/>
|
||||||
|
/// and, within a category, rolls entries up by "module" (the entity's canonical name up
|
||||||
|
/// to its last dot) with counts, capping the breadth with "… and N more". The full,
|
||||||
|
/// per-entry list is still available on <see cref="Errors"/> for a detail view or log.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="maxGroups">
|
||||||
|
/// Maximum number of modules (or messages, when entries are not entity-scoped) to list
|
||||||
|
/// per category before collapsing the remainder into a "… and N more" suffix.
|
||||||
|
/// </param>
|
||||||
|
/// <returns>
|
||||||
|
/// A multi-line summary, or an empty string when there are no errors. The first line is
|
||||||
|
/// the total error count; subsequent lines are one per category.
|
||||||
|
/// </returns>
|
||||||
|
public string SummarizeErrors(int maxGroups = 6)
|
||||||
|
{
|
||||||
|
if (Errors.Count == 0)
|
||||||
|
return string.Empty;
|
||||||
|
|
||||||
|
var sb = new StringBuilder();
|
||||||
|
sb.Append(Errors.Count).Append(Errors.Count == 1 ? " error:" : " errors:");
|
||||||
|
|
||||||
|
// Group errors by category, preserving first-seen order for a stable rendering.
|
||||||
|
var byCategory = new List<(ValidationCategory Category, List<ValidationEntry> Items)>();
|
||||||
|
var categoryIndex = new Dictionary<ValidationCategory, int>();
|
||||||
|
foreach (var error in Errors)
|
||||||
|
{
|
||||||
|
if (!categoryIndex.TryGetValue(error.Category, out var i))
|
||||||
|
{
|
||||||
|
i = byCategory.Count;
|
||||||
|
categoryIndex[error.Category] = i;
|
||||||
|
byCategory.Add((error.Category, []));
|
||||||
|
}
|
||||||
|
byCategory[i].Items.Add(error);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var (category, items) in byCategory)
|
||||||
|
{
|
||||||
|
sb.AppendLine();
|
||||||
|
sb.Append(" • ").Append(category).Append(" (").Append(items.Count).Append("): ");
|
||||||
|
|
||||||
|
// Prefer an entity/module rollup when every entry names an entity (the unbound-
|
||||||
|
// binding case). Otherwise fall back to a capped list of the raw messages.
|
||||||
|
if (items.TrueForAll(e => !string.IsNullOrEmpty(e.EntityName)))
|
||||||
|
{
|
||||||
|
var modules = new List<(string Module, int Count)>();
|
||||||
|
var moduleIndex = new Dictionary<string, int>(StringComparer.Ordinal);
|
||||||
|
foreach (var entry in items)
|
||||||
|
{
|
||||||
|
var module = ModuleOf(entry.EntityName!);
|
||||||
|
if (!moduleIndex.TryGetValue(module, out var mi))
|
||||||
|
{
|
||||||
|
mi = modules.Count;
|
||||||
|
moduleIndex[module] = mi;
|
||||||
|
modules.Add((module, 0));
|
||||||
|
}
|
||||||
|
modules[mi] = (module, modules[mi].Count + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
var shown = modules.Take(maxGroups).Select(m => $"{m.Module} ({m.Count})");
|
||||||
|
sb.Append(string.Join(", ", shown));
|
||||||
|
if (modules.Count > maxGroups)
|
||||||
|
sb.Append($", … and {modules.Count - maxGroups} more module(s)");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var shown = items.Take(maxGroups).Select(e => e.Message);
|
||||||
|
sb.Append(string.Join("; ", shown));
|
||||||
|
if (items.Count > maxGroups)
|
||||||
|
sb.Append($"; … and {items.Count - maxGroups} more");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return sb.ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns the "module" portion of a canonical attribute name — everything up to the
|
||||||
|
/// last <c>.</c> (e.g. <c>LeftReactorSide.LeakTest.DeltaVac</c> → <c>LeftReactorSide.LeakTest</c>).
|
||||||
|
/// A name with no dot is reported under <c>(root)</c>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="canonicalName">The entity's canonical name.</param>
|
||||||
|
/// <returns>The module prefix, or <c>(root)</c> for top-level members.</returns>
|
||||||
|
private static string ModuleOf(string canonicalName)
|
||||||
|
{
|
||||||
|
var lastDot = canonicalName.LastIndexOf('.');
|
||||||
|
return lastDot > 0 ? canonicalName[..lastDot] : "(root)";
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>Returns a result with no errors or warnings.</summary>
|
/// <summary>Returns a result with no errors or warnings.</summary>
|
||||||
/// <returns>A <see cref="ValidationResult"/> with empty error and warning lists.</returns>
|
/// <returns>A <see cref="ValidationResult"/> with empty error and warning lists.</returns>
|
||||||
public static ValidationResult Success() => new();
|
public static ValidationResult Success() => new();
|
||||||
|
|||||||
@@ -179,8 +179,18 @@ public class DeploymentService
|
|||||||
|
|
||||||
if (!validationResult.IsValid)
|
if (!validationResult.IsValid)
|
||||||
{
|
{
|
||||||
var errors = string.Join("; ", validationResult.Errors.Select(e => e.Message));
|
// Followup #8: return a grouped/summarized error (leading count + per-module
|
||||||
return Result<DeploymentRecord>.Failure($"Pre-deployment validation failed: {errors}");
|
// rollup, capped) instead of a flat semicolon-joined dump that becomes a wall
|
||||||
|
// of text for instances with dozens of unbound attributes. The full per-entry
|
||||||
|
// list still goes to the deploy log for operators who need every clause.
|
||||||
|
_logger.LogWarning(
|
||||||
|
"Pre-deployment validation failed for instance {InstanceId} ({ErrorCount} error(s)): {Detail}",
|
||||||
|
instanceId,
|
||||||
|
validationResult.Errors.Count,
|
||||||
|
string.Join("; ", validationResult.Errors.Select(e => e.Message)));
|
||||||
|
|
||||||
|
return Result<DeploymentRecord>.Failure(
|
||||||
|
$"Pre-deployment validation failed: {validationResult.SummarizeErrors()}");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Serialize for transmission (also the payload stored in the deployed
|
// Serialize for transmission (also the payload stored in the deployed
|
||||||
|
|||||||
@@ -90,12 +90,17 @@ public class TemplateService
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Updates a template's name and description. Parent template is immutable after creation.
|
/// Updates a template's name and (optionally) description. Optional fields use
|
||||||
|
/// leave-unchanged semantics so an omitted field is not silently wiped (followup #5):
|
||||||
|
/// a <c>null</c> <paramref name="description"/> leaves the stored description as-is —
|
||||||
|
/// pass an empty string to explicitly clear it. Parent template is immutable after
|
||||||
|
/// creation; a <c>null</c> <paramref name="parentTemplateId"/> leaves it unchanged,
|
||||||
|
/// and a non-null value that differs from the current parent is rejected.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="templateId">ID of the template to update.</param>
|
/// <param name="templateId">ID of the template to update.</param>
|
||||||
/// <param name="name">New name for the template.</param>
|
/// <param name="name">New name for the template (required, non-empty).</param>
|
||||||
/// <param name="description">New description.</param>
|
/// <param name="description">New description, or <c>null</c> to leave unchanged. Empty string clears it.</param>
|
||||||
/// <param name="parentTemplateId">Must match the existing parent (cannot be changed).</param>
|
/// <param name="parentTemplateId"><c>null</c> to leave unchanged; a non-null value must match the existing parent (cannot be changed).</param>
|
||||||
/// <param name="user">Username of the user updating the template.</param>
|
/// <param name="user">Username of the user updating the template.</param>
|
||||||
/// <param name="cancellationToken">Cancellation token.</param>
|
/// <param name="cancellationToken">Cancellation token.</param>
|
||||||
/// <returns>Result containing the updated template or failure message.</returns>
|
/// <returns>Result containing the updated template or failure message.</returns>
|
||||||
@@ -115,15 +120,20 @@ public class TemplateService
|
|||||||
return Result<Template>.Failure($"Template with ID {templateId} not found.");
|
return Result<Template>.Failure($"Template with ID {templateId} not found.");
|
||||||
|
|
||||||
// ParentTemplateId is immutable after creation — set once at create time.
|
// ParentTemplateId is immutable after creation — set once at create time.
|
||||||
// Reject any attempt to change it (null→value, value→null, or value→other).
|
// A null parentTemplateId means "leave unchanged" (an omitted CLI/API field),
|
||||||
if (parentTemplateId != template.ParentTemplateId)
|
// so only a non-null value that differs from the current parent is rejected.
|
||||||
|
// Immutability still holds: there is no path that mutates ParentTemplateId here.
|
||||||
|
if (parentTemplateId != null && parentTemplateId != template.ParentTemplateId)
|
||||||
{
|
{
|
||||||
return Result<Template>.Failure(
|
return Result<Template>.Failure(
|
||||||
"Parent template cannot be changed after creation.");
|
"Parent template cannot be changed after creation.");
|
||||||
}
|
}
|
||||||
|
|
||||||
template.Name = name;
|
template.Name = name;
|
||||||
template.Description = description;
|
// Leave-unchanged semantics: only overwrite the description when a value is
|
||||||
|
// supplied. null = not provided (keep existing); "" = explicit clear.
|
||||||
|
if (description != null)
|
||||||
|
template.Description = description;
|
||||||
|
|
||||||
// Check for naming collisions after the change
|
// Check for naming collisions after the change
|
||||||
var collisionResult = await ValidateCollisionsAsync(template, cancellationToken);
|
var collisionResult = await ValidateCollisionsAsync(template, cancellationToken);
|
||||||
|
|||||||
@@ -64,6 +64,58 @@ public class InstanceArgumentParsingTests
|
|||||||
Assert.NotNull(error);
|
Assert.NotNull(error);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void ParseBindings_ThreeElementForm_CapturesReferenceOverride()
|
||||||
|
{
|
||||||
|
// The optional 3rd element carries the per-instance data-source reference
|
||||||
|
// override (followup #4) so the CLI can SET it instead of always wiping it.
|
||||||
|
var ok = InstanceCommands.TryParseBindings(
|
||||||
|
"[[\"Speed\", 5, \"ns=2;s=Spd\"], [\"Mode\", 7]]", out var bindings, out var error);
|
||||||
|
|
||||||
|
Assert.True(ok);
|
||||||
|
Assert.Null(error);
|
||||||
|
Assert.Equal(2, bindings!.Count);
|
||||||
|
Assert.Equal(new ConnectionBinding("Speed", 5, "ns=2;s=Spd"), bindings[0]);
|
||||||
|
// A 2-element entry still leaves the override null (template default applies).
|
||||||
|
Assert.Equal(new ConnectionBinding("Mode", 7, null), bindings[1]);
|
||||||
|
Assert.Null(bindings[1].DataSourceReferenceOverride);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void ParseBindings_ThirdElementExplicitNull_LeavesOverrideUnset()
|
||||||
|
{
|
||||||
|
var ok = InstanceCommands.TryParseBindings(
|
||||||
|
"[[\"Speed\", 5, null]]", out var bindings, out var error);
|
||||||
|
|
||||||
|
Assert.True(ok);
|
||||||
|
Assert.Null(error);
|
||||||
|
Assert.Single(bindings!);
|
||||||
|
Assert.Null(bindings![0].DataSourceReferenceOverride);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void ParseBindings_ThirdElementWrongType_ReturnsErrorNotException()
|
||||||
|
{
|
||||||
|
// A non-string, non-null 3rd element is rejected with a clean error.
|
||||||
|
var ok = InstanceCommands.TryParseBindings(
|
||||||
|
"[[\"Speed\", 5, 99]]", out var bindings, out var error);
|
||||||
|
|
||||||
|
Assert.False(ok);
|
||||||
|
Assert.Null(bindings);
|
||||||
|
Assert.NotNull(error);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void ParseBindings_FourElements_ReturnsErrorNotException()
|
||||||
|
{
|
||||||
|
var ok = InstanceCommands.TryParseBindings(
|
||||||
|
"[[\"Speed\", 5, \"ref\", \"extra\"]]", out var bindings, out var error);
|
||||||
|
|
||||||
|
Assert.False(ok);
|
||||||
|
Assert.Null(bindings);
|
||||||
|
Assert.NotNull(error);
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void ParseOverrides_ValidJson_ReturnsDictionary()
|
public void ParseOverrides_ValidJson_ReturnsDictionary()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -0,0 +1,122 @@
|
|||||||
|
using System.Text.Json;
|
||||||
|
using ZB.MOM.WW.ScadaBridge.CLI.Commands;
|
||||||
|
|
||||||
|
namespace ZB.MOM.WW.ScadaBridge.CLI.Tests;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Tests for the compact <c>template list</c>/<c>get</c> table projection (followup #6):
|
||||||
|
/// the full per-template attribute/alarm/script dumps are collapsed to counts so table
|
||||||
|
/// output is usable in a terminal, while the array/object shape is preserved.
|
||||||
|
/// </summary>
|
||||||
|
public class TemplateTableProjectionTests
|
||||||
|
{
|
||||||
|
private const string ListJson = """
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"id": 3,
|
||||||
|
"name": "MESReceiver",
|
||||||
|
"description": "base",
|
||||||
|
"parentTemplateId": null,
|
||||||
|
"isDerived": false,
|
||||||
|
"attributes": [ {"id":1},{"id":2},{"id":3} ],
|
||||||
|
"alarms": [ {"id":10} ],
|
||||||
|
"scripts": [ {"id":20},{"id":21} ],
|
||||||
|
"compositions": [],
|
||||||
|
"nativeAlarmSources": []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": 5,
|
||||||
|
"name": "LeftMESReceiver",
|
||||||
|
"description": null,
|
||||||
|
"parentTemplateId": 3,
|
||||||
|
"isDerived": false,
|
||||||
|
"attributes": [ {"id":1} ],
|
||||||
|
"alarms": [],
|
||||||
|
"scripts": [],
|
||||||
|
"compositions": [ {"id":99} ],
|
||||||
|
"nativeAlarmSources": [ {"id":7} ]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
""";
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void ProjectSummary_Array_DropsMemberArraysAndKeepsCounts()
|
||||||
|
{
|
||||||
|
var compact = TemplateTableProjection.ProjectSummary(ListJson);
|
||||||
|
|
||||||
|
using var doc = JsonDocument.Parse(compact);
|
||||||
|
var root = doc.RootElement;
|
||||||
|
Assert.Equal(JsonValueKind.Array, root.ValueKind);
|
||||||
|
Assert.Equal(2, root.GetArrayLength());
|
||||||
|
|
||||||
|
var first = root[0];
|
||||||
|
Assert.Equal(3, first.GetProperty("id").GetInt32());
|
||||||
|
Assert.Equal("MESReceiver", first.GetProperty("name").GetString());
|
||||||
|
Assert.Equal("base", first.GetProperty("description").GetString());
|
||||||
|
Assert.Equal(JsonValueKind.Null, first.GetProperty("parentTemplateId").ValueKind);
|
||||||
|
Assert.Equal(3, first.GetProperty("#attrs").GetInt32());
|
||||||
|
Assert.Equal(1, first.GetProperty("#alarms").GetInt32());
|
||||||
|
Assert.Equal(2, first.GetProperty("#scripts").GetInt32());
|
||||||
|
Assert.Equal(0, first.GetProperty("#comps").GetInt32());
|
||||||
|
Assert.Equal(0, first.GetProperty("#nativeAlarms").GetInt32());
|
||||||
|
|
||||||
|
// The full member arrays must NOT survive — that is the whole point of the projection.
|
||||||
|
Assert.False(first.TryGetProperty("attributes", out _));
|
||||||
|
Assert.False(first.TryGetProperty("scripts", out _));
|
||||||
|
|
||||||
|
var second = root[1];
|
||||||
|
Assert.Equal(5, second.GetProperty("id").GetInt32());
|
||||||
|
Assert.Equal(3, second.GetProperty("parentTemplateId").GetInt32());
|
||||||
|
Assert.Equal(1, second.GetProperty("#comps").GetInt32());
|
||||||
|
Assert.Equal(1, second.GetProperty("#nativeAlarms").GetInt32());
|
||||||
|
// A null description stays null (it is not invented).
|
||||||
|
Assert.Equal(JsonValueKind.Null, second.GetProperty("description").ValueKind);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void ProjectSummary_SingleObject_ProducesCompactObject()
|
||||||
|
{
|
||||||
|
const string getJson = """
|
||||||
|
{ "id": 7, "name": "ReactorSide", "description": "d", "parentTemplateId": null,
|
||||||
|
"isDerived": false, "attributes": [ {"id":1},{"id":2} ], "alarms": [], "scripts": [],
|
||||||
|
"compositions": [], "nativeAlarmSources": [] }
|
||||||
|
""";
|
||||||
|
|
||||||
|
var compact = TemplateTableProjection.ProjectSummary(getJson);
|
||||||
|
|
||||||
|
using var doc = JsonDocument.Parse(compact);
|
||||||
|
var root = doc.RootElement;
|
||||||
|
Assert.Equal(JsonValueKind.Object, root.ValueKind);
|
||||||
|
Assert.Equal(7, root.GetProperty("id").GetInt32());
|
||||||
|
Assert.Equal(2, root.GetProperty("#attrs").GetInt32());
|
||||||
|
Assert.False(root.TryGetProperty("attributes", out _));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void ProjectSummary_NonJson_ReturnedVerbatim()
|
||||||
|
{
|
||||||
|
const string notJson = "<html>proxy error</html>";
|
||||||
|
Assert.Equal(notJson, TemplateTableProjection.ProjectSummary(notJson));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void ProjectSummary_IsSubstantiallySmallerThanFullDump()
|
||||||
|
{
|
||||||
|
// Sanity check that the projection actually shrinks output (the reported symptom
|
||||||
|
// was ~171 KB table dumps). A template with a fat attribute array should collapse.
|
||||||
|
var fatAttributes = string.Join(",",
|
||||||
|
Enumerable.Range(0, 200).Select(i =>
|
||||||
|
$"{{\"id\":{i},\"name\":\"Attr{i}\",\"dataType\":\"String\",\"value\":\"some long-ish default value {i}\"}}"));
|
||||||
|
var fullJson = $$"""
|
||||||
|
[ { "id": 1, "name": "T", "description": "d", "parentTemplateId": null, "isDerived": false,
|
||||||
|
"attributes": [ {{fatAttributes}} ], "alarms": [], "scripts": [], "compositions": [], "nativeAlarmSources": [] } ]
|
||||||
|
""";
|
||||||
|
|
||||||
|
var compact = TemplateTableProjection.ProjectSummary(fullJson);
|
||||||
|
|
||||||
|
Assert.True(compact.Length * 4 < fullJson.Length,
|
||||||
|
$"Expected compact ({compact.Length}) to be far smaller than full ({fullJson.Length}).");
|
||||||
|
using var doc = JsonDocument.Parse(compact);
|
||||||
|
Assert.Equal(200, doc.RootElement[0].GetProperty("#attrs").GetInt32());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,111 @@
|
|||||||
|
using ZB.MOM.WW.ScadaBridge.Commons.Types.Flattening;
|
||||||
|
|
||||||
|
namespace ZB.MOM.WW.ScadaBridge.Commons.Tests.Types;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Tests for <see cref="ValidationResult.SummarizeErrors"/> (followup #8) — the grouped,
|
||||||
|
/// capped summary that replaces the flat semicolon-joined deploy error dump.
|
||||||
|
/// </summary>
|
||||||
|
public class ValidationResultSummaryTests
|
||||||
|
{
|
||||||
|
private static ValidationEntry Unbound(string canonicalName) =>
|
||||||
|
ValidationEntry.Error(
|
||||||
|
ValidationCategory.ConnectionBinding,
|
||||||
|
$"Attribute '{canonicalName}' has a data source reference but no connection binding.",
|
||||||
|
canonicalName);
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void SummarizeErrors_NoErrors_ReturnsEmpty()
|
||||||
|
{
|
||||||
|
Assert.Equal(string.Empty, ValidationResult.Success().SummarizeErrors());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void SummarizeErrors_LeadsWithTotalCount()
|
||||||
|
{
|
||||||
|
var result = new ValidationResult
|
||||||
|
{
|
||||||
|
Errors = [Unbound("A.X"), Unbound("A.Y"), Unbound("B.Z")]
|
||||||
|
};
|
||||||
|
|
||||||
|
var summary = result.SummarizeErrors();
|
||||||
|
|
||||||
|
Assert.StartsWith("3 errors:", summary);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void SummarizeErrors_SingleError_UsesSingularNoun()
|
||||||
|
{
|
||||||
|
var result = new ValidationResult { Errors = [Unbound("A.X")] };
|
||||||
|
Assert.StartsWith("1 error:", result.SummarizeErrors());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void SummarizeErrors_RollsUpUnboundAttributesByModuleWithCounts()
|
||||||
|
{
|
||||||
|
// 12 + 12 unbound across two LeakTest modules — the reported wall-of-text case.
|
||||||
|
var errors = new List<ValidationEntry>();
|
||||||
|
for (var i = 0; i < 12; i++) errors.Add(Unbound($"LeftReactorSide.LeakTest.Attr{i}"));
|
||||||
|
for (var i = 0; i < 12; i++) errors.Add(Unbound($"RightReactorSide.LeakTest.Attr{i}"));
|
||||||
|
|
||||||
|
var summary = new ValidationResult { Errors = errors }.SummarizeErrors();
|
||||||
|
|
||||||
|
Assert.Contains("24 errors:", summary);
|
||||||
|
Assert.Contains("ConnectionBinding (24)", summary);
|
||||||
|
Assert.Contains("LeftReactorSide.LeakTest (12)", summary);
|
||||||
|
Assert.Contains("RightReactorSide.LeakTest (12)", summary);
|
||||||
|
// The full 24 individual clauses must NOT all appear inline.
|
||||||
|
Assert.DoesNotContain("Attr11' has a data source reference", summary);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void SummarizeErrors_CapsModuleBreadthWithAndNMore()
|
||||||
|
{
|
||||||
|
// 10 distinct modules, cap at 3 → 7 collapsed.
|
||||||
|
var errors = Enumerable.Range(0, 10).Select(i => Unbound($"Module{i}.Attr")).ToList();
|
||||||
|
|
||||||
|
var summary = new ValidationResult { Errors = errors }.SummarizeErrors(maxGroups: 3);
|
||||||
|
|
||||||
|
Assert.Contains("… and 7 more module(s)", summary);
|
||||||
|
Assert.Contains("Module0 (1)", summary);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void SummarizeErrors_TopLevelMember_GroupsUnderRoot()
|
||||||
|
{
|
||||||
|
var summary = new ValidationResult { Errors = [Unbound("Speed")] }.SummarizeErrors();
|
||||||
|
Assert.Contains("(root) (1)", summary);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void SummarizeErrors_EntriesWithoutEntityName_FallBackToCappedMessageList()
|
||||||
|
{
|
||||||
|
var errors = Enumerable.Range(0, 5)
|
||||||
|
.Select(i => ValidationEntry.Error(ValidationCategory.ScriptCompilation, $"compile error {i}"))
|
||||||
|
.ToList();
|
||||||
|
|
||||||
|
var summary = new ValidationResult { Errors = errors }.SummarizeErrors(maxGroups: 2);
|
||||||
|
|
||||||
|
Assert.Contains("ScriptCompilation (5)", summary);
|
||||||
|
Assert.Contains("compile error 0", summary);
|
||||||
|
Assert.Contains("… and 3 more", summary);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void SummarizeErrors_MixedCategories_ListsEachCategoryGroup()
|
||||||
|
{
|
||||||
|
var result = new ValidationResult
|
||||||
|
{
|
||||||
|
Errors =
|
||||||
|
[
|
||||||
|
Unbound("A.X"),
|
||||||
|
ValidationEntry.Error(ValidationCategory.NamingCollision, "dup name 'Foo'"),
|
||||||
|
]
|
||||||
|
};
|
||||||
|
|
||||||
|
var summary = result.SummarizeErrors();
|
||||||
|
|
||||||
|
Assert.Contains("ConnectionBinding (1)", summary);
|
||||||
|
Assert.Contains("NamingCollision (1)", summary);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1286,17 +1286,39 @@ public class TemplateServiceTests
|
|||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task UpdateTemplate_ClearParent_Fails()
|
public async Task UpdateTemplate_OmittedParentAndDescription_LeavesUnchanged()
|
||||||
{
|
{
|
||||||
var child = new Template("Child") { Id = 2, ParentTemplateId = 1 };
|
// Followup #5: a null parent/description means "leave unchanged" (an omitted
|
||||||
|
// CLI/API field), not an attempt to clear. Renaming without re-passing the
|
||||||
|
// description must not wipe it, and must not trip the parent-immutability guard.
|
||||||
|
var child = new Template("Child") { Id = 2, ParentTemplateId = 1, Description = "keep me" };
|
||||||
_repoMock.Setup(r => r.GetTemplateByIdAsync(2, It.IsAny<CancellationToken>())).ReturnsAsync(child);
|
_repoMock.Setup(r => r.GetTemplateByIdAsync(2, It.IsAny<CancellationToken>())).ReturnsAsync(child);
|
||||||
|
_repoMock.Setup(r => r.GetAllTemplatesAsync(It.IsAny<CancellationToken>()))
|
||||||
|
.ReturnsAsync(new List<Template> { child });
|
||||||
|
|
||||||
// Attempt to clear the parent.
|
var result = await _service.UpdateTemplateAsync(2, "ChildRenamed", null, null, "admin");
|
||||||
var result = await _service.UpdateTemplateAsync(2, "Child", null, null, "admin");
|
|
||||||
|
|
||||||
Assert.True(result.IsFailure);
|
Assert.True(result.IsSuccess, result.IsFailure ? result.Error : null);
|
||||||
Assert.Contains("cannot be changed", result.Error, StringComparison.OrdinalIgnoreCase);
|
Assert.Equal("ChildRenamed", result.Value.Name);
|
||||||
_repoMock.Verify(r => r.UpdateTemplateAsync(It.IsAny<Template>(), It.IsAny<CancellationToken>()), Times.Never);
|
Assert.Equal("keep me", result.Value.Description); // null description left unchanged
|
||||||
|
Assert.Equal(1, result.Value.ParentTemplateId); // parent preserved
|
||||||
|
_repoMock.Verify(r => r.UpdateTemplateAsync(It.IsAny<Template>(), It.IsAny<CancellationToken>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task UpdateTemplate_EmptyDescription_ClearsIt()
|
||||||
|
{
|
||||||
|
// An explicit empty string (distinct from omitted/null) clears the description.
|
||||||
|
var t = new Template("T") { Id = 1, Description = "old" };
|
||||||
|
_repoMock.Setup(r => r.GetTemplateByIdAsync(1, It.IsAny<CancellationToken>())).ReturnsAsync(t);
|
||||||
|
_repoMock.Setup(r => r.GetAllTemplatesAsync(It.IsAny<CancellationToken>()))
|
||||||
|
.ReturnsAsync(new List<Template> { t });
|
||||||
|
|
||||||
|
var result = await _service.UpdateTemplateAsync(1, "T", "", null, "admin");
|
||||||
|
|
||||||
|
Assert.True(result.IsSuccess, result.IsFailure ? result.Error : null);
|
||||||
|
Assert.Equal("", result.Value.Description);
|
||||||
|
_repoMock.Verify(r => r.UpdateTemplateAsync(It.IsAny<Template>(), It.IsAny<CancellationToken>()), Times.Once);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
|
|||||||
Reference in New Issue
Block a user