Files
lmxopcua/tests/Core/ZB.MOM.WW.OtOpcUa.Configuration.Tests/DraftValidatorUnsLeafNameAndScriptTests.cs
T
Joseph Doherty d2818e2ef2 fix(uns): #978 one UNS name authority and a script-reference gate — an illegal VirtualTag name or override is refused in the editor and at publish, and an in-use script cannot be deleted
- V3NodeIds.ValidateSegment is the single rule Uns()/UnsChild() enforce; the VirtualTag modal
  ([UnsLeafName]), UnsTreeService (VirtualTag create/update, SetReferenceOverrideAsync) and the
  deploy gate (VirtualTagNameInvalid, UnsEffectiveNameInvalid) all call it (C-1, C-4).
- Effective name is stated once (EquipmentReferenceMap.EffectiveName): an empty override falls
  back to the tag name in the gate and the Monaco catalog, as at both compose seams (C-4).
- UnsSegment patterns anchored \A..\z, so a trailing newline is rejected (C-7).
- VirtualTagScriptNotFound / ScriptedAlarmScriptNotFound deploy-gate rules; ScriptEdit refuses to
  delete a script used by a VirtualTag, ScriptedAlarm or Calculation tag and names them (C-2).
- R-5 backstop: VirtualTagHostActor skips (Error log) a plan whose NodeId cannot be built instead
  of crashing on every apply; AddressSpaceApplier counts it as a failed node instead of aborting
  the VirtualTag materialise pass.
2026-12-03 10:28:27 -05:00

200 lines
7.7 KiB
C#

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;
/// <summary>
/// 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
/// <see cref="V3NodeIds.ValidateSegment"/> rule <see cref="V3NodeIds.Uns(string[])"/> throws on, and every
/// VirtualTag / ScriptedAlarm must reference a script that exists.
/// </summary>
[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 -----
/// <summary>Each name the identity authority rejects is a deploy error, naming the VirtualTag.</summary>
[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<ArgumentException>(() => 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");
}
/// <summary>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.</summary>
[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");
}
/// <summary>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).</summary>
[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();
/// <summary>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 (<c>??</c> kept the empty string) and
/// miss the collision the composer then materialised.</summary>
[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");
}
/// <summary>C-4 secondary, resolution side: an empty override resolves <c>{{equip}}/&lt;tag name&gt;</c>, as the
/// compose seams do.</summary>
[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 -----
/// <summary>A timer-triggered VirtualTag whose script is gone — exactly the case that also passed
/// <c>VirtualTagNoEffectiveTrigger</c> — is a deploy error.</summary>
[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");
}
/// <summary>Positive control: both rows referencing an existing script yield no error.</summary>
[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();
}