feat(client-dotnet): add WriteArrayElementsAsync default-fill helper and document semantics

Adds a public WriteArrayElementsAsync helper on MxGatewaySession that builds
an MxValue{SparseArrayValue} and delegates to the existing WriteAsync. Extracts
the MxValue construction into an internal static BuildSparseArray builder for
unit-testability. Two new tests cover builder output shape and the full write
command path. README documents the reset (not preserve) semantics alongside
the existing whole-array guidance.
This commit is contained in:
Joseph Doherty
2026-06-18 02:59:43 -04:00
parent 627c17fae1
commit 95b5b09a67
3 changed files with 144 additions and 0 deletions
@@ -303,6 +303,69 @@ public sealed class MxGatewayClientSessionTests
Assert.Equal(cancellation.Token, Assert.Single(transport.InvokeCalls).CallOptions.CancellationToken);
}
/// <summary>Verifies that BuildSparseArray produces a SparseArrayValue MxValue with the correct total length and elements.</summary>
[Fact]
public void BuildSparseArray_ProducesSparseArrayValueWithCorrectTotalLengthAndElements()
{
MxValue element0 = 42.ToMxValue();
MxValue element3 = 99.ToMxValue();
Dictionary<uint, MxValue> elements = new()
{
[0u] = element0,
[3u] = element3,
};
MxValue result = MxGatewaySession.BuildSparseArray(MxDataType.Integer, totalLength: 10, elements);
Assert.Equal(MxValue.KindOneofCase.SparseArrayValue, result.KindCase);
Assert.Equal(10u, result.SparseArrayValue.TotalLength);
Assert.Equal(MxDataType.Integer, result.SparseArrayValue.ElementDataType);
Assert.Equal(2, result.SparseArrayValue.Elements.Count);
MxSparseElement el0 = Assert.Single(result.SparseArrayValue.Elements, e => e.Index == 0u);
Assert.Same(element0, el0.Value);
MxSparseElement el3 = Assert.Single(result.SparseArrayValue.Elements, e => e.Index == 3u);
Assert.Same(element3, el3.Value);
}
/// <summary>Verifies that WriteArrayElementsAsync builds a write command whose value is a SparseArrayValue.</summary>
[Fact]
public async Task WriteArrayElementsAsync_BuildsWriteCommandWithSparseArrayValue()
{
FakeGatewayTransport transport = CreateTransport();
transport.AddInvokeReply(new MxCommandReply
{
SessionId = "session-fixture",
Kind = MxCommandKind.Write,
ProtocolStatus = new ProtocolStatus { Code = ProtocolStatusCode.Ok },
});
await using MxGatewayClient client = CreateClient(transport);
MxGatewaySession session = await client.OpenSessionAsync();
Dictionary<uint, MxValue> elements = new() { [1u] = 7.ToMxValue() };
await session.WriteArrayElementsAsync(
serverHandle: 12,
itemHandle: 34,
elementDataType: MxDataType.Integer,
totalLength: 5,
elements: elements,
userId: 56);
MxCommandRequest request = Assert.Single(transport.InvokeCalls).Request;
Assert.Equal(MxCommandKind.Write, request.Command.Kind);
Assert.Equal(12, request.Command.Write.ServerHandle);
Assert.Equal(34, request.Command.Write.ItemHandle);
Assert.Equal(56, request.Command.Write.UserId);
MxValue written = request.Command.Write.Value;
Assert.Equal(MxValue.KindOneofCase.SparseArrayValue, written.KindCase);
Assert.Equal(5u, written.SparseArrayValue.TotalLength);
Assert.Equal(MxDataType.Integer, written.SparseArrayValue.ElementDataType);
MxSparseElement el = Assert.Single(written.SparseArrayValue.Elements);
Assert.Equal(1u, el.Index);
Assert.Equal(7, el.Value.Int32Value);
}
private static MxGatewayClient CreateClient(FakeGatewayTransport transport)
{
return new MxGatewayClient(transport.Options, transport);