Add CaptureConformanceTests: drive one Scripted lifecycle, harvest all ten server-authored synchronize frames (InitNetwork/Matched/BattleStart/Deal/Swap/ Ready/TurnStart/TurnEnd/Judge/BattleFinish), re-serialize via MsgEnvelope.ToJson, and diff each against representative prod TK2 capture frames embedded as a fixture. Comparison is capture-subset-of-ours on body shape (recursive keys + value category), so missing/miscased/mistyped fields fail but extra envelope fields we emit don't; pure sequencing keys are excluded. Because PvP reuses the same ScriptedLifecycle builders for the handshake/mulligan frames, this transitively locks the PvP handshake shape -- a regression oracle that outlives the June-2026 server shutdown. Also replace the stale v1-only README with a pointer to the canonical docs/battle-node.md hub (outer repo). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
301 lines
14 KiB
C#
301 lines
14 KiB
C#
using System.Text.Json;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using NUnit.Framework;
|
|
using SVSim.BattleNode.Bridge;
|
|
using SVSim.BattleNode.Protocol;
|
|
using SVSim.BattleNode.Wire;
|
|
using SVSim.UnitTests.Infrastructure;
|
|
|
|
namespace SVSim.UnitTests.BattleNode.Integration;
|
|
|
|
/// <summary>
|
|
/// Wire-shape conformance of our server-authored synchronize frames against real prod TK2
|
|
/// captures (<c>data_dumps/captures/battle-traffic_tk2_regular.ndjson</c> +
|
|
/// <c>…_tk2_second.ndjson</c>, captured 2026-05-31 from a real client mid-PvP).
|
|
///
|
|
/// <para><b>What this guards:</b> for every frame our server *authors* (as opposed to forwarding a
|
|
/// client's bytes), the payload it emits must carry every key prod sent, with a matching value
|
|
/// *category* (object / array / string / number / bool). This is the bug class that has bitten the
|
|
/// node repeatedly — wrong casing (<c>card_id</c> vs <c>cardID</c>), a missing field the client
|
|
/// reads without a guard, or a string where the client expects a number. The existing
|
|
/// <see cref="BattleNodeFlowTests"/> assert frame *ordering and routing*; they never inspect the
|
|
/// body. This closes that gap and turns the prod captures into a permanent regression oracle that
|
|
/// survives the June-2026 server shutdown.</para>
|
|
///
|
|
/// <para><b>Direction of the check is capture ⊆ ours</b> — we must emit at least what prod emits
|
|
/// (missing/miscased/mistyped = fail), but we may emit extra envelope fields (we send
|
|
/// <c>viewerId/uuid/try/cat</c> on pushes; prod's receive frames omit them). Pure
|
|
/// envelope/sequencing keys (<c>viewerId, uuid, try, cat, bid, pubSeq, playSeq</c>) are excluded
|
|
/// from the comparison: they're transport concerns assigned by the sequencer, covered by the
|
|
/// reliability layer + integration tests, and legitimately vary per frame (e.g. the no-stock
|
|
/// <c>BattleFinish</c> frame is played immediately whether or not it carries a <c>playSeq</c>).
|
|
/// The check is on *body shape*.</para>
|
|
///
|
|
/// <para><b>Coverage:</b> a single Scripted session emits all ten server-authored URIs
|
|
/// (<c>InitNetwork, Matched, BattleStart, Deal, Swap, Ready, TurnStart, TurnEnd, Judge,
|
|
/// BattleFinish</c>). PvP uses the same <see cref="SVSim.BattleNode.Lifecycle.ScriptedLifecycle"/>
|
|
/// builders for the handshake/mulligan frames, so this transitively covers the PvP handshake shape
|
|
/// too. Forwarded frames (<c>PlayActions / TurnEndActions / ChatStamp / TurnEndFinal</c>) relay the
|
|
/// client's own bytes verbatim, so their shape is the client's contract, not ours — out of scope
|
|
/// here.</para>
|
|
/// </summary>
|
|
[TestFixture]
|
|
public class CaptureConformanceTests
|
|
{
|
|
private const long ViewerId = 906243102L;
|
|
|
|
// Top-level keys that are envelope/transport, not body shape. Excluded from the comparison
|
|
// at the root level only (nested objects never contain these).
|
|
private static readonly HashSet<string> IgnoredEnvelopeKeys = new()
|
|
{
|
|
"viewerId", "uuid", "try", "cat", "bid", "pubSeq", "playSeq",
|
|
};
|
|
|
|
[Test]
|
|
[Timeout(30000)]
|
|
public async Task ServerAuthoredFrames_MatchProdCaptureShapes()
|
|
{
|
|
await using var factory = new SVSimTestFactory();
|
|
var bridge = factory.Services.GetRequiredService<IMatchingBridge>();
|
|
|
|
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(15));
|
|
var ct = cts.Token;
|
|
|
|
var pending = bridge.RegisterBattle(
|
|
new BattlePlayer(ViewerId, BattleNodeFlowTests.FixtureCtx()),
|
|
p2: null,
|
|
SVSim.BattleNode.Sessions.BattleType.Scripted);
|
|
|
|
var key = MakeKey();
|
|
var encryptedVid = NodeCrypto.EncryptForNode(ViewerId.ToString(), key);
|
|
var wsUri = new Uri(
|
|
$"ws://localhost/socket.io/?BattleId={pending.BattleId}&viewerId={Uri.EscapeDataString(encryptedVid)}&EIO=3&transport=websocket");
|
|
|
|
var wsClient = factory.Server.CreateWebSocketClient();
|
|
var ws = await wsClient.ConnectAsync(wsUri, ct);
|
|
await using var client = new RawSocketIoTestClient(ws);
|
|
await client.ConsumeHandshakeAsync(ct);
|
|
|
|
// Drive the full Scripted lifecycle, harvesting every server-pushed frame by URI.
|
|
var harvested = new Dictionary<NetworkBattleUri, MsgEnvelope>();
|
|
|
|
async Task DriveAsync(NetworkBattleUri send, long pubSeq, int expectPushes,
|
|
Dictionary<string, object?>? body = null)
|
|
{
|
|
await client.SendMsgAsync(MakeEnvelope(send, pubSeq, body), key, ct);
|
|
for (var i = 0; i < expectPushes; i++)
|
|
{
|
|
var frame = await client.ReceiveSynchronizeAsync(ct);
|
|
harvested[frame.Uri] = frame;
|
|
}
|
|
}
|
|
|
|
await DriveAsync(NetworkBattleUri.InitNetwork, 1, expectPushes: 1);
|
|
await DriveAsync(NetworkBattleUri.InitBattle, 2, expectPushes: 1); // Matched
|
|
await DriveAsync(NetworkBattleUri.Loaded, 3, expectPushes: 2); // BattleStart + Deal
|
|
await DriveAsync(NetworkBattleUri.Swap, 4, expectPushes: 2, // Swap + Ready
|
|
body: new Dictionary<string, object?> { ["idxList"] = new List<object?>() });
|
|
await DriveAsync(NetworkBattleUri.TurnEnd, 5, expectPushes: 3); // TurnStart + TurnEnd + Judge
|
|
await DriveAsync(NetworkBattleUri.Retire, 6, expectPushes: 1); // BattleFinish
|
|
|
|
// Compare each harvested frame's wire JSON against the prod capture fixture.
|
|
using var fixtures = JsonDocument.Parse(ProdCaptureFixture.Json);
|
|
var failures = new List<string>();
|
|
|
|
foreach (var uriName in ExpectedUris)
|
|
{
|
|
var uri = Enum.Parse<NetworkBattleUri>(uriName);
|
|
if (!harvested.TryGetValue(uri, out var env))
|
|
{
|
|
failures.Add($"[{uriName}] our server never pushed this frame during the Scripted lifecycle.");
|
|
continue;
|
|
}
|
|
|
|
var expected = fixtures.RootElement.GetProperty(uriName);
|
|
using var ourDoc = JsonDocument.Parse(MsgEnvelope.ToJson(env));
|
|
CompareSubset(expected, ourDoc.RootElement, uriName, isRoot: true, failures);
|
|
}
|
|
|
|
if (failures.Count > 0)
|
|
{
|
|
Assert.Fail(
|
|
"Server-authored frames diverge from the prod TK2 capture shapes:\n - " +
|
|
string.Join("\n - ", failures));
|
|
}
|
|
}
|
|
|
|
private static readonly string[] ExpectedUris =
|
|
{
|
|
"InitNetwork", "Matched", "BattleStart", "Deal", "Swap", "Ready",
|
|
"TurnStart", "TurnEnd", "Judge", "BattleFinish",
|
|
};
|
|
|
|
/// <summary>
|
|
/// Recursively assert every key/element in <paramref name="expected"/> (the prod capture)
|
|
/// exists in <paramref name="actual"/> (our wire JSON) with a matching value category.
|
|
/// </summary>
|
|
private static void CompareSubset(JsonElement expected, JsonElement actual, string path,
|
|
bool isRoot, List<string> failures)
|
|
{
|
|
switch (expected.ValueKind)
|
|
{
|
|
case JsonValueKind.Object:
|
|
if (actual.ValueKind != JsonValueKind.Object)
|
|
{
|
|
failures.Add($"{path}: prod is an object, ours is {actual.ValueKind}");
|
|
return;
|
|
}
|
|
foreach (var prop in expected.EnumerateObject())
|
|
{
|
|
if (isRoot && IgnoredEnvelopeKeys.Contains(prop.Name)) continue;
|
|
if (!actual.TryGetProperty(prop.Name, out var av))
|
|
{
|
|
failures.Add($"{path}.{prop.Name}: MISSING — prod sends this key, we don't");
|
|
continue;
|
|
}
|
|
CompareSubset(prop.Value, av, $"{path}.{prop.Name}", isRoot: false, failures);
|
|
}
|
|
break;
|
|
|
|
case JsonValueKind.Array:
|
|
if (actual.ValueKind != JsonValueKind.Array)
|
|
{
|
|
failures.Add($"{path}: prod is an array, ours is {actual.ValueKind}");
|
|
return;
|
|
}
|
|
if (expected.GetArrayLength() > 0)
|
|
{
|
|
if (actual.GetArrayLength() == 0)
|
|
{
|
|
failures.Add($"{path}: prod array is non-empty, ours is empty");
|
|
return;
|
|
}
|
|
// Arrays here are uniform (decks, pos/idx lists) — element 0 defines the shape.
|
|
CompareSubset(expected[0], actual[0], $"{path}[0]", isRoot: false, failures);
|
|
}
|
|
break;
|
|
|
|
case JsonValueKind.Null:
|
|
// Can't infer an expected type from a null; accept whatever we emit.
|
|
break;
|
|
|
|
default:
|
|
var ec = Category(expected.ValueKind);
|
|
var ac = Category(actual.ValueKind);
|
|
if (ec != ac)
|
|
{
|
|
failures.Add(
|
|
$"{path}: type mismatch — prod is {ec} ({Trunc(expected)}), ours is {ac} ({Trunc(actual)})");
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
|
|
private static string Category(JsonValueKind k) => k switch
|
|
{
|
|
JsonValueKind.String => "string",
|
|
JsonValueKind.Number => "number",
|
|
JsonValueKind.True or JsonValueKind.False => "bool",
|
|
JsonValueKind.Null => "null",
|
|
JsonValueKind.Object => "object",
|
|
JsonValueKind.Array => "array",
|
|
_ => k.ToString(),
|
|
};
|
|
|
|
private static string Trunc(JsonElement el)
|
|
{
|
|
var s = el.GetRawText();
|
|
return s.Length > 40 ? s[..40] + "…" : s;
|
|
}
|
|
|
|
private static MsgEnvelope MakeEnvelope(NetworkBattleUri uri, long pubSeq,
|
|
Dictionary<string, object?>? body = null) =>
|
|
new(uri, ViewerId: ViewerId, Uuid: "udid-test", Bid: null, Try: 0,
|
|
Cat: uri == NetworkBattleUri.InitNetwork ? EmitCategory.General
|
|
: uri == NetworkBattleUri.InitBattle ? EmitCategory.Matching
|
|
: EmitCategory.Battle,
|
|
PubSeq: pubSeq, PlaySeq: null, Body: new RawBody(body ?? new Dictionary<string, object?>()));
|
|
|
|
private static string MakeKey()
|
|
{
|
|
var seq = 0;
|
|
return NodeCrypto.GenerateKey(() => (seq++ * 13) % 16);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Representative server→client (<c>receive</c>) frames lifted verbatim from the prod TK2 captures.
|
|
/// One frame per server-authored URI, picked as the richest observed instance. The
|
|
/// <c>selfDeck</c> in <c>Matched</c> is trimmed to three cards (the array is uniform — three
|
|
/// entries are enough to lock the element shape). Numbers and string/number typing are preserved
|
|
/// exactly as captured, including the deliberate prod quirk that <c>BattleStart.selfInfo.battlePoint</c>
|
|
/// is a string while <c>oppoInfo.battlePoint</c> is a number.
|
|
///
|
|
/// Provenance (line numbers in the capture files):
|
|
/// InitNetwork regular:1 | Matched regular:2 | BattleStart regular:3
|
|
/// Deal regular:4 | Swap regular:7 | Ready regular:9
|
|
/// TurnStart regular:14 | TurnEnd regular:18 | Judge regular:20
|
|
/// BattleFinish regular:274 (result=102, a real loss capture)
|
|
/// </summary>
|
|
internal static class ProdCaptureFixture
|
|
{
|
|
public const string Json = """
|
|
{
|
|
"InitNetwork": { "uri": "InitNetwork", "resultCode": 1 },
|
|
"Matched": {
|
|
"uri": "Matched",
|
|
"selfInfo": {
|
|
"country_code": "KOR", "userName": "combusty7", "sleeveId": "3000011",
|
|
"emblemId": "701441011", "degreeId": "300003", "fieldId": 43,
|
|
"isOfficial": 0, "oppoId": 847666884, "seed": 17548138
|
|
},
|
|
"oppoInfo": {
|
|
"country_code": "JPN", "userName": "AtagoSuki", "sleeveId": "704141010",
|
|
"emblemId": "400001100", "degreeId": "120027", "fieldId": 5,
|
|
"isOfficial": 0, "oppoId": 906243102, "seed": 17548138, "oppoDeckCount": 30
|
|
},
|
|
"selfDeck": [
|
|
{ "idx": 1, "cardId": 128111020 },
|
|
{ "idx": 2, "cardId": 128121010 },
|
|
{ "idx": 3, "cardId": 127134010 }
|
|
],
|
|
"bid": "975695075012", "playSeq": 1, "resultCode": 1
|
|
},
|
|
"BattleStart": {
|
|
"uri": "BattleStart",
|
|
"turnState": 0,
|
|
"selfInfo": {
|
|
"rank": "10", "battlePoint": "6270", "classId": "1", "charaId": "1",
|
|
"cardMasterName": "card_master_node_10015"
|
|
},
|
|
"oppoInfo": {
|
|
"rank": "25", "isMasterRank": "1", "battlePoint": 50000, "masterPoint": "2144",
|
|
"classId": "8", "charaId": "4608", "cardMasterName": "card_master_node_10015"
|
|
},
|
|
"battleType": 11, "resultCode": 1, "playSeq": 2
|
|
},
|
|
"Deal": {
|
|
"uri": "Deal",
|
|
"self": [ { "pos": 0, "idx": 2 }, { "pos": 1, "idx": 16 }, { "pos": 2, "idx": 25 } ],
|
|
"oppo": [ { "pos": 0, "idx": 28 }, { "pos": 1, "idx": 20 }, { "pos": 2, "idx": 18 } ],
|
|
"playSeq": 3, "resultCode": 1
|
|
},
|
|
"Swap": {
|
|
"uri": "Swap",
|
|
"self": [ { "pos": 0, "idx": 2 }, { "pos": 1, "idx": 16 }, { "pos": 2, "idx": 25 } ],
|
|
"playSeq": 4, "resultCode": 1
|
|
},
|
|
"Ready": {
|
|
"uri": "Ready",
|
|
"self": [ { "pos": 0, "idx": 2 }, { "pos": 1, "idx": 16 }, { "pos": 2, "idx": 25 } ],
|
|
"oppo": [ { "pos": 0, "idx": 28 }, { "pos": 1, "idx": 24 }, { "pos": 2, "idx": 18 } ],
|
|
"idxChangeSeed": 771335280, "spin": 243, "playSeq": 5, "resultCode": 1
|
|
},
|
|
"TurnStart": { "uri": "TurnStart", "spin": 189, "resultCode": 1, "playSeq": 6 },
|
|
"TurnEnd": { "uri": "TurnEnd", "turnState": 0, "resultCode": 1, "playSeq": 8 },
|
|
"Judge": { "uri": "Judge", "spin": 55, "playSeq": 9, "resultCode": 1 },
|
|
"BattleFinish": { "uri": "BattleFinish", "result": 102, "playSeq": 99, "resultCode": 1 }
|
|
}
|
|
""";
|
|
}
|