Files
SVSimServer/SVSim.UnitTests/BattleNode/Sessions/BattleSessionDispatchTests.cs
2026-05-31 22:21:55 -04:00

65 lines
2.7 KiB
C#

// SVSim.UnitTests/BattleNode/Sessions/BattleSessionDispatchTests.cs
using Microsoft.Extensions.Logging.Abstractions;
using NUnit.Framework;
using SVSim.BattleNode.Protocol;
using SVSim.BattleNode.Sessions;
namespace SVSim.UnitTests.BattleNode.Sessions;
[TestFixture]
public class BattleSessionDispatchTests
{
private static BattleSession NewSession()
{
// ws is unused by ComputeResponses; pass null! and rely on the test never invoking the pump.
return new BattleSession(ws: null!, battleId: "bid-1", viewerId: 1, log: NullLogger<BattleSession>.Instance);
}
private static MsgEnvelope NewEnvelope(NetworkBattleUri uri) =>
new(uri, ViewerId: 1, Uuid: "u", Bid: null, Try: 0, Cat: EmitCategory.Battle,
PubSeq: null, PlaySeq: null, Body: new Dictionary<string, object?>());
[Test]
public void InitNetwork_PushesAckThenMatchedThenBattleStart()
{
var s = NewSession();
var responses = s.ComputeResponses(NewEnvelope(NetworkBattleUri.InitNetwork));
Assert.That(responses.Select(r => r.Envelope.Uri),
Is.EqualTo(new[] { NetworkBattleUri.InitNetwork, NetworkBattleUri.Matched, NetworkBattleUri.BattleStart }));
Assert.That(s.Phase, Is.EqualTo(BattleSessionPhase.AwaitingLoaded));
}
[Test]
public void Loaded_PushesDeal_TransitionsToAwaitingSwap()
{
var s = NewSession();
s.ComputeResponses(NewEnvelope(NetworkBattleUri.InitNetwork)); // advance phase
var responses = s.ComputeResponses(NewEnvelope(NetworkBattleUri.Loaded));
Assert.That(responses.Single().Envelope.Uri, Is.EqualTo(NetworkBattleUri.Deal));
Assert.That(s.Phase, Is.EqualTo(BattleSessionPhase.AwaitingSwap));
}
[Test]
public void Swap_PushesSwapResponseThenReady_TransitionsToAfterReady()
{
var s = NewSession();
s.ComputeResponses(NewEnvelope(NetworkBattleUri.InitNetwork));
s.ComputeResponses(NewEnvelope(NetworkBattleUri.Loaded));
var responses = s.ComputeResponses(NewEnvelope(NetworkBattleUri.Swap));
Assert.That(responses.Select(r => r.Envelope.Uri),
Is.EqualTo(new[] { NetworkBattleUri.Swap, NetworkBattleUri.Ready }));
Assert.That(s.Phase, Is.EqualTo(BattleSessionPhase.AfterReady));
}
[Test]
public void Retire_PushesBattleFinishNoContest_TransitionsToTerminal()
{
var s = NewSession();
var responses = s.ComputeResponses(NewEnvelope(NetworkBattleUri.Retire));
var (env, noStock) = responses.Single();
Assert.That(env.Uri, Is.EqualTo(NetworkBattleUri.BattleFinish));
Assert.That(noStock, Is.True);
Assert.That(s.Phase, Is.EqualTo(BattleSessionPhase.Terminal));
}
}