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

settingsScopeNavigation.test.ts

339 lines13.7 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 { Route as ProvidersRoute } from "../../routes/_chat.settings.providers";
15import { validateScheduledTasksSearch } from "./scheduledTasksSettings.logic";
16​
17const checkoutSearch = {
18 project: "repository:t3code",
19 machine: "remote-server",
20 checkout: "remote-server:/home/user/T3 Code",
21};
22​
23function createSettingsRouter(initialEntry = "/settings/general") {
24 const root = createRootRoute();
25 const settings = createRoute({
26 getParentRoute: () => root,
27 path: "settings",
28 validateSearch: validateSettingsRouteSearch,
29 search: { middlewares: [retainSettingsScope] },
30 beforeLoad: ({ location }) => {
31 if (location.pathname === "/settings") {
32 throw redirect({ to: "/settings/general", replace: true });
33 }
34 },
35 });
36 const general = createRoute({ getParentRoute: () => settings, path: "general" });
37 const projects = createRoute({ getParentRoute: () => settings, path: "projects" });
38 const integrations = createRoute({ getParentRoute: () => settings, path: "integrations" });
39 const sourceControl = createRoute({ getParentRoute: () => settings, path: "source-control" });
40 const providers = createRoute({
41 getParentRoute: () => settings,
42 path: "providers",
43 validateSearch: ProvidersRoute.options.validateSearch,
44 });
45 const scheduledTasks = createRoute({
46 getParentRoute: () => settings,
47 path: "scheduled-tasks",
48 validateSearch: validateScheduledTasksSearch,
49 });
50 const legacyProject = createRoute({
51 getParentRoute: () => root,
52 path: "projects/$projectKey",
53 beforeLoad: ({ params }) => {
54 throw redirect({
55 to: "/settings/projects",
56 search: { project: params.projectKey, machine: undefined },
57 replace: true,
58 });
59 },
60 });
61 return createRouter({
62 routeTree: root.addChildren([
63 settings.addChildren([
64 general,
65 projects,
66 integrations,
67 sourceControl,
68 providers,
69 scheduledTasks,
70 ]),
71 legacyProject,
72 ]),
73 history: createMemoryHistory({ initialEntries: [initialEntry] }),
74 });
76​
77describe("settings scope navigation", () => {
78 it("clears a checkout when selecting all environments and all projects", async () => {
79 const router = createSettingsRouter();
80 await router.navigate({ to: "/settings/general", search: checkoutSearch, hash: "old-setting" });
81 await router.navigate({
82 to: "/settings/general",
83 search: { project: undefined, machine: undefined, checkout: undefined },
84 hash: "",
85 });
86 expect(router.state.location.search).toEqual({});
87 expect(router.state.location.hash).toBe("");
88 });
89​
90 it("preserves the checkout through category and settings-search navigation", async () => {
91 const router = createSettingsRouter();
92 await router.navigate({ to: "/settings/general", search: checkoutSearch, hash: "new-threads" });
93 await router.navigate({ to: "/settings/integrations", hash: "", replace: true });
94 expect(router.state.location.search).toEqual(checkoutSearch);
95 expect(router.state.location.hash).toBe("");
96 await router.navigate({ to: "/settings/source-control", hash: "source-control-writing-style" });
97 expect(router.state.location.search).toEqual(checkoutSearch);
98 expect(router.state.location.hash).toBe("source-control-writing-style");
99 await router.navigate({ to: "/settings/projects", hash: "project-defaults" });
100 expect(router.state.location.search).toEqual(checkoutSearch);
101 });
102​
103 it.each(["/settings/projects", "/settings/scheduled-tasks"] as const)(
104 "keeps %s when regrouping or selecting a target from the shared settings layout",
105 async (to) => {
106 const router = createSettingsRouter();
107 await router.navigate({ to, search: checkoutSearch });
108​
109 const regroupedCheckout = { ...checkoutSearch, project: "separate:t3code" };
110 await router.navigate({
111 from: "/settings",
112 to: router.state.location.pathname,
113 search: () => regroupedCheckout,
114 replace: true,
115 hashScrollIntoView: false,
116 });
117 expect(router.state.location.pathname).toBe(to);
118 expect(router.state.location.search).toEqual(regroupedCheckout);
119 expect(router.state.redirect).toBeUndefined();
120​
121 await router.navigate({
122 from: "/settings",
123 to: router.state.location.pathname,
124 search: () => ({ machine: "another-server" }),
125 hash: "",
126 resetScroll: false,
127 });
128 expect(router.state.location.pathname).toBe(to);
129 expect(router.state.location.search).toEqual({ machine: "another-server" });
130 expect(router.state.location.hash).toBe("");
131​
132 const selectedCheckout = {
133 project: "another-project",
134 machine: "another-server",
135 checkout: "another-server:/home/user/Another checkout",
136 };
137 await router.navigate({
138 from: "/settings",
139 to: router.state.location.pathname,
140 search: () => selectedCheckout,
141 hash: "requested-setting",
142 });
143 expect(router.state.location.pathname).toBe(to);
144 expect(router.state.location.search).toEqual(selectedCheckout);
145 expect(router.state.location.hash).toBe("requested-setting");
146 },
147 );
148​
149 it("honors an explicit provider environment and drops its instance on category navigation", async () => {
150 const router = createSettingsRouter();
151 await router.navigate({ to: "/settings/general", search: checkoutSearch });
152 await router.navigate({
153 to: "/settings/providers",
154 search: {
155 environmentId: EnvironmentId.make("provider-server"),
156 instanceId: ProviderInstanceId.make("codex-work"),
157 },
158 });
159 expect(router.state.location.search).toEqual({
160 machine: "provider-server",
161 environmentId: "provider-server",
162 instanceId: "codex-work",
163 });
164 await router.navigate({ to: "/settings/general", hash: "" });
165 expect(router.state.location.search).toEqual({ machine: "provider-server" });
166 });
167​
168 it("preserves the environment from an initially loaded legacy provider URL", async () => {
169 const router = createSettingsRouter(
170 "/settings/providers?environmentId=provider-server&instanceId=codex-work",
171 );
172 await router.load();
173 await router.navigate({ to: "/settings/general" });
174 expect(router.state.location.search).toEqual({ machine: "provider-server" });
175 });
176​
177 it.each(["my%20codex", "1codex"])(
178 "ignores the invalid provider instance id %s instead of erroring the page",
179 async (instanceId) => {
180 const router = createSettingsRouter();
181 await router.navigate({
182 href: `/settings/providers?environmentId=provider-server&instanceId=${instanceId}`,
183 });
184 const match = router.state.matches.at(-1);
185 expect(match).toMatchObject({ status: "success", error: undefined });
186 expect(match?.search).toEqual({
187 machine: "provider-server",
188 environmentId: "provider-server",
189 });
190 },
191 );
192​
193 it("retains an explicit unavailable target rather than reviving the previous environment", async () => {
194 const router = createSettingsRouter();
195 await router.navigate({ to: "/settings/general", search: { machine: "online" } });
196 await router.navigate({ to: "/settings/general", search: { machine: "removed" } });
197 expect(router.state.location.search).toEqual({ machine: "removed" });
198 expect(
199 resolveSettingsScope(
200 router.state.location.search,
201 [],
202 [{ environmentId: EnvironmentId.make("online"), label: "Online" }],
203 ),
204 ).toMatchObject({
205 kind: "unavailable",
206 reason: "environment-missing",
207 environmentIds: [],
208 });
209 });
210​
211 it("preserves escaped checkout identifiers across reload and browser history", async () => {
212 const router = createSettingsRouter();
213 await router.navigate({ to: "/settings/general", search: checkoutSearch });
214 const checkoutHref = router.state.location.href;
215 await router.navigate({ to: "/settings/general", search: { machine: "another" } });
216 router.history.back();
217 await router.load();
218 expect(router.state.location.search).toEqual(checkoutSearch);
219 const reloaded = createSettingsRouter(checkoutHref);
220 await reloaded.load();
221 expect(reloaded.state.location.search).toEqual(checkoutSearch);
222 await reloaded.navigate({ to: "/settings/integrations", hash: "agent-browser-access" });
223 expect(reloaded.state.location.search).toEqual(checkoutSearch);
224 });
225​
226 it("redirects legacy project links without carrying the prior checkout scope", async () => {
227 const router = createSettingsRouter();
228 await router.navigate({ to: "/settings/general", search: checkoutSearch });
229 await router.navigate({
230 to: "/projects/$projectKey",
231 params: { projectKey: "legacy-project" },
232 });
233 expect(router.state.redirect).not.toBeUndefined();
234 await router.navigate(router.state.redirect!.options);
235 expect(router.state.location.pathname).toBe("/settings/projects");
236 expect(router.state.location.search).toEqual({ project: "legacy-project" });
237 });
238​
239 it("keeps scope through the settings index redirect", async () => {
240 const router = createSettingsRouter();
241 await router.navigate({ to: "/settings", search: { machine: "remote-server" } });
242 expect(router.state.redirect).not.toBeUndefined();
243 await router.navigate(router.state.redirect!.options);
244 expect(router.state.location.pathname).toBe("/settings/general");
245 expect(router.state.location.search).toEqual({ machine: "remote-server" });
246 });
247​
248 it("drops boolean, numeric, and blank scope params and keeps legacy environmentId fallback", async () => {
249 for (const [url, expected] of [
250 ["/settings/integrations?checkout=true", {}],
251 ["/settings/integrations?project=123&machine=456&checkout=789", {}],
252 ["/settings/integrations?project=%20&machine=%20&checkout=%20", {}],
253 ["/settings/integrations?project=t3code&checkout=true", { project: "t3code" }],
254 [
255 "/settings/providers?environmentId=provider-server&machine=123",
256 { machine: "provider-server" },
257 ],
258 [
259 "/settings/providers?environmentId=provider-server&project=%20",
260 { machine: "provider-server" },
261 ],
262 ["/settings/providers?environmentId=%20", {}],
263 ] as const) {
264 const router = createSettingsRouter(url);
265 const matched = router.matchRoutes(router.latestLocation).at(-1)?.search;
266 expect(matched).toBeDefined();
267 expect(matched?.project).toBe("project" in expected ? expected.project : undefined);
268 expect(matched?.machine).toBe("machine" in expected ? expected.machine : undefined);
269 expect(matched?.checkout).toBeUndefined();
270 // Navigate straight from the uncleaned URL to exercise retainSettingsScope on client navigation.
271 await router.navigate({ to: "/settings/source-control" });
272 expect(router.state.location.search).toEqual(expected);
273 }
274 });
275});
276​
277describe("scheduled task scope navigation", () => {
278 it("opens a task link on its owning environment, then clears the task when changing filters", async () => {
279 const router = createSettingsRouter();
280 await router.navigate({
281 to: "/settings/scheduled-tasks",
282 search: {
283 environmentId: EnvironmentId.make("remote-server"),
284 taskId: ScheduledTaskId.make("task-1"),
285 },
286 });
287 expect(router.state.matches.at(-1)?.search).toEqual({
288 machine: "remote-server",
289 environmentId: "remote-server",
290 taskId: "task-1",
291 });
292 await router.navigate({
293 from: "/settings",
294 to: router.state.location.pathname,
295 search: () => ({ machine: undefined, project: undefined, checkout: undefined }),
296 });
297 expect(router.state.matches.at(-1)?.search).toEqual({});
298 });
299​
300 it("keeps the scope when leaving scheduled tasks with its search", async () => {
301 const router = createSettingsRouter("/settings/general?machine=remote-server");
302 await router.navigate({ to: "/settings/scheduled-tasks" });
303 await router.navigate({
304 from: "/settings",
305 to: "/settings/source-control",
306 search: (prev) => prev,
307 });
308 expect(router.state.location.search).toEqual({ machine: "remote-server" });
309 });
310​
311 it("drops numeric, boolean, and blank taskId and environmentId params instead of leaking raw search values", async () => {
312 for (const [url, expectedMatched, expectedNavigated] of [
313 ["/settings/scheduled-tasks?taskId=123", {}, {}],
314 ["/settings/scheduled-tasks?taskId=%20", {}, {}],
315 ["/settings/scheduled-tasks?environmentId=123&taskId=true", {}, {}],
316 [
317 "/settings/scheduled-tasks?environmentId=remote-server&taskId=123",
318 { machine: "remote-server", environmentId: "remote-server" },
319 { machine: "remote-server" },
320 ],
321 ["/settings/scheduled-tasks?environmentId=%20&taskId=task-1", { taskId: "task-1" }, {}],
322 ] as const) {
323 const router = createSettingsRouter(url);
324 const matched = router.matchRoutes(router.latestLocation).at(-1)?.search;
325 expect(matched).toBeDefined();
326 expect(matched?.machine).toBe(
327 "machine" in expectedMatched ? expectedMatched.machine : undefined,
328 );
329 expect(matched?.environmentId).toBe(
330 "environmentId" in expectedMatched ? expectedMatched.environmentId : undefined,
331 );
332 expect(matched?.taskId).toBe(
333 "taskId" in expectedMatched ? expectedMatched.taskId : undefined,
334 );
335 await router.navigate({ to: "/settings/source-control" });
336 expect(router.state.location.search).toEqual(expectedNavigated);
337 }
338 });
339});