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

settingsSearch.test.ts

387 lines15.0 KB

1import { describe, expect, it, vi } from "vite-plus/test";
2import {
3 EnvironmentId,
4 makeProviderSettingsSchema,
5 SourceControlProviderKind,
6} from "@t3tools/contracts";
7import { UNKNOWN_SOURCE_CONTROL_CLIENT } from "@t3tools/client-runtime/source-control-clients";
8import * as Schema from "effect/Schema";
9​
10import {
11 filterAvailableSettingsSearchItems,
12 getSettingsSearchTargetScope,
13 getThreadAutoSettlementSearchAvailability,
14 isSettingsSearchScopeAvailable,
15 movedSettingsPath,
16 searchSettings,
17 SETTINGS_NAV_GROUPS,
18 SETTINGS_SEARCH_ITEMS,
19 sourceControlHostSearchItems,
20 sourceControlHostSettingsSearchId,
21 type SettingsSearchItem,
22} from "./settingsSearch";
23​
24vi.mock("~/env", async (importOriginal) => ({
25 ...(await importOriginal<typeof import("~/env")>()),
26 isSecretsFeatureEnabled: false,
27}));
28​
29const ITEMS: ReadonlyArray<SettingsSearchItem> = [
30 {
31 id: "word-wrap",
32 title: "Word wrap",
33 to: "/settings/general",
34 searchTerms: ["long lines in code previews"],
35 },
36 {
37 id: "network-access",
38 title: "Network access",
39 to: "/settings/access",
40 searchTerms: ["remote pairing backend"],
41 },
42 {
43 id: "providers",
44 title: "Providers",
45 to: "/settings/providers",
46 searchTerms: ["claude codex agents"],
47 },
48 {
49 id: "provider-updates",
50 title: "Update checks",
51 to: "/settings/general",
52 },
53 {
54 id: "automatic-updates",
55 title: "Automatic updates",
56 to: "/settings/general",
57 },
58];
59​
60describe("searchSettings", () => {
61 it("matches titles, sections, and remembered setting details", () => {
62 expect(searchSettings("word", ITEMS).map((item) => item.id)).toEqual(["word-wrap"]);
63 expect(searchSettings("network", ITEMS).map((item) => item.id)).toEqual(["network-access"]);
64 expect(searchSettings("sharing", ITEMS).map((item) => item.id)).toEqual(["network-access"]);
65 expect(searchSettings("claude", ITEMS).map((item) => item.id)).toEqual(["providers"]);
66 expect(searchSettings("long lines", ITEMS).map((item) => item.id)).toEqual(["word-wrap"]);
67 for (const query of ["fallback", "fallback agent", "fallback model"]) {
68 expect(searchSettings(query)[0]).toMatchObject({
69 id: "fallback-model",
70 to: "/settings/threads",
71 scope: "environment-defaults",
72 });
73 }
74 });
75​
76 it("hides the Secrets page from nav and search while the secrets feature is disabled", () => {
77 expect(SETTINGS_NAV_GROUPS.flatMap((group) => group.paths)).not.toContain("/settings/secrets");
78 expect(searchSettings("1password").map((item) => item.to)).not.toContain("/settings/secrets");
79 });
80​
81 it("matches normalized title substrings", () => {
82 expect(searchSettings(" WORD WRAP ", ITEMS).map((item) => item.id)).toEqual(["word-wrap"]);
83 expect(searchSettings("glass").map((item) => item.id)).toEqual(["setting-glass-opacity"]);
84 expect(searchSettings("panel animations").map((item) => item.id)).toEqual(["panel-animations"]);
85 expect(searchSettings("thè\u{1ab0}mes")[0]?.id).toBe("theme");
86 const localeLowerCase = vi.spyOn(String.prototype, "toLocaleLowerCase").mockReturnValue("gıt");
87 try {
88 expect(searchSettings("GIT")[0]?.id).toBe("git-fetch-interval");
89 expect(localeLowerCase).not.toHaveBeenCalled();
90 } finally {
91 localeLowerCase.mockRestore();
92 }
93 expect(searchSettings("xyzzy")).toEqual([]);
94 });
95​
96 it("matches query words across fields and ranks the strongest result first", () => {
97 expect(searchSettings("pairing remote", ITEMS).map((item) => item.id)).toEqual([
98 "network-access",
99 ]);
100 expect(
101 searchSettings("remote pairing")
102 .slice(0, 2)
103 .map((item) => item.id),
104 ).toEqual(["network-access", "connections-environment"]);
105 });
106​
107 it("hides desktop-only settings from browser search", () => {
108 expect(SETTINGS_SEARCH_ITEMS.some((item) => item.id === "quit-confirmation")).toBe(true);
109 expect(searchSettings("hold to quit")).toEqual([]);
110 expect(searchSettings("wsl")).toEqual([]);
111 expect(searchSettings("update track")).toEqual([]);
112 });
113​
114 it("hides settings whose controls are unavailable", () => {
115 const available = filterAvailableSettingsSearchItems({
116 hasCloudPublicConfig: false,
117 hasEnvironment: false,
118 hasProviderSettingsEnvironment: false,
119 canManageLocalBackend: false,
120 isWslSettingsRowVisible: false,
121 hasThreadAutoSettlement: false,
122 });
123​
124 const gatedIds = new Set<string>([
125 "follow-change-request-templates",
126 "git-fetch-interval",
127 "network-access",
128 "publish-agent-activity",
129 "provider-health-check-interval",
130 "source-control-writer-model",
131 "review-guide-model",
132 "review-guide-instructions",
133 "source-control-writing-style",
134 "t3-connect",
135 "hold-webhooks-while-offline",
136 "tailscale-https",
137 "wsl-backend",
138 "auto-settle-inactive-threads",
139 "auto-settle-merged-threads",
140 "days-before-auto-settle",
141 "linked-environments",
142 ]);
143 expect(available.map((item) => item.id).filter((id) => gatedIds.has(id))).toEqual([]);
144 });
145​
146 it("finds linked environments on a remote host without local maintenance access", () => {
147 const availability = {
148 hasCloudPublicConfig: false,
149 hasEnvironment: true,
150 hasProviderSettingsEnvironment: true,
151 canManageLocalBackend: false,
152 localEnvironmentDisabled: true,
153 isWslSettingsRowVisible: false,
154 hasThreadAutoSettlement: false,
155 };
156 const results = (hasPeerLinkHost: boolean) =>
157 searchSettings(
158 "linked environments",
159 filterAvailableSettingsSearchItems({ ...availability, hasPeerLinkHost }),
160 );
161 expect(results(false).map((item) => item.id)).not.toContain("linked-environments");
162 expect(results(true)).toContainEqual(
163 expect.objectContaining({
164 id: "linked-environments",
165 to: "/settings/connections",
166 targetId: "linked-environments",
167 }),
168 );
169 });
170​
171 it("finds the Welcome page only where a primary computer can open it", () => {
172 const availability = {
173 hasCloudPublicConfig: true,
174 hasEnvironment: true,
175 hasProviderSettingsEnvironment: true,
176 canManageLocalBackend: false,
177 isWslSettingsRowVisible: false,
178 hasThreadAutoSettlement: false,
179 };
180 const ids = (hasPrimaryEnvironment: boolean) =>
181 searchSettings(
182 "welcome page",
183 filterAvailableSettingsSearchItems({ ...availability, hasPrimaryEnvironment }),
184 ).map((item) => item.id);
185 // Hosted web reaches remote computers but has no primary one to set up.
186 expect(ids(false)).not.toContain("welcome-page");
187 expect(ids(true)).toContain("welcome-page");
188 });
189​
190 it("finds keybinding commands by label, command id, and default key", () => {
191 expect(searchSettings("toggle sidebar")[0]?.id).toBe("keybinding-sidebar.toggle");
192 expect(searchSettings("sidebar.toggle")[0]?.id).toBe("keybinding-sidebar.toggle");
193 expect(searchSettings("mod+b")[0]?.id).toBe("keybinding-sidebar.toggle");
194 expect(searchSettings("copy link")[0]).toMatchObject({
195 id: "keybinding-thread.copyReference",
196 to: "/settings/keybindings",
197 });
198 });
199​
200 it("sends commands without a default binding to the section", () => {
201 expect(searchSettings("thread.stop")[0]).toMatchObject({
202 id: "keybinding-thread.stop",
203 targetId: "keybindings",
204 });
205 expect(searchSettings("sidebar.toggle")[0]?.targetId).toBeUndefined();
206 });
207​
208 it("finds rows by the titles shown on their pages", () => {
209 for (const [title, id] of [
210 ["Automatic worktree cleanup", "storage-worktrees"],
211 ["Delete settled worktrees", "storage-worktrees"],
212 ["Delete old browser artifacts", "storage-artifacts"],
213 ["Delete old rotated logs", "storage-artifacts"],
214 ["Custom search URL", "browser-search-engine"],
215 ["Native sessions", "providers"],
216 ["CODEX_HOME path", "providers"],
217 ["Shadow home path", "providers"],
218 ] as const) {
219 expect(searchSettings(title)[0]?.id, title).toBe(id);
220 }
221 });
222​
223 it("keeps catalog result ids unique", () => {
224 const ids = SETTINGS_SEARCH_ITEMS.map((item) => item.id);
225 expect(new Set(ids).size).toBe(ids.length);
226 });
227});
228​
229describe("settings search targets", () => {
230 it("requires an environment for the resolved telemetry export destination", () => {
231 const result = searchSettings("OpenTelemetry export").find(
232 (item) => item.id === "telemetry-export",
233 )!;
234 const target = getSettingsSearchTargetScope(result.targetId ?? result.id)!;
235 expect(isSettingsSearchScopeAvailable(target.scope, "all")).toBe(false);
236 expect(isSettingsSearchScopeAvailable(target.scope, "project")).toBe(false);
237 expect(isSettingsSearchScopeAvailable(target.scope, "environment")).toBe(true);
238 });
239​
240 it("keeps environment-wide settings out of project scopes", () => {
241 const updates = getSettingsSearchTargetScope("provider-update-checks")!;
242 expect(updates.scope).toBe("environment-defaults");
243 expect(isSettingsSearchScopeAvailable(updates.scope, "environment")).toBe(true);
244 expect(isSettingsSearchScopeAvailable(updates.scope, "all")).toBe(true);
245 expect(isSettingsSearchScopeAvailable(updates.scope, "project")).toBe(false);
246 const fallback = getSettingsSearchTargetScope("fallback-model")!;
247 expect(fallback.scope).toBe("environment-defaults");
248 expect(isSettingsSearchScopeAvailable(fallback.scope, "environment")).toBe(true);
249 expect(isSettingsSearchScopeAvailable(fallback.scope, "all")).toBe(true);
250 expect(isSettingsSearchScopeAvailable(fallback.scope, "project")).toBe(false);
251 for (const id of ["response-streaming", "speech-project-dictionary"]) {
252 expect(getSettingsSearchTargetScope(id)?.scope).toBe("project-defaults");
253 }
254 for (const scope of ["all", "environment", "project"] as const) {
255 expect(isSettingsSearchScopeAvailable("project-defaults", scope)).toBe(true);
256 }
257 for (const id of ["tool-call-visibility", "boring-sidebar"]) {
258 expect(getSettingsSearchTargetScope(id)!.scope).toBeNull();
259 }
260 });
261});
262​
263describe("auto-settlement search availability", () => {
264 function environment(id: string, { connected = true, loaded = true } = {}) {
265 return {
266 environmentId: EnvironmentId.make(id),
267 connection: { phase: connected ? ("connected" as const) : ("offline" as const) },
268 serverConfig: loaded ? { environment: { capabilities: {} } } : null,
269 };
270 }
271​
272 const capable = environment("capable");
273 const offline = environment("offline", { connected: false });
274 const loading = environment("loading", { loaded: false });
275 const environments = [capable, offline, loading];
276​
277 it("keeps results discoverable when one connected environment supports them", () => {
278 const availability = getThreadAutoSettlementSearchAvailability(environments);
279 expect(availability.eligibleEnvironmentIds).toEqual([capable.environmentId]);
280 const items = filterAvailableSettingsSearchItems({
281 hasCloudPublicConfig: false,
282 hasEnvironment: true,
283 hasProviderSettingsEnvironment: true,
284 canManageLocalBackend: false,
285 isWslSettingsRowVisible: false,
286 hasThreadAutoSettlement: availability.eligibleEnvironmentIds.length > 0,
287 });
288 expect(searchSettings("auto-settle", items).map((item) => item.id)).toEqual([
289 "auto-settle-inactive-threads",
290 "auto-settle-merged-threads",
291 "days-before-auto-settle",
292 "keep-threads-with-pull-requests",
293 ]);
294 });
295​
296 it.each([offline, loading])(
297 "does not render the target on $environmentId or silently fall back",
298 (selected) => {
299 expect(
300 getThreadAutoSettlementSearchAvailability(environments, {
301 kind: "environment",
302 environmentIds: [selected.environmentId],
303 }),
304 ).toEqual({ eligibleEnvironmentIds: [capable.environmentId], isTargetAvailable: false });
305 },
306 );
307​
308 it("ignores offline and unloaded targets when all connected targets support the setting", () => {
309 const selected = [capable, offline, loading];
310 expect(
311 getThreadAutoSettlementSearchAvailability(selected, {
312 kind: "all",
313 environmentIds: selected.map((entry) => entry.environmentId),
314 }).isTargetAvailable,
315 ).toBe(true);
316 });
317});
318​
319describe("source control host search items", () => {
320 const definition = {
321 ...UNKNOWN_SOURCE_CONTROL_CLIENT,
322 kind: SourceControlProviderKind.make("example"),
323 label: "Example",
324 searchKeywords: ["sample"],
325 settings: makeProviderSettingsSchema({ apiToken: Schema.String }),
326 };
327​
328 it("lists a host by its label, its keywords, and its settings fields", () => {
329 const items = sourceControlHostSearchItems([UNKNOWN_SOURCE_CONTROL_CLIENT, definition]);
330 expect(items.map((item) => item.title)).toEqual(["Example credentials"]);
331 expect(searchSettings("sample apitoken", items).map((item) => item.id)).toEqual([
332 sourceControlHostSettingsSearchId("example"),
333 ]);
334 });
335​
336 it("lists a host with credentials by its accounts, token and CLI", () => {
337 const items = sourceControlHostSearchItems([
338 {
339 ...definition,
340 credentials: {
341 cliName: "ex",
342 cliLabel: "Example CLI",
343 loginCommand: "ex login",
344 tokenEnvVar: "EX_TOKEN",
345 tokenAccess: "access",
346 newTokenUrl: (host) => `https://${host}/tokens`,
347 },
348 },
349 ]);
350 expect(items.map((item) => item.title)).toEqual(["Example accounts and token"]);
351 expect(searchSettings("ex switch accounts", items)).toHaveLength(1);
352 });
353​
354 it("finds the built-in hosts by their own words", () => {
355 expect(searchSettings("codeberg").map((item) => item.id)).toContain("source-control");
356 expect(searchSettings("ghes").map((item) => item.id)).toContain(
357 sourceControlHostSettingsSearchId("github"),
358 );
359 });
360});
361​
362describe("movedSettingsPath", () => {
363 it("sends old links to the page their row moved to", () => {
364 expect(movedSettingsPath("/settings/general", "text-generation-model")).toBe(
365 "/settings/threads",
366 );
367 expect(movedSettingsPath("/settings/connections", "authorized-clients")).toBe(
368 "/settings/access",
369 );
370 expect(movedSettingsPath("/settings/source-control", "worktree-branch-naming")).toBe(
371 "/settings/worktrees",
372 );
373 expect(movedSettingsPath("/settings/appearance", "diff-color-scheme")).toBe("/settings/diffs");
374 expect(movedSettingsPath("/settings/connections", "network-access")).toBe("/settings/access");
375 expect(movedSettingsPath("/settings/storage", "storage-worktrees")).toBe("/settings/worktrees");
376 expect(movedSettingsPath("/settings/storage", "")).toBe("/settings/maintenance");
377 expect(movedSettingsPath("/settings/integrations", "devices")).toBe("/settings/devices");
378 expect(movedSettingsPath("/settings/beta", "")).toBe("/settings/labs");
379 });
380​
381 it("leaves current pages alone", () => {
382 expect(movedSettingsPath("/settings/general", "time-format")).toBeNull();
383 expect(movedSettingsPath("/settings/projects", "project-defaults")).toBeNull();
384 expect(movedSettingsPath("/settings/connections", "remote-environments")).toBeNull();
385 expect(movedSettingsPath("/settings/appearance", "theme")).toBeNull();
386 });
387});