Files
SVSimServer/SVSim.UnitTests/BattleNode/Protocol/Bodies/SmallBodiesTests.cs

70 lines
2.5 KiB
C#

using System.Text.Json;
using System.Text.Json.Nodes;
using NUnit.Framework;
using SVSim.BattleNode.Protocol;
using SVSim.BattleNode.Protocol.Bodies;
namespace SVSim.UnitTests.BattleNode.Protocol.Bodies;
[TestFixture]
public class SmallBodiesTests
{
[Test]
public void OpponentTurnStartBody_SerializesSpin_AndDefaultsResultCodeToOne()
{
var body = new OpponentTurnStartBody(Spin: 100);
var node = (JsonObject)JsonSerializer.SerializeToNode(body)!;
Assert.That(node["spin"]!.GetValue<int>(), Is.EqualTo(100));
Assert.That(node["resultCode"]!.GetValue<int>(), Is.EqualTo(1));
}
[Test]
public void ResultCodeOnlyBody_SerializesJustResultCode()
{
var body = new ResultCodeOnlyBody();
var node = (JsonObject)JsonSerializer.SerializeToNode(body)!;
Assert.That(node.Count, Is.EqualTo(1));
Assert.That(node["resultCode"]!.GetValue<int>(), Is.EqualTo(1));
}
[Test]
public void BattleFinishBody_SerializesResultAndResultCode_AsNumericWireValues()
{
// The wire field is the int code (Win=1); BattleResult uses JsonNumberEnumConverter
// to override the default JsonStringEnumConverter (which would emit "Win" instead).
var body = new BattleFinishBody(Result: BattleResult.Win);
var node = (JsonObject)JsonSerializer.SerializeToNode(body)!;
Assert.That(node["result"]!.GetValue<int>(), Is.EqualTo(1));
Assert.That(node["resultCode"]!.GetValue<int>(), Is.EqualTo(1));
}
[Test]
public void BattleFinishBody_LoseAndConsistency_SerializeAsZeroAndTwo()
{
// Lock the wire values per BattleFinishResponsProcessing's switch (0=LOSE, 2=CONSISTENCY).
var lose = (JsonObject)JsonSerializer.SerializeToNode(new BattleFinishBody(BattleResult.Lose))!;
var consistency = (JsonObject)JsonSerializer.SerializeToNode(new BattleFinishBody(BattleResult.Consistency))!;
Assert.That(lose["result"]!.GetValue<int>(), Is.EqualTo(0));
Assert.That(consistency["result"]!.GetValue<int>(), Is.EqualTo(2));
}
[Test]
public void AlivePushBody_SerializesScsAndOcs_AndDoesNotIncludeResultCode()
{
var body = new AlivePushBody(Scs: "ONLINE", Ocs: "ONLINE");
var node = (JsonObject)JsonSerializer.SerializeToNode(body)!;
Assert.That(node["scs"]!.GetValue<string>(), Is.EqualTo("ONLINE"));
Assert.That(node["ocs"]!.GetValue<string>(), Is.EqualTo("ONLINE"));
Assert.That(node.ContainsKey("resultCode"), Is.False);
}
}