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

settingsScopeNavigation.test.ts

266 lines10.3 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([
110 "/settings/projects",
111 "/settings/integrations",
112 "/settings/source-control",
113 "/settings/scheduled-tasks",
114 ] as const)(
115 "keeps %s when regrouping or selecting a target from the shared settings layout",
116 async (to) => {
117 const router = createSettingsRouter();
118 await router.navigate({ to, search: checkoutSearch });
119​
120 const regroupedCheckout = { ...checkoutSearch, project: "separate:t3code" };
121 await router.navigate({
122 from: "/settings",
123 to: router.state.location.pathname,
124 search: () => regroupedCheckout,
125 replace: true,
126 hashScrollIntoView: false,
127 });
128 expect(router.state.location.pathname).toBe(to);
129 expect(router.state.location.search).toEqual(regroupedCheckout);
130 expect(router.state.redirect).toBeUndefined();
131​
132 await router.navigate({
133 from: "/settings",
134 to: router.state.location.pathname,
135 search: () => ({ machine: "another-server" }),
136 hash: "",
137 resetScroll: false,
138 });
139 expect(router.state.location.pathname).toBe(to);
140 expect(router.state.location.search).toEqual({ machine: "another-server" });
141 expect(router.state.location.hash).toBe("");
142​
143 const selectedCheckout = {
144 project: "another-project",
145 machine: "another-server",
146 checkout: "another-server:/home/user/Another checkout",
147 };
148 await router.navigate({
149 from: "/settings",
150 to: router.state.location.pathname,
151 search: () => selectedCheckout,
152 hash: "requested-setting",
153 });
154 expect(router.state.location.pathname).toBe(to);
155 expect(router.state.location.search).toEqual(selectedCheckout);
156 expect(router.state.location.hash).toBe("requested-setting");
157 },
158 );
159​
160 it("honors an explicit provider environment and drops its instance on category navigation", async () => {
161 const router = createSettingsRouter();
162 await router.navigate({ to: "/settings/general", search: checkoutSearch });
163 await router.navigate({
164 to: "/settings/providers",
165 search: {
166 environmentId: EnvironmentId.make("provider-server"),
167 instanceId: ProviderInstanceId.make("codex-work"),
168 },
169 });
170 expect(router.state.location.search).toEqual({
171 machine: "provider-server",
172 environmentId: "provider-server",
173 instanceId: "codex-work",
174 });
175 await router.navigate({ to: "/settings/general", hash: "" });
176 expect(router.state.location.search).toEqual({ machine: "provider-server" });
177 });
178​
179 it("preserves the environment from an initially loaded legacy provider URL", async () => {
180 const router = createSettingsRouter(
181 "/settings/providers?environmentId=provider-server&instanceId=codex-work",
182 );
183 await router.load();
184 await router.navigate({ to: "/settings/general" });
185 expect(router.state.location.search).toEqual({ machine: "provider-server" });
186 });
187​
188 it("retains an explicit unavailable target rather than reviving the previous environment", async () => {
189 const router = createSettingsRouter();
190 await router.navigate({ to: "/settings/general", search: { machine: "online" } });
191 await router.navigate({ to: "/settings/general", search: { machine: "removed" } });
192 expect(router.state.location.search).toEqual({ machine: "removed" });
193 expect(
194 resolveSettingsScope(
195 router.state.location.search,
196 [],
197 [{ environmentId: EnvironmentId.make("online"), label: "Online" }],
198 ),
199 ).toMatchObject({
200 kind: "unavailable",
201 reason: "environment-missing",
202 environmentIds: [],
203 });
204 });
205​
206 it("preserves escaped checkout identifiers across reload and browser history", async () => {
207 const router = createSettingsRouter();
208 await router.navigate({ to: "/settings/general", search: checkoutSearch });
209 const checkoutHref = router.state.location.href;
210 await router.navigate({ to: "/settings/general", search: { machine: "another" } });
211 router.history.back();
212 await router.load();
213 expect(router.state.location.search).toEqual(checkoutSearch);
214 const reloaded = createSettingsRouter(checkoutHref);
215 await reloaded.load();
216 expect(reloaded.state.location.search).toEqual(checkoutSearch);
217 await reloaded.navigate({ to: "/settings/integrations", hash: "agent-browser-access" });
218 expect(reloaded.state.location.search).toEqual(checkoutSearch);
219 });
220​
221 it("redirects legacy project links without carrying the prior checkout scope", async () => {
222 const router = createSettingsRouter();
223 await router.navigate({ to: "/settings/general", search: checkoutSearch });
224 await router.navigate({
225 to: "/projects/$projectKey",
226 params: { projectKey: "legacy-project" },
227 });
228 expect(router.state.redirect).not.toBeUndefined();
229 await router.navigate(router.state.redirect!.options);
230 expect(router.state.location.pathname).toBe("/settings/projects");
231 expect(router.state.location.search).toEqual({ project: "legacy-project" });
232 });
233​
234 it("keeps scope through the settings index redirect", async () => {
235 const router = createSettingsRouter();
236 await router.navigate({ to: "/settings", search: { machine: "remote-server" } });
237 expect(router.state.redirect).not.toBeUndefined();
238 await router.navigate(router.state.redirect!.options);
239 expect(router.state.location.pathname).toBe("/settings/general");
240 expect(router.state.location.search).toEqual({ machine: "remote-server" });
241 });
242});
243​
244describe("scheduled task scope navigation", () => {
245 it("opens a task link on its owning environment, then clears the task when changing filters", async () => {
246 const router = createSettingsRouter();
247 await router.navigate({
248 to: "/settings/scheduled-tasks",
249 search: {
250 environmentId: EnvironmentId.make("remote-server"),
251 taskId: ScheduledTaskId.make("task-1"),
252 },
253 });
254 expect(router.state.matches.at(-1)?.search).toEqual({
255 machine: "remote-server",
256 environmentId: "remote-server",
257 taskId: "task-1",
258 });
259 await router.navigate({
260 from: "/settings",
261 to: router.state.location.pathname,
262 search: () => ({ machine: undefined, project: undefined, checkout: undefined }),
263 });
264 expect(router.state.matches.at(-1)?.search).toEqual({});
265 });
266});