using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Runtime.CompilerServices; using System.Text.Json; using RynthCore.Plugin.RynthAi; using RynthCore.Plugin.RynthAi.LegacyUi; using RynthCore.Plugin.RynthAi.Meta; using RynthCore.PluginSdk; using RynthCore.RynthAiHostTests.Fakes; using RynthCore.RynthAiTests.Harness; namespace RynthCore.RynthAiHostTests.Tests; // Rebuff When Idle (RebuffWhenIdle + RebuffIdleMarginMinutes, VTank's "idlebufftopoff"). The checkbox // was saved and shown but nothing read it. What it means (the owner, 2026-10-30): an earlier timer for // idle moments. With it on and the bot idle (no fight target, no monster within Monster Range, no // corpse to loot, no salvage), a rebuff starts once a buff has Rebuff With + the margin (default 2 // minutes) left. The regular rebuff is unchanged; an early top-off never starts in a fight and stops // when one begins; margin 0 adds nothing. 2026-11-20 to 2026-12-01 no buff cast started in a fight at all. // 2026-12-01: once under the set timer, the rebuff starts regardless, and the idle timer is higher and for // quiet moments: when a monster comes, the fight goes on until a buff reaches the regular timer. So at or under Rebuff With (or dispelled) a buff goes now, fight or not; only health under // Heal At in a fight holds it back. internal static class RebuffWhenIdleTests { private const uint Player = 0x50000F01; private const uint STypeCreature = 31, STypeLife = 33; private const string Strength = "Strength Self VI", Willpower = "Willpower Self VI"; public static void Register(Runner r) { r.Add("rebuff when idle: idle, a buff inside Rebuff With + the margin is rebuffed early", IdleInsideMarginRebuffed); r.Add("rebuff when idle: in a fight, a buff inside the margin but over Rebuff With is not rebuffed", FightInsideMarginWaits); r.Add("rebuff (2026-12-01): in a fight, a buff at or under Rebuff With (or dispelled) is rebuffed right away", FightRegularRebuff); r.Add("rebuff: a fight that never ends (constant respawn): rebuffed at Rebuff With at once, never under Heal At (R-20261125-130039-e3bc)", FightThatNeverEnds); r.Add("rebuff (2026-12-01): a regular rebuff run under way when a monster comes carries on in the fight", RunGoesOnInFight); r.Add("rebuff (2026-12-01): Recast dispelled buffs in a fight off: a dispelled buff waits for the fight's end, a buff at Rebuff With still goes and takes it along; the setting is saved, loaded, bridged, mapped", DispelOption); r.Add("rabuffsdue[seconds]: how many buffs a rebuff with that line would cast (a dispelled one too); -1 with buffing off or no buff manager; asking casts nothing", BuffsDueCount); r.Add("rebuff when idle: margin 0, or the option off, adds nothing", MarginZeroAddsNothing); r.Add("rebuff when idle: an early top-off under way stops when a fight starts (the cast sent resolves); the regular rules take over", EarlyRunStopsForFight); r.Add("rebuff when idle: Force Buff in a fight is unchanged", ForceBuffUnchanged); r.Add("rebuff when idle: the margin defaults to 2, is saved, loaded, copied, bridged, in the settings map; idlebufftopoff imports the option", Setting); } // ── Rig ─────────────────────────────────────────────────────────────────── private static int Id(string name) => SpellTable.Named(name).Select(r => r.Id).FirstOrDefault(); private static string NameOf(int id) => SpellTable.ById(id)?.Name ?? $"#{id}"; /// Creature and Life Magic 300 (tier 6), in Magic stance, knowing Strength and Willpower /// Self VI; the macro on, Rebuff With 300 s, no vital lines, full vitals. is what /// the plugin's idle test answers (and not idle = a monster within Monster Range). private sealed class Rig { public readonly LegacyUiSettings S; public readonly BuffManager B; public bool Idle = true; private DateTime _now = new(2026, 10, 30, 12, 0, 0); public Rig(bool rebuffWhenIdle, int marginMinutes = 2) { FakeHost.Reset(); foreach (string n in new[] { Strength, Willpower }) foreach (var row in SpellTable.Named(n)) FakeHost.Spellbook.Add((uint)row.Id); FakeHost.Skills[(Player, STypeCreature)] = (300, 2); FakeHost.Skills[(Player, STypeLife)] = (300, 2); FakeHost.WeaponCalls = true; // GetCurrentCombatMode answers CombatModeValue FakeHost.CombatModeValue = CombatMode.Magic; var host = FakeHost.Create(); S = new LegacyUiSettings { IsMacroRunning = true, EnableBuffing = true, SpellCastIntervalMs = 0, RebuffWhenIdle = rebuffWhenIdle, RebuffIdleMarginMinutes = marginMinutes, RebuffSecondsRemaining = 300, HealAt = 0, TopOffHP = 0, EmergencyHealAt = 0, StaminaToHealthAt = 0, GetManaAt = 0, TopOffMana = 0, RestamAt = 0, TopOffStam = 0, UsePotions = false, UseBuffItems = false, }; var skills = new CharacterSkills(host); skills.SetPlayerId(Player); var sm = new SpellManager(host, S); sm.InitializeNatively(); sm.SetCharacterSkills(skills); var v = new PlayerVitalsCache { MaxHealth = 100, CurrentHealth = 100, MaxStamina = 100, CurrentStamina = 100, MaxMana = 100, CurrentMana = 100 }; B = new BuffManager(host, S, sm, v) { InCombatOverride = () => !Idle, IdleForTopOff = () => Idle }; B.SetCharacterSkills(skills); FakeHost.ServerTime = 100_000; B.Clock = () => _now; B.MarkLiveBuffsReadyForTests(); } /// The live enchantment registry (spell, seconds left), read. public void Live(params (string Name, double Left)[] enchantments) { FakeHost.Enchantments.Clear(); foreach (var (name, left) in enchantments) FakeHost.Enchantments.Add(((uint)Id(name), FakeHost.ServerTime + left)); B.RefreshFromLiveMemory(); } /// A monster's dispel takes off, as the engine reports it. public void Dispel(string buff) { FakeHost.Enchantments.RemoveAll(e => e.SpellId == (uint)Id(buff)); B.OnEnchantmentRemoved((uint)Id(buff)); } /// The server's answer to a buff cast: it lands (registry and result line). public void Answer(string name) { FakeHost.Enchantments.RemoveAll(e => e.SpellId == (uint)Id(name)); FakeHost.Enchantments.Add(((uint)Id(name), FakeHost.ServerTime + 2700)); B.OnChatWindowText($"You cast {name} on yourself", 7); } /// One heartbeat a second; a cast it sends is answered unless is false. The spell sent, or "". public string Beat(bool answer = true) { _now = _now.AddSeconds(1); int before = FakeHost.Casts.Count; B.OnHeartbeat(); if (FakeHost.Casts.Count == before) return ""; string name = NameOf(FakeHost.Casts.Last().SpellId); if (answer) Answer(name); return name; } public List Beats(int n) { var sent = new List(); for (int i = 0; i < n; i++) { string s = Beat(); if (s.Length > 0) sent.Add(s); } return sent; } /// The arbiter's choice, from the inputs RynthAiPlugin gives it (combat wants the tick when not idle). public BotActivity Arbiter() { bool wantBuff = B.PendingSpellId != 0 || B.WantsVitalRecharge || (S.EnableBuffing && B.NeedsAnyBuff() && !B.BuffsWaitForFight()); return ActivityArbiter.Decide(new ArbiterInputs(true, wantBuff, wantCombat: !Idle, wantLooting: false, wantSalvaging: false, wantNav: false, combatEngaged: !Idle)); } public bool Running => B.GetStateSnapshot().IsForceRebuffing; public DateTime Now => _now; public int Logged(string part) => FakeHost.Logs.Count(l => l.Contains(part, StringComparison.Ordinal)); } private static string Show(IEnumerable sent) => sent.Any() ? string.Join(", ", sent) : "(none)"; // ── Tests ───────────────────────────────────────────────────────────────── private static void IdleInsideMarginRebuffed() { var rig = new Rig(rebuffWhenIdle: true); Check.Eq(rig.B.RebuffLineSec(), 420, "idle: Rebuff With 300 s + 2 min"); rig.Live((Strength, 400), (Willpower, 3000)); // 400 s: over 300, inside 420 Check.True(rig.B.NeedsAnyBuff(), "Strength is due for an early top-off"); Check.Eq(rig.Arbiter(), BotActivity.Buffing, "buffing takes the idle tick"); Check.Eq(Show(rig.Beats(5)), Strength, "Strength is topped off (Willpower, 3000 s left, is left alone)"); Check.Eq(rig.B.IdleTopOffRuns, 1, "one early top-off run"); Check.Eq(rig.Logged("[Buff] Rebuff When Idle: idle, and a buff has under 420 s left"), 1, "logged"); Check.False(rig.Running, "the run ends"); Check.False(rig.B.NeedsAnyBuff(), "nothing due afterwards"); // Just over the margin: nothing yet. var over = new Rig(rebuffWhenIdle: true); over.Live((Strength, 430), (Willpower, 3000)); Check.Eq(Show(over.Beats(5)), "(none)", "430 s left, over the 420 s line: not yet"); // Also Refresh Under at or below Rebuff With: the early run still casts the buff that started it, once. var narrow = new Rig(rebuffWhenIdle: true); narrow.S.RebuffTopOffSecondsRemaining = 300; narrow.Live((Strength, 400), (Willpower, 3000)); Check.Eq(Show(narrow.Beats(8)), Strength, "the triggering buff is in the run, no loop"); Check.Eq(narrow.B.IdleTopOffRuns, 1, "one run"); } private static void FightInsideMarginWaits() { var rig = new Rig(rebuffWhenIdle: true) { Idle = false }; Check.Eq(rig.B.RebuffLineSec(), 300, "in a fight: Rebuff With only"); rig.Live((Strength, 400), (Willpower, 3000)); Check.False(rig.B.NeedsAnyBuff(), "400 s left in a fight: not due"); Check.Eq(rig.Arbiter(), BotActivity.Combat, "combat keeps the tick"); Check.Eq(Show(rig.Beats(10)), "(none)", "no early top-off in a fight"); Check.Eq(rig.B.IdleTopOffRuns, 0, "none started"); rig.Idle = true; Check.Eq(Show(rig.Beats(5)), Strength, "the fight over: topped off then"); } // 2026-12-01: once under the set timer, the rebuff starts regardless. (2026-11-20 to 12-01 it waited for the fight's end.) private static void FightRegularRebuff() { var low = new Rig(rebuffWhenIdle: true) { Idle = false }; low.Live((Strength, 290), (Willpower, 3000)); // under Rebuff With 300 Check.True(low.B.NeedsAnyBuff(), "290 s left: due, fight or not"); Check.False(low.B.BuffsWaitForFight(), "nothing holds it in the fight"); Check.Eq(low.Arbiter(), BotActivity.Buffing, "buffing takes the tick in the fight"); Check.Eq(Show(low.Beats(5)), Strength, "rebuffed in the fight (Willpower, 3000 s left, left alone)"); Check.Eq(low.Logged("[Buff] rebuffing in the fight (a fight): a buff is at or under Rebuff With (300 s)"), 1, "logged once"); Check.Eq(low.Logged("[Buff] buffs wait"), 0, "no wait"); Check.Eq(low.B.IdleTopOffRuns, 0, "a regular run, not an early top-off"); Check.Eq(low.Arbiter(), BotActivity.Combat, "then combat has it"); // 2026-12-01: when one buff reaches the timer, everything with under 20 minutes left is rebuffed too, so // it isn't forever switching to a wand: the run takes every buff under Also Refresh Under // (RebuffTopOffSecondsRemaining, default 1200 s), in a fight too. Check.Eq(low.S.RebuffTopOffSecondsRemaining, 1200, "Also Refresh Under defaults to 20 minutes"); var batch = new Rig(rebuffWhenIdle: false) { Idle = false }; batch.Live((Strength, 290), (Willpower, 1100)); // Willpower: over Rebuff With, under 20 minutes var both = batch.Beats(6); Check.True(both.Contains(Strength) && both.Contains(Willpower), "in a fight, one buff at Rebuff With takes the one under 20 minutes along: " + Show(both)); Check.Eq(Show(batch.Beats(5)), "(none)", "then nothing more"); var single = new Rig(rebuffWhenIdle: false) { Idle = false }; single.Live((Strength, 290), (Willpower, 1300)); // Willpower: over 20 minutes Check.Eq(Show(single.Beats(6)), Strength, "a buff with over 20 minutes left is left alone"); var dispel = new Rig(rebuffWhenIdle: true); dispel.Live((Strength, 3000), (Willpower, 3000)); dispel.Beats(2); dispel.Idle = false; dispel.Dispel(Strength); Check.True(dispel.B.NeedsAnyBuff(), "a buff dispelled mid-fight is due"); Check.Eq(dispel.Arbiter(), BotActivity.Buffing, "and goes in the fight"); Check.Eq(Show(dispel.Beats(5)), Strength, "recast right away"); var off = new Rig(rebuffWhenIdle: false) { Idle = false }; off.Live((Strength, 290), (Willpower, 3000)); Check.Eq(Show(off.Beats(5)), Strength, "the same with the option off"); // Under Heal At in the fight: the heals first, the buff once health is back over it. var hurt = new Rig(rebuffWhenIdle: false) { Idle = false }; hurt.S.HealAt = 60; hurt.Live((Strength, 290), (Willpower, 3000)); SetHealth(hurt, 50); Check.True(hurt.B.BuffsWaitForFight(), "50% under Heal At 60% in a fight: it waits"); Check.Eq(Show(hurt.Beats(5)), "(none)", "nothing cast"); Check.Eq(hurt.Logged("[Buff] buffs wait: a fight, health 50% under Heal At 60%"), 1, "the wait is logged once"); SetHealth(hurt, 90); Check.Eq(Show(hurt.Beats(5)), Strength, "back over Heal At, the fight still on: rebuffed"); Check.Eq(hurt.Logged("[Buff] buffs go on: health 90%, back over Heal At"), 1, "the end is logged"); // Fighting with every buff up: nothing is logged. var up = new Rig(rebuffWhenIdle: false) { Idle = false }; up.Live((Strength, 3000), (Willpower, 3000)); up.Beats(5); Check.Eq(up.Logged("[Buff] buffs wait") + up.Logged("[Buff] rebuffing in the fight"), 0, "nothing logged with nothing due"); } // Conquest, constant respawn, Monster Range 10: buffs waited almost 7 hours for the fight's end, ran out, and the // character died. Now nothing waits for the fight's end at all. private static void FightThatNeverEnds() { var rig = new Rig(rebuffWhenIdle: false) { Idle = false }; RynthClock.TestNow = () => rig.Now; try { rig.Live((Strength, 2900), (Willpower, 3000)); rig.Beats(2); rig.Live((Strength, 290), (Willpower, 3000)); Check.Eq(Show(rig.Beats(5)), Strength, "an unbroken fight: Strength (290 s left) is rebuffed at once"); Check.Eq(Show(rig.Beats(120)), "(none)", "nothing more to cast"); // Under Heal At it never goes. var hurt = new Rig(rebuffWhenIdle: false) { Idle = false }; RynthClock.TestNow = () => hurt.Now; hurt.S.HealAt = 50; hurt.Live((Strength, 2900), (Willpower, 3000)); hurt.Beats(2); hurt.Live((Strength, 290), (Willpower, 3000)); SetHealth(hurt, 30); Check.Eq(Show(hurt.Beats(150)), "(none)", "health 30% under Heal At 50%: no buff cast however long the fight"); } finally { RynthClock.TestNow = null; } } // A script asks before a Thread (2026-11-20): Lua sees only the character's enchantments, not the item spells. private static void BuffsDueCount() { var rig = new Rig(rebuffWhenIdle: false); rig.Live((Strength, 400), (Willpower, 3000)); int casts = FakeHost.Casts.Count; Check.Eq(rig.B.CountBuffsDue(300), 0, "a 300 s line: nothing due"); Check.Eq(rig.B.CountBuffsDue(1200), 1, "a 20 minute line: Strength (400 s left)"); Check.Eq(rig.B.CountBuffsDue(3600), 2, "an hour: both"); rig.Dispel(Willpower); Check.Eq(rig.B.CountBuffsDue(0), 1, "a dispelled buff counts at any line"); Check.Eq(FakeHost.Casts.Count, casts, "asking casts nothing"); var m = new MetaManager(rig.S, FakeHost.Create(), new PlayerVitalsCache()); Check.Eq(m.Expressions.Evaluate("rabuffsdue[1200]"), "-1", "no buff manager: -1"); m.SetBuffManager(rig.B); Check.Eq(m.Expressions.Evaluate("rabuffsdue[1200]"), "2", "rabuffsdue[1200]: Strength and the dispelled Willpower"); Check.Eq(m.Expressions.Evaluate("rabuffsdue[0]"), "1", "rabuffsdue[0]: the dispelled one"); rig.S.EnableBuffing = false; Check.Eq(m.Expressions.Evaluate("rabuffsdue[1200]"), "-1", "buffing off: -1"); } // A regular rebuff run under way when a monster comes: it carries on (2026-12-01; it used to wait for the fight's end). private static void RunGoesOnInFight() { var rig = new Rig(rebuffWhenIdle: false); rig.Live((Strength, 200), (Willpower, 250)); // both under Rebuff With: one run string first = rig.Beat(answer: false); Check.True(first == Strength || first == Willpower, $"the run starts ('{first}', hold: {rig.B.LastBuffSkipReason})"); rig.Idle = false; // a monster comes rig.Answer(first); Check.Eq(rig.Arbiter(), BotActivity.Buffing, "the run keeps the tick in the fight"); Check.Eq(Show(rig.Beats(5)), first == Strength ? Willpower : Strength, "and finishes"); Check.False(rig.Running, "then ends"); Check.Eq(rig.Arbiter(), BotActivity.Combat, "combat has it"); } // 2026-12-01: recasting dispelled buffs mid-fight is an option. private static void DispelOption() { var rig = new Rig(rebuffWhenIdle: false); Check.True(rig.S.RecastDispelledInFight, "on by default"); rig.S.RecastDispelledInFight = false; rig.Live((Strength, 3000), (Willpower, 3000)); rig.Beats(2); rig.Idle = false; rig.Dispel(Strength); Check.True(rig.B.NeedsAnyBuff(), "dispelled: due"); Check.True(rig.B.BuffsWaitForFight(), "option off: it waits in the fight"); Check.Eq(rig.Arbiter(), BotActivity.Combat, "combat keeps the tick"); Check.Eq(Show(rig.Beats(5)), "(none)", "not recast in the fight"); Check.Eq(rig.Logged("a dispelled buff waits for the fight's end"), 1, "the wait is logged once"); rig.Idle = true; Check.Eq(Show(rig.Beats(5)), Strength, "the fight over: recast"); // A buff at Rebuff With still goes in the fight, and the run takes the dispelled one along. var both = new Rig(rebuffWhenIdle: false); both.S.RecastDispelledInFight = false; both.Live((Strength, 3000), (Willpower, 3000)); both.Beats(2); both.Idle = false; both.Dispel(Strength); both.Beats(3); both.Live((Willpower, 290)); var sent = both.Beats(6); Check.True(sent.Contains(Willpower) && sent.Contains(Strength), "Willpower at Rebuff With goes in the fight, Strength with it: " + Show(sent)); // Option on (the default): recast in the fight. var on = new Rig(rebuffWhenIdle: false); on.Live((Strength, 3000), (Willpower, 3000)); on.Beats(2); on.Idle = false; on.Dispel(Strength); Check.Eq(Show(on.Beats(5)), Strength, "option on: recast in the fight"); // Saved, loaded, bridged, in the settings map. var old = JsonSerializer.Deserialize("{\"EnableBuffing\":true,\"RebuffSecondsRemaining\":300}", RynthAiJsonContext.Default.LegacyUiSettings); Check.True(old != null && old.RecastDispelledInFight, "a profile saved before the setting: on"); var off = JsonSerializer.Deserialize( JsonSerializer.Serialize(new LegacyUiSettings { RecastDispelledInFight = false }, RynthAiJsonContext.Default.LegacyUiSettings), RynthAiJsonContext.Default.LegacyUiSettings); Check.True(off != null && !off.RecastDispelledInFight, "off is saved and loaded"); var copy = typeof(LegacyDashboardRenderer).GetMethod("CopySettings", BindingFlags.NonPublic | BindingFlags.Static)!; var dst = new LegacyUiSettings(); copy.Invoke(null, new object[] { new LegacyUiSettings { RecastDispelledInFight = false }, dst }); Check.False(dst.RecastDispelledInFight, "a profile with it off loads off"); var payload = JsonSerializer.Deserialize("{\"PatrolOnLogin\":false}", RynthAiJsonContext.Default.SettingsBridgePayload); Check.True(payload != null && payload.RecastDispelledInFight == null, "an older engine's payload has no value (left alone)"); var live = new LegacyUiSettings(); var map = ExpressionTests.Engine(settings: live).BuildSettingsMapPublic(); Check.True(map.TryGetValue("RecastDispelledInFight", out var entry), "in the settings map"); entry.Set("0"); Check.False(live.RecastDispelledInFight, "and sets off"); } private static void MarginZeroAddsNothing() { var zero = new Rig(rebuffWhenIdle: true, marginMinutes: 0); Check.Eq(zero.B.RebuffLineSec(), 300, "margin 0: Rebuff With only, idle too"); zero.Live((Strength, 400), (Willpower, 3000)); Check.Eq(Show(zero.Beats(5)), "(none)", "margin 0: 400 s left is not due"); zero.Live((Strength, 290), (Willpower, 3000)); Check.Eq(Show(zero.Beats(5)), Strength, "the regular rebuff still goes"); Check.Eq(zero.B.IdleTopOffRuns, 0, "never an early top-off"); var off = new Rig(rebuffWhenIdle: false, marginMinutes: 2); Check.Eq(off.B.RebuffLineSec(), 300, "option off: the margin is not read"); off.Live((Strength, 400), (Willpower, 3000)); Check.Eq(Show(off.Beats(5)), "(none)", "option off: not due early"); var negative = new Rig(rebuffWhenIdle: true, marginMinutes: -5); Check.Eq(negative.B.RebuffLineSec(), 300, "a negative margin counts as 0"); } private static void EarlyRunStopsForFight() { var rig = new Rig(rebuffWhenIdle: true); rig.Live((Strength, 400), (Willpower, 400)); // both inside the margin: one early run string first = rig.Beat(answer: false); Check.True(first == Strength || first == Willpower, $"idle: the early run starts ('{first}', hold: {rig.B.LastBuffSkipReason})"); Check.True(rig.Running, "the run is under way"); rig.Idle = false; // a monster comes Check.Eq(rig.Arbiter(), BotActivity.Buffing, "the cast in flight keeps the slot until it resolves"); rig.Answer(first); Check.Eq(rig.Beat(), "", "the next heartbeat casts nothing"); Check.False(rig.Running, "the run stopped at that safe point (before the arbiter reads it)"); Check.False(rig.B.NeedsAnyBuff(), "the early run no longer holds the tick"); Check.Eq(rig.Arbiter(), BotActivity.Combat, "combat has it"); Check.Eq(Show(rig.Beats(10)), "(none)", "nothing more is cast in the fight"); Check.Eq(rig.Logged("the early top-off stops"), 1, "logged once"); // The regular rules take over: the other buff, still over Rebuff With, waits; once it drops under, it goes in // the fight (2026-12-01: the fight goes on until a buff reaches the regular timer). string other = first == Strength ? Willpower : Strength; rig.Live((first, 2700), (other, 350)); Check.Eq(Show(rig.Beats(5)), "(none)", "350 s left in the fight, over Rebuff With 300: it waits"); rig.Live((first, 2700), (other, 290)); Check.Eq(Show(rig.Beats(5)), other, "under Rebuff With: rebuffed in the fight"); Check.Eq(rig.B.IdleTopOffRuns, 1, "that was a regular run"); // And an early run interrupted with the other buff still inside the margin finishes when idle again. var again = new Rig(rebuffWhenIdle: true); again.Live((Strength, 400), (Willpower, 400)); string a = again.Beat(answer: false); again.Idle = false; again.Answer(a); again.Beats(5); again.Idle = true; Check.Eq(Show(again.Beats(5)), a == Strength ? Willpower : Strength, "idle again: the rest is topped off"); Check.Eq(again.B.IdleTopOffRuns, 2, "a second early run"); } private static void ForceBuffUnchanged() { var rig = new Rig(rebuffWhenIdle: true) { Idle = false }; rig.Live((Strength, 3000), (Willpower, 3000)); rig.B.RequestForceFullRebuff(); var sent = rig.Beats(10); Check.True(sent.Contains(Strength) && sent.Contains(Willpower), "Force Buff recasts everything, in a fight too: " + Show(sent)); Check.False(rig.Running, "and ends"); // The player's own Force Buff, hurt in the fight: it waits for Heal At, as since 2026-11-07. var hurt = new Rig(rebuffWhenIdle: false) { Idle = false }; hurt.S.HealAt = 60; hurt.Live((Strength, 3000), (Willpower, 3000)); hurt.B.RequestForceFullRebuff(); SetHealth(hurt, 50); Check.Eq(Show(hurt.Beats(5)), "(none)", "50% under Heal At 60% in a fight: the Force Buff waits"); SetHealth(hurt, 90); var after = hurt.Beats(10); Check.True(after.Contains(Strength) && after.Contains(Willpower), "back over Heal At, the fight on: it goes (" + Show(after) + ")"); } private static void SetHealth(Rig rig, int pct) { var v = (PlayerVitalsCache)typeof(BuffManager).GetField("_vitals", BindingFlags.NonPublic | BindingFlags.Instance)!.GetValue(rig.B)!; v.CurrentHealth = (uint)pct; // MaxHealth 100 } private static void Setting() { var fresh = new LegacyUiSettings(); Check.False(fresh.RebuffWhenIdle, "a new profile: Rebuff When Idle off"); Check.Eq(fresh.RebuffIdleMarginMinutes, 2, "the margin defaults to 2 minutes"); var old = JsonSerializer.Deserialize("{\"EnableBuffing\":true,\"RebuffWhenIdle\":true,\"RebuffSecondsRemaining\":300}", RynthAiJsonContext.Default.LegacyUiSettings); Check.True(old != null && old.RebuffWhenIdle && old.RebuffIdleMarginMinutes == 2, "a profile saved before the margin: 2"); var saved = JsonSerializer.Deserialize( JsonSerializer.Serialize(new LegacyUiSettings { RebuffWhenIdle = true, RebuffIdleMarginMinutes = 0 }, RynthAiJsonContext.Default.LegacyUiSettings), RynthAiJsonContext.Default.LegacyUiSettings); Check.True(saved != null && saved.RebuffWhenIdle && saved.RebuffIdleMarginMinutes == 0, "saved and loaded (0 stays 0)"); var copy = typeof(LegacyDashboardRenderer).GetMethod("CopySettings", BindingFlags.NonPublic | BindingFlags.Static)!; var dst = new LegacyUiSettings(); copy.Invoke(null, new object[] { new LegacyUiSettings { RebuffIdleMarginMinutes = 7 }, dst }); Check.Eq(dst.RebuffIdleMarginMinutes, 7, "a profile switch copies it"); // The engine's Settings face: sent; a save without it (an engine that doesn't know it) leaves it alone. var s = new LegacyUiSettings { RebuffIdleMarginMinutes = 5 }; var dash = (LegacyDashboardRenderer)RuntimeHelpers.GetUninitializedObject(typeof(LegacyDashboardRenderer)); typeof(LegacyDashboardRenderer).GetField("_profileLoaded", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic)!.SetValue(dash, true); // a stand-in with its profile in memory typeof(LegacyDashboardRenderer).GetField("_settings", BindingFlags.NonPublic | BindingFlags.Instance)!.SetValue(dash, s); typeof(LegacyDashboardRenderer).GetField("_advancedSettingsUi", BindingFlags.NonPublic | BindingFlags.Instance)! .SetValue(dash, new LegacyAdvancedSettingsUi(s)); var sent = JsonSerializer.Deserialize(dash.BuildSettingsJson(), RynthAiJsonContext.Default.SettingsBridgePayload); Check.True(sent?.RebuffIdleMarginMinutes == 5, "sent to the engine face"); dash.ApplySettingsJson("{\"PatrolOnLogin\":false}"); Check.Eq(s.RebuffIdleMarginMinutes, 5, "a save without it leaves it alone"); dash.ApplySettingsJson("{\"RebuffIdleMarginMinutes\":0}"); Check.Eq(s.RebuffIdleMarginMinutes, 0, "a save with 0 sets 0"); var live = new LegacyUiSettings(); var map = ExpressionTests.Engine(settings: live).BuildSettingsMapPublic(); Check.True(map.TryGetValue("RebuffIdleMarginMinutes", out var entry), "RebuffIdleMarginMinutes is in the settings map"); Check.Eq(entry.Get(), "2", "reads 2"); entry.Set("4"); Check.Eq(live.RebuffIdleMarginMinutes, 4, "and sets it"); Check.True(map.TryGetValue("RebuffWhenIdle", out var on), "RebuffWhenIdle is in the settings map"); on.Set("1"); Check.True(live.RebuffWhenIdle, "and sets it on"); // VTank import: idlebufftopoff is the option; VTank has no time field for it in our tables, so the // margin keeps its default on import. Check.True(MetaManager.TryMapVtOption("idlebufftopoff", out string ra) && ra == "RebuffWhenIdle", "VTank's idlebufftopoff imports Rebuff When Idle"); } }