Files
SVSimServer/SVSim.UnitTests/BattleNode/Protocol/NumericBoolJsonConverterTests.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

52 lines
1.9 KiB
C#

using System.Text.Json;
using System.Text.Json.Serialization;
using NUnit.Framework;
using SVSim.BattleNode.Protocol;
namespace SVSim.UnitTests.BattleNode.Protocol;
[TestFixture]
public class NumericBoolJsonConverterTests
{
private sealed record Probe(
[property: JsonPropertyName("flag")]
[property: JsonConverter(typeof(NumericBoolJsonConverter))]
bool Flag,
[property: JsonPropertyName("opt")]
[property: JsonConverter(typeof(NumericBoolJsonConverter))]
bool? Opt = null);
private static readonly JsonSerializerOptions Options = new()
{
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
};
[Test]
public void Writes_true_as_numeric_1_and_false_as_numeric_0()
{
var node = JsonSerializer.SerializeToElement(new Probe(Flag: true), Options);
Assert.That(node.GetProperty("flag").ValueKind, Is.EqualTo(JsonValueKind.Number));
Assert.That(node.GetProperty("flag").GetInt32(), Is.EqualTo(1));
var falseNode = JsonSerializer.SerializeToElement(new Probe(Flag: false), Options);
Assert.That(falseNode.GetProperty("flag").GetInt32(), Is.EqualTo(0));
}
[Test]
public void Reads_numeric_0_and_1_back_to_bool()
{
Assert.That(JsonSerializer.Deserialize<Probe>("{\"flag\":1}", Options)!.Flag, Is.True);
Assert.That(JsonSerializer.Deserialize<Probe>("{\"flag\":0}", Options)!.Flag, Is.False);
}
[Test]
public void Nullable_true_emits_1_and_null_is_omitted()
{
var present = JsonSerializer.SerializeToElement(new Probe(Flag: false, Opt: true), Options);
Assert.That(present.GetProperty("opt").GetInt32(), Is.EqualTo(1));
var absent = JsonSerializer.SerializeToElement(new Probe(Flag: false, Opt: null), Options);
Assert.That(absent.TryGetProperty("opt", out _), Is.False, "null bool? must be omitted, not emitted");
}
}