Files
yugioh-legacy-engine/binding/YGOLegacy.Engine.Tests/FeatureCodecTests.cs
T
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

524 lines
28 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using NUnit.Framework;
using YGOLegacy.Protocol.Codecs;
using YGOLegacy.Protocol.Contracts;
namespace YGOLegacy.Protocol.Tests
{
public class FeatureCodecTests
{
private static byte[] Bytes(Action<BinaryWriter> write)
{
using (var stream = new MemoryStream())
using (var writer = new BinaryWriter(stream))
{
write(writer);
writer.Flush();
return stream.ToArray();
}
}
private static BinaryReader Reader(byte[] bytes)
{
return new BinaryReader(new MemoryStream(bytes));
}
private static byte[] Utf16(string text)
{
return Encoding.Unicode.GetBytes(text);
}
private static byte[] Utf16Fixed(string text, int chars)
{
return Concat(Utf16(text), Zeros(chars * 2 - text.Length * 2));
}
private static byte[] Concat(params byte[][] parts)
{
return parts.SelectMany(p => p).ToArray();
}
private static byte[] Le16(int value)
{
return new[] { (byte)value, (byte)(value >> 8) };
}
private static byte[] Le32(int value)
{
return new[] { (byte)value, (byte)(value >> 8), (byte)(value >> 16), (byte)(value >> 24) };
}
private static byte[] Zeros(int count)
{
return new byte[count];
}
// The probe's reference bytes: appearance_bytes(1), dialogue_bytes(4), capability_bytes().
private static readonly int[] ProbeItems = { 1010, 1011, 1012, 1013, 1014, 1015, 1016, 1017 };
private static byte[] ProbeAppearance()
{
return Concat(
Concat(ProbeItems.Select(Le32).ToArray()),
Utf16Fixed("deck-1", 48),
Utf16Fixed("character-1", 64),
Utf16Fixed("group-1", 48));
}
private static byte[] ProbeDialogue()
{
return Concat(
new byte[] { 1, 1 },
Le16(4),
Le32(4004),
Le32(5004),
Utf16Fixed("corpus-4", 48),
Utf16Fixed("event-4", 48));
}
[Test]
public void Ids_MatchTheCatalog_InTheForkBlock()
{
Assert.That((int)LobbyCodecs.Capability.Id, Is.EqualTo(0x40));
Assert.That((int)LobbyCodecs.Appearance.Id, Is.EqualTo(0x41));
Assert.That((int)LobbyCodecs.Dialogue.Id, Is.EqualTo(0x42));
Assert.That(StocCodecs.Capability.Id, Is.EqualTo(StocMessage.Capability));
Assert.That(StocCodecs.Appearance.Id, Is.EqualTo(StocMessage.Appearance));
Assert.That(StocCodecs.Dialogue.Id, Is.EqualTo(StocMessage.Dialogue));
Assert.That((int)StocMessage.Capability, Is.EqualTo(0x40));
Assert.That((int)StocMessage.Appearance, Is.EqualTo(0x41));
Assert.That((int)StocMessage.Dialogue, Is.EqualTo(0x42));
Assert.That(StocCodecs.SetupPairs.Id, Is.EqualTo(StocMessage.SetupPairs));
Assert.That((int)StocMessage.SetupPairs, Is.EqualTo(0x43));
Assert.That((int)LobbyCodecs.SeatPlan.Id, Is.EqualTo(0x50));
Assert.That((int)LobbyCodecs.SeatRequest.Id, Is.EqualTo(0x51));
Assert.That((int)LobbyCodecs.SeatSwap.Id, Is.EqualTo(0x52));
Assert.That((int)LobbyCodecs.SeatMove.Id, Is.EqualTo(0x53));
Assert.That((int)StocCodecs.SeatPlan.Id, Is.EqualTo(0x50));
}
[Test]
public void SeatPlan_Ctos_Is91Bytes_AndRoundTripsTokens()
{
var token = Enumerable.Range(1, 16).Select(i => (byte)i).ToArray();
var plan = new SeatPlan(null, 0, 7, 4, new[] { SeatPlanKind.Open, SeatPlanKind.Cpu, SeatPlanKind.Open, SeatPlanKind.Cpu }, new[] { null, token, null, token });
var bytes = Bytes(w => LobbyCodecs.SeatPlan.Write(w, plan));
Assert.That(bytes.Length, Is.EqualTo(91));
Assert.That(bytes.Skip(20).Take(7), Is.EqualTo(Concat(Le16(7), new byte[] { 4, 0, 1, 0, 1 })));
Assert.That(bytes.Skip(27 + 16).Take(16), Is.EqualTo(token));
var read = LobbyCodecs.SeatPlan.Read(Reader(bytes));
Assert.That(read.Revision, Is.EqualTo(7));
Assert.That(read.Kind(3), Is.EqualTo(SeatPlanKind.Cpu));
Assert.That(read.Tokens[0], Is.EqualTo(Zeros(16)));
Assert.That(SeatIds.FromHex(SeatIds.ToHex(token)), Is.EqualTo(token));
}
[Test]
public void SeatPlan_Stoc_Is25Bytes_CarriesNoTokens_AndAShortReadThrows()
{
var plan = new SeatPlan(null, 9, 3, 2, new[] { SeatPlanKind.Open, SeatPlanKind.Cpu }, new[] { Zeros(16), Zeros(16) });
var bytes = Bytes(w => StocCodecs.SeatPlan.Write(w, new Stamped<SeatPlan>(0xFF, plan)));
Assert.That(bytes.Length, Is.EqualTo(25));
Assert.That(bytes.Skip(18), Is.EqualTo(Concat(Le16(3), new byte[] { 2, 0, 1, 0, 0 })));
var read = StocCodecs.SeatPlan.Read(Reader(bytes));
Assert.That(read.Seat, Is.EqualTo(Capability.ServerSender));
Assert.That(read.Payload.Kind(1), Is.EqualTo(SeatPlanKind.Cpu));
Assert.That(read.Payload.Kind(3), Is.EqualTo(SeatPlanKind.Open));
Assert.Throws<EndOfStreamException>(() => StocCodecs.SeatPlan.Read(Reader(bytes.Take(24).ToArray())));
}
[Test]
public void SeatRequest_SwapAndMove_HaveTheirPinnedSizes()
{
Assert.That(Bytes(w => LobbyCodecs.SeatRequest.Write(w, new SeatRequest(2, null, 3, null))).Length, Is.EqualTo(35));
Assert.That(Bytes(w => LobbyCodecs.SeatSwap.Write(w, new SeatSwap(0, 3))), Is.EqualTo(new byte[] { 0, 3 }));
Assert.That(Bytes(w => LobbyCodecs.SeatMove.Write(w, new SeatMove(1, SeatMove.Watchers))), Is.EqualTo(new byte[] { 1, 0xFF }));
var request = LobbyCodecs.SeatRequest.Read(Reader(Bytes(w => LobbyCodecs.SeatRequest.Write(w, new SeatRequest(2, null, 3, Enumerable.Repeat((byte)9, 16).ToArray())))));
Assert.That(request.Seat, Is.EqualTo(3));
Assert.That(request.Token[15], Is.EqualTo(9));
}
[Test]
public void Restart_Ids_AndCtosSizes_ArePinned_101_17_17_18_32()
{
Assert.That((int)LobbyCodecs.RestartPrepare.Id, Is.EqualTo(0x54));
Assert.That((int)LobbyCodecs.RestartAck.Id, Is.EqualTo(0x55));
Assert.That((int)LobbyCodecs.RestartControl.Id, Is.EqualTo(0x56));
Assert.That((int)LobbyCodecs.SeatResume.Id, Is.EqualTo(0x57));
Assert.That((int)LobbyCodecs.RestartOpen.Id, Is.EqualTo(0x58));
Assert.That((int)StocCodecs.RoomRestarting.Id, Is.EqualTo(0x54));
Assert.That((int)StocCodecs.RestartStatus.Id, Is.EqualTo(0x56));
Assert.That((int)StocCodecs.SeatResumeResult.Id, Is.EqualTo(0x57));
Assert.That((int)StocCodecs.RestartOpened.Id, Is.EqualTo(0x58));
Assert.That(FeatureFlags.RulesRestart, Is.EqualTo(0x10));
Assert.That(FeatureFlags.All, Is.EqualTo(0x7FB));
var id = Enumerable.Repeat((byte)0xAB, 16).ToArray();
var token = Enumerable.Range(1, 16).Select(i => (byte)i).ToArray();
var prepare = Bytes(w => LobbyCodecs.RestartPrepare.Write(w, new RestartPrepare(id, 30000, 2, new[] { token, null }, token)));
Assert.That(prepare.Length, Is.EqualTo(101));
Assert.That(prepare, Is.EqualTo(Concat(id, Le32(30000), new byte[] { 2 }, token, Zeros(48), token)));
Assert.That(Bytes(w => LobbyCodecs.RestartAck.Write(w, new RestartAck(id, RestartPhase.Commit))), Is.EqualTo(Concat(id, new byte[] { 2 })));
Assert.That(Bytes(w => LobbyCodecs.RestartControl.Write(w, new RestartControl(id, RestartPhase.Abort))), Is.EqualTo(Concat(id, new byte[] { 3 })));
Assert.That(Bytes(w => LobbyCodecs.SeatResume.Write(w, new SeatResume(id, SeatResumeAction.Release, 7))), Is.EqualTo(Concat(id, new byte[] { 2, 7 })));
Assert.That(Bytes(w => LobbyCodecs.RestartOpen.Write(w, new RestartOpen(id, token))), Is.EqualTo(Concat(id, token)));
var read = LobbyCodecs.RestartPrepare.Read(Reader(prepare));
Assert.That(read.SeatTokens[0], Is.EqualTo(token));
Assert.That(read.SeatTokens[1], Is.EqualTo(Zeros(16)));
Assert.That(read.ObserverToken, Is.EqualTo(token));
}
[Test]
public void MatchOver_Id_Flag_AndSize_ArePinned_0x5A_0x40_8()
{
Assert.That((int)StocCodecs.MatchOver.Id, Is.EqualTo(0x5A));
Assert.That(FeatureFlags.LobbyAfterMatch, Is.EqualTo(0x40));
var over = Bytes(w => StocCodecs.MatchOver.Write(w, new Stamped<MatchOver>(0xFF, new MatchOver(4, new[] { false, true, true, true }))));
Assert.That(over, Is.EqualTo(Concat(Le16(0xFF), new byte[] { 1, 4, 0, 1, 1, 1 })));
var read = StocCodecs.MatchOver.Read(Reader(over));
Assert.That(read.Seat, Is.EqualTo(0xFF));
Assert.That(read.Payload.Version, Is.EqualTo(1));
Assert.That(read.Payload.SeatCount, Is.EqualTo(4));
Assert.That(new[] { read.Payload.Occupied(0), read.Payload.Occupied(1), read.Payload.Occupied(2), read.Payload.Occupied(3) }, Is.EqualTo(new[] { false, true, true, true }));
Assert.Throws<EndOfStreamException>(() => StocCodecs.MatchOver.Read(Reader(over.Take(7).ToArray())));
}
[Test]
public void CoinToss_Id_Flag_Protocol_AndSize_ArePinned_0x5B_0x80_5_4()
{
Assert.That((int)StocCodecs.CoinToss.Id, Is.EqualTo(0x5B));
Assert.That(FeatureFlags.CoinToss, Is.EqualTo(0x80));
var toss = Bytes(w => StocCodecs.CoinToss.Write(w, new CoinTossAnnounce(2, CoinTossAnnounce.Tails, 30)));
Assert.That(toss, Is.EqualTo(new byte[] { 1, 2, 1, 30 }));
var read = StocCodecs.CoinToss.Read(Reader(toss));
Assert.That(read.Version, Is.EqualTo(1));
Assert.That(read.ChooserSeat, Is.EqualTo(2));
Assert.That(read.Face, Is.EqualTo(CoinTossAnnounce.Tails));
Assert.That(read.ChooseSeconds, Is.EqualTo(30));
Assert.That(StocCodecs.CoinToss.Read(Reader(new byte[] { 1, 0, 0xFF, 15 })).Tossed, Is.False);
Assert.Throws<EndOfStreamException>(() => StocCodecs.CoinToss.Read(Reader(toss.Take(3).ToArray())));
}
[Test]
public void Profile_Id_Flag_Protocol_AndSizes_ArePinned_0x5D_0x200_7_98_100()
{
Assert.That((int)LobbyCodecs.Profile.Id, Is.EqualTo(0x5D));
Assert.That((int)StocCodecs.Profile.Id, Is.EqualTo(0x5D));
Assert.That(FeatureFlags.Profile, Is.EqualTo(0x200));
Assert.That(FeatureFlags.All & FeatureFlags.Profile, Is.EqualTo(FeatureFlags.Profile));
Assert.That(Capability.ProtocolVersion, Is.EqualTo(8));
Assert.That(ProfileCodec.Size, Is.EqualTo(98));
Assert.That(ProfileCodec.StampedSize, Is.EqualTo(100));
var sent = Bytes(w => LobbyCodecs.Profile.Write(w, new ProfileState("atem", "gold")));
Assert.That(sent.Length, Is.EqualTo(98));
Assert.That(sent.Take(2), Is.EqualTo(new byte[] { 1, 0 }));
Assert.That(sent.Skip(2).Take(8), Is.EqualTo(new byte[] { (byte)'a', 0, (byte)'t', 0, (byte)'e', 0, (byte)'m', 0 }));
Assert.That(sent.Skip(10).Take(56), Is.EqualTo(Zeros(56)));
Assert.That(sent.Skip(66).Take(8), Is.EqualTo(new byte[] { (byte)'g', 0, (byte)'o', 0, (byte)'l', 0, (byte)'d', 0 }));
var stamped = Bytes(w => StocCodecs.Profile.Write(w, new Stamped<ProfileState>(3, new ProfileState("mai-ori", string.Empty))));
Assert.That(stamped.Length, Is.EqualTo(100));
Assert.That(stamped.Take(2), Is.EqualTo(Le16(3)));
var read = StocCodecs.Profile.Read(Reader(stamped));
Assert.That(read.Seat, Is.EqualTo(3));
Assert.That(read.Payload.Version, Is.EqualTo(1));
Assert.That(read.Payload.AvatarId, Is.EqualTo("mai-ori"));
Assert.That(read.Payload.FrameId, Is.EqualTo(string.Empty));
var longId = Bytes(w => LobbyCodecs.Profile.Write(w, new ProfileState(new string('x', 40), new string('y', 20))));
Assert.That(longId.Length, Is.EqualTo(98));
var clipped = LobbyCodecs.Profile.Read(Reader(longId));
Assert.That(clipped.AvatarId.Length, Is.EqualTo(31));
Assert.That(clipped.FrameId.Length, Is.EqualTo(15));
Assert.Throws<EndOfStreamException>(() => StocCodecs.Profile.Read(Reader(stamped.Take(99).ToArray())));
}
[Test]
public void AttackPreview_Id_Flag_Protocol_AndSizes_ArePinned_0x5E_0x400_8_4_28()
{
Assert.That((int)LobbyCodecs.AttackPreview.Id, Is.EqualTo(0x5E));
Assert.That((int)StocCodecs.AttackPreview.Id, Is.EqualTo(0x5E));
Assert.That(FeatureFlags.AttackPreview, Is.EqualTo(0x400));
Assert.That(FeatureFlags.All & FeatureFlags.AttackPreview, Is.EqualTo(FeatureFlags.AttackPreview));
Assert.That(Capability.ProtocolVersion, Is.EqualTo(8));
Assert.That(AttackPreviewPair.Size, Is.EqualTo(4));
Assert.That(AttackPreviewEntry.Size, Is.EqualTo(28));
var query = new AttackPreviewQuery(new[] { new AttackPreviewPair(0x04, 2, 0x04, 1), AttackPreviewPair.Direct(0x04, 2) });
var asked = Bytes(w => LobbyCodecs.AttackPreview.Write(w, query));
Assert.That(asked, Is.EqualTo(new byte[] { 1, 2, 0x04, 2, 0x04, 1, 0x04, 2, 0xFF, 0 }));
var readQuery = LobbyCodecs.AttackPreview.Read(Reader(asked));
Assert.That(readQuery.Version, Is.EqualTo(1));
Assert.That(readQuery.Pairs.Length, Is.EqualTo(2));
Assert.That(readQuery.Pairs[1].IsDirect, Is.True);
Assert.That(readQuery.Pairs[0].TargetSequence, Is.EqualTo(1));
Assert.That(Bytes(w => LobbyCodecs.AttackPreview.Write(w, new AttackPreviewQuery(null))), Is.EqualTo(new byte[] { 1, 0 }));
var answer = Enumerable.Range(0, 24).Select(i => (byte)(100 + i)).ToArray();
var result = new AttackPreviewResult(new[] { new AttackPreviewEntry(query.Pairs[0], answer) });
var answered = Bytes(w => StocCodecs.AttackPreview.Write(w, result));
Assert.That(answered.Length, Is.EqualTo(2 + 28));
Assert.That(answered, Is.EqualTo(Concat(new byte[] { 1, 1, 0x04, 2, 0x04, 1 }, answer)));
var readResult = StocCodecs.AttackPreview.Read(Reader(answered));
Assert.That(readResult.Version, Is.EqualTo(1));
Assert.That(readResult.Entries.Length, Is.EqualTo(1));
Assert.That(readResult.Entries[0].Pair.AttackerSequence, Is.EqualTo(2));
Assert.That(readResult.Entries[0].Answer, Is.EqualTo(answer));
Assert.That(StocCodecs.AttackPreview.Read(Reader(new byte[] { 1, 0 })).Entries, Is.Empty);
Assert.Throws<EndOfStreamException>(() => StocCodecs.AttackPreview.Read(Reader(answered.Take(29).ToArray())));
Assert.Throws<EndOfStreamException>(() => StocCodecs.AttackPreview.Read(Reader(new byte[] { 1 })));
}
[Test]
public void SeatState_Id_Flag_AndSizes_ArePinned_0x59_0x20_4_6()
{
Assert.That((int)LobbyCodecs.SeatState.Id, Is.EqualTo(0x59));
Assert.That((int)StocCodecs.SeatState.Id, Is.EqualTo(0x59));
Assert.That(FeatureFlags.SeatState, Is.EqualTo(0x20));
var picking = Bytes(w => LobbyCodecs.SeatState.Write(w, new SeatState(SeatStateKind.PickingDeck)));
Assert.That(picking, Is.EqualTo(new byte[] { 1, 1, 0, 0 }));
var none = Bytes(w => LobbyCodecs.SeatState.Write(w, new SeatState(SeatStateKind.None)));
Assert.That(none, Is.EqualTo(new byte[] { 1, 0, 0, 0 }));
var stamped = Bytes(w => StocCodecs.SeatState.Write(w, new Stamped<SeatState>(2, new SeatState(SeatStateKind.PickingDeck, 7))));
Assert.That(stamped, Is.EqualTo(Concat(Le16(2), new byte[] { 1, 1 }, Le16(7))));
var read = StocCodecs.SeatState.Read(Reader(stamped));
Assert.That(read.Seat, Is.EqualTo(2));
Assert.That(read.Payload.Kind, Is.EqualTo(SeatStateKind.PickingDeck));
Assert.That(read.Payload.Value, Is.EqualTo(7));
Assert.Throws<EndOfStreamException>(() => StocCodecs.SeatState.Read(Reader(stamped.Take(5).ToArray())));
}
[Test]
public void Restart_StocSizes_ArePinned_40_23_21_19_AndShortReadsThrow()
{
var id = Enumerable.Repeat((byte)0xAB, 16).ToArray();
var token = Enumerable.Range(1, 16).Select(i => (byte)i).ToArray();
var restarting = Bytes(w => StocCodecs.RoomRestarting.Write(w, new Stamped<RoomRestarting>(0xFF, new RoomRestarting(RestartPhase.Prepare, id, 0xFF, token, 30000))));
Assert.That(restarting.Length, Is.EqualTo(40));
Assert.That(restarting, Is.EqualTo(Concat(Le16(0xFF), new byte[] { 1 }, id, new byte[] { 0xFF }, token, Le32(30000))));
var readRestarting = StocCodecs.RoomRestarting.Read(Reader(restarting));
Assert.That(readRestarting.Payload.Phase, Is.EqualTo(RestartPhase.Prepare));
Assert.That(readRestarting.Payload.Seat, Is.EqualTo(SeatRequest.Observer));
Assert.That(readRestarting.Payload.Token, Is.EqualTo(token));
Assert.Throws<EndOfStreamException>(() => StocCodecs.RoomRestarting.Read(Reader(restarting.Take(39).ToArray())));
var status = Bytes(w => StocCodecs.RestartStatus.Write(w, new Stamped<RestartStatus>(0xFF, new RestartStatus(RestartPhase.Commit, id, 2, 3, false, RestartReason.AckTimeout))));
Assert.That(status.Length, Is.EqualTo(23));
Assert.That(status, Is.EqualTo(Concat(Le16(0xFF), new byte[] { 2 }, id, new byte[] { 2, 3, 0, 2 })));
Assert.That(StocCodecs.RestartStatus.Read(Reader(status)).Payload.Reason, Is.EqualTo(RestartReason.AckTimeout));
var result = Bytes(w => StocCodecs.SeatResumeResult.Write(w, new Stamped<SeatResumeResult>(0xFF, new SeatResumeResult(id, true, 1, 0))));
Assert.That(result.Length, Is.EqualTo(21));
Assert.That(result, Is.EqualTo(Concat(Le16(0xFF), id, new byte[] { 1, 1, 0 })));
Assert.That(StocCodecs.SeatResumeResult.Read(Reader(result)).Payload.Accepted, Is.True);
var opened = Bytes(w => StocCodecs.RestartOpened.Write(w, new Stamped<RestartOpened>(0xFF, new RestartOpened(id, true))));
Assert.That(opened.Length, Is.EqualTo(19));
Assert.That(opened, Is.EqualTo(Concat(Le16(0xFF), id, new byte[] { 1 })));
Assert.Throws<EndOfStreamException>(() => StocCodecs.RestartOpened.Read(Reader(opened.Take(18).ToArray())));
Assert.That(SeatIds.Same(id, SeatIds.Fixed(id)), Is.True);
Assert.That(SeatIds.IsZero(Zeros(16)), Is.True);
}
[Test]
public void SetupPairs_GoldenBytes_MatchPatch0009_AndRoundTrip()
{
var tokens = new[] { "ygol.ruleset=rush", "ygol.scripts=a.lua,b%20c.lua" };
var golden = Concat(Le16(0xFF), Le16(2),
Le16(tokens[0].Length), Encoding.ASCII.GetBytes(tokens[0]),
Le16(tokens[1].Length), Encoding.ASCII.GetBytes(tokens[1]));
var pairs = tokens.Select(SetupPair.FromToken).ToList();
Assert.That(Bytes(w => StocCodecs.SetupPairs.Write(w, new Stamped<IReadOnlyList<SetupPair>>(0xFF, pairs))), Is.EqualTo(golden));
var read = StocCodecs.SetupPairs.Read(Reader(golden));
Assert.That(read.Seat, Is.EqualTo(Capability.ServerSender));
Assert.That(read.Payload.Select(p => p.Key), Is.EqualTo(new[] { "ygol.ruleset", "ygol.scripts" }));
Assert.That(read.Payload.Select(p => p.Value), Is.EqualTo(new[] { "rush", "a.lua,b%20c.lua" }));
Assert.That(SetupPair.FromToken("ygol.x=a=b").Value, Is.EqualTo("a=b"));
}
[Test]
public void SetupPairs_EmptyBlock_IsFourBytes_AndATruncatedTokenThrows()
{
var empty = Bytes(w => StocCodecs.SetupPairs.Write(w, new Stamped<IReadOnlyList<SetupPair>>(0xFF, new SetupPair[0])));
Assert.That(empty, Is.EqualTo(new byte[] { 0xFF, 0, 0, 0 }));
Assert.That(StocCodecs.SetupPairs.Read(Reader(empty)).Payload, Is.Empty);
var truncated = Concat(Le16(0xFF), Le16(1), Le16(10), Encoding.ASCII.GetBytes("ygol.a"));
Assert.Throws<EndOfStreamException>(() => StocCodecs.SetupPairs.Read(Reader(truncated)));
}
[Test]
public void SetupPairs_ReplayBlock_IsLengthPrefixed_AndLeavesTheNamesNext()
{
var pairs = new[] { SetupPair.FromToken("ygol.ruleset=rush") };
var bytes = Bytes(w =>
{
SetupPairsCodec.WriteReplayBlock(w, pairs);
w.Write(Utf16Fixed("Alice", 20));
});
Assert.That(bytes.Take(4), Is.EqualTo(Le32(2 + 2 + 17)));
using var reader = Reader(bytes);
Assert.That(SetupPairsCodec.ReadReplayBlock(reader).Single().Token, Is.EqualTo("ygol.ruleset=rush"));
Assert.That(Encoding.Unicode.GetString(reader.ReadBytes(10)), Is.EqualTo("Alice"));
}
[Test]
public void Sizes_ArePinned_6_8_352_354_204_206()
{
Assert.That(CapabilityCodec.Size, Is.EqualTo(6));
Assert.That(CapabilityCodec.StampedSize, Is.EqualTo(8));
Assert.That(AppearanceCodec.Size, Is.EqualTo(352));
Assert.That(AppearanceCodec.StampedSize, Is.EqualTo(354));
Assert.That(DialogueCodec.Size, Is.EqualTo(204));
Assert.That(DialogueCodec.StampedSize, Is.EqualTo(206));
Assert.That(StocCodecs.Capability.Size, Is.EqualTo(8));
Assert.That(StocCodecs.Appearance.Size, Is.EqualTo(354));
Assert.That(StocCodecs.Dialogue.Size, Is.EqualTo(206));
}
[Test]
public void Capability_GoldenBytes_MatchTheProbe_AndRoundTrip()
{
var bytes = Bytes(w => LobbyCodecs.Capability.Write(w, new Capability(Capability.ProtocolVersion, FeatureFlags.All)));
Assert.That(bytes, Is.EqualTo(Concat(Le16(8), Le32(0x7FB))));
var read = LobbyCodecs.Capability.Read(Reader(bytes));
Assert.That(read.Protocol, Is.EqualTo(Capability.ProtocolVersion));
Assert.That(read.Flags, Is.EqualTo(FeatureFlags.All));
Assert.That(read.Has(FeatureFlags.Appearance), Is.True);
Assert.That(read.Has(FeatureFlags.Dialogue), Is.True);
var appearanceOnly = LobbyCodecs.Capability.Read(Reader(Concat(Le16(1), Le32(1))));
Assert.That(appearanceOnly.Has(FeatureFlags.Appearance), Is.True);
Assert.That(appearanceOnly.Has(FeatureFlags.Dialogue), Is.False);
}
[Test]
public void Capability_TheServersOwnLine_ReadsAsSeat0xFF()
{
var bytes = Concat(Le16(0xFF), Le16(1), Le32(3));
var read = StocCodecs.Capability.Read(Reader(bytes));
Assert.That(bytes.Length, Is.EqualTo(8));
Assert.That(read.Seat, Is.EqualTo(Capability.ServerSender));
Assert.That(read.Payload.Protocol, Is.EqualTo(1));
Assert.That(read.Payload.Flags, Is.EqualTo(3));
var seated = StocCodecs.Capability.Read(Reader(Bytes(w =>
StocCodecs.Capability.Write(w, new Stamped<Capability>(2, new Capability(1, 1))))));
Assert.That(seated.Seat, Is.EqualTo(2));
Assert.That(seated.Payload.Flags, Is.EqualTo(1));
Assert.Throws<EndOfStreamException>(() => StocCodecs.Capability.Read(Reader(Concat(Le16(0xFF), Le16(1)))));
}
[Test]
public void Appearance_GoldenBytes_MatchTheProbe_AndRoundTrip()
{
var state = new AppearanceState(ProbeItems, "deck-1", "character-1", "group-1");
var bytes = Bytes(w => LobbyCodecs.Appearance.Write(w, state));
Assert.That(bytes.Length, Is.EqualTo(352));
Assert.That(bytes, Is.EqualTo(ProbeAppearance()));
var read = LobbyCodecs.Appearance.Read(Reader(bytes));
Assert.That(read.Items, Is.EqualTo(ProbeItems));
Assert.That(read.DeckName, Is.EqualTo("deck-1"));
Assert.That(read.CharacterId, Is.EqualTo("character-1"));
Assert.That(read.DeckGroup, Is.EqualTo("group-1"));
}
[Test]
public void Appearance_Stamped_Is354Bytes_AndA353ByteReadThrows()
{
var bytes = Bytes(w => StocCodecs.Appearance.Write(w,
new Stamped<AppearanceState>(1, new AppearanceState(ProbeItems, "deck-1", "character-1", "group-1"))));
Assert.That(bytes.Length, Is.EqualTo(354));
Assert.That(bytes, Is.EqualTo(Concat(Le16(1), ProbeAppearance())));
var read = StocCodecs.Appearance.Read(Reader(bytes));
Assert.That(read.Seat, Is.EqualTo(1));
Assert.That(read.Payload.DeckName, Is.EqualTo("deck-1"));
Assert.That(read.Payload.Items[7], Is.EqualTo(1017));
var short353 = bytes.Take(353).ToArray();
Assert.Throws<EndOfStreamException>(() => StocCodecs.Appearance.Read(Reader(short353)));
}
[Test]
public void Appearance_A49CharDeckName_ClipsTo47PlusTerminator()
{
var name = new string('x', 49);
var bytes = Bytes(w => LobbyCodecs.Appearance.Write(w, new AppearanceState(ProbeItems, name, "", "")));
Assert.That(bytes.Length, Is.EqualTo(352));
var nameSlot = bytes.Skip(32).Take(96).ToArray();
Assert.That(nameSlot, Is.EqualTo(Concat(Utf16(new string('x', 47)), Zeros(2))));
Assert.That(LobbyCodecs.Appearance.Read(Reader(bytes)).DeckName, Is.EqualTo(new string('x', 47)));
Assert.That(LobbyCodecs.Appearance.Read(Reader(bytes)).CharacterId, Is.EqualTo(""));
}
[Test]
public void Appearance_NullFields_WriteZeros_AndReadEmpty()
{
var bytes = Bytes(w => LobbyCodecs.Appearance.Write(w, new AppearanceState(null, null, null, null)));
Assert.That(bytes.Length, Is.EqualTo(352));
Assert.That(bytes, Is.All.EqualTo(0));
var read = LobbyCodecs.Appearance.Read(Reader(bytes));
Assert.That(read.Items, Is.All.EqualTo(0));
Assert.That(read.DeckName, Is.EqualTo(""));
Assert.That(read.CharacterId, Is.EqualTo(""));
Assert.That(read.DeckGroup, Is.EqualTo(""));
}
[Test]
public void Dialogue_GoldenBytes_MatchTheProbe_AndRoundTrip()
{
var line = new DialogueLine(1, true, 4, 4004, 5004, "corpus-4", "event-4");
var bytes = Bytes(w => LobbyCodecs.Dialogue.Write(w, line));
Assert.That(bytes.Length, Is.EqualTo(204));
Assert.That(bytes, Is.EqualTo(ProbeDialogue()));
var read = LobbyCodecs.Dialogue.Read(Reader(bytes));
Assert.That(read.Version, Is.EqualTo(1));
Assert.That(read.TargetFaceDown, Is.True);
Assert.That(read.Index, Is.EqualTo(4));
Assert.That(read.CardCode, Is.EqualTo(4004));
Assert.That(read.TargetCode, Is.EqualTo(5004));
Assert.That(read.CorpusKey, Is.EqualTo("corpus-4"));
Assert.That(read.EventKey, Is.EqualTo("event-4"));
}
[Test]
public void Dialogue_Stamped_Is206Bytes_AndAHelloLineRoundTrips()
{
var hello = new DialogueLine(DialogueLine.CurrentVersion, false, -1, 0, 0, null, null);
var bytes = Bytes(w => StocCodecs.Dialogue.Write(w, new Stamped<DialogueLine>(0, hello)));
Assert.That(bytes.Length, Is.EqualTo(206));
Assert.That(bytes.Skip(2).Take(2), Is.EqualTo(new byte[] { 1, 0 }));
Assert.That(bytes.Skip(4).Take(2), Is.EqualTo(Le16(unchecked((short)-1))));
var read = StocCodecs.Dialogue.Read(Reader(bytes));
Assert.That(read.Seat, Is.EqualTo(0));
Assert.That(read.Payload.Index, Is.EqualTo(-1));
Assert.That(read.Payload.TargetFaceDown, Is.False);
Assert.That(read.Payload.CorpusKey, Is.EqualTo(""));
Assert.That(read.Payload.EventKey, Is.EqualTo(""));
Assert.Throws<EndOfStreamException>(() => StocCodecs.Dialogue.Read(Reader(bytes.Take(205).ToArray())));
}
}
}