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

settingsScopeNavigation.test.ts

247 lines10.2 KB

1import { EnvironmentId, ProviderInstanceId } 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​
14const checkoutSearch = {
15 project: "repository:t3code",
16 machine: "remote-server",
17 checkout: "remote-server:/home/user/T3 Code",
18};
19​
20function createSettingsRouter(initialEntry = "/settings/general") {
21 const root = createRootRoute();
22 const settings = createRoute({
23 getParentRoute: () => root,
24 path: "settings",
25 validateSearch: validateSettingsRouteSearch,
26 search: { middlewares: [retainSettingsScope] },
27 beforeLoad: ({ location }) => {
28 if (location.pathname === "/settings") {
29 throw redirect({ to: "/settings/general", replace: true });
30 }
31 },
32 });
33 const general = createRoute({ getParentRoute: () => settings, path: "general" });
34 const projects = createRoute({ getParentRoute: () => settings, path: "projects" });
35 const integrations = createRoute({ getParentRoute: () => settings, path: "integrations" });
36 const sourceControl = createRoute({ getParentRoute: () => settings, path: "source-control" });
37 const providers = createRoute({
38 getParentRoute: () => settings,
39 path: "providers",
40 validateSearch: (raw: Record<string, unknown>) => ({
41 ...(typeof raw.environmentId === "string" && raw.environmentId.trim()
42 ? { environmentId: EnvironmentId.make(raw.environmentId) }
43 : {}),
44 ...(typeof raw.instanceId === "string" && raw.instanceId.trim()
45 ? { instanceId: ProviderInstanceId.make(raw.instanceId) }
46 : {}),
47 }),
48 });
49 const legacyProject = createRoute({
50 getParentRoute: () => root,
51 path: "projects/$projectKey",
52 beforeLoad: ({ params }) => {
53 throw redirect({
54 to: "/settings/projects",
55 search: { project: params.projectKey, machine: undefined },
56 replace: true,
57 });
58 },
59 });
60 return createRouter({
61 routeTree: root.addChildren([
62 settings.addChildren([general, projects, integrations, sourceControl, providers]),
63 legacyProject,
64 ]),
65 history: createMemoryHistory({ initialEntries: [initialEntry] }),
66 });
68​
69describe("settings scope navigation", () => {
70 it("replaces the default scope with an explicit environment, then replaces it with a project", async () => {
71 const router = createSettingsRouter();
72 await router.load();
73 await router.navigate({
74 to: "/settings/general",
75 search: { machine: "remote-server" },
76 hash: "",
77 });
78 expect(router.state.location.search).toEqual({ machine: "remote-server" });
79 await router.navigate({ to: "/settings/projects", search: { project: "another-project" } });
80 expect(router.state.location.search).toEqual({ project: "another-project" });
81 });
82​
83 it("clears a checkout when selecting all environments and all projects", async () => {
84 const router = createSettingsRouter();
85 await router.navigate({ to: "/settings/general", search: checkoutSearch, hash: "old-setting" });
86 // The scope selects send every axis explicitly so "all" does not read as "unchanged".
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/integrations", "/settings/source-control"] 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("respects explicit clearing keys instead of restoring the previous checkout", async () => {
202 const router = createSettingsRouter();
203 await router.navigate({ to: "/settings/general", search: checkoutSearch });
204 await router.navigate({
205 to: "/settings/projects",
206 search: { project: "different-project", machine: undefined },
207 });
208 expect(router.state.location.search).toEqual({ project: "different-project" });
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});