using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
using ZB.MOM.WW.OtOpcUa.Core.Scripting;
namespace ZB.MOM.WW.OtOpcUa.ScriptWorker.Tests;
///
/// lmxopcua#780 design §13 "Parity": the same script, through (today's guard)
/// and through the worker, gives the same status, reason, value and exact CLR value type.
///
[Collection(ScriptWorkerCollection.Name)]
[Trait("Category", "ScriptWorker")]
public sealed class ParityTests
{
private static CancellationToken Ct => TestContext.Current.CancellationToken;
public static TheoryData Cases() => new()
{
{ "arithmetic", "return 1 + 2;" },
{ "string", "return \"a\" + ctx.GetTag(\"x\").Value;" },
{ "throw", "throw new System.InvalidOperationException(\"boom\");" },
{ "compile error", "return undefinedThing;" },
{ "sandbox violation", "return System.IO.File.Exists(\"x\");" },
{ "missing input status", "return ctx.GetTag(\"missing\").StatusCode;" },
{ "input status", "return ctx.GetTag(\"x\").StatusCode;" },
{ "source timestamp", "return ctx.GetTag(\"x\").SourceTimestampUtc;" },
{ "clock", "return ctx.Now > new System.DateTime(2000, 1, 1);" },
{ "null", "return null;" },
{ "array", "return new double[] { 1.5, 2.5 };" },
{ "cast of wrong type", "return (short)ctx.GetTag(\"x\").Value;" },
};
private static readonly IReadOnlyDictionary Inputs =
new Dictionary(StringComparer.Ordinal)
{
["x"] = new DataValueSnapshot(5, 0x40000000u, new DateTime(2026, 1, 2, 3, 4, 5, DateTimeKind.Utc), DateTime.UtcNow),
};
[Theory]
[MemberData(nameof(Cases))]
public async Task In_and_out_of_process_agree(string name, string source)
{
using var inProcess = new InProcessScriptRunner();
await using var outOfProcess = Scripts.Runner(Scripts.Options());
var invocation = Scripts.Vt(name, source, inputs: Inputs);
var expected = await inProcess.RunAsync(invocation, Ct);
var actual = await outOfProcess.RunAsync(invocation, Ct);
actual.Status.ShouldBe(expected.Status, $"{name}: {actual.Reason} vs {expected.Reason}");
actual.Reason.ShouldBe(expected.Reason);
Same(actual.Value, expected.Value, name);
}
public static TheoryData Typed() => new()
{
{ "bool", true }, { "sbyte", (sbyte)-2 }, { "byte", (byte)2 }, { "short", (short)-3 }, { "ushort", (ushort)3 },
{ "int", -4 }, { "uint", 4u }, { "long", -5L }, { "ulong", 5UL }, { "float", 1.25f }, { "double", 2.5 },
{ "string", "s" }, { "System.DateTime", new DateTime(2026, 11, 9, 0, 0, 0, DateTimeKind.Utc) },
// lmxopcua#1100: a Guid input used to fail "unsupported type" out of process.
{ "System.Guid", new Guid("0f8fad5b-d9cb-469f-a165-70867728950e") },
};
///
/// Unboxing parity (pins design assumption A-7): (T)ctx.GetTag("x").Value works out of process for every
/// tag type, which needs the exact boxed CLR type to survive the wire.
///
[Theory]
[MemberData(nameof(Typed))]
public async Task Unboxing_to_the_exact_type_matches(string typeName, object value)
{
using var inProcess = new InProcessScriptRunner();
await using var outOfProcess = Scripts.Runner(Scripts.Options());
var invocation = Scripts.Vt("unbox-" + typeName, $"var v = ({typeName})ctx.GetTag(\"x\").Value; return v;",
inputs: Scripts.Input("x", value));
var expected = await inProcess.RunAsync(invocation, Ct);
var actual = await outOfProcess.RunAsync(invocation, Ct);
expected.Status.ShouldBe(ScriptRunStatus.Ok, expected.Reason);
actual.Status.ShouldBe(ScriptRunStatus.Ok, actual.Reason);
Same(actual.Value, expected.Value, typeName);
}
///
/// lmxopcua#1064: the result rule of #1059 () holds in both runners — a lazy
/// sequence becomes the same typed array, and an over-cap / open-slot sequence fails with the same reason. Before
/// the fix the worker answered every lazy sequence "unsupported type" and shipped an over-cap array whole.
///
public static TheoryData ResultCases() => new()
{
{ "lazy query", "return System.Linq.Enumerable.Range(0, 3).Select(i => i * 1.5);" },
{ "iterator", "System.Collections.Generic.IEnumerable F() { for (var i = 0; i < 3; i++) yield return i; } return F();" },
{ "endless iterator", "System.Collections.Generic.IEnumerable F() { var i = 0.0; while (true) yield return i++; } return F();" },
{ "over-cap lazy", "return System.Linq.Enumerable.Range(0, int.MaxValue).Select(i => (double)i);" },
{ "over-cap array", "return new double[5];" },
{ "at-cap array", "return new double[4];" },
{ "empty lazy", "return System.Linq.Enumerable.Empty();" },
// lmxopcua#1092: a concrete non-array collection. In process it used to come back as the List itself (capped by
// Count, but not an array), while the worker answered "unsupported type" — now both return the same typed array.
{ "list", "return new System.Collections.Generic.List { 1.5, 2.5 };" },
{ "at-cap list", "return new System.Collections.Generic.List { 1, 2, 3, 4 };" },
{ "over-cap list", "return new System.Collections.Generic.List(new double[5]);" },
{ "hash set", "return new System.Collections.Generic.HashSet { \"a\" };" },
{ "queue", "var q = new System.Collections.Generic.Queue(); q.Enqueue(3); q.Enqueue(4); return q;" },
{ "list of object", "return new System.Collections.Generic.List