Files
SVSimServer/SVSim.UnitTests/BattleNode/Protocol/MsgEnvelopeTests.cs
gamer147 4cc8b3c01c fix(battle-node): MsgEnvelope rejects reserved Body keys + complete ReceiveNodeResultCode
ToJson now throws ArgumentException when a Body key collides with a reserved
envelope field (uri/viewerId/uuid/bid/try/cat/pubSeq/playSeq); FromJson reuses
the same shared ReservedEnvelopeKeys HashSet. ReceiveNodeResultCode expanded
from 9 to 31 codes to mirror the full enums.md catalog. Two regression tests
added for the collision guard and PascalCase uri serialization.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-31 21:55:11 -04:00

105 lines
3.2 KiB
C#

using System.Text.Json;
using NUnit.Framework;
using SVSim.BattleNode.Protocol;
namespace SVSim.UnitTests.BattleNode.Protocol;
[TestFixture]
public class MsgEnvelopeTests
{
[Test]
public void Roundtrip_PreservesEnvelopeAndBody()
{
var env = new MsgEnvelope(
Uri: NetworkBattleUri.InitNetwork,
ViewerId: 906243102,
Uuid: "udid-1234",
Bid: "597830888107",
Try: 0,
Cat: EmitCategory.General,
PubSeq: null,
PlaySeq: null,
Body: new Dictionary<string, object?> { ["foo"] = 42 });
var json = MsgEnvelope.ToJson(env);
var back = MsgEnvelope.FromJson(json);
Assert.That(back.Uri, Is.EqualTo(NetworkBattleUri.InitNetwork));
Assert.That(back.ViewerId, Is.EqualTo(906243102));
Assert.That(back.Uuid, Is.EqualTo("udid-1234"));
Assert.That(back.Bid, Is.EqualTo("597830888107"));
Assert.That(back.Cat, Is.EqualTo(EmitCategory.General));
Assert.That(back.Body["foo"], Is.EqualTo(42L)); // JsonElement → int64
}
[Test]
public void ToJson_OmitsNullEnvelopeFields()
{
var env = new MsgEnvelope(
Uri: NetworkBattleUri.Ready,
ViewerId: 1,
Uuid: "u",
Bid: null,
Try: 0,
Cat: EmitCategory.Battle,
PubSeq: null,
PlaySeq: 5,
Body: new Dictionary<string, object?>());
var json = MsgEnvelope.ToJson(env);
Assert.That(json, Does.Not.Contain("\"bid\""));
Assert.That(json, Does.Not.Contain("\"pubSeq\""));
Assert.That(json, Does.Contain("\"playSeq\":5"));
}
[Test]
public void FromJson_DispatchesUriToEnum()
{
const string json = "{\"uri\":\"PlayActions\",\"viewerId\":1,\"uuid\":\"u\",\"try\":0,\"cat\":1}";
var env = MsgEnvelope.FromJson(json);
Assert.That(env.Uri, Is.EqualTo(NetworkBattleUri.PlayActions));
Assert.That(env.Cat, Is.EqualTo(EmitCategory.Battle));
}
[Test]
public void ToJson_BodyContainingReservedKey_Throws()
{
var env = new MsgEnvelope(
Uri: NetworkBattleUri.Loaded,
ViewerId: 1,
Uuid: "u",
Bid: null,
Try: 0,
Cat: EmitCategory.Battle,
PubSeq: null,
PlaySeq: null,
Body: new Dictionary<string, object?> { ["uri"] = "Injected" });
var ex = Assert.Throws<ArgumentException>(() => MsgEnvelope.ToJson(env));
Assert.That(ex!.Message, Does.Contain("uri"));
}
[Test]
public void ToJson_UriField_SerializesAsExactPascalCaseMemberName()
{
var env = new MsgEnvelope(
Uri: NetworkBattleUri.PlayActions,
ViewerId: 1,
Uuid: "u",
Bid: null,
Try: 0,
Cat: EmitCategory.Battle,
PubSeq: null,
PlaySeq: null,
Body: new Dictionary<string, object?>());
var json = MsgEnvelope.ToJson(env);
// Wire form must be PascalCase exactly — not "playActions", not "play_actions".
Assert.That(json, Does.Contain("\"uri\":\"PlayActions\""));
}
}