using Shouldly; using Xunit; using ZB.MOM.WW.OtOpcUa.Commons.OpcUa; using ZB.MOM.WW.OtOpcUa.Configuration.Entities; using ZB.MOM.WW.OtOpcUa.Configuration.Enums; using ZB.MOM.WW.OtOpcUa.Configuration.Validation; namespace ZB.MOM.WW.OtOpcUa.Configuration.Tests; /// /// lmxopcua#978 — the deploy-gate half of "one name authority + script gate" (review 2026-12-02 C-1 ≡ R-5, /// C-4, C-7, C-2). Every name that becomes a UNS leaf NodeId segment is checked with the same /// rule throws on, and every /// VirtualTag / ScriptedAlarm must reference a script that exists. /// [Trait("Category", "Unit")] public sealed class DraftValidatorUnsLeafNameAndScriptTests { private static Script ScriptRow(string id) => new() { ScriptId = id, Name = id, SourceCode = "return 1;", SourceHash = "h-" + id, }; private static VirtualTag Vt(string name, string scriptId = "s1", string id = "vt-1") => new() { VirtualTagId = id, EquipmentId = "eq-1", Name = name, DataType = "Double", ScriptId = scriptId, ChangeTriggered = true, TimerIntervalMs = 1000, }; private static ScriptedAlarm Alarm(string predicateScriptId, bool enabled = true) => new() { ScriptedAlarmId = "al-1", EquipmentId = "eq-1", Name = "HighTemp", AlarmType = "LimitAlarm", MessageTemplate = "hot", PredicateScriptId = predicateScriptId, Enabled = enabled, }; private static Tag RawTag(string name = "raw-speed") => new() { TagId = "tag-1", DeviceId = "dev-1", Name = name, DataType = "Float", AccessLevel = TagAccessLevel.Read, TagConfig = "{}", }; private static UnsTagReference Ref(string? overrideName) => new() { UnsTagReferenceId = "ref-1", EquipmentId = "eq-1", TagId = "tag-1", DisplayNameOverride = overrideName, }; // ----- C-1 / R-5: VirtualTag name ----- /// Each name the identity authority rejects is a deploy error, naming the VirtualTag. [Theory] [InlineData("a/b")] [InlineData(" a")] [InlineData("a ")] [InlineData("a\n")] [InlineData("")] public void An_illegal_virtual_tag_name_is_rejected(string name) { // Premise: this is exactly a name the runtime would throw on. Should.Throw(() => V3NodeIds.Uns("eq-1", name)); var errors = DraftValidator.Validate(new DraftSnapshot { GenerationId = 1, ClusterId = "c", VirtualTags = [Vt(name)], Scripts = [ScriptRow("s1")], }); errors.ShouldHaveSingleItem().Code.ShouldBe(DraftValidator.VirtualTagNameInvalidCode); errors[0].Context.ShouldBe("vt-1"); } /// Positive control: a legal name (spaces inside, mixed case, dots) produces no error at all, so /// the rule is not simply firing on every VirtualTag. [Theory] [InlineData("Flow Rate")] [InlineData("speed.avg")] [InlineData("_x")] public void A_legal_virtual_tag_name_is_accepted(string name) { V3NodeIds.Uns("eq-1", name).ShouldBe("eq-1/" + name); DraftValidator.Validate(new DraftSnapshot { GenerationId = 1, ClusterId = "c", VirtualTags = [Vt(name)], Scripts = [ScriptRow("s1")], }).ShouldBeEmpty(); } // ----- C-4: DisplayNameOverride ----- [Theory] [InlineData("Temp/Inlet")] [InlineData(" Temp")] [InlineData("Temp ")] public void An_illegal_display_name_override_is_rejected(string overrideName) { var errors = DraftValidator.Validate(new DraftSnapshot { GenerationId = 1, ClusterId = "c", Tags = [RawTag()], UnsTagReferences = [Ref(overrideName)], }); var error = errors.ShouldHaveSingleItem(); error.Code.ShouldBe(DraftValidator.UnsEffectiveNameInvalidCode); error.Context.ShouldBe("ref-1"); } /// Positive control: a legal override is accepted, and an empty/null one is not checked (the raw /// tag's own name is RawNameInvalid's job). [Theory] [InlineData("Temp Inlet")] [InlineData("")] [InlineData(null)] public void A_legal_or_absent_override_is_accepted(string? overrideName) => DraftValidator.Validate(new DraftSnapshot { GenerationId = 1, ClusterId = "c", Tags = [RawTag()], UnsTagReferences = [Ref(overrideName)], }).ShouldBeEmpty(); /// C-4 secondary: an EMPTY-string override means "use the raw tag name" at both compose seams, so /// the gate must see the reference under that name — it used to skip it (?? kept the empty string) and /// miss the collision the composer then materialised. [Fact] public void An_empty_override_collides_under_the_backing_tag_name() { var errors = DraftValidator.Validate(new DraftSnapshot { GenerationId = 1, ClusterId = "c", Tags = [RawTag("speed")], UnsTagReferences = [Ref("")], VirtualTags = [Vt("speed")], Scripts = [ScriptRow("s1")], }); errors.ShouldContain(e => e.Code == "UnsEffectiveNameCollision"); } /// C-4 secondary, resolution side: an empty override resolves {{equip}}/<tag name>, as the /// compose seams do. [Fact] public void An_empty_override_resolves_equip_references_under_the_backing_tag_name() { var script = new Script { ScriptId = "s1", Name = "s1", SourceHash = "h", SourceCode = "return (double)ctx.GetTag(\"{{equip}}/speed\").Value;", }; var errors = DraftValidator.Validate(new DraftSnapshot { GenerationId = 1, ClusterId = "c", Tags = [RawTag("speed")], UnsTagReferences = [Ref("")], VirtualTags = [Vt("computed")], Scripts = [script], }); errors.ShouldNotContain(e => e.Code == "EquipReferenceUnresolved"); } // ----- C-2: script existence ----- /// A timer-triggered VirtualTag whose script is gone — exactly the case that also passed /// VirtualTagNoEffectiveTrigger — is a deploy error. [Fact] public void A_virtual_tag_with_a_missing_script_is_rejected() { var errors = DraftValidator.Validate(new DraftSnapshot { GenerationId = 1, ClusterId = "c", VirtualTags = [Vt("computed", scriptId: "s-gone")], Scripts = [ScriptRow("s1")], }); var error = errors.ShouldHaveSingleItem(); error.Code.ShouldBe(DraftValidator.VirtualTagScriptNotFoundCode); error.Context.ShouldBe("vt-1"); error.Message.ShouldContain("s-gone"); } [Theory] [InlineData(true)] [InlineData(false)] public void A_scripted_alarm_with_a_missing_predicate_is_rejected(bool enabled) { var errors = DraftValidator.Validate(new DraftSnapshot { GenerationId = 1, ClusterId = "c", ScriptedAlarms = [Alarm("s-gone", enabled)], Scripts = [ScriptRow("s1")], }); var error = errors.ShouldHaveSingleItem(); error.Code.ShouldBe(DraftValidator.ScriptedAlarmScriptNotFoundCode); error.Context.ShouldBe("al-1"); error.Message.ShouldContain("s-gone"); } /// Positive control: both rows referencing an existing script yield no error. [Fact] public void Rows_referencing_an_existing_script_are_accepted() => DraftValidator.Validate(new DraftSnapshot { GenerationId = 1, ClusterId = "c", VirtualTags = [Vt("computed")], ScriptedAlarms = [Alarm("s1")], Scripts = [ScriptRow("s1")], }).ShouldBeEmpty(); }