Files
lmxopcua/tests/Server/ZB.MOM.WW.OtOpcUa.AdminUI.Tests/ScriptAnalysis/EditorGateParityTests.cs
T
Joseph Doherty bd4fff6444 fix(scripting): #1083 a dynamic tag path is an editor error only where the gate refuses it (predicate), a warning in value/Calculation mode; #1093 Calculation-mode {{equip}} hover says it is refused
#1083: OTSCRIPT_DYNPATH was an error in every editor mode while the deploy gate refuses a
non-literal tag path only for a scripted-alarm predicate (editor stricter than the gate).
Decision: do not make the gate stricter (it would reject configurations that deploy today).
ScriptCompileRules now owns the split — RefusesNonLiteralTagPaths(kind) and
TagPathFindings(kind, source), each finding carrying Refuses — the gate refuses on exactly
those findings and the editor draws them as errors (8) and the rest as warnings (4), whose
text says what the path does at runtime (not a declared input: not subscribed, does not
re-trigger, reads BadNodeIdUnknown unless also read literally). DependencyRejection gains
Kind + MethodName for that wording. EditorGateParityTests carries dynamic / concatenated /
interpolated / empty paths in all three modes (14 rows fail with the old always-error
severity).

#1093: hovering a {{equip}} path literal in Calculation mode showed the VirtualTag
"resolves ... when deployed" text, contradicting the OTSCRIPT_EQUIPCALC marker on the same
span. It now says the token is refused in a Calculation script and why (no owning
equipment, never substituted, the gate refuses it). The {{equip}} hover no longer needs a
tag catalog.
2026-12-04 15:02:41 -05:00

148 lines
8.7 KiB
C#

