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 { ["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()); 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 { ["uri"] = "Injected" }); var ex = Assert.Throws(() => 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()); var json = MsgEnvelope.ToJson(env); // Wire form must be PascalCase exactly — not "playActions", not "play_actions". Assert.That(json, Does.Contain("\"uri\":\"PlayActions\"")); } }