Open History to see the last commit
1import { describe, expect, it, vi } from "vite-plus/test";
2import { EnvironmentId } from "@t3tools/contracts";
3
4import {
5 filterAvailableSettingsSearchItems,
6 getSettingsSearchTargetScope,
7 getThreadAutoSettlementSearchAvailability,
8 isSettingsOverviewVisible,
9 isSettingsSearchScopeAvailable,
10 searchableSetting,
11 searchSettings,
12 SETTINGS_SEARCH_ITEMS,
13 type SettingsSearchItem,
14} from "./settingsSearch";
15
16const ITEMS: ReadonlyArray<SettingsSearchItem> = [
17 {
18 id: "word-wrap",
19 title: "Word wrap",
20 to: "/settings/general",
21 searchTerms: ["long lines in code previews"],
22 },
23 {
24 id: "network-access",
25 title: "Network access",
26 to: "/settings/connections",
27 searchTerms: ["remote pairing backend"],
28 },
29 {
30 id: "providers",
31 title: "Providers",
32 to: "/settings/providers",
33 searchTerms: ["claude codex agents"],
34 },
35 {
36 id: "provider-updates",
37 title: "Update checks",
38 to: "/settings/general",
39 },
40 {
41 id: "automatic-updates",
42 title: "Automatic updates",
43 to: "/settings/general",
44 },
45];
46
47describe("searchSettings", () => {
48 it.each(["send shortcut", "multiline", "new line"])("finds Send shortcut for %s", (query) => {
49 expect(searchSettings(query).map((item) => item.id)).toContain("send-shortcut");
50 });
51
52 it("matches titles, sections, and remembered setting details", () => {
53 expect(searchSettings("word", ITEMS).map((item) => item.id)).toEqual(["word-wrap"]);
54 expect(searchSettings("network", ITEMS).map((item) => item.id)).toEqual(["network-access"]);
55 expect(searchSettings("connections", ITEMS).map((item) => item.id)).toEqual(["network-access"]);
56 expect(searchSettings("claude", ITEMS).map((item) => item.id)).toEqual(["providers"]);
57 expect(searchSettings("long lines", ITEMS).map((item) => item.id)).toEqual(["word-wrap"]);
58 });
59
60 it("matches normalized title substrings", () => {
61 expect(searchSettings(" WORD WRAP ", ITEMS).map((item) => item.id)).toEqual(["word-wrap"]);
62 expect(searchSettings("glass").map((item) => item.id)).toEqual(["setting-glass-opacity"]);
63 expect(searchSettings("panel animations").map((item) => item.id)).toEqual(["panel-animations"]);
64 expect(searchSettings("thè\u{1ab0}mes")[0]?.id).toBe("theme");
65 const localeLowerCase = vi.spyOn(String.prototype, "toLocaleLowerCase").mockReturnValue("gıt");
66 try {
67 expect(searchSettings("GIT")[0]?.id).toBe("git-fetch-interval");
68 expect(localeLowerCase).not.toHaveBeenCalled();
69 } finally {
70 localeLowerCase.mockRestore();
71 }
72 expect(searchSettings("xyzzy")).toEqual([]);
73 });
74
75 it("keeps catalog order for multiple title matches", () => {
76 expect(searchSettings("update", ITEMS).map((item) => item.id)).toEqual([
77 "provider-updates",
78 "automatic-updates",
79 ]);
80 });
81
82 it("matches query words across fields and ranks the strongest result first", () => {
83 expect(searchSettings("pairing remote", ITEMS).map((item) => item.id)).toEqual([
84 "network-access",
85 ]);
86 expect(
87 searchSettings("remote pairing")
88 .slice(0, 2)
89 .map((item) => item.id),
90 ).toEqual(["network-access", "connections-environment"]);
91 });
92
93 it("finds settings that used to be reachable only through their section", () => {
94 expect(searchSettings("pull request template")[0]?.id).toBe("follow-change-request-templates");
95 expect(searchSettings("git security keys")[0]?.id).toBe("git-fetch-interval");
96 expect(searchSettings("push notifications")[0]?.id).toBe("publish-agent-activity");
97 expect(searchSettings("battery saver")[0]?.id).toBe("background-activity");
98 expect(searchSettings("binary path")[0]?.id).toBe("providers");
99 expect(searchSettings("Antigravity")[0]?.id).toBe("providers");
100 expect(searchSettings("Google sign in")[0]?.id).toBe("providers");
101 expect(searchSettings("authorized clients")[0]?.id).toBe("connections-environment");
102 expect(searchSettings("administrative access")[0]?.id).toBe("connections-environment");
103 });
104
105 it("lists thread confirmations in panel order", () => {
106 expect(searchSettings("confirmation").map((item) => item.id)).toEqual([
107 "unpin-confirmation",
108 "archive-confirmation",
109 "delete-confirmation",
110 ]);
111 });
112
113 it.each(["usage providers", "CLIProxyAPI", "CLI proxy hub", "management key"])(
114 "finds usage-provider management by %s",
115 (query) => {
116 expect(searchSettings(query)[0]).toMatchObject({
117 id: "usage-providers",
118 to: "/settings/providers",
119 });
120 },
121 );
122
123 it("returns no results for an empty query", () => {
124 expect(searchSettings(" ", ITEMS)).toEqual([]);
125 });
126
127 it("hides desktop-only settings from browser search", () => {
128 expect(SETTINGS_SEARCH_ITEMS.some((item) => item.id === "quit-confirmation")).toBe(true);
129 expect(searchSettings("hold to quit")).toEqual([]);
130 expect(searchSettings("wsl")).toEqual([]);
131 });
132
133 it("hides macOS-only settings on other platforms", () => {
134 vi.stubGlobal("navigator", { platform: "Win32" });
135 try {
136 expect(searchSettings("font smoothing")).toEqual([]);
137 } finally {
138 vi.unstubAllGlobals();
139 }
140 });
141
142 it("registers the WSL backend as a desktop-only setting", () => {
143 expect(SETTINGS_SEARCH_ITEMS.find((item) => item.id === "wsl-backend")).toMatchObject({
144 id: "wsl-backend",
145 title: "WSL backend",
146 to: "/settings/connections",
147 desktopOnly: true,
148 windowsOnly: true,
149 });
150 });
151
152 it("hides settings whose controls are unavailable", () => {
153 const available = filterAvailableSettingsSearchItems({
154 hasCloudPublicConfig: false,
155 hasEnvironment: false,
156 hasProviderSettingsEnvironment: false,
157 hasMacProviderSettingsEnvironment: false,
158 canManageLocalBackend: false,
159 isWslSettingsRowVisible: false,
160 hasThreadAutoSettlement: false,
161 });
162
163 const gatedIds = new Set<string>([
164 "follow-change-request-templates",
165 "git-fetch-interval",
166 "network-access",
167 "publish-agent-activity",
168 "provider-health-check-interval",
169 "cursor-keychain-usage",
170 "source-control-writer-model",
171 "source-control-writing-style",
172 "t3-connect",
173 "tailscale-https",
174 "wsl-backend",
175 "auto-settle-inactive-threads",
176 "auto-settle-merged-threads",
177 "days-before-auto-settle",
178 ]);
179 expect(available.map((item) => item.id).filter((id) => gatedIds.has(id))).toEqual([]);
180 });
181
182 it("offers Cursor Keychain settings only when a macOS provider environment is available", () => {
183 const availability = {
184 hasCloudPublicConfig: false,
185 hasEnvironment: true,
186 hasProviderSettingsEnvironment: true,
187 hasMacProviderSettingsEnvironment: false,
188 canManageLocalBackend: false,
189 isWslSettingsRowVisible: false,
190 hasThreadAutoSettlement: false,
191 };
192 const itemIds = (macAvailable: boolean) =>
193 filterAvailableSettingsSearchItems({
194 ...availability,
195 hasMacProviderSettingsEnvironment: macAvailable,
196 }).map((item) => item.id);
197 expect(itemIds(false)).not.toContain("cursor-keychain-usage");
198 expect(itemIds(true)).toContain("cursor-keychain-usage");
199 });
200
201 it("keeps the local toggle searchable without offering hidden host publishing controls", () => {
202 const availability = {
203 hasCloudPublicConfig: true,
204 hasEnvironment: true,
205 hasProviderSettingsEnvironment: true,
206 hasMacProviderSettingsEnvironment: false,
207 canManageLocalBackend: false,
208 isWslSettingsRowVisible: false,
209 hasThreadAutoSettlement: false,
210 };
211 const remoteOnly = filterAvailableSettingsSearchItems({
212 ...availability,
213 localEnvironmentDisabled: true,
214 }).map((item) => item.id);
215 expect(remoteOnly).toContain("local-environment");
216 expect(remoteOnly).not.toContain("t3-connect");
217 expect(remoteOnly).not.toContain("publish-agent-activity");
218 expect(remoteOnly).not.toContain("wsl-backend");
219 // Browsers without access:write still render CloudLinkRow for their host.
220 const browser = filterAvailableSettingsSearchItems(availability).map((item) => item.id);
221 expect(browser).toContain("publish-agent-activity");
222 });
223
224 it("shows automatic settlement settings when the server supports them", () => {
225 const available = filterAvailableSettingsSearchItems({
226 hasCloudPublicConfig: false,
227 hasEnvironment: false,
228 hasProviderSettingsEnvironment: false,
229 hasMacProviderSettingsEnvironment: false,
230 canManageLocalBackend: false,
231 isWslSettingsRowVisible: false,
232 hasThreadAutoSettlement: true,
233 });
234
235 expect(searchSettings("auto-settle", available).map((item) => item.id)).toEqual([
236 "auto-settle-inactive-threads",
237 "auto-settle-merged-threads",
238 "days-before-auto-settle",
239 ]);
240 });
241
242 it("finds keybinding commands by label, command id, and default key", () => {
243 expect(searchSettings("toggle sidebar")[0]?.id).toBe("keybinding-sidebar.toggle");
244 expect(searchSettings("sidebar.toggle")[0]?.id).toBe("keybinding-sidebar.toggle");
245 expect(searchSettings("mod+b")[0]?.id).toBe("keybinding-sidebar.toggle");
246 expect(searchSettings("copy link")[0]).toMatchObject({
247 id: "keybinding-thread.copyReference",
248 to: "/settings/keybindings",
249 });
250 });
251
252 it("ranks keybinding commands after other settings", () => {
253 const ids = searchSettings("model").map((item) => item.id);
254 expect(ids[0]).toBe("default-model");
255 expect(ids.indexOf("keybinding-modelPicker.toggle")).toBeGreaterThan(
256 ids.indexOf("text-generation-model"),
257 );
258 });
259
260 it("sends commands without a default binding to the section", () => {
261 expect(searchSettings("thread.stop")[0]).toMatchObject({
262 id: "keybinding-thread.stop",
263 targetId: "keybindings",
264 });
265 expect(searchSettings("sidebar.toggle")[0]?.targetId).toBeUndefined();
266 });
267
268 it("keeps catalog result ids unique", () => {
269 const ids = SETTINGS_SEARCH_ITEMS.map((item) => item.id);
270 expect(new Set(ids).size).toBe(ids.length);
271 });
272
273 it("serves anchor props to panels from the catalog", () => {
274 expect(searchableSetting("word-wrap")).toEqual({ id: "word-wrap", title: "Word wrap" });
275 expect(searchableSetting("archive")).toEqual({ id: "archive", title: "Archived threads" });
276 });
277
278 it("routes appearance settings to their current section", () => {
279 expect(searchSettings("theme")[0]).toMatchObject({
280 id: "theme",
281 to: "/settings/appearance",
282 });
283 expect(searchSettings("word wrap")[0]).toMatchObject({
284 id: "word-wrap",
285 to: "/settings/appearance",
286 });
287 expect(searchSettings("composer context")[0]).toMatchObject({
288 id: "composer-context",
289 to: "/settings/appearance",
290 });
291 expect(searchSettings("environment identification")[0]).toMatchObject({
292 id: "environment-identification",
293 to: "/settings/appearance",
294 targetId: "appearance-interface",
295 });
296 });
297
298 it("routes conditional window capture settings to the stable toggle row", () => {
299 const targets = [
300 "capture accessibility data",
301 "capture shortcut",
302 "capture sound",
303 "capture flash",
304 "capture animations",
305 ].map((query) => {
306 const match = searchSettings(query)[0];
307 return [match?.id, match?.targetId];
308 });
309
310 expect(targets).toEqual([
311 ["snap-shot-accessibility", "snap-shot-enabled"],
312 ["snap-shot-shortcut", "snap-shot-enabled"],
313 ["snap-shot-sound", "snap-shot-enabled"],
314 ["snap-shot-flash", "snap-shot-enabled"],
315 ["snap-shot-animations", "snap-shot-enabled"],
316 ]);
317 });
318
319 it("routes browser recording quality to integrations", () => {
320 const result = searchSettings("recording frame rate")[0];
321 expect(result).toMatchObject({
322 id: "browser-recording-frame-rate",
323 to: "/settings/integrations",
324 });
325 expect(result).not.toHaveProperty("targetId");
326 });
327
328 it("routes where links open to integrations", () => {
329 expect(searchSettings("open links in")[0]).toMatchObject({
330 id: "browser-link-target",
331 to: "/settings/integrations",
332 });
333 expect(searchSettings("external links")[0]).toMatchObject({ id: "browser-link-target" });
334 });
335
336 it("finds the default browser profile action in the profiles list", () => {
337 expect(searchSettings("default profile")[0]).toMatchObject({
338 id: "browser-default-profile",
339 to: "/settings/integrations",
340 targetId: "browser-profiles",
341 });
342 });
343
344 it.each([
345 ["default model", "default-model", "/settings/general"],
346 ["new threads", "new-threads", "/settings/general"],
347 ["agent browser access", "agent-browser-access", "/settings/integrations"],
348 ["automatically pull", "automatic-pull", "/settings/source-control"],
349 ["actions", "project-actions", "/settings/projects"],
350 ["project overview", "project-overview", "/settings/projects"],
351 ])("routes %s to its owning category", (query, id, to) => {
352 expect(searchSettings(query)[0]).toMatchObject({ id, to });
353 });
354
355 it("keeps environment settings discoverable without a primary environment", () => {
356 const available = filterAvailableSettingsSearchItems({
357 hasCloudPublicConfig: false,
358 hasEnvironment: true,
359 hasProviderSettingsEnvironment: true,
360 hasMacProviderSettingsEnvironment: false,
361 canManageLocalBackend: false,
362 isWslSettingsRowVisible: false,
363 hasThreadAutoSettlement: true,
364 });
365 expect(searchSettings("writing style", available)[0]?.id).toBe("source-control-writing-style");
366 expect(searchSettings("auto-settle", available)).toHaveLength(3);
367 });
368});
369
370describe("settings search targets", () => {
371 it.each([
372 "auto-settle-inactive-threads",
373 "auto-settle-merged-threads",
374 "days-before-auto-settle",
375 ])("retains the capability requirement for %s", (targetId) => {
376 expect(getSettingsSearchTargetScope(targetId)).toMatchObject({
377 scope: "project-defaults",
378 requiresThreadAutoSettlement: true,
379 });
380 });
381
382 it("treats device-local rows as reachable from every selection", () => {
383 const setting = getSettingsSearchTargetScope("time-format")!;
384 expect(setting).toEqual({ title: "Time format", scope: null });
385 expect(isSettingsSearchScopeAvailable(setting.scope, "project")).toBe(true);
386 expect(isSettingsSearchScopeAvailable(setting.scope, "all")).toBe(true);
387 expect(getSettingsSearchTargetScope("appearance")).toMatchObject({ scope: null });
388 expect(getSettingsSearchTargetScope("missing-setting")).toBeNull();
389 });
390
391 it.each(["all", "environment", "project", "checkout"] as const)(
392 "makes browser access editable at the %s scope",
393 (kind) => {
394 const setting = getSettingsSearchTargetScope("agent-browser-access")!;
395 expect(isSettingsSearchScopeAvailable(setting.scope, kind)).toBe(true);
396 expect(isSettingsSearchScopeAvailable(setting.scope, "unavailable")).toBe(false);
397 },
398 );
399
400 it("lets project-scopable rows resolve at every server-backed scope", () => {
401 const model = getSettingsSearchTargetScope("text-generation-model")!;
402 expect(isSettingsSearchScopeAvailable(model.scope, "all")).toBe(true);
403 expect(isSettingsSearchScopeAvailable(model.scope, "environment")).toBe(true);
404 expect(isSettingsSearchScopeAvailable(model.scope, "project")).toBe(true);
405 });
406
407 it("reaches source control discovery and git fetch interval from the default scope", () => {
408 for (const id of ["source-control", "git-fetch-interval"]) {
409 const item = getSettingsSearchTargetScope(id)!;
410 expect(isSettingsSearchScopeAvailable(item.scope, "all")).toBe(true);
411 expect(isSettingsSearchScopeAvailable(item.scope, "environment")).toBe(true);
412 expect(isSettingsSearchScopeAvailable(item.scope, "project")).toBe(false);
413 }
414 });
415
416 it("keeps environment-wide settings out of project scopes", () => {
417 const updates = getSettingsSearchTargetScope("provider-update-checks")!;
418 expect(updates.scope).toBe("environment-defaults");
419 expect(isSettingsSearchScopeAvailable(updates.scope, "environment")).toBe(true);
420 expect(isSettingsSearchScopeAvailable(updates.scope, "all")).toBe(true);
421 expect(isSettingsSearchScopeAvailable(updates.scope, "project")).toBe(false);
422 const streaming = getSettingsSearchTargetScope("response-streaming")!;
423 expect(streaming.scope).toBe("project-defaults");
424 expect(isSettingsSearchScopeAvailable(streaming.scope, "project")).toBe(true);
425 for (const id of ["legacy-plan-mode", "legacy-context-window-indicator", "legacy-sidebar"]) {
426 expect(getSettingsSearchTargetScope(id)!.scope).toBeNull();
427 }
428 });
429});
430
431describe("auto-settlement search availability", () => {
432 function environment(id: string, { connected = true, loaded = true, supported = true } = {}) {
433 return {
434 environmentId: EnvironmentId.make(id),
435 connection: { phase: connected ? ("connected" as const) : ("offline" as const) },
436 serverConfig: loaded
437 ? { environment: { capabilities: { threadAutoSettlement: supported } } }
438 : null,
439 };
440 }
441
442 const capable = environment("capable");
443 const unsupported = environment("unsupported", { supported: false });
444 const offline = environment("offline", { connected: false });
445 const loading = environment("loading", { loaded: false });
446 const environments = [capable, unsupported, offline, loading];
447
448 it("keeps results discoverable when one connected environment supports them", () => {
449 const availability = getThreadAutoSettlementSearchAvailability(environments);
450 expect(availability.eligibleEnvironmentIds).toEqual([capable.environmentId]);
451 const items = filterAvailableSettingsSearchItems({
452 hasCloudPublicConfig: false,
453 hasEnvironment: true,
454 hasProviderSettingsEnvironment: true,
455 hasMacProviderSettingsEnvironment: false,
456 canManageLocalBackend: false,
457 isWslSettingsRowVisible: false,
458 hasThreadAutoSettlement: availability.eligibleEnvironmentIds.length > 0,
459 });
460 expect(searchSettings("auto-settle", items).map((item) => item.id)).toEqual([
461 "auto-settle-inactive-threads",
462 "auto-settle-merged-threads",
463 "days-before-auto-settle",
464 ]);
465 });
466
467 it("offers only capable environments when an aggregate has mixed capabilities", () => {
468 expect(
469 getThreadAutoSettlementSearchAvailability(environments, {
470 kind: "all",
471 environmentIds: environments.map((entry) => entry.environmentId),
472 }),
473 ).toEqual({ eligibleEnvironmentIds: [capable.environmentId], isTargetAvailable: false });
474 });
475
476 it("allows a capable named environment regardless of other environments' capabilities", () => {
477 expect(
478 getThreadAutoSettlementSearchAvailability(environments, {
479 kind: "environment",
480 environmentIds: [capable.environmentId],
481 }).isTargetAvailable,
482 ).toBe(true);
483 });
484
485 it.each([unsupported, offline, loading])(
486 "does not render the target on $environmentId or silently fall back",
487 (selected) => {
488 expect(
489 getThreadAutoSettlementSearchAvailability(environments, {
490 kind: "environment",
491 environmentIds: [selected.environmentId],
492 }),
493 ).toEqual({ eligibleEnvironmentIds: [capable.environmentId], isTargetAvailable: false });
494 },
495 );
496
497 it("offers a capable environment instead of a dead target at an unavailable scope", () => {
498 expect(
499 getThreadAutoSettlementSearchAvailability(environments, {
500 kind: "unavailable",
501 environmentIds: [capable.environmentId],
502 }),
503 ).toEqual({ eligibleEnvironmentIds: [capable.environmentId], isTargetAvailable: false });
504 });
505
506 it("ignores offline and unloaded targets when all connected targets support the setting", () => {
507 const selected = [capable, offline, loading];
508 expect(
509 getThreadAutoSettlementSearchAvailability(selected, {
510 kind: "all",
511 environmentIds: selected.map((entry) => entry.environmentId),
512 }).isTargetAvailable,
513 ).toBe(true);
514 });
515
516 it("offers no unavailable environments when none can render the setting", () => {
517 const selected = [unsupported, offline, loading];
518 expect(
519 getThreadAutoSettlementSearchAvailability(selected, {
520 kind: "all",
521 environmentIds: selected.map((entry) => entry.environmentId),
522 }),
523 ).toEqual({ eligibleEnvironmentIds: [], isTargetAvailable: false });
524 expect(getThreadAutoSettlementSearchAvailability([]).eligibleEnvironmentIds).toEqual([]);
525 });
526});
527
528describe("settings sidebar scope", () => {
529 it("shows Overview only for project and checkout targets", () => {
530 expect(isSettingsOverviewVisible({})).toBe(false);
531 expect(isSettingsOverviewVisible({ machine: "remote" })).toBe(false);
532 expect(isSettingsOverviewVisible({ project: "project" })).toBe(true);
533 expect(isSettingsOverviewVisible({ project: "project", checkout: "checkout" })).toBe(true);
534 });
535});