test(mqtt): Sparkplug array metrics in the simulator + live gate (lmxopcua#770)

The simulator now packs Int32/Boolean/Float/Double/String arrays per the
Sparkplug B spec (independent of the driver codec) and seeds Profile, Flags
and Curve in the standalone catalog. SparkplugLiveTests gains a live test that
asserts typed int[]/bool[]/float[] values from the birth, a changed vector
(including a different Boolean length) from alias-only DATA, the read path,
and BadTypeMismatch for an array metric bound without isArray. Run against a
throwaway local Mosquitto; mutation-checked (scalar binding / seed removed).

Claude-Session: https://claude.ai/code/session_01GT67AEHd2Acv1YPwGfXEoV
This commit is contained in:
Joseph Doherty
2026-11-09 18:06:46 -05:00
parent 241318e7e5
commit b0ef23c087
3 changed files with 192 additions and 1 deletions
@@ -51,6 +51,9 @@ public sealed class SparkplugLiveTests(MqttFixture fixture)
/// </summary>
private const uint BadCommunicationError = 0x80050000u;
/// <summary>OPC UA <c>BadTypeMismatch</c> — an array metric on a scalar tag (or the reverse).</summary>
private const uint BadTypeMismatch = 0x80740000u;
/// <summary>
/// Bound on every wait for broker traffic. Generous enough to absorb a TLS handshake plus a
/// rebirth round-trip; short enough that a genuinely dark path fails rather than hangs.
@@ -154,6 +157,96 @@ public sealed class SparkplugLiveTests(MqttFixture fixture)
read[1].Value.ShouldBe(-1234);
}
// -----------------------------------------------------------------------
// array metrics (lmxopcua#770 / driver support #754)
// -----------------------------------------------------------------------
/// <summary>
/// Sparkplug <c>*Array</c> metrics over a real broker: the edge node packs the vectors itself
/// (spec encoding, independent of the driver's codec), the birth carries them, the driver
/// publishes typed 1-D arrays under the RawPath, and a changed DATA vector arrives as a change.
/// <para>
/// Covers Int32Array (a negative element and one wider than 16 bits), BooleanArray (nine
/// elements, so the bit-packing crosses a byte boundary) and FloatArray. A shape mismatch
/// is also exercised: the same array metric bound WITHOUT <c>isArray</c> must be
/// BadTypeMismatch, never a packed buffer served as a scalar.
/// </para>
/// </summary>
[Fact]
public async Task Array_metrics_arrive_as_typed_arrays_and_a_changed_vector_is_a_change()
{
SkipIfUnconfigured();
var ct = TestContext.Current.CancellationToken;
var group = NewGroup("array");
const string node = "EdgeA";
const string profilePath = "Fixture/Sparkplug/EdgeA/Profile";
const string flagsPath = "Fixture/Sparkplug/EdgeA/Flags";
const string curvePath = "Fixture/Sparkplug/EdgeA/Curve";
const string scalarViewPath = "Fixture/Sparkplug/EdgeA/ProfileAsScalar";
bool[] flags = [true, false, true, true, false, false, true, false, true];
await using var sim = new SparkplugEdgeNode(SimOptions(group, node));
sim.SetNodeMetrics(
new SparkplugMetricSeed("Profile", 1, DataType.Int32Array, new[] { 1, -2, 300000 }),
new SparkplugMetricSeed("Flags", 2, DataType.BooleanArray, flags),
new SparkplugMetricSeed("Curve", 3, DataType.FloatArray, new[] { 0.5f, 1.25f, -2.5f }));
await sim.ConnectAsync(ct);
await sim.BirthAsync(ct);
await using var driver = new MqttDriver(
SparkplugOptions(
group,
ArrayTag(profilePath, group, node, "Profile", isArray: true),
ArrayTag(flagsPath, group, node, "Flags", isArray: true),
ArrayTag(curvePath, group, node, "Curve", isArray: true),
ArrayTag(scalarViewPath, group, node, "Profile", isArray: false)),
"mqtt-live-sp-array");
var recorder = new SparkplugRecorder(driver);
await driver.InitializeAsync(string.Empty, ct);
await driver.SubscribeAsync([profilePath, flagsPath, curvePath, scalarViewPath], TimeSpan.FromSeconds(1), ct);
var birthProfile = await recorder.WaitAsync(profilePath, v => v.Value is int[], Timeout, ct);
birthProfile.Snapshot.StatusCode.ShouldBe(Good);
((int[])birthProfile.Snapshot.Value!).ShouldBe([1, -2, 300000]);
var birthFlags = await recorder.WaitAsync(flagsPath, v => v.Value is bool[], Timeout, ct);
birthFlags.Snapshot.StatusCode.ShouldBe(Good);
((bool[])birthFlags.Snapshot.Value!).ShouldBe(flags);
var birthCurve = await recorder.WaitAsync(curvePath, v => v.Value is float[], Timeout, ct);
birthCurve.Snapshot.StatusCode.ShouldBe(Good);
((float[])birthCurve.Snapshot.Value!).ShouldBe([0.5f, 1.25f, -2.5f]);
var mismatch = await recorder.WaitAsync(scalarViewPath, v => v.StatusCode != Good, Timeout, ct);
mismatch.Snapshot.StatusCode.ShouldBe(BadTypeMismatch);
// The value-change: alias-only DATA with new vectors (and a different Boolean length).
bool[] newFlags = [false, true, true];
await sim.PublishNodeDataAsync(
[("Profile", new[] { 7, 8, 9, -10 }), ("Flags", newFlags), ("Curve", new[] { 9.75f })],
ct);
var changedProfile = await recorder.WaitAsync(
profilePath, v => v.Value is int[] a && a.Length == 4, Timeout, ct);
((int[])changedProfile.Snapshot.Value!).ShouldBe([7, 8, 9, -10]);
changedProfile.Snapshot.StatusCode.ShouldBe(Good);
var changedFlags = await recorder.WaitAsync(flagsPath, v => v.Value is bool[] { Length: 3 }, Timeout, ct);
((bool[])changedFlags.Snapshot.Value!).ShouldBe(newFlags);
var changedCurve = await recorder.WaitAsync(curvePath, v => v.Value is float[] { Length: 1 }, Timeout, ct);
((float[])changedCurve.Snapshot.Value!).ShouldBe([9.75f]);
// The read path serves the latest vector.
var read = await driver.ReadAsync([profilePath], ct);
read[0].StatusCode.ShouldBe(Good);
((int[])read[0].Value!).ShouldBe([7, 8, 9, -10]);
}
// -----------------------------------------------------------------------
// rebirth recovery (§3.6 #5)
// -----------------------------------------------------------------------
@@ -697,6 +790,15 @@ public sealed class SparkplugLiveTests(MqttFixture fixture)
WriteIdempotent: false);
}
/// <summary>An authored Sparkplug node-level raw tag, optionally declared an array (<c>isArray</c>).</summary>
private static RawTagEntry ArrayTag(string rawPath, string groupId, string edgeNodeId, string metricName, bool isArray) =>
new(
rawPath,
$$"""
{"groupId":"{{groupId}}","edgeNodeId":"{{edgeNodeId}}","metricName":"{{metricName}}"{{(isArray ? ",\"isArray\":true" : "")}}}
""",
WriteIdempotent: false);
/// <summary>The browse configuration blob, parsed through the driver's ONE shared JSON options.</summary>
private string BrowseConfigJson(string groupId) =>
JsonSerializer.Serialize(SparkplugOptions(groupId), MqttJson.Options);