using System; using System.IO; using System.Linq; using System.Reflection; using RynthCore.Plugin.RynthAi.LegacyUi; using RynthCore.RynthAiHostTests.Fakes; using RynthCore.RynthAiTests.Harness; namespace RynthCore.RynthAiHostTests.Tests; // Named window layouts in the Loaded files drawer (2026-12-01, report R-20261127-150927-7938; named // layouts, in the files drawer). The engine keeps them // (/rc layout save|load|delete ); the LAYOUT row lists the folder, loads a pick, and its "..." saves, // saves as, renames and deletes. Here against a test folder, never the PC's real layouts. internal static class NamedLayoutRowTests { private const BindingFlags Any = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public; public static void Register(Runner r) { r.Add("named layouts (R-20261127 7938): the LAYOUT row lists the saved layouts, loads a pick and remembers it per character", ListAndLoad); r.Add("named layouts: Save, Save As (asks before replacing), Rename and Delete; names checked as the engine does", Verbs); } private static (LegacyDashboardRenderer Dash, string Root, string Char) Rig(string name) { FakeHost.Reset(); var host = FakeHost.Create(); var dash = new LegacyDashboardRenderer(host); string root = Path.Combine(Program.TempRoot, "layouts-" + name); string saved = Path.Combine(root, "_saved"); string charFolder = Path.Combine(root, "char"); Directory.CreateDirectory(saved); Directory.CreateDirectory(charFolder); LegacyDashboardRenderer.LayoutsFolderForTests = saved; typeof(LegacyDashboardRenderer).GetField("_charFolder", Any)!.SetValue(dash, charFolder); return (dash, saved, charFolder); } private static void Saved(string folder, string name) { Directory.CreateDirectory(Path.Combine(folder, name)); File.WriteAllText(Path.Combine(folder, name, "panel_state.txt"), "x\n"); } private static void ListAndLoad() { var (dash, saved, charFolder) = Rig("list"); try { Saved(saved, "Raid"); Saved(saved, "Mule"); Directory.CreateDirectory(Path.Combine(saved, "_deleted", "Old")); Directory.CreateDirectory(Path.Combine(saved, "Empty")); // no panel_state.txt: not a layout dash.RefreshLayoutFiles(); DashboardFiles f = dash.ReadDashboardFiles(); Check.True(f.Layouts != null && f.Layouts.SequenceEqual(new[] { "Mule", "Raid" }), "listed, sorted, _deleted and empty folders left out: " + string.Join(",", f.Layouts ?? Array.Empty())); Check.Eq(f.LayoutName, "None", "none in use yet"); dash.SelectProfileAtIndex(5, 1); // Raid Check.True(FakeHost.ChatCommands.Contains("/rc layout load Raid"), "a pick loads it through the engine: " + string.Join(" | ", FakeHost.ChatCommands)); Check.True(FakeHost.Chat.Any(c => c.Contains("Loading the layout 'Raid'")), "and says so"); f = dash.ReadDashboardFiles(); Check.True(f.LayoutName == "Raid" && f.LayoutIdx == 1, $"the row shows it ({f.LayoutName}, {f.LayoutIdx})"); Check.Eq(File.ReadAllText(Path.Combine(charFolder, "layout.txt")).Trim(), "Raid", "remembered beside the character's profiles"); var again = new LegacyDashboardRenderer(FakeHost.Create()); typeof(LegacyDashboardRenderer).GetField("_charFolder", Any)!.SetValue(again, charFolder); typeof(LegacyDashboardRenderer).GetMethod("ReadLayoutName", Any)!.Invoke(again, null); Check.Eq(again.LayoutName, "Raid", "read back at the next login"); Check.True(again.ProfileInfo(ProfileKind.Layout).HasFile, "the menu has a layout in use"); } finally { LegacyDashboardRenderer.LayoutsFolderForTests = null; } } private static void Verbs() { var (dash, saved, _) = Rig("verbs"); try { ProfileResult r = dash.LayoutAction(ProfileVerb.Save, null, false); Check.False(r.Ok, "Save with no layout in use: says to use Save As"); r = dash.LayoutAction(ProfileVerb.SaveAs, "a/b", false); Check.True(!r.Ok && r.Text.Contains("can't be in a layout name"), "a bad name: said why: " + r.Text); Check.Eq(FakeHost.ChatCommands.Count, 0, "nothing sent for either"); r = dash.LayoutAction(ProfileVerb.SaveAs, "Raid Setup", false); Check.True(r.Ok && FakeHost.ChatCommands.Contains("/rc layout save Raid Setup"), "Save As: the engine saves it: " + r.Text); Check.Eq(dash.LayoutName, "Raid Setup", "and it is the one in use"); Saved(saved, "Raid Setup"); // what the engine's save leaves r = dash.LayoutAction(ProfileVerb.Save, null, false); Check.True(r.Ok && FakeHost.ChatCommands.Count(c => c == "/rc layout save Raid Setup") == 2, "Save: into the one in use"); Saved(saved, "Other"); r = dash.LayoutAction(ProfileVerb.SaveAs, "Other", false); Check.True(!r.Ok && r.NeedsReplace, "Save As onto another layout asks first"); r = dash.LayoutAction(ProfileVerb.SaveAs, "Other", true); Check.True(r.Ok && FakeHost.ChatCommands.Contains("/rc layout save Other"), "and replaces it on the yes"); r = dash.LayoutAction(ProfileVerb.Rename, "Raid", false); Check.True(r.Ok && Directory.Exists(Path.Combine(saved, "Raid")) && !Directory.Exists(Path.Combine(saved, "Other")), "Rename moves the layout's folder: " + r.Text); Check.Eq(dash.LayoutName, "Raid", "the renamed one is in use"); r = dash.LayoutAction(ProfileVerb.Rename, "Raid Setup", false); Check.True(!r.Ok && r.Text.Contains("already"), "not onto another layout"); r = dash.LayoutAction(ProfileVerb.Delete, null, false); Check.True(r.Ok && FakeHost.ChatCommands.Contains("/rc layout delete Raid"), "Delete: the engine moves it aside: " + r.Text); Check.Eq(dash.LayoutName, string.Empty, "none in use after"); } finally { LegacyDashboardRenderer.LayoutsFolderForTests = null; } } }