Files
donnyandClaude Fable 5.1 7ec61d18e6
Release / windows (push) Successful in 33s
Release / android (push) Successful in 41s
Release / linux (push) Successful in 1m8s
Release / release (push) Skipped
Release / attach-windows (push) Skipped
binding: YGOLegacy.Engine carries the wire protocol; CI builds, tests and ships ygol-engine-binding.zip (TICKET-397)
binding/YGOLegacy.Engine (netstandard2.1, LangVersion 9, unsafe, no package, no Unity
reference) holds the Unity repo's Assets/YGOLegacy/Scripts/Protocol/ as
Protocol/{Contracts,Codecs,Framing,Routing}: 12 files copied byte-identical (LF), namespaces
YGOLegacy.Protocol.* unchanged. EngineVersion (default local) is stamped as the informational
version, so the DLL's ProductVersion is the tag it was built for.

binding/YGOLegacy.Engine.Tests (net8.0, NUnit 3.14) holds the six fixtures. 114 tests pass,
the Unity Edit Mode count. Two deviations from "as-is", lead-approved 2026-10-15:
LegacyReaders.cs is a frozen test-only copy of the legacy MDPro3 / YGOSharp readers and enums
the parity tests compare against (they live only in the Unity repo), and FrameCodecTests.cs
line 35 casts (short) instead of (byte): BitConverter.GetBytes((byte)) is ambiguous on
net8.0 (the Half overload), same bytes written.

