using System.Globalization; using RynthCore.Engine.ImGuiBackend.Hud; namespace RynthCore.Engine.UI { /// Stub: counts saves, never runs them (no file I/O in the tests). internal static class UiBackgroundWriter { public static int Enqueued; public static void Enqueue(string label, Action work) => Enqueued++; } } namespace SelfPlateTests { using S = MonsterHudSettings; /// Your own plate's look switches, the lock from Follow mode, and the "Debuffs on you" selection. internal static class Program { private static int _checks, _failed; private static string _case = ""; private static int Main() { LookDefaultsOn(); OldFileLoadsThemOn(); EachToggleRoundTrips(); ResetTurnsThemBackOn(); UnlockFromFixed(); UnlockFromFollowFixesIt(); LockFromFollowStaysFollow(); HarmfulOnly(); CooldownsAndZeroIdsDropped(); ExpiredAndEndlessDropped(); NoServerTimeOrNoSnapshot(); SameCategoryMerges(); KindMergesCategories(); KindOrder(); OverflowCount(); TicksAndDuration(); CategoryZeroStandsAlone(); ShortArraysAndHugeExpiry(); Console.WriteLine(); Console.WriteLine($"{_checks - _failed}/{_checks} checks passed."); Console.WriteLine(_failed == 0 ? "ALL SELF PLATE TESTS PASSED." : $"{_failed} FAILED."); return _failed == 0 ? 0 : 1; } // ── settings ───────────────────────────────────────────────────────── private static (string Key, Func Get, Action Set)[] Toggles => new (string, Func, Action)[] { ("selfBarTrack", () => S.SelfBarTrack, v => S.SelfBarTrack = v), ("selfBarChip", () => S.SelfBarChip, v => S.SelfBarChip = v), ("selfTextOutline", () => S.SelfTextOutline, v => S.SelfTextOutline = v), ("selfNumberBand", () => S.SelfNumberBand, v => S.SelfNumberBand = v), ("selfDebuffs", () => S.SelfDebuffs, v => S.SelfDebuffs = v), }; private static string[] Written() { var sw = new StringWriter(CultureInfo.InvariantCulture); S.WriteAll(sw); return sw.ToString().Split('\n', StringSplitOptions.RemoveEmptyEntries).Select(l => l.TrimEnd('\r')).ToArray(); } private static void LookDefaultsOn() { Case("defaults: the four look switches and Debuffs on you are on (the look before they existed)"); foreach (var t in Toggles) Check(t.Get(), $"{t.Key} defaults on"); string[] lines = Written(); foreach (var t in Toggles) Check(lines.Contains(t.Key + "=1"), $"{t.Key}=1 written"); } private static void OldFileLoadsThemOn() { Case("a file from 2026.10.27.1 (no new keys) loads them on, and its other values"); // That release's file = today's lines without the five new keys. S.ResetToDefaults(); S.SelfNumbers = true; S.Scale = 1.4f; S.SelfPlacement = SelfPlacement.Follow; S.SelfLocked = false; string[] old = Written().Where(l => !Toggles.Any(t => l.StartsWith(t.Key + "=", StringComparison.Ordinal))).ToArray(); Check(old.Length == Written().Length - Toggles.Length, "the old file lacks exactly the five keys"); S.ResetToDefaults(); S.ApplyLines(old); foreach (var t in Toggles) Check(t.Get(), $"{t.Key} on after loading the old file"); Check(S.SelfNumbers && MathF.Abs(S.Scale - 1.4f) < 0.001f && S.SelfPlacement == SelfPlacement.Follow && !S.SelfLocked, "the old file's own values still load"); S.ResetToDefaults(); S.ApplyLines(new[] { "# RynthCore monster nameplates", "selfPlate=1", "selfNumbers=1" }); foreach (var t in Toggles) Check(t.Get(), $"{t.Key} on after a hand-written two-line file"); S.ResetToDefaults(); } private static void EachToggleRoundTrips() { Case("each switch saves off and loads off, alone, and back on"); var toggles = Toggles; foreach (var t in toggles) { S.ResetToDefaults(); t.Set(false); string[] lines = Written(); Check(lines.Contains(t.Key + "=0"), $"{t.Key}=0 written"); S.ResetToDefaults(); S.ApplyLines(lines); Check(!t.Get(), $"{t.Key} off after reload"); foreach (var o in toggles) if (o.Key != t.Key) Check(o.Get(), $"{o.Key} still on when only {t.Key} is off"); Check(S.ShowDebuffs && S.Enabled, $"monster plates' switches untouched by {t.Key}"); t.Set(true); S.ApplyLines(Written()); Check(t.Get(), $"{t.Key} back on"); } Case("all off together round-trips; write -> apply -> write is stable"); foreach (var t in toggles) t.Set(false); string[] first = Written(); S.ResetToDefaults(); S.ApplyLines(first); foreach (var t in toggles) Check(!t.Get(), $"{t.Key} off"); Check(first.SequenceEqual(Written()), "identical text after a reload"); Check(S.Describe().Contains("debuffs on you off") && S.Describe().Contains("text outline off"), "status line names them"); S.ResetToDefaults(); } private static void ResetTurnsThemBackOn() { Case("Reset all to defaults turns them back on"); foreach (var t in Toggles) t.Set(false); S.ResetToDefaults(); foreach (var t in Toggles) Check(t.Get(), $"{t.Key} on after reset"); } private static void UnlockFromFixed() { Case("unlock a fixed plate: stays fixed, unlocked, movable; nothing saved by the setter"); S.ResetToDefaults(); int saves = RynthCore.Engine.UI.UiBackgroundWriter.Enqueued; Check(!S.SelfMovable, "locked by default"); bool switched = S.SetSelfLocked(false); Check(!switched, "no placement switch"); Check(S.SelfPlacement == SelfPlacement.Fixed && !S.SelfLocked && S.SelfMovable, "fixed, unlocked, movable"); S.SetSelfLocked(true); Check(S.SelfLocked && !S.SelfMovable, "locked again"); Check(RynthCore.Engine.UI.UiBackgroundWriter.Enqueued == saves, "the setter queues no save (callers save)"); } private static void UnlockFromFollowFixesIt() { Case("unlock a plate that follows you: fixed on screen at its saved spot, then movable"); S.ResetToDefaults(); S.SelfPlacement = SelfPlacement.Follow; S.SelfFixedX = 0.3f; S.SelfFixedY = 0.2f; Check(!S.SelfMovable, "a following plate isn't movable"); bool switched = S.SetSelfLocked(false); Check(switched, "reports the switch to fixed"); Check(S.SelfPlacement == SelfPlacement.Fixed && !S.SelfLocked && S.SelfMovable, "fixed and unlocked"); Check(S.SelfFixedX == 0.3f && S.SelfFixedY == 0.2f, "the saved spot is kept"); S.ResetToDefaults(); } private static void LockFromFollowStaysFollow() { Case("lock while following: stays following"); S.ResetToDefaults(); S.SelfPlacement = SelfPlacement.Follow; S.SelfLocked = false; bool switched = S.SetSelfLocked(true); Check(!switched && S.SelfPlacement == SelfPlacement.Follow && S.SelfLocked, "still follows, locked"); S.ResetToDefaults(); } // ── Debuffs on you: selection ─────────────────────────────────────── private const long Freq = 1000; // ticks per second in these checks private const long Now = 5_000_000; // Stopwatch ticks private const double ServerNow = 100_000; // Fake spell table: harmful spells only (as PortalSpellTable keeps), with a kind and a base length. private static readonly Dictionary Harmful = new() { [1] = (DebuffKind.Imperil, 60f), [2] = (DebuffKind.VulnFire, 45f), [3] = (DebuffKind.Attributes, 120f), [4] = (DebuffKind.Attributes, 90f), [5] = (DebuffKind.MagicYield, 30f), [6] = (DebuffKind.Skills, 240f), [7] = (DebuffKind.Dot, 20f), [8] = (DebuffKind.Movement, 50f), [9] = (DebuffKind.VulnCold, 45f), [10] = (DebuffKind.Imperil, 75f), [0x8001] = (DebuffKind.Other, 10f), // would be harmful, but ids from 0x8000 are cooldowns }; private static bool Lookup(uint id, out DebuffKind kind, out float dur) { if (Harmful.TryGetValue(id, out var h)) { kind = h.Kind; dur = h.Dur; return true; } kind = DebuffKind.Other; dur = 0; return false; } /// One snapshot entry: spell id, seconds left (server time), spell category. private readonly record struct E(uint Id, double Left, uint Cat); private static int Run(E[] entries, out DebuffIcon[] icons, int dstLen = 5, double serverNow = ServerNow, int? n = null) { var ids = entries.Select(e => e.Id).ToArray(); var exp = entries.Select(e => e.Left >= SelfDebuffs.NoEnd ? double.MaxValue : serverNow + e.Left).ToArray(); var cats = entries.Select(e => e.Cat).ToArray(); icons = new DebuffIcon[dstLen]; return SelfDebuffs.Select(ids, exp, cats, n ?? entries.Length, serverNow, Now, Freq, Lookup, icons); } private static void HarmfulOnly() { Case("harmful only: buffs (not in the table) never show"); int c = Run(new[] { new E(100, 600, 50), new E(1, 30, 116), new E(200, 900, 51) }, out var ic); Check(c == 1, $"one icon (got {c})"); Check(ic[0].Kind == DebuffKind.Imperil && ic[0].Stack == 1, "the Imperil"); c = Run(new[] { new E(100, 600, 50), new E(200, 900, 51) }, out _); Check(c == 0, "buffs alone: no row"); } private static void CooldownsAndZeroIdsDropped() { Case("cooldowns (0x8000+) and id 0 are dropped"); int c = Run(new[] { new E(0x8001, 30, 0), new E(0, 30, 116), new E(2, 30, 110) }, out var ic); Check(c == 1 && ic[0].Kind == DebuffKind.VulnFire, $"only the fire vuln (got {c})"); } private static void ExpiredAndEndlessDropped() { Case("expired entries and ones that never end (vitae, item spells) are dropped"); int c = Run(new[] { new E(1, -1, 116), new E(2, 0, 110), new E(3, 1e13, 2), new E(5, 10, 42) }, out var ic); Check(c == 1 && ic[0].Kind == DebuffKind.MagicYield, $"only the live Magic Yield (got {c})"); var ids = new uint[] { 1 }; var exp = new[] { double.NaN }; var cats = new uint[] { 116 }; Check(SelfDebuffs.Select(ids, exp, cats, 1, ServerNow, Now, Freq, Lookup, new DebuffIcon[5]) == 0, "NaN expiry dropped"); } private static void NoServerTimeOrNoSnapshot() { Case("no server time yet, or no snapshot (-1): no row"); Check(Run(new[] { new E(1, 30, 116) }, out _, serverNow: 0) == 0, "server time 0"); Check(Run(new[] { new E(1, 30, 116) }, out _, n: -1) == 0, "count -1 (CopyPlayerStatMods: nothing valid)"); Check(Run(Array.Empty(), out _) == 0, "empty snapshot"); } private static void SameCategoryMerges() { Case("one per category: two Imperils in category 116 are one icon, lasting to the later end"); int c = Run(new[] { new E(1, 20, 116), new E(10, 50, 116) }, out var ic); Check(c == 1, $"one icon (got {c})"); Check(ic[0].Stack == 1, "a stack of one (one category)"); Check(ic[0].Expiry == Now + 50 * Freq, $"ends with the later one (got {ic[0].Expiry - Now})"); Check(ic[0].Duration == 75f, "that one's length"); c = Run(new[] { new E(10, 50, 116), new E(1, 20, 116) }, out ic); Check(c == 1 && ic[0].Expiry == Now + 50 * Freq && ic[0].Duration == 75f, "same result in the other order"); } private static void KindMergesCategories() { Case("one per kind: two attribute debuffs (categories 2 and 4) are one icon, stack 2, soonest end"); int c = Run(new[] { new E(3, 80, 2), new E(4, 25, 4) }, out var ic); Check(c == 1 && ic[0].Kind == DebuffKind.Attributes, $"one Attributes icon (got {c})"); Check(ic[0].Stack == 2, $"stack 2 (got {ic[0].Stack})"); Check(ic[0].Expiry == Now + 25 * Freq && ic[0].Duration == 90f, "the soonest end and its length"); } private static void KindOrder() { Case("icons in kind order (the monster row's), whatever the snapshot order"); int c = Run(new[] { new E(6, 100, 20), new E(1, 30, 116), new E(9, 30, 106), new E(2, 30, 110), new E(5, 30, 42) }, out var ic); Check(c == 5, $"five icons (got {c})"); var want = new[] { DebuffKind.VulnFire, DebuffKind.VulnCold, DebuffKind.Imperil, DebuffKind.MagicYield, DebuffKind.Skills }; Check(ic.Select(i => i.Kind).SequenceEqual(want), "fire, cold, Imperil, Magic Yield, skills: " + string.Join(", ", ic.Select(i => i.Kind))); } private static void OverflowCount() { Case("more kinds than fit: the count includes the rest (+N), the first by kind are kept"); var all = new[] { new E(8, 30, 78), new E(7, 10, 600), new E(6, 100, 20), new E(1, 30, 116), new E(9, 30, 106), new E(2, 30, 110), new E(5, 30, 42), }; int c = Run(all, out var ic); Check(c == 7, $"count 7 (got {c})"); var want = new[] { DebuffKind.VulnFire, DebuffKind.VulnCold, DebuffKind.Imperil, DebuffKind.MagicYield, DebuffKind.Movement }; Check(ic.Select(i => i.Kind).SequenceEqual(want), "the five first kinds: " + string.Join(", ", ic.Select(i => i.Kind))); } private static void TicksAndDuration() { Case("server seconds left -> Stopwatch ticks from now; length from the table"); Run(new[] { new E(5, 12.5, 42) }, out var ic); Check(ic[0].Expiry == Now + 12_500, $"12.5 s -> 12500 ticks (got {ic[0].Expiry - Now})"); Check(ic[0].Duration == 30f, "Magic Yield's 30 s"); } private static void CategoryZeroStandsAlone() { Case("no category recorded: each spell stands alone"); int c = Run(new[] { new E(3, 40, 0), new E(4, 60, 0) }, out var ic); Check(c == 1 && ic[0].Stack == 2, $"two spells, one Attributes icon, stack 2 (got {c}, {ic[0].Stack})"); c = Run(new[] { new E(3, 40, 0), new E(3, 60, 0) }, out ic); Check(c == 1 && ic[0].Stack == 1 && ic[0].Expiry == Now + 60 * Freq, "the same spell twice is one entry"); } private static void ShortArraysAndHugeExpiry() { Case("a count past the arrays is clamped; a huge (but finite) end doesn't overflow"); var ids = new uint[] { 1, 2 }; var exp = new[] { ServerNow + 30, ServerNow + 9e11 }; var cats = new uint[] { 116, 110 }; var dst = new DebuffIcon[5]; int c = SelfDebuffs.Select(ids, exp, cats, 999, ServerNow, Now, Freq, Lookup, dst); Check(c == 2, $"two icons (got {c})"); Check(dst.Take(2).All(i => i.Expiry > Now), "both end after now"); c = SelfDebuffs.Select(ids, exp, cats, 2, ServerNow, Now, Freq, Lookup, Array.Empty()); Check(c == 2, "an empty destination still counts"); } // ── harness ───────────────────────────────────────────────────────── private static void Case(string name) { _case = name; Console.WriteLine("- " + name); } private static void Check(bool ok, string what) { _checks++; if (ok) return; _failed++; Console.WriteLine($" FAIL [{_case}] {what}"); } } }