refactor(historian): single-parse ExtractTagHistorize + review-nit tests/docs

Stop parsing TagConfig twice per tag on the deploy hot path: Phase7Composer's
equipment-tag Select lambda is now block-bodied (captures isHistorized/historianTagname
once), and DeploymentArtifact.BuildEquipmentTagPlans captures locals before result.Add.
Add wrong-type-historianTagname InlineData to ExtractTagHistorizeTests. Extend the
parity round-trip fixture with a 4th tag (isHistorized:false + JSON-null tagname)
exercising the artifact-side private guard path. Align DeploymentArtifact's
ExtractTagHistorize doc-comment with the composer-side phrasing (ExtractTagFullName /
ExtractTagAlarm cross-reference).
This commit is contained in:
Joseph Doherty
2026-06-14 19:02:02 -04:00
parent 440929c82a
commit c35c1d3734
4 changed files with 52 additions and 17 deletions
@@ -24,6 +24,8 @@ public class ExtractTagHistorizeTests
[InlineData("[1,2]", false, null)]
// Wrong type for isHistorized (string, not bool) ⇒ false.
[InlineData("{\"isHistorized\":\"yes\"}", false, null)]
// Wrong type for historianTagname (number, not string) ⇒ tagname null, flag still honoured.
[InlineData("{\"isHistorized\":true,\"historianTagname\":123}", true, null)]
public void ExtractTagHistorize_parses_or_returns_defaults(string? cfg, bool expectedHistorized, string? expectedTagname)
{
var (isHistorized, historianTagname) = Phase7Composer.ExtractTagHistorize(cfg);
@@ -93,12 +93,30 @@ public sealed class DeploymentArtifactHistorizeParityTests
AccessLevel = TagAccessLevel.ReadWrite,
TagConfig = "{\"FullName\":\"40003\"}",
};
// Well-formed but non-historized: explicit isHistorized:false + a JSON-null historianTagname
// (not a string token → falls through the ValueKind==String guard → tagname null).
// This exercises the artifact-side private ExtractTagHistorize through the real round-trip
// (it can't be unit-tested directly because it's private). A truly malformed TagConfig string
// would cause ExtractTagFullName to return "" and break the SequenceEqual on other fields, so
// we use a well-formed blob and note that the malformed/throws path is already covered by
// the composer unit test in ExtractTagHistorizeTests.
var notHistorizedNullNameTag = new Tag
{
TagId = "tag-not-hist-nullname",
DriverInstanceId = "drv-modbus",
EquipmentId = "eq-1",
FolderPath = null,
Name = "Temp",
DataType = "Float",
AccessLevel = TagAccessLevel.Read,
TagConfig = "{\"FullName\":\"40004\",\"isHistorized\":false,\"historianTagname\":null}",
};
var areas = new[] { area };
var lines = new[] { line };
var equipment = new[] { equip };
var drivers = new[] { driver };
var tags = new[] { histDefaultTag, histOverrideTag, plainTag };
var tags = new[] { histDefaultTag, histOverrideTag, plainTag, notHistorizedNullNameTag };
var namespaces = new[] { ns };
// ---- Side 1: the live-edit composer ----
@@ -121,13 +139,14 @@ public sealed class DeploymentArtifactHistorizeParityTests
ToSnapshot(histDefaultTag),
ToSnapshot(histOverrideTag),
ToSnapshot(plainTag),
ToSnapshot(notHistorizedNullNameTag),
},
});
var decoded = DeploymentArtifact.ParseComposition(blob);
// ---- Full byte-parity: every field, same order (positional-record value equality) ----
decoded.EquipmentTags.Count.ShouldBe(3);
decoded.EquipmentTags.Count.ShouldBe(4);
decoded.EquipmentTags.SequenceEqual(composed.EquipmentTags).ShouldBeTrue();
// Spell out the Phase C fields per-tag so a divergence names the offending tag.
@@ -148,6 +167,14 @@ public sealed class DeploymentArtifactHistorizeParityTests
plain.HistorianTagname.ShouldBeNull();
composed.EquipmentTags.Single(t => t.TagId == "tag-plain").IsHistorized.ShouldBeFalse();
composed.EquipmentTags.Single(t => t.TagId == "tag-plain").HistorianTagname.ShouldBeNull();
// 4th tag: explicit isHistorized:false + JSON-null historianTagname ⇒ (false, null) on both sides.
// Exercises the artifact-side private ExtractTagHistorize's null-JSON-token guard path.
var notHistorizedNullName = decoded.EquipmentTags.Single(t => t.TagId == "tag-not-hist-nullname");
notHistorizedNullName.IsHistorized.ShouldBeFalse();
notHistorizedNullName.HistorianTagname.ShouldBeNull();
composed.EquipmentTags.Single(t => t.TagId == "tag-not-hist-nullname").IsHistorized.ShouldBeFalse();
composed.EquipmentTags.Single(t => t.TagId == "tag-not-hist-nullname").HistorianTagname.ShouldBeNull();
}
/// <summary>The Pascal-case snapshot a <see cref="Tag"/> EF entity serialises to in the artifact