Files
SVSimServer/SVSim.UnitTests/BattleNode/Protocol/Bodies/BattleStartBodyTests.cs
gamer147 24180d5b4b refactor(battle-node): de-magic wire flags and scattered constants
Quality pass from the 2026-06-04 BattleNode review (audit in the outer
repo). All changes are behavior-preserving — identical wire bytes,
verified by the full 1008-test suite staying green.

- Name scattered magic numbers: crypto key/IV lengths, outbound-sequencer
  base, WS receive buffer / EIO ping / SID length, polite-close timeout,
  upgrade-credential keys, battle-id digit math, deterministic-turn spin.
- resultCode = 1 -> (int)ReceiveNodeResultCode.Success across body records.
- Pong "3" -> EngineIoPacketType.Pong; remove dead NoOpBotParticipant.Touch
  (replace with #pragma warning disable CS0067).
- Wire-flag enums, serialized as numbers via JsonNumberEnumConverter:
  turnState -> TurnState{First,Second}, isSelf -> CardOwner{Opponent,Self},
  open -> ChoiceVisibility{Hidden,Open}.
- isOfficial / isInvoke -> bool / bool? via new NumericBoolJsonConverter
  (reads/writes 0/1; TDD'd). Scoped to the BattleNode wire boundary only;
  MatchContext and the HTTP/AI-start path stay int (AI-start uses -1 as a
  sentinel, so it is not boolean).

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

56 lines
2.1 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 BattleStartBodyTests
{
[Test]
public void Serializes_TopLevelFields_WithCorrectWireKeys()
{
var body = new BattleStartBody(
TurnState: TurnState.First, BattleType: 11,
SelfInfo: new BattleStartSelfInfo("10", "6270", "1", "1", "card_master_node_10015"),
OppoInfo: new BattleStartOppoInfo("1", "0", 0, "0", "8", "8", "card_master_node_10015"));
var node = (JsonObject)JsonSerializer.SerializeToNode(body)!;
Assert.That(node["turnState"]!.GetValue<int>(), Is.EqualTo(0));
Assert.That(node["battleType"]!.GetValue<int>(), Is.EqualTo(11));
Assert.That(node["resultCode"]!.GetValue<int>(), Is.EqualTo(1)); // default
}
[Test]
public void SelfInfo_BattlePoint_IsString_OnTheWire()
{
var body = new BattleStartBody(0, 11,
new BattleStartSelfInfo("10", "6270", "1", "1", "cm"),
new BattleStartOppoInfo("1", "0", 0, "0", "8", "8", "cm"));
var node = (JsonObject)JsonSerializer.SerializeToNode(body)!;
var selfInfo = (JsonObject)node["selfInfo"]!;
// Wire shape: self.battlePoint is a string, oppo.battlePoint is an int. Preserved verbatim.
Assert.That(selfInfo["battlePoint"]!.GetValue<string>(), Is.EqualTo("6270"));
}
[Test]
public void OppoInfo_BattlePoint_IsInt_OnTheWire()
{
var body = new BattleStartBody(0, 11,
new BattleStartSelfInfo("10", "6270", "1", "1", "cm"),
new BattleStartOppoInfo("1", "0", 0, "0", "8", "8", "cm"));
var node = (JsonObject)JsonSerializer.SerializeToNode(body)!;
var oppoInfo = (JsonObject)node["oppoInfo"]!;
Assert.That(oppoInfo["battlePoint"]!.GetValue<int>(), Is.EqualTo(0));
Assert.That(oppoInfo["isMasterRank"]!.GetValue<string>(), Is.EqualTo("0"));
Assert.That(oppoInfo["masterPoint"]!.GetValue<string>(), Is.EqualTo("0"));
}
}