using System.Text.Json; using RadicadeServer.Extensions; using RadicadeServer.RecNet.Enums.Players; using RadicadeServer.RecNet.Notifications; using Xunit; namespace RadicadeServer.Tests; /// /// A saved announcement has to reach players without a restart. The only lever the client offers /// for that is its notification table, which is keyed by name and drops any frame whose Id is not /// the exact registered key - the same contract the community board and the progression frame live /// under. The server used to push the numeric enum AnnouncementText (26), which matches no /// registration, so an announcement only ever appeared on the next login. /// public class AnnouncementWireTests { [Fact] public void TheAnnouncementGoesOutUnderTheLiteralNameTheClientRegistersItsHandlerFor() { var frame = AnnouncementBroadcast.Update(new Announcement { AnnouncementId = 7, Title = "Weekend event", Body = "Double XP all weekend.", Platform = Platforms.All, }); Assert.Equal("AnnouncementUpdate", (string)frame.Id); Assert.Contains("\"Id\":\"AnnouncementUpdate\"", frame.ToJson()); // Not the numeric enum (26): the client registers the announcement handler by name and // drops any frame whose Id is not the exact key, which is why a saved announcement used to // be invisible until the game was restarted. Assert.DoesNotContain("\"Id\":\"26\"", frame.ToJson()); } [Fact] public void TheAnnouncementFrameCarriesTheSavedAnnouncement() { var frame = AnnouncementBroadcast.Update(new Announcement { AnnouncementId = 7, Title = "Weekend event", Body = "Double XP all weekend.", }); using var doc = JsonDocument.Parse(frame.ToJson()); var msg = doc.RootElement.GetProperty("Msg"); Assert.Equal(7L, msg.GetProperty("AnnouncementId").GetInt64()); Assert.Equal("Weekend event", msg.GetProperty("Title").GetString()); Assert.Equal("Double XP all weekend.", msg.GetProperty("Body").GetString()); } [Fact] public void ARemovedAnnouncementGoesOutUnderTheDeleteLiteralTheClientRegisters() { var frame = AnnouncementBroadcast.Delete(new Announcement { AnnouncementId = 7, Title = "Weekend event", Body = "Double XP all weekend.", Platform = Platforms.All, }); // A delete is its own registration on the client, so a removed announcement leaves the // on-screen list on the spot instead of waiting for the next api/announcement/v1/get. Assert.Equal("AnnouncementDelete", (string)frame.Id); Assert.Contains("\"Id\":\"AnnouncementDelete\"", frame.ToJson()); using var doc = JsonDocument.Parse(frame.ToJson()); Assert.Equal(7L, doc.RootElement.GetProperty("Msg").GetProperty("AnnouncementId").GetInt64()); } }