Files
lmxopcua/tests/Server/ZB.MOM.WW.OtOpcUa.AdminUI.Tests/ScriptAnalysis/EquipTokenEditorTests.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

189 lines
9.9 KiB
C#

using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.AdminUI.ScriptAnalysis;
namespace ZB.MOM.WW.OtOpcUa.AdminUI.Tests.ScriptAnalysis;
/// <summary>
/// v3 {{equip}}/&lt;RefName&gt; editor niceties: hover on a path literal containing the token shows an
/// "equipment-relative" note (not the "not a known configured tag path" warning); completion after
/// <c>{{equip}}/</c> offers the owning equipment's reference effective names; and the diagnostic flags an
/// unresolved <c>{{equip}}/&lt;RefName&gt;</c> exactly as the deploy gate does (editor accepts ⇔ publish
/// accepts).
/// </summary>
public sealed class EquipTokenEditorTests
{
/// <summary>A fake catalog whose equipment has two references — "Speed" and "Temp" — so those are the
/// resolvable {{equip}}/&lt;RefName&gt; names for the completion + diagnostic.</summary>
private sealed class FakeCatalog : IScriptTagCatalog
{
public Task<IReadOnlyList<string>> GetPathsAsync(string? filter, CancellationToken ct)
=> Task.FromResult<IReadOnlyList<string>>(new[] { "Cell1/Modbus/Dev1/Speed" });
public Task<ScriptTagInfo?> GetTagInfoAsync(string path, CancellationToken ct)
=> Task.FromResult<ScriptTagInfo?>(null);
public Task<IReadOnlyList<string>> GetEquipmentReferenceNamesAsync(string equipmentId, string? filter, CancellationToken ct)
{
IEnumerable<string> names = new[] { "Speed", "Temp" };
if (!string.IsNullOrWhiteSpace(filter))
names = names.Where(n => n.StartsWith(filter, StringComparison.OrdinalIgnoreCase));
return Task.FromResult<IReadOnlyList<string>>(names.ToList());
}
}
private static readonly ScriptAnalysisService Svc = new(new FakeCatalog());
private static async Task<IReadOnlyList<CompletionItem>> Items(string code, int line, int col, string? equipmentId)
=> (await Svc.CompleteAsync(new CompletionsRequest(code, line, col, equipmentId))).Items;
[Fact] public async Task Completion_after_equip_slash_offers_reference_names()
{
// caret right after the slash of "{{equip}}/" — column 30 sits between the trailing slash and the
// closing quote of ctx.GetTag("{{equip}}/").
var items = await Items("""return ctx.GetTag("{{equip}}/").Value;""", 1, 30, "EQ-1");
items.Select(i => i.Label).ShouldContain("{{equip}}/Speed");
items.Select(i => i.Label).ShouldContain("{{equip}}/Temp");
items.ShouldAllBe(i => i.Detail == "tag path");
}
[Fact] public async Task Completion_after_equip_slash_inserts_the_full_token_qualified_ref()
{
var items = await Items("""return ctx.GetTag("{{equip}}/").Value;""", 1, 30, "EQ-1");
var speed = items.FirstOrDefault(i => i.Label == "{{equip}}/Speed");
speed.ShouldNotBeNull();
speed!.InsertText.ShouldBe("{{equip}}/Speed");
}
[Fact] public async Task Completion_after_equip_slash_is_inert_without_equipment_context()
{
// Shared ScriptEdit page (no EquipmentId) can't resolve references → no equip completions.
var items = await Items("""return ctx.GetTag("{{equip}}/").Value;""", 1, 30, null);
items.ShouldBeEmpty();
}
[Fact] public async Task Hover_on_equip_token_literal_notes_equipment_relative()
{
// caret inside ctx.GetTag("{{equip}}/Speed")
var md = (await Svc.Hover(new HoverRequest("""return ctx.GetTag("{{equip}}/Speed").Value;""", 1, 24), TestContext.Current.CancellationToken)).Markdown;
md.ShouldNotBeNull();
md!.ShouldContain("Equipment-relative");
// the {{equip}} token must render literally (non-interpolated markdown segment)
md.ShouldContain("{{equip}}");
md.ShouldNotContain("Not a known");
}
/// <summary>
/// lmxopcua#1093: in Calculation mode the token is refused (OTSCRIPT_EQUIPCALC on the same span, #1065), so the
/// hover says so instead of the VirtualTag "resolves … when deployed" text that contradicted the marker. Also
/// answered without a catalog (the hover needs none for this literal). The value-mode hover above is the control.
/// </summary>
[Theory]
[InlineData(true)]
[InlineData(false)]
public async Task Hover_on_equip_token_literal_in_calculation_mode_says_it_is_refused(bool withCatalog)
{
var svc = withCatalog ? Svc : new ScriptAnalysisService();
const string code = """return ctx.GetTag("{{equip}}/Speed").Value;""";
var md = (await svc.Hover(new HoverRequest(code, 1, 24, Mode: ScriptAnalysisModes.Calculation), TestContext.Current.CancellationToken)).Markdown;
md.ShouldNotBeNull();
md!.ShouldContain("refused in a Calculation script");
md.ShouldContain("no owning equipment");
md.ShouldContain("{{equip}}/Speed");
md.ShouldNotContain("resolves through the owning equipment");
// The marker on the same span agrees.
svc.Diagnose(new DiagnoseRequest(code, Mode: ScriptAnalysisModes.Calculation), TestContext.Current.CancellationToken)
.Markers.ShouldContain(m => m.Code == "OTSCRIPT_EQUIPCALC" && m.Severity == 8);
// Value mode on the same service keeps the VirtualTag explanation.
(await svc.Hover(new HoverRequest(code, 1, 24, Mode: ScriptAnalysisModes.Value), TestContext.Current.CancellationToken))
.Markdown.ShouldNotBeNull().ShouldContain("Equipment-relative");
}
[Fact] public async Task Diagnostic_flags_unresolved_equip_reference()
{
// "Missing" is not one of the equipment's references (Speed/Temp) → a diagnostic, matching the
// deploy gate. Uses the equipment context via the request's EquipmentId.
var resp = await Svc.DiagnoseAsync(
new DiagnoseRequest("""return ctx.GetTag("{{equip}}/Missing").Value;""", "EQ-1"), TestContext.Current.CancellationToken);
resp.Markers.ShouldContain(m => m.Code == "OTSCRIPT_EQUIPREF" && m.Message.Contains("Missing"));
}
[Fact] public async Task Diagnostic_clean_for_resolved_equip_reference()
{
var resp = await Svc.DiagnoseAsync(
new DiagnoseRequest("""return ctx.GetTag("{{equip}}/Speed").Value;""", "EQ-1"), TestContext.Current.CancellationToken);
resp.Markers.ShouldNotContain(m => m.Code == "OTSCRIPT_EQUIPREF");
}
[Fact] public async Task Diagnostic_equipref_inert_without_equipment_context()
{
// No EquipmentId (shared ScriptEdit page) → the equip-ref check is inert (base markers only),
// matching the deploy gate which only resolves per owning equipment.
var resp = await Svc.DiagnoseAsync(
new DiagnoseRequest("""return ctx.GetTag("{{equip}}/Missing").Value;"""), TestContext.Current.CancellationToken);
resp.Markers.ShouldNotContain(m => m.Code == "OTSCRIPT_EQUIPREF");
}
}
/// <summary>
/// Reference names containing <c>\</c> or <c>"</c> are written ESCAPED inside the script's regular string literal
/// (<c>{{equip}}/a\\b</c> for a reference named <c>a\b</c>). Substitution decodes the name before the lookup, so the
/// editor's <c>OTSCRIPT_EQUIPREF</c> diagnostic and completion must apply the same rule (editor accepts ⇔ publish
/// accepts).
/// </summary>
public sealed class EquipTokenEscapedNameEditorTests
{
private sealed class Catalog : IScriptTagCatalog
{
public Task<IReadOnlyList<string>> GetPathsAsync(string? filter, CancellationToken ct)
=> Task.FromResult<IReadOnlyList<string>>(new[] { "Cell1/de\\qv/Speed" });
public Task<ScriptTagInfo?> GetTagInfoAsync(string path, CancellationToken ct)
=> Task.FromResult<ScriptTagInfo?>(null);
public Task<IReadOnlyList<string>> GetEquipmentReferenceNamesAsync(string equipmentId, string? filter, CancellationToken ct)
{
IEnumerable<string> names = new[] { "a\\b", "q\"t" };
if (!string.IsNullOrEmpty(filter)) names = names.Where(n => n.StartsWith(filter, StringComparison.Ordinal));
return Task.FromResult<IReadOnlyList<string>>(names.ToList());
}
}
private static readonly ScriptAnalysisService Svc = new(new Catalog());
[Theory]
[InlineData("return ctx.GetTag(\"{{equip}}/a\\\\b\").Value;")] // a\b written with an escaped backslash
[InlineData("return ctx.GetTag(\"{{equip}}/q\\\"t\").Value;")] // q"t written with an escaped quote
public async Task Diagnostic_resolves_names_written_with_escapes(string code)
{
var resp = await Svc.DiagnoseAsync(new DiagnoseRequest(code, "EQ-1"), TestContext.Current.CancellationToken);
resp.Markers.ShouldNotContain(m => m.Code == "OTSCRIPT_EQUIPREF");
}
[Fact]
public async Task Diagnostic_flags_a_name_written_without_the_escape()
{
// `a\b` unescaped is the string a<backspace>: it does NOT resolve to the reference `a\b`, at publish or here.
var resp = await Svc.DiagnoseAsync(
new DiagnoseRequest("return ctx.GetTag(\"{{equip}}/a\\b\").Value;", "EQ-1"), TestContext.Current.CancellationToken);
resp.Markers.ShouldContain(m => m.Code == "OTSCRIPT_EQUIPREF");
}
[Fact]
public async Task Completion_inserts_the_name_escaped_for_the_literal()
{
var items = (await Svc.CompleteAsync(new CompletionsRequest(
"return ctx.GetTag(\"{{equip}}/\").Value;", 1, 30, "EQ-1"), TestContext.Current.CancellationToken)).Items;
items.Single(i => i.Label == "{{equip}}/a\\b").InsertText.ShouldBe("{{equip}}/a\\\\b");
items.Single(i => i.Label == "{{equip}}/q\"t").InsertText.ShouldBe("{{equip}}/q\\\"t");
}
[Fact]
public async Task Path_completion_inserts_a_RawPath_escaped_for_the_literal()
{
var items = (await Svc.CompleteAsync(new CompletionsRequest(
"return ctx.GetTag(\"Cell1\").Value;", 1, 24, "EQ-1"), TestContext.Current.CancellationToken)).Items;
items.Single(i => i.Label == "Cell1/de\\qv/Speed").InsertText.ShouldBe("Cell1/de\\\\qv/Speed");
}
}