Files
lmxopcua/tests/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer.Tests/SdkValueBoundaryScriptResultTests.cs
T
Joseph Doherty d81990bcbf fix(scripting): #1104 a jagged-array script result is refused identically in both runners
int[][] / byte[][] used to be the one result divergence left after #1100: in process
ScriptResultMaterializer passed it (SdkValueBoundary.FromClr carries it as an array of Variants),
while the worker's wire has no nested-array tag and answered "unsupported type". A script-fed node
has a scalar DataType, so a Variant-of-arrays value is not what it declares either; not documented as
a feature anywhere (only noted as the known divergence). Decision: refuse, via the shared rule.

- IsPublishable: a 1-D array's element must be a PublishableScalarTypes scalar (no recursion).
- EnsurePublishable: a nested array gets its own reason, still "... which an OPC UA node cannot
  carry: an array of arrays ...", with a flatten hint (SelectMany). Same "script threw: ..." text in
  both runners.
- SdkValueBoundaryScriptResultTests: int[][] leaves the rule==FromClr theory; a separate fact pins
  the one deliberate difference (FromClr takes it, the rule refuses).
- ParityTests: "jagged array" + "jagged byte array" rows. ShadowModeTests census: the jagged script
  now MATCHES; the unsupported_type mismatch is exercised by a decimal INPUT (design §14 #11), the
  real remaining worker-only refusal.
- ContainmentTests.Unsupported_input_and_result_types: jagged now expects the materializer reason.

Mutation: restoring the recursive element check fails 4 tests (2 parity rows, shadow census,
containment unsupported-type).

Behaviour change: a value script returning a jagged array now fails (Bad + script log) in process
too, instead of publishing a Variant array onto a scalar-typed node.
2026-12-04 20:31:25 -05:00

76 lines
3.6 KiB
C#

using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Core.Scripting;
namespace ZB.MOM.WW.OtOpcUa.OpcUaServer.Tests;
/// <summary>
/// lmxopcua#1100: the script-result rule (<see cref="ScriptResultMaterializer.IsPublishable"/>, applied by both
/// script runners) must agree with what the publish path really takes — <see cref="SdkValueBoundary.FromClr"/>, which
/// every node value write goes through. A value the rule passes but FromClr refuses is the defect #1100 found (the
/// node write threw, the node silently kept its previous value and status); a value FromClr takes but the rule refuses
/// would take a working result away from clients.
/// </summary>
[Trait("Category", "Unit")]
public sealed class SdkValueBoundaryScriptResultTests
{
public static TheoryData<string, object> Values() => new()
{
// publishable
{ "bool", true }, { "sbyte", (sbyte)1 }, { "byte", (byte)1 }, { "short", (short)1 }, { "ushort", (ushort)1 },
{ "int", 1 }, { "uint", 1u }, { "long", 1L }, { "ulong", 1UL }, { "float", 1f }, { "double", 1.0 },
{ "string", "s" }, { "DateTime", DateTime.UtcNow }, { "Guid", Guid.NewGuid() },
{ "int[]", new[] { 1 } }, { "byte[]", new byte[] { 1 } }, { "string[]", new[] { "a", null! } },
{ "DateTime[]", new[] { DateTime.UtcNow } }, { "Guid[]", new[] { Guid.NewGuid() } },
// not publishable
{ "decimal", 1.5m }, { "decimal[]", new[] { 1.5m } }, { "enum", DayOfWeek.Monday }, { "enum[]", new[] { DayOfWeek.Monday } },
{ "TimeSpan", TimeSpan.FromSeconds(1) }, { "char", 'a' }, { "char[]", new[] { 'a' } },
{ "DateTimeOffset", DateTimeOffset.UnixEpoch }, { "KeyValuePair", new KeyValuePair<int, int>(1, 2) },
{ "KeyValuePair[]", new[] { new KeyValuePair<int, int>(1, 2) } }, { "tuple[]", new[] { (1, 2) } },
{ "double[,]", new double[1, 1] }, { "Guid?[]", new Guid?[] { Guid.Empty } },
};
[Theory]
[MemberData(nameof(Values))]
public void The_script_result_rule_is_what_the_publish_path_takes(string name, object value)
{
var publishable = ScriptResultMaterializer.IsPublishable(value.GetType());
bool taken;
try
{
SdkValueBoundary.FromClr(value);
taken = true;
}
catch (Exception)
{
taken = false;
}
publishable.ShouldBe(taken, name);
}
/// <summary>
/// lmxopcua#1104: the one deliberate difference — the rule is STRICTER than <see cref="SdkValueBoundary.FromClr"/>
/// for an array of arrays. FromClr takes it (an array of Variants), but a script-fed node's DataType is a scalar
/// type, so that value is not what the node declares, and the worker's wire cannot carry it; both runners refuse
/// it. Pinned here so a FromClr change that stopped taking it shows this exception can go.
/// </summary>
[Fact]
public void A_jagged_array_is_refused_by_the_rule_although_the_boundary_would_take_it()
{
var jagged = new[] { new[] { 1 } };
Should.NotThrow(() => SdkValueBoundary.FromClr(jagged));
ScriptResultMaterializer.IsPublishable(jagged.GetType()).ShouldBeFalse();
}
[Fact]
public void Every_publishable_scalar_type_and_its_array_reach_the_publish_path()
{
foreach (var type in ScriptResultMaterializer.PublishableScalarTypes)
{
var scalar = type == typeof(string) ? "s" : Activator.CreateInstance(type)!;
Should.NotThrow(() => SdkValueBoundary.FromClr(scalar), type.Name);
Should.NotThrow(() => SdkValueBoundary.FromClr(Array.CreateInstance(type, 1)), type.Name + "[]");
}
}
}