using System; using System.Collections.Generic; using System.IO; using System.Reflection; using System.Runtime.CompilerServices; using System.Text.Json; using RynthCore.Plugin.RynthAi; using RynthCore.Plugin.RynthAi.LegacyUi; using RynthCore.RynthAiHostTests.Fakes; using RynthCore.RynthAiTests.Harness; using T = RynthCore.Plugin.RynthAi.LegacyUi.NavPointType; namespace RynthCore.RynthAiHostTests.Tests; // The new engine Nav panel (2026-11-29, NavFace.cs): its buttons beside Add waypoint here (Jump, Vendor, // NPC), a Jump step edited in place (facing, power, walk), Undo after a delete or clear, New route, and // what the nav JSON now says about each step and where the character stands. internal static class NavPanelTests { private const BindingFlags Any = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public; public static void Register(Runner r) { r.Add("nav panel: setJump changes a Jump step's facing, power and walk, and the file keeps them", SetJumpEdits); r.Add("nav panel: addJump adds a jump where you stand, facing your way at 30% unless told", AddJump); r.Add("nav panel: addVendor and addNpc take the selected object", AddVendorAndNpc); r.Add("nav panel: Undo puts deleted steps back in their places (delete, multi-delete, clear)", UndoPutsBack); r.Add("nav panel: newRoute starts an empty, unsaved route", NewRoute); r.Add("nav panel: the nav JSON has each step's name, seconds and jump, where you stand, and the Undo count", JsonFields); r.Add("nav panel: the engine's jump fields read; left out they mean unchanged", CommandJson); } private sealed class Rig { public readonly LegacyUiSettings S = new(); public readonly LegacyDashboardRenderer Dash; public readonly string Root; public Rig(string name, params NavPoint[] pts) { FakeHost.Reset(); Root = Path.Combine(Program.TempRoot, "nav-panel", name); if (Directory.Exists(Root)) Directory.Delete(Root, recursive: true); Directory.CreateDirectory(Root); S.CurrentRoute = new NavRouteParser { RouteType = NavRouteType.Circular }; S.CurrentRoute.Points.AddRange(pts); var nav = (LegacyNavigationUi)RuntimeHelpers.GetUninitializedObject(typeof(LegacyNavigationUi)); typeof(LegacyNavigationUi).GetField("_settings", Any)!.SetValue(nav, S); Dash = (LegacyDashboardRenderer)RuntimeHelpers.GetUninitializedObject(typeof(LegacyDashboardRenderer)); Set("_profileLoaded", true); // a stand-in with its profile in memory Set("_settings", S); Set("_host", FakeHost.Create()); Set("_navigationUi", nav); Set("_navFiles", new List()); Set("_navFolder", Root); Set("_profileListsLock", new object()); Set("_navEditStatus", string.Empty); // field initializers don't run on an uninitialized object // _settingsFilePath stays null: SaveSettings writes nothing. } private void Set(string f, object v) => typeof(LegacyDashboardRenderer).GetField(f, Any)!.SetValue(Dash, v); public void Cmd(string json) => Dash.HandleNavCommand(json); public NavBridgePayload Nav() => JsonSerializer.Deserialize(Dash.BuildNavJson(), RynthAiJsonContext.Default.NavBridgePayload)!; public string Status() => Nav().EditStatus; } private static NavPoint P(double ns, double ew, T type = T.Point) => new() { Type = type, NS = ns, EW = ew, Z = -0.1 }; private static NavPoint Jump(double heading, double ms, bool shift) => new() { Type = T.Jump, NS = 1, EW = 1, Z = 0, JumpHeading = heading, JumpMs = ms, JumpShift = shift }; private static void SetJumpEdits() { var rig = new Rig("setjump", P(1, 1), Jump(90, 300, false), P(2, 2)); string path = Path.Combine(rig.Root, "jumps.nav"); rig.S.CurrentRoute.Save(path); rig.S.CurrentNavPath = path; rig.Cmd("{\"Cmd\":\"setJump\",\"Index\":1,\"Heading\":212,\"Ms\":-1,\"Shift\":false}"); NavPoint j = rig.S.CurrentRoute.Points[1]; Check.Near(j.JumpHeading, 212, 1e-9, "facing set"); Check.Near(j.JumpMs, 300, 1e-9, "power unchanged (Ms -1)"); rig.Cmd("{\"Cmd\":\"setJump\",\"Index\":1,\"Heading\":-1,\"Ms\":600,\"Shift\":true}"); Check.Near(j.JumpHeading, 212, 1e-9, "facing unchanged (Heading -1)"); Check.Near(j.JumpMs, 600, 1e-9, "power 60%"); Check.True(j.JumpShift, "walking jump"); rig.Cmd("{\"Cmd\":\"setJump\",\"Index\":1,\"Heading\":-450,\"Ms\":5000,\"Shift\":true}"); Check.Near(j.JumpHeading, 212, 1e-9, "a negative facing changes nothing"); Check.Near(j.JumpMs, 1000, 1e-9, "power capped at 100%"); rig.Cmd("{\"Cmd\":\"setJump\",\"Index\":1,\"Heading\":450,\"Ms\":-1,\"Shift\":true}"); Check.Near(j.JumpHeading, 90, 1e-9, "450° is 90°"); // Not a jump, or out of range: nothing changes. rig.Cmd("{\"Cmd\":\"setJump\",\"Index\":0,\"Heading\":10,\"Ms\":10,\"Shift\":true}"); Check.Near(rig.S.CurrentRoute.Points[0].JumpHeading, 0, 0, "a Point isn't made a jump"); rig.Cmd("{\"Cmd\":\"setJump\",\"Index\":9,\"Heading\":10,\"Ms\":10,\"Shift\":true}"); NavRouteParser reread = NavRouteParser.Load(path); Check.Eq(reread.Points[1].Type, T.Jump, "still a jump in the file"); Check.Near(reread.Points[1].JumpHeading, 90, 1e-6, "file: facing"); Check.Near(reread.Points[1].JumpMs, 1000, 1e-6, "file: power"); Check.True(reread.Points[1].JumpShift, "file: walking"); } private static void AddJump() { var rig = new Rig("addjump", P(1, 1), P(2, 2)); FakeHost.SetNavPosition(10.5, -20.25); FakeHost.HeadingDeg = 135; rig.Cmd("{\"Cmd\":\"addJump\",\"AddMode\":0,\"InsertAt\":-1,\"Heading\":-1,\"Ms\":-1,\"Shift\":false}"); Check.Eq(rig.S.CurrentRoute.Points.Count, 3, "a step added"); NavPoint j = rig.S.CurrentRoute.Points[2]; Check.Eq(j.Type, T.Jump, "a Jump step"); Check.Near(j.NS, 10.5, 1e-4, "where you stand (NS)"); Check.Near(j.EW, -20.25, 1e-4, "where you stand (EW)"); Check.Near(j.JumpHeading, 135, 0.5, "facing your way"); Check.Near(j.JumpMs, 300, 1e-9, "30% power"); Check.False(j.JumpShift, "not walking"); // Told: after step 0, facing 270, full power, walking. rig.Cmd("{\"Cmd\":\"addJump\",\"AddMode\":2,\"InsertAt\":0,\"Heading\":270,\"Ms\":1000,\"Shift\":true}"); NavPoint k = rig.S.CurrentRoute.Points[1]; Check.Eq(k.Type, T.Jump, "after step 0"); Check.Near(k.JumpHeading, 270, 1e-9, "facing as told"); Check.Near(k.JumpMs, 1000, 1e-9, "power as told"); Check.True(k.JumpShift, "walking as told"); } private static void AddVendorAndNpc() { var rig = new Rig("vendor-npc", P(1, 1)); FakeHost.SetNavPosition(5, 5); // Nothing selected: nothing added, and the panel says why. rig.Cmd("{\"Cmd\":\"addVendor\",\"AddMode\":0,\"InsertAt\":-1}"); Check.Eq(rig.S.CurrentRoute.Points.Count, 1, "no selection: nothing added"); Check.True(rig.Status().Contains("Select"), "says to select: " + rig.Status()); const uint Vendor = 0x7A000010, Npc = 0x7A000011; uint cell = FakeHost.NavCell(5.01, 5.01, out float vx, out float vy); FakeHost.Names[Vendor] = "Renald the Grocer"; FakeHost.Positions[Vendor] = (cell, vx, vy, 0); FakeHost.Names[Npc] = "Town Crier"; FakeHost.Positions[Npc] = (cell, vx, vy, 0); FakeHost.SelectedItem = Vendor; rig.Cmd("{\"Cmd\":\"addVendor\",\"AddMode\":0,\"InsertAt\":-1}"); NavPoint v = rig.S.CurrentRoute.Points[^1]; Check.Eq(v.Type, T.OpenVendor, "a Vendor step"); Check.Eq(v.TargetName, "Renald the Grocer", "the vendor's name"); Check.Eq(v.VendorId, Vendor, "the vendor's id"); Check.Near(v.NS, 5, 1e-4, "where you stand"); FakeHost.SelectedItem = Npc; rig.Cmd("{\"Cmd\":\"addNpc\",\"AddMode\":1,\"InsertAt\":0}"); NavPoint n = rig.S.CurrentRoute.Points[0]; Check.Eq(n.Type, T.Npc, "an NPC step, before step 0"); Check.Eq(n.TargetName, "Town Crier", "the NPC's name"); Check.Near(n.PortalExitNS, 5.01, 1e-3, "the NPC's own spot"); // Saved and read back: both keep their names (the file format's trailers). string path = Path.Combine(rig.Root, "stops.nav"); rig.S.CurrentRoute.Save(path); NavRouteParser reread = NavRouteParser.Load(path); Check.Eq(reread.Points.Count, 3, "three steps in the file"); Check.Eq(reread.Points[0].TargetName, "Town Crier", "file: NPC"); Check.Eq(reread.Points[2].TargetName, "Renald the Grocer", "file: vendor"); Check.Eq(reread.Points[2].VendorId, Vendor, "file: vendor id"); } private static void UndoPutsBack() { NavPoint a = P(1, 1), b = P(2, 2), c = P(3, 3), d = P(4, 4); var rig = new Rig("undo", a, b, c, d); Check.Eq(rig.Nav().UndoCount, 0, "nothing to undo yet"); rig.Cmd("{\"Cmd\":\"deletePoints\",\"Indices\":[1,3]}"); Check.Eq(rig.S.CurrentRoute.Points.Count, 2, "two deleted"); var nav = rig.Nav(); Check.Eq(nav.UndoCount, 2, "Undo can put two back"); Check.True(nav.EditStatus.StartsWith("Deleted"), "the panel's Undo shows after 'Deleted...': " + nav.EditStatus); rig.Cmd("{\"Cmd\":\"undoDelete\"}"); var pts = rig.S.CurrentRoute.Points; Check.Eq(pts.Count, 4, "both back"); Check.True(ReferenceEquals(pts[1], b) && ReferenceEquals(pts[3], d), "each in its place"); Check.Eq(rig.Nav().UndoCount, 0, "and Undo is used up"); rig.Cmd("{\"Cmd\":\"deletePoint\",\"Index\":0}"); Check.True(rig.Status().StartsWith("Deleted step 0"), "the row's X says so: " + rig.Status()); rig.Cmd("{\"Cmd\":\"undoDelete\"}"); Check.True(ReferenceEquals(rig.S.CurrentRoute.Points[0], a), "the X's step is back first"); rig.Cmd("{\"Cmd\":\"clearRoute\"}"); Check.Eq(rig.S.CurrentRoute.Points.Count, 0, "cleared"); Check.True(rig.Status().StartsWith("Cleared"), "says cleared: " + rig.Status()); rig.Cmd("{\"Cmd\":\"undoDelete\"}"); Check.Eq(rig.S.CurrentRoute.Points.Count, 4, "all four back"); Check.True(ReferenceEquals(rig.S.CurrentRoute.Points[2], c), "in order"); // Another route loaded: Undo has nothing to put into it. rig.Cmd("{\"Cmd\":\"deletePoint\",\"Index\":0}"); rig.S.CurrentRoute = new NavRouteParser { RouteType = NavRouteType.Once }; Check.Eq(rig.Nav().UndoCount, 0, "a different route: nothing to undo"); rig.Cmd("{\"Cmd\":\"undoDelete\"}"); Check.Eq(rig.S.CurrentRoute.Points.Count, 0, "nothing put into the other route"); } private static void NewRoute() { var rig = new Rig("newroute", P(1, 1), P(2, 2)); rig.S.CurrentNavPath = Path.Combine(rig.Root, "kept.nav"); rig.S.ActiveNavIndex = 1; rig.S.IsRecordingNav = true; rig.Cmd("{\"Cmd\":\"newRoute\"}"); Check.Eq(rig.S.CurrentRoute.Points.Count, 0, "empty"); Check.Eq(rig.S.CurrentRoute.RouteType, NavRouteType.Once, "Run once"); Check.Eq(rig.S.CurrentNavPath, string.Empty, "unsaved: no file"); Check.Eq(rig.S.ActiveNavIndex, 0, "from step 0"); Check.False(rig.S.IsRecordingNav, "recording stopped"); Check.False(File.Exists(Path.Combine(rig.Root, "kept.nav")), "no file written"); } private static void JsonFields() { var rig = new Rig("json", P(1, 1), new NavPoint { Type = T.Pause, NS = 1, EW = 1, PauseTimeMs = 5000 }, new NavPoint { Type = T.OpenVendor, NS = 1, EW = 1, TargetName = "Renald the Grocer", VendorId = 7 }, new NavPoint { Type = T.PortalNPC, NS = 1, EW = 1, TargetName = "Portal to Holtburg" }, new NavPoint { Type = T.Npc, NS = 1, EW = 1, TargetName = "Town Crier" }, Jump(212, 600, true), new NavPoint { Type = T.Chat, NS = 1, EW = 1, ChatCommand = "/ub usei Healing Kit" }); rig.S.ActiveNavIndex = 0; var nav = rig.Nav(); Check.Near(nav.Points[1].Seconds, 5, 1e-9, "Pause: seconds"); Check.Eq(nav.Points[2].Name, "Renald the Grocer", "Vendor: name"); Check.Eq(nav.Points[3].Name, "Portal to Holtburg", "Portal: name"); Check.Eq(nav.Points[4].Name, "Town Crier", "NPC: name"); Check.Near(nav.Points[5].Heading, 212, 1e-9, "Jump: facing"); Check.Near(nav.Points[5].Ms, 600, 1e-9, "Jump: power"); Check.True(nav.Points[5].Shift, "Jump: walking"); Check.Eq(nav.Points[6].Text, "/ub usei Healing Kit", "Chat: the command (as before)"); Check.Eq(nav.Points[0].Name, string.Empty, "Point: no name"); // Where you stand, and how far the next step is (yd = nav units x 240). FakeHost.SetNavPosition(1.1, 1); nav = rig.Nav(); Check.True(nav.HereKnown, "position known"); Check.Near(nav.HereNS, 1.1, 1e-4, "here NS"); Check.Near(nav.ActiveDist, 24, 0.05, "step 0 is 24 yd away"); // No plugin hook (a stand-in renderer): the mode fields stay at their defaults. Check.Eq(nav.Mode, string.Empty, "no mode without the plugin"); Check.False(nav.HuntAvailable, "no Hunt tab without the plugin"); // The plugin's hook fills the mode fields. rig.Dash.NavPanelState = p => { p.Mode = "follow"; p.FollowName = "Leader"; p.HuntAvailable = true; }; nav = rig.Nav(); Check.Eq(nav.Mode, "follow", "the hook's mode"); Check.Eq(nav.FollowName, "Leader", "the hook's follow name"); Check.True(nav.HuntAvailable, "the hook's Hunt tab"); // A hook that throws doesn't take the panel's data with it. rig.Dash.NavPanelState = _ => throw new InvalidOperationException("test"); nav = rig.Nav(); Check.Eq(nav.Points.Count, 7, "points still there"); } private static void CommandJson() { NavCommand? c = JsonSerializer.Deserialize("{\"Cmd\":\"setJump\",\"Index\":2,\"Heading\":90.5,\"Ms\":450,\"Shift\":true,\"Text\":\"face\"}", RynthAiJsonContext.Default.NavCommand); Check.Near(c!.Heading, 90.5, 1e-9, "Heading"); Check.Near(c.Ms, 450, 1e-9, "Ms"); Check.True(c.Shift, "Shift"); Check.Eq(c.Text, "face", "Text"); NavCommand? old = JsonSerializer.Deserialize("{\"Cmd\":\"addJump\"}", RynthAiJsonContext.Default.NavCommand); Check.Near(old!.Heading, -1, 0, "no Heading: -1 (your facing / unchanged)"); Check.Near(old.Ms, -1, 0, "no Ms: -1 (30% / unchanged)"); } }