using System.Linq;
using System.Threading.Tasks;
using Opc.Ua;
using Shouldly;
using Xunit;
using ZB.MOM.WW.LmxOpcUa.Host.Domain;
using ZB.MOM.WW.LmxOpcUa.Tests.Helpers;
namespace ZB.MOM.WW.LmxOpcUa.Tests.Integration
{
///
/// Verifies OPC UA indexed array writes against the bridge's whole-array runtime update behavior.
///
public class ArrayWriteTests
{
///
/// Confirms that writing a single array element updates the correct slot while preserving the rest of the array.
///
[Fact]
public async Task Write_SingleArrayElement_UpdatesWholeArrayValue()
{
var fixture = OpcUaServerFixture.WithFakeMxAccessClient();
fixture.MxAccessClient!.TagValues["MESReceiver_001.MoveInPartNumbers[]"] = Vtq.Good(
Enumerable.Range(0, 50).Select(i => $"PART-{i:00}").ToArray());
await fixture.InitializeAsync();
try
{
using var client = new OpcUaTestClient();
await client.ConnectAsync(fixture.EndpointUrl);
// Browse path: TestMachine_001 -> MESReceiver -> MoveInPartNumbers
var nodeId = client.MakeNodeId("MESReceiver_001.MoveInPartNumbers[]");
var before = client.Read(nodeId).Value as string[];
before.ShouldNotBeNull();
before.Length.ShouldBe(50);
before[1].ShouldBe("PART-01");
var status = client.Write(nodeId, new[] { "UPDATED-PART" }, indexRange: "1");
StatusCode.IsGood(status).ShouldBe(true);
var after = client.Read(nodeId).Value as string[];
after.ShouldNotBeNull();
after.Length.ShouldBe(50);
after[0].ShouldBe("PART-00");
after[1].ShouldBe("UPDATED-PART");
after[2].ShouldBe("PART-02");
}
finally
{
await fixture.DisposeAsync();
}
}
}
}