RynthSuite/Tools/RynthCore.RynthAiHostTests/Tests/ProfileStartTests.cs

246 lines
13 KiB
C#

using System;
using System.IO;
using System.Linq;
using System.Reflection;
using RynthCore.Plugin.RynthAi;
using RynthCore.Plugin.RynthAi.LegacyUi;
using RynthCore.Plugin.Shared;
using RynthCore.PluginCore;
using RynthCore.RynthAiHostTests.Fakes;
using RynthCore.RynthAiTests.Harness;
namespace RynthCore.RynthAiHostTests.Tests;
// 2026-11-27 (R-20261127-124531-b5d3): a new settings profile was always a copy of the one in use,
// and the Monsters rules belong to the profile (2026-11-18), so a T11 profile made from a T10 one
// had every T10 monster rule in it. A new profile now starts from a copy (Save As, which says what
// it copied) or fresh (New: the Monsters list down to Default, no items or consumables, weapons
// filled from the pack, every other setting kept). The Monsters list also has a Clear list, which
// keeps the Default row and saves.
internal static class ProfileStartTests
{
private const BindingFlags Any = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public;
private const int HandWraps = unchecked((int)0x801A1F0D);
private const int Sword = 0x50000101, Kit = 0x50000202;
public static void Register(Runner r)
{
r.Add("new profile: the fresh reset keeps Default (or makes one) and only empties the lists", ResetUnit);
r.Add("new profile: Save As says it is a copy and how many monster rules came with it", CopySays);
r.Add("new profile: Fresh has only the Default row and no items, keeps the other settings", FreshProfile);
r.Add("new profile: Fresh never replaces an existing profile, and the one left keeps its lists", FreshRefusesExisting);
r.Add("monsters: Clear list keeps the Default row and saves the profile and the Monsters file", ClearList);
r.Add("new profile: /ra settings new is fresh, saveas a copy; the menu's Clear list verb", CommandsAndVerbs);
}
private sealed class Scratch : IDisposable
{
private readonly string? _buffs = BuffProfileStore.FolderOverride;
public readonly string CharDir, MonstersFile;
public Scratch(string name)
{
string root = Path.Combine(Program.TempRoot, "profile-start", name);
if (Directory.Exists(root)) Directory.Delete(root, recursive: true);
Directory.CreateDirectory(root);
LegacyDashboardRenderer.ProfilesBase = Path.Combine(root, "SettingsProfiles");
LegacyDashboardRenderer.MonstersBase = Path.Combine(root, "MonsterProfiles");
BuffProfileStore.FolderOverride = Path.Combine(root, "BuffProfiles");
CharDir = Path.Combine(LegacyDashboardRenderer.ProfilesBase, "TestWorld", "Tester");
MonstersFile = Path.Combine(LegacyDashboardRenderer.MonstersBase, "TestWorld", "Tester.json");
}
public string Tag => File.Exists(MonstersFile + ".profile") ? File.ReadAllText(MonstersFile + ".profile").Trim() : "";
public string Profile(string name) => File.ReadAllText(Path.Combine(CharDir, name + ".json"));
public void Dispose() => BuffProfileStore.FolderOverride = _buffs;
}
private static LegacyDashboardRenderer Login(out LegacyUiSettings s)
{
FakeHost.Reset();
DataWriteGuard.Reset();
FakeHost.WorldName = "TestWorld";
var dash = new LegacyDashboardRenderer(FakeHost.Create());
FakeHost.WorldName = "TestWorld";
dash.LoadSettings("Tester");
s = (LegacyUiSettings)typeof(LegacyDashboardRenderer).GetField("_settings", Any)!.GetValue(dash)!;
return dash;
}
private static MonsterRule DefaultRow(LegacyUiSettings s) =>
s.MonsterRules.Single(r => r.Name.Equals("Default", StringComparison.OrdinalIgnoreCase));
private static void Select(LegacyDashboardRenderer dash, string profile) =>
dash.SelectProfileAtIndex(3, Array.IndexOf(dash.ReadDashboardFiles().Profiles, profile));
private static string PackSetup(LegacyDashboardRenderer dash) =>
typeof(LegacyDashboardRenderer).GetField("_packSetup", Any)!.GetValue(dash)!.ToString()!;
/// <summary>A T10 profile in use: Default's off hand set, two named rules, a weapon, a kit, a buff row, Monster Range 33.</summary>
private static LegacyDashboardRenderer T10(out LegacyUiSettings s)
{
var dash = Login(out s);
DefaultRow(s).OffhandId = HandWraps;
s.MonsterRules.Add(new MonsterRule { Name = "Tusker", Priority = 3 });
s.MonsterRules.Add(new MonsterRule { Name = "Olthoi", Priority = 4 });
s.ItemRules.Add(new ItemRule { Id = Sword, Name = "T10 Sword", Action = "Weapon" });
s.ConsumableRules.Add(new ConsumableRule { Id = Kit, Name = "Healing Kit", Type = "Heal" });
s.BuffRules.Add(new BuffRule { SpellName = "Strength Self VII" });
s.MonsterRange = 33;
s.ProfileItemsChecked = true;
dash.SaveMonsterRules();
ProfileResult r = dash.SaveProfileAs(ProfileKind.Settings, "T10", replace: false);
Check.True(r.Ok, "Save As T10: " + r.Text);
Check.Eq(s.SelectedProfile, "T10", "T10 in use");
return dash;
}
private static void ResetUnit()
{
var s = new LegacyUiSettings { MonsterRange = 41 };
s.MonsterRules.Add(new MonsterRule { Name = "Tusker" });
s.MonsterRules.Add(new MonsterRule { Name = "default", OffhandId = HandWraps });
s.ItemRules.Add(new ItemRule { Id = Sword, Name = "T10 Sword" });
s.ConsumableRules.Add(new ConsumableRule { Id = Kit, Name = "Healing Kit" });
s.BuffRules.Add(new BuffRule { SpellName = "Strength Self VII" });
s.ProfileItemsChecked = true;
var oldMonsters = s.MonsterRules;
LegacyDashboardRenderer.ResetForFreshProfile(s);
Check.Eq(s.MonsterRules.Count, 1, "one rule left");
Check.Eq(s.MonsterRules[0].OffhandId, HandWraps, "the Default row itself is kept, as it was");
Check.Eq(oldMonsters.Count, 2, "a new list: the old one isn't emptied under a reader");
Check.Eq(s.ItemRules.Count + s.ConsumableRules.Count + s.BuffRules.Count, 0, "items, consumables and the buff rows emptied");
Check.False(s.ProfileItemsChecked, "the weapons are filled from the pack again");
Check.Eq(s.MonsterRange, 41, "other settings kept");
var none = LegacyDashboardRenderer.DefaultRuleOnly(new() { new MonsterRule { Name = "Tusker" } });
Check.Eq(none.Count, 1, "no Default row: one is made");
Check.Eq(none[0].Name, "Default", "named Default");
Check.Eq(LegacyDashboardRenderer.NamedMonsterRules(none), 0, "Default isn't counted as a monster rule");
}
private static void CopySays()
{
using var t = new Scratch("copy");
var dash = T10(out var s);
ProfileResult r = dash.SaveProfileAs(ProfileKind.Settings, "T11", replace: false);
Check.True(r.Ok, "Save As T11");
Check.Eq(r.Text, "[RynthAi] New profile 'T11': a copy of 'T10' (2 monster rules).", "the chat line");
Check.Eq(s.MonsterRules.Count, 3, "a copy keeps the monster rules");
Check.Eq(s.ItemRules.Count, 1, "and the weapons");
Select(dash, "T10");
r = dash.SaveProfileAs(ProfileKind.Settings, "T11", replace: true);
Check.Eq(r.Text, "[RynthAi] Profile 'T11' replaced with a copy of 'T10' (2 monster rules).", "a replace says so");
}
private static void FreshProfile()
{
using var t = new Scratch("fresh");
var dash = T10(out var s);
ProfileResult r = dash.NewProfile(ProfileKind.Settings, "T11");
Check.True(r.Ok, "New T11: " + r.Text);
Check.Eq(r.Text, "[RynthAi] New profile 'T11': fresh (no monsters or items; weapons fill from your pack).", "the chat line");
Check.Eq(s.SelectedProfile, "T11", "T11 in use");
Check.Eq(s.MonsterRules.Count, 1, "only the Default row");
Check.Eq(DefaultRow(s).OffhandId, HandWraps, "Default as it was");
Check.Eq(s.ItemRules.Count, 0, "no weapons yet");
Check.Eq(s.ConsumableRules.Count, 0, "no consumables yet");
Check.Eq(s.BuffRules.Count, 0, "no per-profile buff rows");
Check.Eq(s.MonsterRange, 33, "Monster Range kept");
Check.Eq(PackSetup(dash), "FillNew", "weapons and consumables fill from the pack, as a new profile's do");
string json = t.Profile("T11");
Check.False(json.Contains("Tusker") || json.Contains("T10 Sword"), "T11.json holds no T10 monsters or weapons");
Check.Eq(t.Tag, "T11", "the Monsters file is T11's");
Check.False(File.ReadAllText(t.MonstersFile).Contains("Tusker"), "and holds only Default");
Select(dash, "T10");
Check.Eq(s.SelectedProfile, "T10", "back on T10");
Check.Eq(s.MonsterRules.Count, 3, "T10 kept its monster rules");
Check.Eq(s.ItemRules.Count, 1, "and its weapon");
Check.Eq(s.ConsumableRules.Count, 1, "and its kit");
Select(dash, "T11");
Check.Eq(s.MonsterRules.Count, 1, "T11 is still fresh after a switch");
}
private static void FreshRefusesExisting()
{
using var t = new Scratch("fresh-exists");
var dash = T10(out var s);
Check.True(dash.SaveProfileAs(ProfileKind.Settings, "T11", replace: false).Ok, "a T11 copy");
Select(dash, "T10");
string before = t.Profile("T11");
ProfileResult r = dash.NewProfile(ProfileKind.Settings, "T11");
Check.False(r.Ok, "New over an existing profile is refused");
Check.True(r.Text.Contains("already exists"), r.Text);
Check.Eq(t.Profile("T11"), before, "T11 untouched");
Check.Eq(s.SelectedProfile, "T10", "T10 still in use");
Check.Eq(s.MonsterRules.Count, 3, "with its rules");
Check.False(dash.NewProfile(ProfileKind.Settings, "monsters").Ok, "a name RynthAi keeps is refused");
Check.False(dash.NewProfile(ProfileKind.Settings, " ").Ok, "a blank name is refused");
Check.Eq(s.ItemRules.Count, 1, "nothing was reset");
}
private static void ClearList()
{
using var t = new Scratch("clear");
var dash = T10(out var s);
ProfileResult r = dash.ClearMonsterRules();
Check.True(r.Ok, "Clear list: " + r.Text);
Check.Eq(r.Text, "[RynthAi] Cleared the Monsters list of profile 'T10': removed 2 monster rules; the Default row stays.", "the chat line");
Check.Eq(s.MonsterRules.Count, 1, "only Default left");
Check.Eq(DefaultRow(s).OffhandId, HandWraps, "the Default row as it was");
Check.Eq(s.ItemRules.Count, 1, "the weapons stay");
Check.False(t.Profile("T10").Contains("Tusker"), "saved to T10.json at once");
Check.False(File.ReadAllText(t.MonstersFile).Contains("Tusker"), "and to the Monsters file");
Check.Eq(t.Tag, "T10", "tagged T10");
Check.Eq(dash.ProfileInfo(ProfileKind.Settings).MonsterRules, 0, "the PROFILE menu counts no rules");
Check.True(dash.ClearMonsterRules().Text.Contains("nothing to clear"), "a second clear has nothing to do");
Login(out s);
Check.Eq(s.SelectedProfile, "T10", "relogged on T10");
Check.Eq(s.MonsterRules.Count, 1, "still cleared after a relog");
Check.Eq(DefaultRow(s).OffhandId, HandWraps, "Default kept after a relog");
}
private static void CommandsAndVerbs()
{
using var t = new Scratch("commands");
var dash = T10(out var s);
Check.Eq(dash.ProfileInfo(ProfileKind.Settings).MonsterRules, 2, "the PROFILE menu counts T10's two rules");
Check.Eq(dash.ProfileInfo(ProfileKind.Nav).MonsterRules, -1, "other kinds have no Monsters list");
var plugin = new RynthAiPlugin();
typeof(RynthPluginBase).GetMethod("Attach", Any)!.Invoke(plugin, new object[] { FakeHost.LastApi });
typeof(RynthAiPlugin).GetField("_dashboard", Any)!.SetValue(plugin, dash);
MethodInfo dispatch = typeof(RynthAiPlugin).GetMethod("DispatchRaCommand", Any)!;
string Ra(string line)
{
FakeHost.Chat.Clear();
string[] parts = line.Split(' ', StringSplitOptions.RemoveEmptyEntries);
dispatch.Invoke(plugin, new object[] { parts[1].ToLowerInvariant(), parts, 0 });
return string.Join(" / ", FakeHost.Chat);
}
string chat = Ra("/ra settings saveas T10 Copy");
Check.True(chat.Contains("New profile 'T10 Copy': a copy of 'T10' (2 monster rules)."), "/ra settings saveas copies: " + chat);
Check.Eq(s.MonsterRules.Count, 3, "the copy has T10's rules");
chat = Ra("/ra settings new T11");
Check.True(chat.Contains("New profile 'T11': fresh"), "/ra settings new: " + chat);
Check.Eq(s.MonsterRules.Count, 1, "fresh");
chat = Ra("/ra settings new");
Check.True(chat.Contains("needs a name") && chat.Contains("new <name>"), "no name: why + usage: " + chat);
Select(dash, "T10");
var run = typeof(RynthAiPlugin).GetMethod("RunProfileAction", Any)!;
var r = (ProfileResult)run.Invoke(plugin, new object?[] { ProfileKind.Settings, ProfileVerb.ClearMonsters, "", false })!;
Check.True(r.Ok && r.Text.Contains("removed 2 monster rules"), "the menu's Clear list verb: " + r.Text);
r = (ProfileResult)run.Invoke(plugin, new object?[] { ProfileKind.Nav, ProfileVerb.ClearMonsters, "", false })!;
Check.False(r.Ok, "only a settings profile has a Monsters list");
}
}