Files
lmxopcua/tests/Server/ZB.MOM.WW.OtOpcUa.AdminUI.Tests/ScriptAnalysis/ScriptUsageClassifyTests.cs
T
Joseph Doherty 73f49eaf32 fix(scripting): #1063 one compile-refusal authority for editor and deploy gate (ScriptCompileRules + the runtime's own wrapper text) — CS0161 and wrapper injection are now flagged in the editor; #1065 {{equip}} in a Calculation script is refused by the gate and by a new Calculation editor mode
#1063: the Monaco diagnostics dropped every Roslyn error located in the synthesized
wrapper as 'phantom', so CS0161 (not all code paths return a value, pinned on Run)
passed the editor and failed the deploy gate. ScriptCompileRules (Core.Scripting) now
states which diagnostics refuse (every Error, wherever located; LMX001/002 injection)
and which sources skip the compile (blank/passthrough value scripts); ScriptEvaluator
and the editor both use it, and the editor wraps with ScriptEvaluator.WrapperPreamble/
WrapperSuffix instead of its own copy. Wrapper-located refusals are drawn on the
script's last line. EditorGateParityTests runs one corpus through editor and gate in
all three modes.

#1065: a Calculation tag is raw/device-level with no equipment and its seam never
substitutes {{equip}}, so such a script deployed clean and read Bad forever. Refused
rather than substituted: EquipTokenLiteralScanner.FindInCalculation is run by
RoslynDeployScriptChecker for every Calculation use (before the passthrough shortcut,
outside the shared VT/Calc cache) and by the editor in the new 'calculation' mode
(Calculation tag editor; ScriptEdit defaults to it when a Calculation tag uses the
script).
2026-12-03 16:47:14 -05:00

97 lines
4.2 KiB
C#

using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.AdminUI.ScriptAnalysis;
using ZB.MOM.WW.OtOpcUa.AdminUI.Tests.Acls;
using ZB.MOM.WW.OtOpcUa.AdminUI.Uns;
using ZB.MOM.WW.OtOpcUa.Configuration.Entities;
namespace ZB.MOM.WW.OtOpcUa.AdminUI.Tests.ScriptAnalysis;
/// <summary>#1025 — the script editor opens in predicate mode only when scripted alarms are the script's sole users.</summary>
public sealed class ScriptUsageClassifyTests
{
private static async Task<ScriptUsage.SeamUsage> Classify(Action<ZB.MOM.WW.OtOpcUa.Configuration.OtOpcUaConfigDbContext> seed)
{
var factory = AclScopeTestDb.Factory($"script-usage-{Guid.NewGuid():N}");
await using var db = await factory.CreateDbContextAsync(TestContext.Current.CancellationToken);
seed(db);
await db.SaveChangesAsync(TestContext.Current.CancellationToken);
return await ScriptUsage.ClassifyAsync(db, "s-1", TestContext.Current.CancellationToken);
}
[Fact]
public async Task An_unused_script_is_a_value_script()
{
var u = await Classify(_ => { });
u.DefaultMode.ShouldBe(ScriptAnalysisModes.Value);
}
[Fact]
public async Task A_script_used_only_as_a_predicate_opens_in_predicate_mode()
{
var u = await Classify(db => db.ScriptedAlarms.Add(new ScriptedAlarm
{
ScriptedAlarmId = "al-1", EquipmentId = "eq", Name = "a", AlarmType = "LimitAlarm", MessageTemplate = "m", PredicateScriptId = "s-1",
}));
u.UsedAsPredicate.ShouldBeTrue();
u.UsedAsValue.ShouldBeFalse();
u.DefaultMode.ShouldBe(ScriptAnalysisModes.AlarmPredicate);
}
[Fact]
public async Task A_script_also_used_as_a_virtual_tag_stays_a_value_script()
{
var u = await Classify(db =>
{
db.ScriptedAlarms.Add(new ScriptedAlarm
{
ScriptedAlarmId = "al-1", EquipmentId = "eq", Name = "a", AlarmType = "LimitAlarm", MessageTemplate = "m", PredicateScriptId = "s-1",
});
db.VirtualTags.Add(new VirtualTag { VirtualTagId = "vt-1", EquipmentId = "eq", Name = "v", DataType = "Double", ScriptId = "s-1" });
});
u.UsedAsPredicate.ShouldBeTrue();
u.UsedAsValue.ShouldBeTrue();
u.DefaultMode.ShouldBe(ScriptAnalysisModes.Value);
}
[Fact]
public async Task A_calculation_tag_counts_as_a_value_use()
{
var u = await Classify(db => db.Tags.Add(new Tag
{
TagId = "t-1", DeviceId = "d", Name = "calc", DataType = "Double", AccessLevel = ZB.MOM.WW.OtOpcUa.Configuration.Enums.TagAccessLevel.Read, TagConfig = "{\"scriptId\":\"s-1\"}",
}));
u.UsedAsValue.ShouldBeTrue();
u.UsedAsPredicate.ShouldBeFalse();
// #1065: a Calculation use opens the editor in Calculation mode ({{equip}} refused, as the deploy gate does).
u.UsedAsCalculation.ShouldBeTrue();
u.DefaultMode.ShouldBe(ScriptAnalysisModes.Calculation);
}
[Fact]
public async Task A_script_shared_by_a_virtual_tag_and_a_calculation_tag_opens_in_calculation_mode()
{
// Calculation mode is the value shape plus the one Calculation-only refusal, so it shows every refusal either
// value use can meet; value mode would hide the {{equip}} refusal of the Calculation use.
var u = await Classify(db =>
{
db.VirtualTags.Add(new VirtualTag { VirtualTagId = "vt-1", EquipmentId = "eq", Name = "v", DataType = "Double", ScriptId = "s-1" });
db.Tags.Add(new Tag
{
TagId = "t-1", DeviceId = "d", Name = "calc", DataType = "Double", AccessLevel = ZB.MOM.WW.OtOpcUa.Configuration.Enums.TagAccessLevel.Read, TagConfig = "{\"scriptId\":\"s-1\"}",
});
});
u.UsedAsCalculation.ShouldBeTrue();
u.DefaultMode.ShouldBe(ScriptAnalysisModes.Calculation);
}
[Fact]
public async Task A_virtual_tag_only_script_is_not_a_calculation_script()
{
var u = await Classify(db =>
db.VirtualTags.Add(new VirtualTag { VirtualTagId = "vt-1", EquipmentId = "eq", Name = "v", DataType = "Double", ScriptId = "s-1" }));
u.UsedAsCalculation.ShouldBeFalse();
u.DefaultMode.ShouldBe(ScriptAnalysisModes.Value);
}
}