Open History to see the last commit
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 });
81}
82
83describe("settings scope navigation", () => {
84 it("replaces the default scope with an explicit environment, then replaces it with a project", async () => {
85 const router = createSettingsRouter();
86 await router.load();
87 await router.navigate({
88 to: "/settings/general",
89 search: { machine: "remote-server" },
90 hash: "",
91 });
92 expect(router.state.location.search).toEqual({ machine: "remote-server" });
93 await router.navigate({ to: "/settings/projects", search: { project: "another-project" } });
94 expect(router.state.location.search).toEqual({ project: "another-project" });
95 });
96
97 it("clears a checkout when selecting all environments and all projects", async () => {
98 const router = createSettingsRouter();
99 await router.navigate({ to: "/settings/general", search: checkoutSearch, hash: "old-setting" });
100 await router.navigate({
101 to: "/settings/general",
102 search: { project: undefined, machine: undefined, checkout: undefined },
103 hash: "",
104 });
105 expect(router.state.location.search).toEqual({});
106 expect(router.state.location.hash).toBe("");
107 });
108
109 it("preserves the checkout through category and settings-search navigation", async () => {
110 const router = createSettingsRouter();
111 await router.navigate({ to: "/settings/general", search: checkoutSearch, hash: "new-threads" });
112 await router.navigate({ to: "/settings/integrations", hash: "", replace: true });
113 expect(router.state.location.search).toEqual(checkoutSearch);
114 expect(router.state.location.hash).toBe("");
115 await router.navigate({ to: "/settings/source-control", hash: "source-control-writing-style" });
116 expect(router.state.location.search).toEqual(checkoutSearch);
117 expect(router.state.location.hash).toBe("source-control-writing-style");
118 await router.navigate({ to: "/settings/projects", hash: "project-defaults" });
119 expect(router.state.location.search).toEqual(checkoutSearch);
120 });
121
122 it.each([
123 "/settings/projects",
124 "/settings/integrations",
125 "/settings/source-control",
126 "/settings/scheduled-tasks",
127 ] as const)(
128 "keeps %s when regrouping or selecting a target from the shared settings layout",
129 async (to) => {
130 const router = createSettingsRouter();
131 await router.navigate({ to, search: checkoutSearch });
132
133 const regroupedCheckout = { ...checkoutSearch, project: "separate:t3code" };
134 await router.navigate({
135 from: "/settings",
136 to: router.state.location.pathname,
137 search: () => regroupedCheckout,
138 replace: true,
139 hashScrollIntoView: false,
140 });
141 expect(router.state.location.pathname).toBe(to);
142 expect(router.state.location.search).toEqual(regroupedCheckout);
143 expect(router.state.redirect).toBeUndefined();
144
145 await router.navigate({
146 from: "/settings",
147 to: router.state.location.pathname,
148 search: () => ({ machine: "another-server" }),
149 hash: "",
150 resetScroll: false,
151 });
152 expect(router.state.location.pathname).toBe(to);
153 expect(router.state.location.search).toEqual({ machine: "another-server" });
154 expect(router.state.location.hash).toBe("");
155
156 const selectedCheckout = {
157 project: "another-project",
158 machine: "another-server",
159 checkout: "another-server:/home/user/Another checkout",
160 };
161 await router.navigate({
162 from: "/settings",
163 to: router.state.location.pathname,
164 search: () => selectedCheckout,
165 hash: "requested-setting",
166 });
167 expect(router.state.location.pathname).toBe(to);
168 expect(router.state.location.search).toEqual(selectedCheckout);
169 expect(router.state.location.hash).toBe("requested-setting");
170 },
171 );
172
173 it("honors an explicit provider environment and drops its instance on category navigation", async () => {
174 const router = createSettingsRouter();
175 await router.navigate({ to: "/settings/general", search: checkoutSearch });
176 await router.navigate({
177 to: "/settings/providers",
178 search: {
179 environmentId: EnvironmentId.make("provider-server"),
180 instanceId: ProviderInstanceId.make("codex-work"),
181 },
182 });
183 expect(router.state.location.search).toEqual({
184 machine: "provider-server",
185 environmentId: "provider-server",
186 instanceId: "codex-work",
187 });
188 await router.navigate({ to: "/settings/general", hash: "" });
189 expect(router.state.location.search).toEqual({ machine: "provider-server" });
190 });
191
192 it("preserves the environment from an initially loaded legacy provider URL", async () => {
193 const router = createSettingsRouter(
194 "/settings/providers?environmentId=provider-server&instanceId=codex-work",
195 );
196 await router.load();
197 await router.navigate({ to: "/settings/general" });
198 expect(router.state.location.search).toEqual({ machine: "provider-server" });
199 });
200
201 it("retains an explicit unavailable target rather than reviving the previous environment", async () => {
202 const router = createSettingsRouter();
203 await router.navigate({ to: "/settings/general", search: { machine: "online" } });
204 await router.navigate({ to: "/settings/general", search: { machine: "removed" } });
205 expect(router.state.location.search).toEqual({ machine: "removed" });
206 expect(
207 resolveSettingsScope(
208 router.state.location.search,
209 [],
210 [{ environmentId: EnvironmentId.make("online"), label: "Online" }],
211 ),
212 ).toMatchObject({
213 kind: "unavailable",
214 reason: "environment-missing",
215 environmentIds: [],
216 });
217 });
218
219 it("respects explicit clearing keys instead of restoring the previous checkout", async () => {
220 const router = createSettingsRouter();
221 await router.navigate({ to: "/settings/general", search: checkoutSearch });
222 await router.navigate({
223 to: "/settings/projects",
224 search: { project: "different-project", machine: undefined },
225 });
226 expect(router.state.location.search).toEqual({ project: "different-project" });
227 });
228
229 it("preserves escaped checkout identifiers across reload and browser history", async () => {
230 const router = createSettingsRouter();
231 await router.navigate({ to: "/settings/general", search: checkoutSearch });
232 const checkoutHref = router.state.location.href;
233 await router.navigate({ to: "/settings/general", search: { machine: "another" } });
234 router.history.back();
235 await router.load();
236 expect(router.state.location.search).toEqual(checkoutSearch);
237 const reloaded = createSettingsRouter(checkoutHref);
238 await reloaded.load();
239 expect(reloaded.state.location.search).toEqual(checkoutSearch);
240 await reloaded.navigate({ to: "/settings/integrations", hash: "agent-browser-access" });
241 expect(reloaded.state.location.search).toEqual(checkoutSearch);
242 });
243
244 it("redirects legacy project links without carrying the prior checkout scope", async () => {
245 const router = createSettingsRouter();
246 await router.navigate({ to: "/settings/general", search: checkoutSearch });
247 await router.navigate({
248 to: "/projects/$projectKey",
249 params: { projectKey: "legacy-project" },
250 });
251 expect(router.state.redirect).not.toBeUndefined();
252 await router.navigate(router.state.redirect!.options);
253 expect(router.state.location.pathname).toBe("/settings/projects");
254 expect(router.state.location.search).toEqual({ project: "legacy-project" });
255 });
256
257 it("keeps scope through the settings index redirect", async () => {
258 const router = createSettingsRouter();
259 await router.navigate({ to: "/settings", search: { machine: "remote-server" } });
260 expect(router.state.redirect).not.toBeUndefined();
261 await router.navigate(router.state.redirect!.options);
262 expect(router.state.location.pathname).toBe("/settings/general");
263 expect(router.state.location.search).toEqual({ machine: "remote-server" });
264 });
265});
266
267describe("scheduled task scope navigation", () => {
268 it("opens a task link on its owning environment, then clears the task when changing filters", async () => {
269 const router = createSettingsRouter();
270 await router.navigate({
271 to: "/settings/scheduled-tasks",
272 search: {
273 environmentId: EnvironmentId.make("remote-server"),
274 taskId: ScheduledTaskId.make("task-1"),
275 },
276 });
277 expect(router.state.matches.at(-1)?.search).toEqual({
278 machine: "remote-server",
279 environmentId: "remote-server",
280 taskId: "task-1",
281 });
282 await router.navigate({
283 from: "/settings",
284 to: router.state.location.pathname,
285 search: () => ({ machine: undefined, project: undefined, checkout: undefined }),
286 });
287 expect(router.state.matches.at(-1)?.search).toEqual({});
288 });
289
290 it("keeps the project and checkout filters when entering scheduled tasks", async () => {
291 const router = createSettingsRouter();
292 await router.navigate({ to: "/settings/projects", search: checkoutSearch });
293 await router.navigate({ to: "/settings/scheduled-tasks" });
294 expect(router.state.matches.at(-1)?.search).toEqual(checkoutSearch);
295 });
296});