Files
SVSimServer/SVSim.BattleEngine.Tests/SessionEngine/SessionEngineShadowReplayTests.cs
gamer147 35e9847911 feat(battlenode): receive conductor resolves self Deal+Play headless via view-untangle (M-HC-0)
The engine's receive CONDUCTOR fuses each authoritative mutation behind a view
call: the play mutation is an InstantVfx registered to VfxMgr, and the deal hand
is seated by MulliganPhaseBase.StartDeal wired to OperateReceive.OnReceiveDeal.
Headless, the shared VfxMgr no-op'd registration (correct for the direct
ActionProcessor path the M2-M12 oracles use) and OnReceiveDeal was never wired,
so the receive path resolved nothing.

Untangle (Candidate B, zero Engine logic edits):
- InstantVfx.Run() opt-in executor (authored shim).
- HeadlessConductorVfxMgr : VfxMgr runs registered InstantVfx; wired only via the
  node's SessionContentsCreator.CreateVfxMgr (verified the receive mgr's VfxMgr
  comes from there — BattleManagerBase.cs:768). M2-M12 use HeadlessContentsCreator,
  so they're isolated by construction.
- WireMulliganPhase: construct NetworkMulliganPhase + MulliganEventSetting() to
  install OnReceiveDeal -> StartDeal (the node never pumps the phase machine).

View no-op surface (the 7 from the probe, minus 1 not hit; +1 emergent):
- Deal wiring (NetworkMulliganPhase) [node seed]
- MulliganInfoControl._partsPlayer/_partsOpponent._exchangeMark/_keepZone/_abandonZone [node seed: prefab + SeedMulliganInfoControl]
- Data.BattleRecoveryInfo (IsMulliganEnd=false) [EngineGlobalInit seed]
- IBattlePlayerView.PlayQueueView -> HeadlessPlayQueueViewStub [_IfaceImpl.g.cs, both getters]
- DetailMgr.DetailPanelControl/SubDetailPanelControl [node seed]
- BattleCardIconAnimations.collection (emergent: UpdateInPlayBattleCardIconLabel) -> HeadlessIconAnimations empty SkillCollectionBase [_IfaceImpl.g.cs]
- BattleMenuBtn (probe item 7): NOT hit on the vanilla path; not seeded.

Oracle (HeadlessConductorTests): node Deal seats 3-card hand; a vanilla
hand-card Play leaves hand (-1), adds board (+1), drops PP by cost.

Regression: 24/24 BattleEngine.Tests oracles (M2-M12) green; 241/241
SVSim.UnitTests BattleNode green. The 2 SessionEngine capture-replay shadow
tests are marked Ignore (superseded): they passed VACUOUSLY when the receive
path resolved nothing; with resolution live they hit the documented
capture-replay draw-misalignment artifact. Node-native battles are the oracle.
Drift: no drift.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-06 20:08:53 -04:00

98 lines
5.1 KiB
C#

using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using SVSim.BattleNode.Protocol;
using SVSim.BattleNode.Sessions.Engine;
namespace SVSim.BattleEngine.Tests.SessionEngine
{
[TestFixture]
public class SessionEngineShadowReplayTests
{
// Frames that are transport/keepalive, not game actions — not ingested.
private static readonly HashSet<string> SkipUris = new()
{
nameof(NetworkBattleUri.Echo),
nameof(NetworkBattleUri.ChatStamp),
nameof(NetworkBattleUri.Gungnir),
};
[Test]
public void Shadow_replay_of_captured_battle_tracks_state_without_desync()
{
// SUPERSEDED by the node-native oracle (SVSim.UnitTests HeadlessConductorTests). This test's
// "0 rejects" used to pass VACUOUSLY: before the M-HC-0b view-untangle the receive conductor
// resolved NOTHING headless (InstantVfx mutations no-op'd; OnReceiveDeal unwired), so no
// captured frame could diverge because none was applied. The retracted "shadow tracks the
// capture" claim is documented in memory project_battle_node_engine_shadow / _headless_conductor.
// Now that the conductor RESOLVES, replaying a captured stream against a node-seated deck hits
// the documented capture-replay draw-misalignment: the seated deck order can't reproduce the
// capture's post-mulligan idx references, so played cards aren't in the seated hand
// (HandCardToField/RemoveSpellCardFromHand: not found). The decision (memory
// project_battle_headless_conductor) is to validate headless resolution via NODE-NATIVE
// battles, not capture replay. The node-native oracle now covers Deal+Play.
Assert.Ignore("Superseded by node-native HeadlessConductorTests (M-HC-0b). Capture-replay " +
"against a node-seated deck hits the documented draw-misalignment artifact once the " +
"receive path actually resolves. Revive if a capture-replay alignment path lands.");
HeadlessEngineEnv.EnsureInitialized();
var cl1 = CaptureReplay.Load("battle_test_cl1.ndjson");
var cl2 = CaptureReplay.Load("battle_test_cl2.ndjson");
var deckA = CaptureReplay.SelfDeckFrom(cl1);
var deckB = CaptureReplay.SelfDeckFrom(cl2);
// One Load call with every id — Load replaces the static master each call.
HeadlessCardMaster.Load(deckA.Concat(deckB).Select(x => (int)x).Distinct().ToArray());
var engine = new SessionBattleEngine();
engine.Setup(masterSeed: CaptureReplay.SeedFrom(cl1), seatADeck: deckA, seatBDeck: deckB);
// Single-client full-stream replay (cl1 as the player seat): cl1's SENT frames are its own
// actions (seat=true); its RECEIVED frames are the opponent/server actions (seat=false),
// incl. the Deal that establishes both hands. This is exactly the stream cl1's receiver
// processed, in capture (ts) order. (The node-side both-clients-sends model is exercised
// live in Task 7; here we validate engine tracking against ground truth.)
var stream = cl1.Where(f => !SkipUris.Contains(f.Uri))
.OrderBy(f => f.Ts)
.ToList();
var rejects = new List<string>();
var violations = new List<string>();
foreach (var f in stream)
{
bool seat = f.Direction == "send";
var r = engine.Receive(f.Env, isPlayerSeat: seat);
if (r.RejectReason is not null)
rejects.Add($"{f.Direction} {f.Uri}: {r.RejectReason}");
if (f.Uri == nameof(NetworkBattleUri.TurnEnd))
CheckInvariants(engine, violations, atUri: f.Uri);
}
foreach (var line in rejects) TestContext.WriteLine("REJECT " + line);
foreach (var line in violations) TestContext.WriteLine("VIOLATION " + line);
TestContext.WriteLine($"frames={stream.Count} rejects={rejects.Count} violations={violations.Count}");
Assert.Multiple(() =>
{
Assert.That(rejects, Is.Empty, "engine diverged / rejected a captured frame");
Assert.That(violations, Is.Empty, "engine state left a structural invariant");
});
}
private static void CheckInvariants(SessionBattleEngine engine, List<string> violations, string atUri)
{
foreach (var seat in new[] { true, false })
{
int life = engine.LeaderLife(seat), pp = engine.Pp(seat);
int board = engine.BoardCount(seat), hand = engine.HandCount(seat);
if (life is < 0 or > 20) violations.Add($"{atUri} seat={seat} life={life}");
if (pp is < 0 or > 10) violations.Add($"{atUri} seat={seat} pp={pp}");
if (board is < 0 or > 7) violations.Add($"{atUri} seat={seat} board={board}");
if (hand is < 0 or > 9) violations.Add($"{atUri} seat={seat} hand={hand}");
}
}
}
}