fix(m9/T28b): forward --trigger-kind on alarm-update/script-add/script-update; fix default-selector test

Three dead-code bugs: --trigger-kind was registered but never read or forwarded on the
alarm-update, script-add, and script-update paths. Introduced TriggerConfigJson.InjectAnalysisKind
helper that rewrites any raw --trigger-config JSON blob, writing "analysisKind":"Strict" when
the flag is strict (case-insensitive) and stripping the key for any other value. Wired the
helper into all three handlers alongside the existing alarm-add path (which already used
AlarmTriggerConfigJson.Build). Added 6 unit tests for the new helper in TemplateTriggerKindTests.

Also fixed a false-positive bUnit test (AlarmTriggerEditor_Expression_NoAnalysisKindInConfig_
SelectorDefaultsAdvisory) that passed because "Advisory" appeared anywhere in the HTML; now
asserts select.GetAttribute("value") == "Advisory". Added the missing equivalent test for
ScriptTriggerEditor (ScriptTriggerEditor_Expression_NoAnalysisKindInConfig_SelectorDefaultsAdvisory).
This commit is contained in:
Joseph Doherty
2026-06-18 10:54:42 -04:00
parent ea69178dd9
commit 0bd5e0986f
4 changed files with 171 additions and 15 deletions
@@ -421,6 +421,9 @@ public static class TemplateCommands
updateCmd.Add(updateTriggerKindOption);
updateCmd.SetAction(async (ParseResult result) =>
{
var triggerConfig = TriggerConfigJson.InjectAnalysisKind(
result.GetValue(updateTriggerConfigOption),
result.GetValue(updateTriggerKindOption));
return await CommandHelpers.ExecuteCommandAsync(
result, urlOption, formatOption, usernameOption, passwordOption,
new UpdateTemplateAlarmCommand(
@@ -429,7 +432,7 @@ public static class TemplateCommands
result.GetValue(updateTriggerTypeOption)!,
result.GetValue(updatePriorityOption)!,
result.GetValue(updateDescOption),
result.GetValue(updateTriggerConfigOption),
triggerConfig,
result.GetValue(updateLockedOption)));
});
group.Add(updateCmd);
@@ -549,6 +552,9 @@ public static class TemplateCommands
addCmd.Add(scriptTriggerKindOption);
addCmd.SetAction(async (ParseResult result) =>
{
var triggerConfig = TriggerConfigJson.InjectAnalysisKind(
result.GetValue(triggerConfigOption),
result.GetValue(scriptTriggerKindOption));
return await CommandHelpers.ExecuteCommandAsync(
result, urlOption, formatOption, usernameOption, passwordOption,
new AddTemplateScriptCommand(
@@ -556,7 +562,7 @@ public static class TemplateCommands
result.GetValue(nameOption)!,
result.GetValue(codeOption)!,
result.GetValue(triggerTypeOption)!,
result.GetValue(triggerConfigOption),
triggerConfig,
result.GetValue(lockedOption),
result.GetValue(paramsOption),
result.GetValue(returnOption)));
@@ -591,6 +597,9 @@ public static class TemplateCommands
updateCmd.Add(updateScriptTriggerKindOption);
updateCmd.SetAction(async (ParseResult result) =>
{
var triggerConfig = TriggerConfigJson.InjectAnalysisKind(
result.GetValue(updateTriggerConfigOption),
result.GetValue(updateScriptTriggerKindOption));
return await CommandHelpers.ExecuteCommandAsync(
result, urlOption, formatOption, usernameOption, passwordOption,
new UpdateTemplateScriptCommand(
@@ -598,7 +607,7 @@ public static class TemplateCommands
result.GetValue(updateNameOption)!,
result.GetValue(updateCodeOption)!,
result.GetValue(updateTriggerTypeOption)!,
result.GetValue(updateTriggerConfigOption),
triggerConfig,
result.GetValue(updateLockedOption),
result.GetValue(updateParamsOption),
result.GetValue(updateReturnOption)));
@@ -0,0 +1,61 @@
using System.Text;
using System.Text.Json;
namespace ZB.MOM.WW.ScadaBridge.CLI;
/// <summary>
/// Shared trigger-config JSON helpers for CLI paths that receive a raw
/// <c>--trigger-config</c> JSON blob and need to inject or strip the
/// <c>"analysisKind"</c> key based on <c>--trigger-kind</c>.
/// Used by <c>alarm update</c>, <c>script add</c>, and <c>script update</c>
/// (alarm add uses <see cref="AlarmTriggerConfigJson.Build"/> instead).
/// </summary>
internal static class TriggerConfigJson
{
/// <summary>
/// Copies every property from <paramref name="json"/> into a new object,
/// adding or replacing <c>"analysisKind"</c> when
/// <paramref name="analysisKind"/> is <c>"strict"</c> (case-insensitive),
/// or omitting it for any other value (including <see langword="null"/>).
/// Returns <see langword="null"/> when <paramref name="json"/> is
/// <see langword="null"/> or empty, preserving the caller's null-means-no-config
/// semantics.
/// </summary>
/// <param name="json">Raw trigger-config JSON object, or null.</param>
/// <param name="analysisKind">
/// Value of <c>--trigger-kind</c>: <c>"strict"</c> → write
/// <c>"analysisKind":"Strict"</c>; anything else → omit the key.
/// </param>
/// <returns>
/// The rewritten JSON string, or <see langword="null"/> when
/// <paramref name="json"/> is null/empty.
/// </returns>
internal static string? InjectAnalysisKind(string? json, string? analysisKind)
{
if (string.IsNullOrWhiteSpace(json))
return null;
var isStrict = string.Equals(analysisKind?.Trim(), "Strict", StringComparison.OrdinalIgnoreCase);
using var inputDoc = JsonDocument.Parse(json);
using var stream = new MemoryStream();
using (var writer = new Utf8JsonWriter(stream))
{
writer.WriteStartObject();
// Copy all existing properties except "analysisKind" (we'll re-add it below if needed)
foreach (var prop in inputDoc.RootElement.EnumerateObject())
{
if (!prop.NameEquals("analysisKind"))
prop.WriteTo(writer);
}
if (isStrict)
writer.WriteString("analysisKind", "Strict");
writer.WriteEndObject();
}
return Encoding.UTF8.GetString(stream.ToArray());
}
}