using System; using System.Linq; using System.Reflection; using System.Runtime.CompilerServices; using RynthCore.Plugin.RynthAi; using RynthCore.Plugin.RynthAi.LegacyUi; using RynthCore.Plugin.RynthAi.Meta; using RynthCore.PluginCore; using RynthCore.PluginSdk; using RynthCore.RynthAiHostTests.Fakes; using RynthCore.RynthAiTests.Harness; namespace RynthCore.RynthAiHostTests.Tests; // Open fellowship on create (2026-11-04, a Discord request): with Auto Fellowship Management on, a // fellowship RynthAi creates is opened once it exists and this character leads it, as VTank's auto // fellow management does. Never a fellowship made or closed by hand. internal static class FellowOpenTests { public static void Register(Runner r) { r.Add("fellow open: /mt fellow create, then /fellowship open once we lead it (setting on)", OpenAfterCreate); r.Add("fellow open: nothing opened with the setting off, or with Auto Fellowship Management off", NotWhenOff); r.Add("fellow open: a meta's own /fellowship create is opened too", MetaCreateLine); r.Add("fellow open: never when already in a fellowship, led by another, already open, or a hand-made one", NeverOthers); r.Add("fellow open: the wait ends when no fellowship comes", WaitEnds); r.Add("fellow open: on by default, a meta setting, and an older engine leaves it alone", DefaultsAndPlumbing); } private const uint Player = 0x50000F41; private const BindingFlags Any = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public; private const string Open = "/fellowship open"; private sealed class World { public readonly RynthAiPlugin P = new(); public readonly LegacyUiSettings S = new() { AutoFellowMgmt = true }; public bool InFellowship, IsLeader, IsOpen; public World() { FakeHost.Reset(); FakeHost.Names[Player] = "Tester"; FakeHost.PlayerIdValue = Player; var host = FakeHost.Create(playerId: Player); typeof(RynthPluginBase).GetMethod("Attach", Any)!.Invoke(P, new object[] { FakeHost.LastApi }); 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", Any)!.SetValue(dash, S); Set("_dashboard", dash); Set("_playerId", Player); Set("_initialized", true); Set("_loginComplete", true); P.FellowStateOverride = () => (InFellowship, IsLeader, IsOpen); } public void Set(string f, object? v) => typeof(RynthAiPlugin).GetField(f, Any)!.SetValue(P, v); public bool Mt(string line) => (bool)typeof(RynthAiPlugin).GetMethod("HandleMtCommand", Any)!.Invoke(P, new object[] { line })!; public bool Meta(string line) => (bool)typeof(RynthAiPlugin).GetMethod("HandleMetaChatCommand", Any)!.Invoke(P, new object[] { line })!; public void Tick() => typeof(RynthAiPlugin).GetMethod("TickFellowOpen", Any)!.Invoke(P, null); public long WaitUntil { get => (long)typeof(RynthAiPlugin).GetField("_fellowOpenUntil", Any)!.GetValue(P)!; set => Set("_fellowOpenUntil", value); } public int Opens => FakeHost.ChatCommands.Count(c => c == Open); /// The server made the fellowship: we lead it, closed. public void Created() { InFellowship = true; IsLeader = true; IsOpen = false; } } private static void OpenAfterCreate() { var w = new World(); Check.True(w.Mt("/mt fellow create Hunters"), "/mt fellow create is handled"); Check.True(FakeHost.ChatCommands.Contains("/fellowship create Hunters"), "the create went to the chat parser"); w.Tick(); Check.Eq(w.Opens, 0, "not opened before the fellowship exists"); w.Created(); w.Tick(); Check.Eq(w.Opens, 1, "opened once we lead it"); Check.True(FakeHost.ChatCommands.IndexOf("/fellowship create Hunters") < FakeHost.ChatCommands.IndexOf(Open), "after the create"); w.Tick(); w.Tick(); Check.Eq(w.Opens, 1, "and only once"); // The player closes it by hand later: it stays closed. w.IsOpen = false; for (int i = 0; i < 5; i++) w.Tick(); Check.Eq(w.Opens, 1, "a fellowship closed by hand is not opened again"); } private static void NotWhenOff() { var w = new World(); w.S.OpenFellowOnCreate = false; w.Mt("/mt fellow create Hunters"); Check.True(FakeHost.ChatCommands.Contains("/fellowship create Hunters"), "the create still goes out"); w.Created(); w.Tick(); w.Tick(); Check.Eq(w.Opens, 0, "Open fellowship on create off: nothing opened"); var w2 = new World(); w2.S.AutoFellowMgmt = false; // Open fellowship on create left on (its default) w2.Mt("/mt fellow create Hunters"); w2.Created(); w2.Tick(); Check.Eq(w2.Opens, 0, "Auto Fellowship Management off: nothing opened"); // Switched off while the create waits. var w3 = new World(); w3.Mt("/mt fellow create Hunters"); w3.S.OpenFellowOnCreate = false; w3.Tick(); w3.S.OpenFellowOnCreate = true; w3.Created(); w3.Tick(); Check.Eq(w3.Opens, 0, "switched off before the fellowship came: nothing opened"); } private static void MetaCreateLine() { var w = new World(); Check.False(w.Meta("/fellowship create Hunters"), "a meta's /fellowship create goes on to the chat parser"); w.Created(); w.Tick(); Check.Eq(w.Opens, 1, "and is opened"); var w2 = new World(); w2.Meta("/mt fellow create Hunters"); w2.Created(); w2.Tick(); Check.Eq(w2.Opens, 1, "a meta's /mt fellow create is opened"); var w3 = new World(); w3.Meta("/fellowship recruit Hunters"); w3.Meta("/fellowship create"); w3.Created(); w3.Tick(); Check.Eq(w3.Opens, 0, "other fellowship lines, and a create without a name, arm nothing"); } private static void NeverOthers() { // Already in a fellowship: the create fails, and the fellowship we're in isn't ours to open. var w = new World(); w.Created(); w.Mt("/mt fellow create Hunters"); w.Tick(); w.Tick(); Check.Eq(w.Opens, 0, "already in a fellowship: nothing opened"); // Someone else's fellowship turns up (we were recruited meanwhile). var w2 = new World(); w2.Mt("/mt fellow create Hunters"); w2.InFellowship = true; w2.IsLeader = false; w2.Tick(); w2.IsLeader = true; // made leader later w2.Tick(); Check.Eq(w2.Opens, 0, "led by another: nothing opened, and the wait is over"); // Already open. var w3 = new World(); w3.Mt("/mt fellow create Hunters"); w3.Created(); w3.IsOpen = true; w3.Tick(); Check.Eq(w3.Opens, 0, "already open: nothing sent"); // A fellowship the player made without RynthAi. var w4 = new World(); w4.Created(); for (int i = 0; i < 3; i++) w4.Tick(); Check.Eq(w4.Opens, 0, "a fellowship RynthAi didn't create is left alone"); } private static void WaitEnds() { var w = new World(); w.Mt("/mt fellow create Hunters"); Check.True(w.WaitUntil > 0, "the create waits"); w.WaitUntil = Environment.TickCount64 - 1; // the wait is over w.Tick(); Check.Eq(w.WaitUntil, 0L, "the wait ends"); w.Created(); w.Tick(); Check.Eq(w.Opens, 0, "a fellowship made after the wait is not opened"); } private static void DefaultsAndPlumbing() { Check.True(new LegacyUiSettings().OpenFellowOnCreate, "on by default"); Check.False(new LegacyUiSettings().AutoFellowMgmt, "Auto Fellowship Management stays off by default, so nothing changes until it's on"); Check.Eq(new SettingsBridgePayload().OpenFellowOnCreate, (bool?)null, "the bridge sends null when an older engine doesn't know it"); FakeHost.Reset(); var s = new LegacyUiSettings(); var m = new MetaManager(s, FakeHost.Create(), new PlayerVitalsCache()); Check.Eq(m.Expressions.Evaluate("ragetsetting[OpenFellowOnCreate]"), "1", "metas read it"); m.Expressions.Evaluate("rasetsetting[OpenFellowOnCreate, 0]"); Check.False(s.OpenFellowOnCreate, "and set it"); } }