Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

settingsScopeNavigation.test.ts

261 lines10.2 KB

1import { EnvironmentId, ProviderInstanceId, ScheduledTaskId } from "@t3tools/contracts";
2import {
3 createMemoryHistory,
4 createRootRoute,
5 createRoute,
6 createRouter,
7 redirect,
8} from "@tanstack/react-router";
9import { describe, expect, it } from "vite-plus/test";
10​
11import { resolveSettingsScope } from "./settingsScope";
12import { retainSettingsScope, validateSettingsRouteSearch } from "./settingsScopeNavigation";
13​
14import { validateScheduledTasksSearch } from "./scheduledTasksSettings.logic";
15​
16const checkoutSearch = {
17 project: "repository:t3code",
18 machine: "remote-server",
19 checkout: "remote-server:/home/user/T3 Code",
20};
21​
22function createSettingsRouter(initialEntry = "/settings/general") {
23 const root = createRootRoute();
24 const settings = createRoute({
25 getParentRoute: () => root,
26 path: "settings",
27 validateSearch: validateSettingsRouteSearch,
28 search: { middlewares: [retainSettingsScope] },
29 beforeLoad: ({ location }) => {
30 if (location.pathname === "/settings") {
31 throw redirect({ to: "/settings/general", replace: true });
32 }
33 },
34 });
35 const general = createRoute({ getParentRoute: () => settings, path: "general" });
36 const projects = createRoute({ getParentRoute: () => settings, path: "projects" });
37 const integrations = createRoute({ getParentRoute: () => settings, path: "integrations" });
38 const sourceControl = createRoute({ getParentRoute: () => settings, path: "source-control" });
39 const providers = createRoute({
40 getParentRoute: () => settings,
41 path: "providers",
42 validateSearch: (raw: Record<string, unknown>) => ({
43 ...(typeof raw.environmentId === "string" && raw.environmentId.trim()
44 ? { environmentId: EnvironmentId.make(raw.environmentId) }
45 : {}),
46 ...(typeof raw.instanceId === "string" && raw.instanceId.trim()
47 ? { instanceId: ProviderInstanceId.make(raw.instanceId) }
48 : {}),
49 }),
50 });
51 const scheduledTasks = createRoute({
52 getParentRoute: () => settings,
53 path: "scheduled-tasks",
54 validateSearch: validateScheduledTasksSearch,
55 });
56 const legacyProject = createRoute({
57 getParentRoute: () => root,
58 path: "projects/$projectKey",
59 beforeLoad: ({ params }) => {
60 throw redirect({
61 to: "/settings/projects",
62 search: { project: params.projectKey, machine: undefined },
63 replace: true,
64 });
65 },
66 });
67 return createRouter({
68 routeTree: root.addChildren([
69 settings.addChildren([
70 general,
71 projects,
72 integrations,
73 sourceControl,
74 providers,
75 scheduledTasks,
76 ]),
77 legacyProject,
78 ]),
79 history: createMemoryHistory({ initialEntries: [initialEntry] }),
80 });
82​
83describe("settings scope navigation", () => {
84 it("clears a checkout when selecting all environments and all projects", async () => {
85 const router = createSettingsRouter();
86 await router.navigate({ to: "/settings/general", search: checkoutSearch, hash: "old-setting" });
87 await router.navigate({
88 to: "/settings/general",
89 search: { project: undefined, machine: undefined, checkout: undefined },
90 hash: "",
91 });
92 expect(router.state.location.search).toEqual({});
93 expect(router.state.location.hash).toBe("");
94 });
95​
96 it("preserves the checkout through category and settings-search navigation", async () => {
97 const router = createSettingsRouter();
98 await router.navigate({ to: "/settings/general", search: checkoutSearch, hash: "new-threads" });
99 await router.navigate({ to: "/settings/integrations", hash: "", replace: true });
100 expect(router.state.location.search).toEqual(checkoutSearch);
101 expect(router.state.location.hash).toBe("");
102 await router.navigate({ to: "/settings/source-control", hash: "source-control-writing-style" });
103 expect(router.state.location.search).toEqual(checkoutSearch);
104 expect(router.state.location.hash).toBe("source-control-writing-style");
105 await router.navigate({ to: "/settings/projects", hash: "project-defaults" });
106 expect(router.state.location.search).toEqual(checkoutSearch);
107 });
108​
109 it.each(["/settings/projects", "/settings/scheduled-tasks"] as const)(
110 "keeps %s when regrouping or selecting a target from the shared settings layout",
111 async (to) => {
112 const router = createSettingsRouter();
113 await router.navigate({ to, search: checkoutSearch });
114​
115 const regroupedCheckout = { ...checkoutSearch, project: "separate:t3code" };
116 await router.navigate({
117 from: "/settings",
118 to: router.state.location.pathname,
119 search: () => regroupedCheckout,
120 replace: true,
121 hashScrollIntoView: false,
122 });
123 expect(router.state.location.pathname).toBe(to);
124 expect(router.state.location.search).toEqual(regroupedCheckout);
125 expect(router.state.redirect).toBeUndefined();
126​
127 await router.navigate({
128 from: "/settings",
129 to: router.state.location.pathname,
130 search: () => ({ machine: "another-server" }),
131 hash: "",
132 resetScroll: false,
133 });
134 expect(router.state.location.pathname).toBe(to);
135 expect(router.state.location.search).toEqual({ machine: "another-server" });
136 expect(router.state.location.hash).toBe("");
137​
138 const selectedCheckout = {
139 project: "another-project",
140 machine: "another-server",
141 checkout: "another-server:/home/user/Another checkout",
142 };
143 await router.navigate({
144 from: "/settings",
145 to: router.state.location.pathname,
146 search: () => selectedCheckout,
147 hash: "requested-setting",
148 });
149 expect(router.state.location.pathname).toBe(to);
150 expect(router.state.location.search).toEqual(selectedCheckout);
151 expect(router.state.location.hash).toBe("requested-setting");
152 },
153 );
154​
155 it("honors an explicit provider environment and drops its instance on category navigation", async () => {
156 const router = createSettingsRouter();
157 await router.navigate({ to: "/settings/general", search: checkoutSearch });
158 await router.navigate({
159 to: "/settings/providers",
160 search: {
161 environmentId: EnvironmentId.make("provider-server"),
162 instanceId: ProviderInstanceId.make("codex-work"),
163 },
164 });
165 expect(router.state.location.search).toEqual({
166 machine: "provider-server",
167 environmentId: "provider-server",
168 instanceId: "codex-work",
169 });
170 await router.navigate({ to: "/settings/general", hash: "" });
171 expect(router.state.location.search).toEqual({ machine: "provider-server" });
172 });
173​
174 it("preserves the environment from an initially loaded legacy provider URL", async () => {
175 const router = createSettingsRouter(
176 "/settings/providers?environmentId=provider-server&instanceId=codex-work",
177 );
178 await router.load();
179 await router.navigate({ to: "/settings/general" });
180 expect(router.state.location.search).toEqual({ machine: "provider-server" });
181 });
182​
183 it("retains an explicit unavailable target rather than reviving the previous environment", async () => {
184 const router = createSettingsRouter();
185 await router.navigate({ to: "/settings/general", search: { machine: "online" } });
186 await router.navigate({ to: "/settings/general", search: { machine: "removed" } });
187 expect(router.state.location.search).toEqual({ machine: "removed" });
188 expect(
189 resolveSettingsScope(
190 router.state.location.search,
191 [],
192 [{ environmentId: EnvironmentId.make("online"), label: "Online" }],
193 ),
194 ).toMatchObject({
195 kind: "unavailable",
196 reason: "environment-missing",
197 environmentIds: [],
198 });
199 });
200​
201 it("preserves escaped checkout identifiers across reload and browser history", async () => {
202 const router = createSettingsRouter();
203 await router.navigate({ to: "/settings/general", search: checkoutSearch });
204 const checkoutHref = router.state.location.href;
205 await router.navigate({ to: "/settings/general", search: { machine: "another" } });
206 router.history.back();
207 await router.load();
208 expect(router.state.location.search).toEqual(checkoutSearch);
209 const reloaded = createSettingsRouter(checkoutHref);
210 await reloaded.load();
211 expect(reloaded.state.location.search).toEqual(checkoutSearch);
212 await reloaded.navigate({ to: "/settings/integrations", hash: "agent-browser-access" });
213 expect(reloaded.state.location.search).toEqual(checkoutSearch);
214 });
215​
216 it("redirects legacy project links without carrying the prior checkout scope", async () => {
217 const router = createSettingsRouter();
218 await router.navigate({ to: "/settings/general", search: checkoutSearch });
219 await router.navigate({
220 to: "/projects/$projectKey",
221 params: { projectKey: "legacy-project" },
222 });
223 expect(router.state.redirect).not.toBeUndefined();
224 await router.navigate(router.state.redirect!.options);
225 expect(router.state.location.pathname).toBe("/settings/projects");
226 expect(router.state.location.search).toEqual({ project: "legacy-project" });
227 });
228​
229 it("keeps scope through the settings index redirect", async () => {
230 const router = createSettingsRouter();
231 await router.navigate({ to: "/settings", search: { machine: "remote-server" } });
232 expect(router.state.redirect).not.toBeUndefined();
233 await router.navigate(router.state.redirect!.options);
234 expect(router.state.location.pathname).toBe("/settings/general");
235 expect(router.state.location.search).toEqual({ machine: "remote-server" });
236 });
237});
238​
239describe("scheduled task scope navigation", () => {
240 it("opens a task link on its owning environment, then clears the task when changing filters", async () => {
241 const router = createSettingsRouter();
242 await router.navigate({
243 to: "/settings/scheduled-tasks",
244 search: {
245 environmentId: EnvironmentId.make("remote-server"),
246 taskId: ScheduledTaskId.make("task-1"),
247 },
248 });
249 expect(router.state.matches.at(-1)?.search).toEqual({
250 machine: "remote-server",
251 environmentId: "remote-server",
252 taskId: "task-1",
253 });
254 await router.navigate({
255 from: "/settings",
256 to: router.state.location.pathname,
257 search: () => ({ machine: undefined, project: undefined, checkout: undefined }),
258 });
259 expect(router.state.matches.at(-1)?.search).toEqual({});
260 });
261});