Files
riojoy/tests/RioJoy.Core.Tests/Serial/FakeTransport.cs
T
CydandClaude Opus 4.8 b3cb764f4d Phase 2: serial + RIO protocol core (RioJoy.Core) with unit tests
Port the RIO wire protocol from legacy/riovjoy2.cpp into testable C#:

- Protocol/: command + length table, 7-bit checksum, packet builder, and a
  streaming receive-side framing state machine (PacketParser) that mirrors the
  legacy ReadCommBlock framing/resync (high-bit-mid-packet abort). Typed RIO->PC
  decodes: AnalogReport (14-bit sign-extend), VersionInfo, CheckStatus; lamp-state
  composition.
- Serial/: RioSerialLink drives an async receive loop with ACK/NAK reply policy
  (legacy force-accept vs. opt-in VerifyInboundChecksum), the analog poll timer,
  and the >5s reset-recovery watchdog. IRioTransport abstracts the COM port; the
  SerialPort-backed transport does 9600 8N1 + DTR reset pulse, and acquire/release
  is just create/dispose (foundation for native-game serial yield).
- tests/RioJoy.Core.Tests: 54 xUnit tests covering checksum, framing/resync,
  builder round-trips, analog sign-extension + sentinel rejection, lamp combos,
  and the read loop driven against an in-memory fake transport.

Hardware verification (version/check/analog against a cabinet) remains; it can't
be done off-device.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-26 13:04:03 -05:00

55 lines
2.0 KiB
C#

using System.Threading.Channels;
using RioJoy.Core.Serial;
namespace RioJoy.Core.Tests.Serial;
/// <summary>
/// In-memory <see cref="IRioTransport"/> for driving <see cref="RioSerialLink"/>
/// in tests: enqueue inbound chunks, inspect outbound writes.
/// </summary>
internal sealed class FakeTransport : IRioTransport
{
private readonly Channel<byte[]> _incoming = Channel.CreateUnbounded<byte[]>();
private readonly Channel<byte[]> _writes = Channel.CreateUnbounded<byte[]>();
public string Description => "fake";
/// <summary>Outbound writes, in order. Each call to WriteAsync yields one item.</summary>
public ChannelReader<byte[]> Writes => _writes.Reader;
/// <summary>Queue an inbound chunk for the receive loop to read.</summary>
public void Enqueue(params byte[] data) => _incoming.Writer.TryWrite(data);
/// <summary>Signal that no more inbound data will arrive (transport closed).</summary>
public void CompleteIncoming() => _incoming.Writer.TryComplete();
public async ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken)
{
while (await _incoming.Reader.WaitToReadAsync(cancellationToken).ConfigureAwait(false))
{
if (_incoming.Reader.TryRead(out byte[]? chunk))
{
chunk.AsSpan().CopyTo(buffer.Span);
return chunk.Length;
}
}
return 0; // completed
}
public ValueTask WriteAsync(ReadOnlyMemory<byte> data, CancellationToken cancellationToken)
{
_writes.Writer.TryWrite(data.ToArray());
return ValueTask.CompletedTask;
}
/// <summary>Read the next outbound write, failing if none arrives in time.</summary>
public async Task<byte[]> NextWriteAsync(TimeSpan? timeout = null)
{
using var cts = new CancellationTokenSource(timeout ?? TimeSpan.FromSeconds(5));
return await _writes.Reader.ReadAsync(cts.Token).ConfigureAwait(false);
}
public void Dispose() { }
}