486628cf0a
C-3 ZTag/SAPID uniqueness enforced from the draft (reservation ledger has no writer); UI/doc claim corrected. C-5 HistorianTagnameDuplicate (owned tags, per cluster, case-insensitive; readOnly exempt). C-8 UX_NodeAcl_PrincipalScope / UX_LdapGroupRoleMapping_Group_Cluster unfiltered (migration with duplicate pre-check) + friendly service/page checks. C-9 RowVersion on ClusterNode/ServerCluster, edit pages use it; node delete removes its NodeDeploymentState rows. C-10 DanglingReference rule; dead PathTooLong/EquipmentUuidImmutable rules + PriorEquipment/Enterprise/Site inputs deleted. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
216 lines
9.5 KiB
C#
216 lines
9.5 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 readonly Guid EqUuid = Guid.Parse("33333333-3333-3333-3333-333333333333");
|
|
private static readonly string EqId = DraftValidator.DeriveEquipmentId(EqUuid);
|
|
|
|
// The logical parents every row needs (DanglingReference, #998): one consistent Area/Line/Equipment + Driver/Device.
|
|
private static readonly Equipment Eq = new()
|
|
{
|
|
EquipmentUuid = EqUuid, EquipmentId = EqId, Name = "eq", UnsLineId = "line-1", MachineCode = "m",
|
|
};
|
|
private static readonly UnsLine Line = new() { UnsLineId = "line-1", UnsAreaId = "area-1", Name = "line" };
|
|
private static readonly UnsArea Area = new() { UnsAreaId = "area-1", ClusterId = "c", Name = "area" };
|
|
private static readonly DriverInstance Drv = new()
|
|
{
|
|
DriverInstanceId = "drv-1", ClusterId = "c", Name = "drv", DriverType = "Modbus", DriverConfig = "{}",
|
|
};
|
|
private static readonly Device Dev = new() { DeviceId = "dev-1", DriverInstanceId = "drv-1", Name = "dev", DeviceConfig = "{}" };
|
|
|
|
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 = EqId, Name = name, DataType = "Double", ScriptId = scriptId,
|
|
ChangeTriggered = true, TimerIntervalMs = 1000,
|
|
};
|
|
|
|
private static ScriptedAlarm Alarm(string predicateScriptId, bool enabled = true) => new()
|
|
{
|
|
ScriptedAlarmId = "al-1", EquipmentId = EqId, 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 = EqId, 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(EqId, name));
|
|
|
|
var errors = DraftValidator.Validate(new DraftSnapshot
|
|
{
|
|
GenerationId = 1, ClusterId = "c", Equipment = [Eq], UnsLines = [Line], UnsAreas = [Area], DriverInstances = [Drv], Devices = [Dev], 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(EqId, name).ShouldBe(EqId + "/" + name);
|
|
DraftValidator.Validate(new DraftSnapshot
|
|
{
|
|
GenerationId = 1, ClusterId = "c", Equipment = [Eq], UnsLines = [Line], UnsAreas = [Area], DriverInstances = [Drv], Devices = [Dev], 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", Equipment = [Eq], UnsLines = [Line], UnsAreas = [Area], DriverInstances = [Drv], Devices = [Dev], 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", Equipment = [Eq], UnsLines = [Line], UnsAreas = [Area], DriverInstances = [Drv], Devices = [Dev], 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", Equipment = [Eq], UnsLines = [Line], UnsAreas = [Area], DriverInstances = [Drv], Devices = [Dev],
|
|
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}}/<tag name></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", Equipment = [Eq], UnsLines = [Line], UnsAreas = [Area], DriverInstances = [Drv], Devices = [Dev],
|
|
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", Equipment = [Eq], UnsLines = [Line], UnsAreas = [Area], DriverInstances = [Drv], Devices = [Dev], 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", Equipment = [Eq], UnsLines = [Line], UnsAreas = [Area], DriverInstances = [Drv], Devices = [Dev], 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", Equipment = [Eq], UnsLines = [Line], UnsAreas = [Area], DriverInstances = [Drv], Devices = [Dev],
|
|
VirtualTags = [Vt("computed")],
|
|
ScriptedAlarms = [Alarm("s1")],
|
|
Scripts = [ScriptRow("s1")],
|
|
}).ShouldBeEmpty();
|
|
}
|