refactor(battle-node): scripted bot drives the handshake as a real participant
Implements IHasHandshakePhase and emits client-shaped InitNetwork/InitBattle/ Loaded/Swap (reacting to the session's pushes) instead of being a passive TurnEnd-only fixture the session narrates around. This is what lets the type-agnostic mulligan barrier (next task) work in Scripted mode. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
using NUnit.Framework;
|
||||
using SVSim.BattleNode.Bridge;
|
||||
using SVSim.BattleNode.Lifecycle;
|
||||
using SVSim.BattleNode.Protocol;
|
||||
using SVSim.BattleNode.Protocol.Bodies;
|
||||
using SVSim.BattleNode.Sessions;
|
||||
@@ -10,6 +10,33 @@ namespace SVSim.UnitTests.BattleNode.Sessions.Participants;
|
||||
[TestFixture]
|
||||
public class ScriptedBotParticipantTests
|
||||
{
|
||||
[Test]
|
||||
public async Task RunAsync_emits_InitNetwork_to_kick_off_handshake()
|
||||
{
|
||||
var p = new ScriptedBotParticipant();
|
||||
var emitted = new List<NetworkBattleUri>();
|
||||
p.FrameEmitted += (env, _) => { emitted.Add(env.Uri); return Task.CompletedTask; };
|
||||
|
||||
await p.RunAsync(CancellationToken.None);
|
||||
|
||||
Assert.That(emitted, Is.EqualTo(new[] { NetworkBattleUri.InitNetwork }));
|
||||
}
|
||||
|
||||
[TestCase(NetworkBattleUri.InitNetwork, NetworkBattleUri.InitBattle)]
|
||||
[TestCase(NetworkBattleUri.Matched, NetworkBattleUri.Loaded)]
|
||||
[TestCase(NetworkBattleUri.Deal, NetworkBattleUri.Swap)]
|
||||
public async Task PushAsync_handshake_push_emits_next_client_frame(
|
||||
NetworkBattleUri pushed, NetworkBattleUri expectedEmit)
|
||||
{
|
||||
var p = new ScriptedBotParticipant();
|
||||
var emitted = new List<NetworkBattleUri>();
|
||||
p.FrameEmitted += (env, _) => { emitted.Add(env.Uri); return Task.CompletedTask; };
|
||||
|
||||
await p.PushAsync(NewEnvelope(pushed), noStock: false, CancellationToken.None);
|
||||
|
||||
Assert.That(emitted, Is.EqualTo(new[] { expectedEmit }));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task PushAsync_TurnEnd_fires_three_FrameEmitted_in_order()
|
||||
{
|
||||
@@ -21,31 +48,12 @@ public class ScriptedBotParticipantTests
|
||||
|
||||
Assert.That(emitted, Is.EqualTo(new[]
|
||||
{
|
||||
NetworkBattleUri.TurnStart,
|
||||
NetworkBattleUri.TurnEnd,
|
||||
NetworkBattleUri.Judge,
|
||||
NetworkBattleUri.TurnStart, NetworkBattleUri.TurnEnd, NetworkBattleUri.Judge,
|
||||
}));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task PushAsync_TurnEndFinal_does_NOT_fire_burst()
|
||||
{
|
||||
// TurnEndFinal is the game-end signal — owned by BattleSession's TurnEndFinal
|
||||
// dispatch arm, which pushes BattleFinish per-side. The bot no longer reacts to
|
||||
// it; reacting would race the BattleFinish with the no-longer-needed 3-frame
|
||||
// burst. Only regular TurnEnd triggers the burst.
|
||||
var p = new ScriptedBotParticipant();
|
||||
var fired = 0;
|
||||
p.FrameEmitted += (_, _) => { fired++; return Task.CompletedTask; };
|
||||
|
||||
await p.PushAsync(NewEnvelope(NetworkBattleUri.TurnEndFinal), noStock: false, CancellationToken.None);
|
||||
|
||||
Assert.That(fired, Is.EqualTo(0),
|
||||
"TurnEndFinal must not trigger the bot's burst — the dispatch arm pushes BattleFinish directly.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task PushAsync_other_uris_do_not_fire()
|
||||
public async Task PushAsync_non_reactive_uris_emit_nothing()
|
||||
{
|
||||
var p = new ScriptedBotParticipant();
|
||||
var fired = 0;
|
||||
@@ -53,10 +61,9 @@ public class ScriptedBotParticipantTests
|
||||
|
||||
foreach (var uri in new[]
|
||||
{
|
||||
NetworkBattleUri.Matched, NetworkBattleUri.BattleStart, NetworkBattleUri.Deal,
|
||||
NetworkBattleUri.Swap, NetworkBattleUri.Ready, NetworkBattleUri.PlayActions,
|
||||
NetworkBattleUri.TurnStart, NetworkBattleUri.TurnEndActions, NetworkBattleUri.Echo,
|
||||
NetworkBattleUri.Judge, NetworkBattleUri.BattleFinish,
|
||||
NetworkBattleUri.BattleStart, NetworkBattleUri.Swap, NetworkBattleUri.Ready,
|
||||
NetworkBattleUri.PlayActions, NetworkBattleUri.TurnStart, NetworkBattleUri.Echo,
|
||||
NetworkBattleUri.Judge, NetworkBattleUri.BattleFinish, NetworkBattleUri.TurnEndFinal,
|
||||
})
|
||||
{
|
||||
await p.PushAsync(NewEnvelope(uri), noStock: false, CancellationToken.None);
|
||||
@@ -66,11 +73,11 @@ public class ScriptedBotParticipantTests
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task RunAsync_returns_immediately()
|
||||
public void Implements_IHasHandshakePhase_starting_at_AwaitingInitNetwork()
|
||||
{
|
||||
var p = new ScriptedBotParticipant();
|
||||
await p.RunAsync(CancellationToken.None);
|
||||
Assert.Pass();
|
||||
Assert.That(p, Is.InstanceOf<IHasHandshakePhase>());
|
||||
Assert.That(((IHasHandshakePhase)p).Phase, Is.EqualTo(BattleSessionPhase.AwaitingInitNetwork));
|
||||
}
|
||||
|
||||
private static MsgEnvelope NewEnvelope(NetworkBattleUri uri) =>
|
||||
|
||||
Reference in New Issue
Block a user