using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.AdminUI.ScriptAnalysis;
using ZB.MOM.WW.OtOpcUa.Core.Scripting;
namespace ZB.MOM.WW.OtOpcUa.AdminUI.Tests.ScriptAnalysis;
/// <summary>
/// lmxopcua#1063 / #1065 — "the editor accepts ⇔ publish accepts", checked over one corpus in all three editor modes
/// against the deploy gate's own checker (<see cref="RoslynDeployScriptChecker"/>, the seam the mode names). The
/// editor used to drop wrapper-located errors, so a script with a path that does not return (CS0161) was clean in the
/// editor and refused at deploy; and a Calculation script carrying <c>{{equip}}</c> was accepted by both and read Bad
/// forever. Both sides now share <see cref="ScriptCompileRules"/> and <see cref="EquipTokenLiteralScanner"/>.
/// </summary>
/// <remarks>
/// A DYNAMIC tag path (<c>ctx.GetTag(variable)</c>, <c>OTSCRIPT_DYNPATH</c>) is in the corpus since lmxopcua#1083: the
/// editor used to mark it an error in every mode while the gate refuses it only for a scripted-alarm predicate. The
/// split is now <see cref="ScriptCompileRules.TagPathFindings"/>'s — an error where the gate refuses, a warning
/// elsewhere — so it gets the same verdict per mode.
/// </remarks>
public sealed class EditorGateParityTests
{
private static readonly ScriptAnalysisService Svc = new();
private static readonly RoslynDeployScriptChecker Gate = new();
public static TheoryData<string, string> Corpus()
{
string[] sources =
[
// CS0161 family (#1063): a path that falls off the end, no return at all, blank, whitespace-only.
"var x = 1;",
"""if (ctx.GetTag("A").Value is null) return 1;""",
"""if (ctx.GetTag("A").Value is null) return true;""",
"",
" \n ",
"while (true) { }",
"throw new System.InvalidOperationException(\"x\");",
// Well-formed returns: value and bool.
"""if (ctx.GetTag("A").Value is null) return 1; return 2;""",
"""return ctx.GetTag("A").Value;""",
"""return Convert.ToDouble(ctx.GetTag("A").Value ?? 0.0) > 5;""",
"return true;",
"return null;",
"return;",
// Ordinary compile errors, sandbox, injection (LMX001/002).
"var x = ; return 1;",
"return ctx.Nope();",
"""var f = new System.IO.FileInfo("/x"); return 1;""",
"return 0; } public static int Evil() { return 0;",
"return 0; } } class Evil { ",
"return 1; }",
"{ return 1;",
// {{equip}} (#1056 / #1065): substituted regular literal, passthrough shape, verbatim, interpolated, mid-path.
"""return ctx.GetTag("{{equip}}/Speed").Value;""",
"""return (double)ctx.GetTag("{{equip}}/Speed").Value * 2;""",
"""return ctx.GetTag(@"{{equip}}/Speed").Value;""",
"""return ctx.GetTag($"{{equip}}/Speed").Value;""",
"""return ctx.GetTag("Plant/{{equip}}/Speed").Value;""",
"""ctx.SetVirtualTag("{{equip}}/Out", 1); return 1;""",
"""ctx.Logger.Information("{{equip}} is only a log string"); return 1;""",
// Dynamic / empty tag paths (#1083): refused for a predicate only. Bool-returning forms too, so the predicate's
// refusal is the path's, not a non-bool return.
"""var p = "A"; return ctx.GetTag(p).Value;""",
"""var p = "A"; return ctx.GetTag(p).Value is null;""",
"""return ctx.GetTag("A" + "B").Value is null;""",
"""return ctx.GetTag($"A{1}").Value is null;""",
"""return ctx.GetTag("").Value is null;""",
"""var p = "Out"; ctx.SetVirtualTag(p, 1); return true;""",
"""var p = "A"; return ctx.GetTag(p).Value is null && ctx.GetTag("A").Value is null;""",
];
string[] modes = [ScriptAnalysisModes.Value, ScriptAnalysisModes.Calculation, ScriptAnalysisModes.AlarmPredicate];
var data = new TheoryData<string, string>();
foreach (var mode in modes)
foreach (var source in sources)
data.Add(mode, source);
return data;
}
[Theory]
[MemberData(nameof(Corpus))]
public void Editor_and_gate_agree(string mode, string source)
{
var markers = Svc.Diagnose(new DiagnoseRequest(source, Mode: mode), TestContext.Current.CancellationToken).Markers;
var editorRejects = markers.Any(m => m.Severity == 8);
var verdict = Gate.Check(
[new DeployScriptCheckRequest(ScriptAnalysisModes.KindOf(mode), source)], TestContext.Current.CancellationToken)[0];
editorRejects.ShouldBe(!verdict.Passed,
$"mode {mode}, source <{source}>: editor [{string.Join(" | ", markers.Select(m => m.Code + " " + m.Message))}] " +
$"vs gate [{string.Join(" | ", verdict.Problems)}]");
}
[Fact]
public void A_missing_return_is_refused_by_both_positive_control_for_the_parity_theory()
{
// Pins that the corpus really exercises #1063 — both sides REFUSE it (a parity theory where both accepted
// everything would pass vacuously).
const string source = """if (ctx.GetTag("A").Value is null) return 1;""";
Svc.Diagnose(new DiagnoseRequest(source), TestContext.Current.CancellationToken).Markers.ShouldContain(m => m.Code == "CS0161" && m.Severity == 8);
Gate.Check([new DeployScriptCheckRequest(ScriptContextKind.VirtualTag, source)], TestContext.Current.CancellationToken)[0]
.Problems.ShouldContain(p => p.Contains("CS0161"));
}
[Fact]
public void Equip_in_a_calculation_is_refused_by_both_but_accepted_by_both_for_a_virtual_tag()
{
const string source = """return ctx.GetTag("{{equip}}/Speed").Value;""";
var ct = TestContext.Current.CancellationToken;
Svc.Diagnose(new DiagnoseRequest(source, Mode: ScriptAnalysisModes.Calculation), TestContext.Current.CancellationToken).Markers
.ShouldContain(m => m.Code == "OTSCRIPT_EQUIPCALC" && m.Severity == 8);
Gate.Check([new DeployScriptCheckRequest(ScriptContextKind.Calculation, source)], ct)[0].Passed.ShouldBeFalse();
// Positive control: the same text is the normal VirtualTag template form.
Svc.Diagnose(new DiagnoseRequest(source, Mode: ScriptAnalysisModes.Value), TestContext.Current.CancellationToken).Markers.ShouldBeEmpty();
Gate.Check([new DeployScriptCheckRequest(ScriptContextKind.VirtualTag, source)], ct)[0].Passed.ShouldBeTrue();
}
/// <summary>
/// lmxopcua#1083: a dynamic tag path is an editor ERROR exactly where the gate refuses it (a predicate) and a
/// WARNING where the gate accepts it (value, Calculation) — and the warning still shows, so the author learns the
/// path is not a declared input. Positive control for the corpus rows: the predicate really is refused by both.
/// </summary>
[Theory]
[InlineData(ScriptAnalysisModes.Value, 4, true)]
[InlineData(ScriptAnalysisModes.Calculation, 4, true)]
[InlineData(ScriptAnalysisModes.AlarmPredicate, 8, false)]
public void A_dynamic_tag_path_is_an_error_only_where_the_gate_refuses_it(string mode, int severity, bool gatePasses)
{
const string source = """var p = "A"; return ctx.GetTag(p).Value is null;""";
var ct = TestContext.Current.CancellationToken;
var dynpath = Svc.Diagnose(new DiagnoseRequest(source, Mode: mode), ct).Markers.Single(m => m.Code == "OTSCRIPT_DYNPATH");
dynpath.Severity.ShouldBe(severity);
(dynpath.StartLineNumber, dynpath.StartColumn).ShouldBe((1, 32)); // on the `p` argument
if (severity == 4) dynpath.Message.ShouldContain("not a declared input");
Gate.Check([new DeployScriptCheckRequest(ScriptAnalysisModes.KindOf(mode), source)], ct)[0].Passed.ShouldBe(gatePasses);
ScriptCompileRules.RefusesNonLiteralTagPaths(ScriptAnalysisModes.KindOf(mode)).ShouldBe(!gatePasses);
}
[Fact]
public void The_editor_wraps_scripts_in_the_runtimes_own_wrapper_text()
{
var tree = Svc.CompilationFor("return 1;").SyntaxTrees.Single().ToString();
tree.ShouldBe(ScriptEvaluator<ZB.MOM.WW.OtOpcUa.Core.VirtualTags.VirtualTagContext, object?>.WrapperPreamble
+ "return 1;" + ScriptEvaluator<ZB.MOM.WW.OtOpcUa.Core.VirtualTags.VirtualTagContext, object?>.WrapperSuffix);
var predicate = Svc.CompilationFor("return true;", ScriptAnalysisModes.AlarmPredicate).SyntaxTrees.Single().ToString();
predicate.ShouldBe(ScriptEvaluator<ZB.MOM.WW.OtOpcUa.Core.ScriptedAlarms.AlarmPredicateContext, bool>.WrapperPreamble
+ "return true;" + ScriptEvaluator<ZB.MOM.WW.OtOpcUa.Core.ScriptedAlarms.AlarmPredicateContext, bool>.WrapperSuffix);
}
}