The windows job builds and tests the binding after the native gates (both with
-p:EngineVersion so the test's project reference does not restamp the DLL as local), checks
the ProductVersion and stages ygol-engine-binding.zip (YGOLegacy.Engine.dll + .pdb) beside
ygol-engine-win64.zip; attach-windows attaches both. engine.json gains "binding": "<version>"
on every platform (windows.ps1 and the Dockerfile's export-gate, the only writers) and the
release job's agreement check includes it. README gains the Binding section and the asset row;
.gitignore ignores binding/**/bin and obj.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DJUFwdiGCHipsxLkgxssSR
2026-10-15 16:14:17 -07:00

181 lines
7.0 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using NUnit.Framework;
using YGOLegacy.Protocol.Codecs;
using YGOLegacy.Protocol.Contracts;
using YGOLegacy.Protocol.Routing;
namespace YGOLegacy.Protocol.Tests
{
public class StocRouterTests
{
private static BinaryReader Packet(StocMessage id, params byte[] payload)
{
var bytes = new byte[payload.Length + 1];
bytes[0] = (byte)id;
Array.Copy(payload, 0, bytes, 1, payload.Length);
return new BinaryReader(new MemoryStream(bytes));
}
[Test]
public void MixedBatch_DispatchesInArrivalOrder_WhateverTheRegistrationOrder()
{
var router = new StocRouter();
var seen = new List<StocMessage>();
router.Register(StocMessage.Chat, r => seen.Add(StocMessage.Chat));
router.Register(StocMessage.GameMsg, r => seen.Add(StocMessage.GameMsg));
router.Register(StocMessage.HandResult, r => seen.Add(StocMessage.HandResult));
var arrivals = new[] { StocMessage.HandResult, StocMessage.GameMsg, StocMessage.Chat, StocMessage.GameMsg };
foreach (var id in arrivals)
{
StocMessage read;
Assert.That(router.Dispatch(Packet(id), out read), Is.EqualTo(RouteOutcome.Handled));
Assert.That(read, Is.EqualTo(id));
}
Assert.That(seen, Is.EqualTo(arrivals));
}
[Test]
public void Handler_GetsTheReaderPositionedAfterTheId()
{
var router = new StocRouter();
var result = default(HandResultAnnounce);
router.Register(StocMessage.HandResult, r => result = StocCodecs.HandResult.Read(r));
StocMessage id;
var outcome = router.Dispatch(Packet(StocMessage.HandResult, 2, 3), out id);
Assert.That(outcome, Is.EqualTo(RouteOutcome.Handled));
Assert.That(id, Is.EqualTo(StocMessage.HandResult));
Assert.That(result.First, Is.EqualTo(2));
Assert.That(result.Second, Is.EqualTo(3));
}
[Test]
public void GameMsg_ReachesItsHandler_WithTheReaderAtTheInnerId()
{
var router = new StocRouter();
var inner = -1;
var tail = -1L;
router.Register(StocMessage.GameMsg, r =>
{
inner = r.ReadByte();
tail = r.BaseStream.Length - r.BaseStream.Position;
});
StocMessage id;
var outcome = router.Dispatch(Packet(StocMessage.GameMsg, 0x5A, 1, 2, 3), out id);
Assert.That(outcome, Is.EqualTo(RouteOutcome.Handled));
Assert.That(inner, Is.EqualTo(0x5A));
Assert.That(tail, Is.EqualTo(3));
}
[Test]
public void IgnoredId_ReturnsIgnored_AndCallsNothing()
{
var router = new StocRouter();
var unknownCalls = 0;
router.OnUnknown = (id, bytes) => unknownCalls++;
router.Ignore(StocMessage.FieldFinish);
StocMessage read;
var outcome = router.Dispatch(Packet(StocMessage.FieldFinish, 9, 9), out read);
Assert.That(outcome, Is.EqualTo(RouteOutcome.Ignored));
Assert.That(read, Is.EqualTo(StocMessage.FieldFinish));
Assert.That(unknownCalls, Is.EqualTo(0));
Assert.That(router.IsRegistered(StocMessage.FieldFinish), Is.True);
}
[Test]
public void UnknownId_ReturnsUnknown_ReportsOncePerId_WithThePayloadLength()
{
var router = new StocRouter();
var reports = new List<KeyValuePair<StocMessage, int>>();
router.OnUnknown = (id, bytes) => reports.Add(new KeyValuePair<StocMessage, int>(id, bytes));
StocMessage read;
Assert.That(router.Dispatch(Packet(StocMessage.SrvproRoomList, 1, 2, 3), out read), Is.EqualTo(RouteOutcome.Unknown));
Assert.That(read, Is.EqualTo(StocMessage.SrvproRoomList));
Assert.That(router.Dispatch(Packet(StocMessage.SrvproRoomList, 1), out read), Is.EqualTo(RouteOutcome.Unknown));
Assert.That(router.Dispatch(Packet(StocMessage.FieldFinish), out read), Is.EqualTo(RouteOutcome.Unknown));
Assert.That(reports.Count, Is.EqualTo(2));
Assert.That(reports[0].Key, Is.EqualTo(StocMessage.SrvproRoomList));
Assert.That(reports[0].Value, Is.EqualTo(3));
Assert.That(reports[1].Key, Is.EqualTo(StocMessage.FieldFinish));
Assert.That(reports[1].Value, Is.EqualTo(0));
Assert.That(router.IsRegistered(StocMessage.SrvproRoomList), Is.False);
}
[Test]
public void UnknownId_WithoutAHook_DoesNotThrow()
{
var router = new StocRouter();
StocMessage read;
Assert.That(router.Dispatch(Packet(StocMessage.SrvproRoomList, 1), out read), Is.EqualTo(RouteOutcome.Unknown));
}
[Test]
public void DuplicateRegistration_Throws()
{
var router = new StocRouter();
router.Register(StocMessage.Chat, r => { });
router.Ignore(StocMessage.DeckCount);
Assert.Throws<ArgumentException>(() => router.Register(StocMessage.Chat, r => { }));
Assert.Throws<ArgumentException>(() => router.Ignore(StocMessage.Chat));
Assert.Throws<ArgumentException>(() => router.Register(StocMessage.DeckCount, r => { }));
Assert.Throws<ArgumentException>(() => router.Ignore(StocMessage.DeckCount));
}
[Test]
public void NullHandler_Throws()
{
var router = new StocRouter();
Assert.Throws<ArgumentNullException>(() => router.Register(StocMessage.Chat, null));
Assert.That(router.IsRegistered(StocMessage.Chat), Is.False);
}
[Test]
public void EmptyPacket_ReturnsEmpty_WithoutReading()
{
var router = new StocRouter();
var calls = 0;
router.OnUnknown = (id, bytes) => calls++;
router.Register(StocMessage.GameMsg, r => calls++);
StocMessage read;
var outcome = router.Dispatch(new BinaryReader(new MemoryStream(new byte[0])), out read);
Assert.That(outcome, Is.EqualTo(RouteOutcome.Empty));
Assert.That(calls, Is.EqualTo(0));
}
[Test]
public void HandlerException_PropagatesUnchanged()
{
var router = new StocRouter();
var thrown = new InvalidDataException("bad chat");
router.Register(StocMessage.Chat, r => { throw thrown; });
var read = default(StocMessage);
var caught = Assert.Throws<InvalidDataException>(() => router.Dispatch(Packet(StocMessage.Chat, 1), out read));
Assert.That(caught, Is.SameAs(thrown));
}
[Test]
public void NullReader_Throws()
{
var router = new StocRouter();
StocMessage read;
Assert.Throws<ArgumentNullException>(() => router.Dispatch(null, out read));
}
}
}