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 canManageLocalBackend: false,
158 isWslSettingsRowVisible: false,
159 hasThreadAutoSettlement: false,
160 });
161
162 const gatedIds = new Set<string>([
163 "follow-change-request-templates",
164 "git-fetch-interval",
165 "network-access",
166 "publish-agent-activity",
167 "provider-health-check-interval",
168 "source-control-writer-model",
169 "source-control-writing-style",
170 "t3-connect",
171 "tailscale-https",
172 "wsl-backend",
173 "auto-settle-inactive-threads",
174 "auto-settle-merged-threads",
175 "days-before-auto-settle",
176 ]);
177 expect(available.map((item) => item.id).filter((id) => gatedIds.has(id))).toEqual([]);
178 });
179
180 it("keeps the local toggle searchable without offering hidden host publishing controls", () => {
181 const availability = {
182 hasCloudPublicConfig: true,
183 hasEnvironment: true,
184 hasProviderSettingsEnvironment: true,
185 canManageLocalBackend: false,
186 isWslSettingsRowVisible: false,
187 hasThreadAutoSettlement: false,
188 };
189 const remoteOnly = filterAvailableSettingsSearchItems({
190 ...availability,
191 localEnvironmentDisabled: true,
192 }).map((item) => item.id);
193 expect(remoteOnly).toContain("local-environment");
194 expect(remoteOnly).not.toContain("t3-connect");
195 expect(remoteOnly).not.toContain("publish-agent-activity");
196 expect(remoteOnly).not.toContain("wsl-backend");
197 // Browsers without access:write still render CloudLinkRow for their host.
198 const browser = filterAvailableSettingsSearchItems(availability).map((item) => item.id);
199 expect(browser).toContain("publish-agent-activity");
200 });
201
202 it("shows automatic settlement settings when the server supports them", () => {
203 const available = filterAvailableSettingsSearchItems({
204 hasCloudPublicConfig: false,
205 hasEnvironment: false,
206 hasProviderSettingsEnvironment: false,
207 canManageLocalBackend: false,
208 isWslSettingsRowVisible: false,
209 hasThreadAutoSettlement: true,
210 });
211
212 expect(searchSettings("auto-settle", available).map((item) => item.id)).toEqual([
213 "auto-settle-inactive-threads",
214 "auto-settle-merged-threads",
215 "days-before-auto-settle",
216 ]);
217 });
218
219 it("finds keybinding commands by label, command id, and default key", () => {
220 expect(searchSettings("toggle sidebar")[0]?.id).toBe("keybinding-sidebar.toggle");
221 expect(searchSettings("sidebar.toggle")[0]?.id).toBe("keybinding-sidebar.toggle");
222 expect(searchSettings("mod+b")[0]?.id).toBe("keybinding-sidebar.toggle");
223 expect(searchSettings("copy link")[0]).toMatchObject({
224 id: "keybinding-thread.copyReference",
225 to: "/settings/keybindings",
226 });
227 });
228
229 it("ranks keybinding commands after other settings", () => {
230 const ids = searchSettings("model").map((item) => item.id);
231 expect(ids[0]).toBe("default-model");
232 expect(ids.indexOf("keybinding-modelPicker.toggle")).toBeGreaterThan(
233 ids.indexOf("text-generation-model"),
234 );
235 });
236
237 it("sends commands without a default binding to the section", () => {
238 expect(searchSettings("thread.stop")[0]).toMatchObject({
239 id: "keybinding-thread.stop",
240 targetId: "keybindings",
241 });
242 expect(searchSettings("sidebar.toggle")[0]?.targetId).toBeUndefined();
243 });
244
245 it("keeps catalog result ids unique", () => {
246 const ids = SETTINGS_SEARCH_ITEMS.map((item) => item.id);
247 expect(new Set(ids).size).toBe(ids.length);
248 });
249
250 it("serves anchor props to panels from the catalog", () => {
251 expect(searchableSetting("word-wrap")).toEqual({ id: "word-wrap", title: "Word wrap" });
252 expect(searchableSetting("archive")).toEqual({ id: "archive", title: "Archived threads" });
253 });
254
255 it("routes appearance settings to their current section", () => {
256 expect(searchSettings("theme")[0]).toMatchObject({
257 id: "theme",
258 to: "/settings/appearance",
259 });
260 expect(searchSettings("word wrap")[0]).toMatchObject({
261 id: "word-wrap",
262 to: "/settings/appearance",
263 });
264 expect(searchSettings("composer context")[0]).toMatchObject({
265 id: "composer-context",
266 to: "/settings/appearance",
267 });
268 expect(searchSettings("environment identification")[0]).toMatchObject({
269 id: "environment-identification",
270 to: "/settings/appearance",
271 targetId: "appearance-interface",
272 });
273 });
274
275 it("routes conditional window capture settings to the stable toggle row", () => {
276 const targets = [
277 "capture accessibility data",
278 "capture shortcut",
279 "capture sound",
280 "capture flash",
281 "capture animations",
282 ].map((query) => {
283 const match = searchSettings(query)[0];
284 return [match?.id, match?.targetId];
285 });
286
287 expect(targets).toEqual([
288 ["snap-shot-accessibility", "snap-shot-enabled"],
289 ["snap-shot-shortcut", "snap-shot-enabled"],
290 ["snap-shot-sound", "snap-shot-enabled"],
291 ["snap-shot-flash", "snap-shot-enabled"],
292 ["snap-shot-animations", "snap-shot-enabled"],
293 ]);
294 });
295
296 it("routes browser recording quality to integrations", () => {
297 const result = searchSettings("recording frame rate")[0];
298 expect(result).toMatchObject({
299 id: "browser-recording-frame-rate",
300 to: "/settings/integrations",
301 });
302 expect(result).not.toHaveProperty("targetId");
303 });
304
305 it("routes where links open to integrations", () => {
306 expect(searchSettings("open links in")[0]).toMatchObject({
307 id: "browser-link-target",
308 to: "/settings/integrations",
309 });
310 expect(searchSettings("external links")[0]).toMatchObject({ id: "browser-link-target" });
311 });
312
313 it("finds the default browser profile action in the profiles list", () => {
314 expect(searchSettings("default profile")[0]).toMatchObject({
315 id: "browser-default-profile",
316 to: "/settings/integrations",
317 targetId: "browser-profiles",
318 });
319 });
320
321 it.each([
322 ["default model", "default-model", "/settings/general"],
323 ["new threads", "new-threads", "/settings/general"],
324 ["agent browser access", "agent-browser-access", "/settings/integrations"],
325 ["automatically pull", "automatic-pull", "/settings/source-control"],
326 ["actions", "project-actions", "/settings/projects"],
327 ["project overview", "project-overview", "/settings/projects"],
328 ])("routes %s to its owning category", (query, id, to) => {
329 expect(searchSettings(query)[0]).toMatchObject({ id, to });
330 });
331
332 it("keeps environment settings discoverable without a primary environment", () => {
333 const available = filterAvailableSettingsSearchItems({
334 hasCloudPublicConfig: false,
335 hasEnvironment: true,
336 hasProviderSettingsEnvironment: true,
337 canManageLocalBackend: false,
338 isWslSettingsRowVisible: false,
339 hasThreadAutoSettlement: true,
340 });
341 expect(searchSettings("writing style", available)[0]?.id).toBe("source-control-writing-style");
342 expect(searchSettings("auto-settle", available)).toHaveLength(3);
343 });
344});
345
346describe("settings search targets", () => {
347 it.each([
348 "auto-settle-inactive-threads",
349 "auto-settle-merged-threads",
350 "days-before-auto-settle",
351 ])("retains the capability requirement for %s", (targetId) => {
352 expect(getSettingsSearchTargetScope(targetId)).toMatchObject({
353 scope: "project-defaults",
354 requiresThreadAutoSettlement: true,
355 });
356 });
357
358 it("treats device-local rows as reachable from every selection", () => {
359 const setting = getSettingsSearchTargetScope("time-format")!;
360 expect(setting).toEqual({ title: "Time format", scope: null });
361 expect(isSettingsSearchScopeAvailable(setting.scope, "project")).toBe(true);
362 expect(isSettingsSearchScopeAvailable(setting.scope, "all")).toBe(true);
363 expect(getSettingsSearchTargetScope("appearance")).toMatchObject({ scope: null });
364 expect(getSettingsSearchTargetScope("missing-setting")).toBeNull();
365 });
366
367 it.each(["all", "environment", "project", "checkout"] as const)(
368 "makes browser access editable at the %s scope",
369 (kind) => {
370 const setting = getSettingsSearchTargetScope("agent-browser-access")!;
371 expect(isSettingsSearchScopeAvailable(setting.scope, kind)).toBe(true);
372 expect(isSettingsSearchScopeAvailable(setting.scope, "unavailable")).toBe(false);
373 },
374 );
375
376 it("lets project-scopable rows resolve at every server-backed scope", () => {
377 const model = getSettingsSearchTargetScope("text-generation-model")!;
378 expect(isSettingsSearchScopeAvailable(model.scope, "all")).toBe(true);
379 expect(isSettingsSearchScopeAvailable(model.scope, "environment")).toBe(true);
380 expect(isSettingsSearchScopeAvailable(model.scope, "project")).toBe(true);
381 });
382
383 it("reaches source control discovery and git fetch interval from the default scope", () => {
384 for (const id of ["source-control", "git-fetch-interval"]) {
385 const item = getSettingsSearchTargetScope(id)!;
386 expect(isSettingsSearchScopeAvailable(item.scope, "all")).toBe(true);
387 expect(isSettingsSearchScopeAvailable(item.scope, "environment")).toBe(true);
388 expect(isSettingsSearchScopeAvailable(item.scope, "project")).toBe(false);
389 }
390 });
391
392 it("keeps environment-wide settings out of project scopes", () => {
393 const updates = getSettingsSearchTargetScope("provider-update-checks")!;
394 expect(updates.scope).toBe("environment-defaults");
395 expect(isSettingsSearchScopeAvailable(updates.scope, "environment")).toBe(true);
396 expect(isSettingsSearchScopeAvailable(updates.scope, "all")).toBe(true);
397 expect(isSettingsSearchScopeAvailable(updates.scope, "project")).toBe(false);
398 const streaming = getSettingsSearchTargetScope("response-streaming")!;
399 expect(streaming.scope).toBe("project-defaults");
400 expect(isSettingsSearchScopeAvailable(streaming.scope, "project")).toBe(true);
401 for (const id of ["legacy-plan-mode", "legacy-context-window-indicator", "legacy-sidebar"]) {
402 expect(getSettingsSearchTargetScope(id)!.scope).toBeNull();
403 }
404 });
405});
406
407describe("auto-settlement search availability", () => {
408 function environment(id: string, { connected = true, loaded = true, supported = true } = {}) {
409 return {
410 environmentId: EnvironmentId.make(id),
411 connection: { phase: connected ? ("connected" as const) : ("offline" as const) },
412 serverConfig: loaded
413 ? { environment: { capabilities: { threadAutoSettlement: supported } } }
414 : null,
415 };
416 }
417
418 const capable = environment("capable");
419 const unsupported = environment("unsupported", { supported: false });
420 const offline = environment("offline", { connected: false });
421 const loading = environment("loading", { loaded: false });
422 const environments = [capable, unsupported, offline, loading];
423
424 it("keeps results discoverable when one connected environment supports them", () => {
425 const availability = getThreadAutoSettlementSearchAvailability(environments);
426 expect(availability.eligibleEnvironmentIds).toEqual([capable.environmentId]);
427 const items = filterAvailableSettingsSearchItems({
428 hasCloudPublicConfig: false,
429 hasEnvironment: true,
430 hasProviderSettingsEnvironment: true,
431 canManageLocalBackend: false,
432 isWslSettingsRowVisible: false,
433 hasThreadAutoSettlement: availability.eligibleEnvironmentIds.length > 0,
434 });
435 expect(searchSettings("auto-settle", items).map((item) => item.id)).toEqual([
436 "auto-settle-inactive-threads",
437 "auto-settle-merged-threads",
438 "days-before-auto-settle",
439 ]);
440 });
441
442 it("offers only capable environments when an aggregate has mixed capabilities", () => {
443 expect(
444 getThreadAutoSettlementSearchAvailability(environments, {
445 kind: "all",
446 environmentIds: environments.map((entry) => entry.environmentId),
447 }),
448 ).toEqual({ eligibleEnvironmentIds: [capable.environmentId], isTargetAvailable: false });
449 });
450
451 it("allows a capable named environment regardless of other environments' capabilities", () => {
452 expect(
453 getThreadAutoSettlementSearchAvailability(environments, {
454 kind: "environment",
455 environmentIds: [capable.environmentId],
456 }).isTargetAvailable,
457 ).toBe(true);
458 });
459
460 it.each([unsupported, offline, loading])(
461 "does not render the target on $environmentId or silently fall back",
462 (selected) => {
463 expect(
464 getThreadAutoSettlementSearchAvailability(environments, {
465 kind: "environment",
466 environmentIds: [selected.environmentId],
467 }),
468 ).toEqual({ eligibleEnvironmentIds: [capable.environmentId], isTargetAvailable: false });
469 },
470 );
471
472 it("offers a capable environment instead of a dead target at an unavailable scope", () => {
473 expect(
474 getThreadAutoSettlementSearchAvailability(environments, {
475 kind: "unavailable",
476 environmentIds: [capable.environmentId],
477 }),
478 ).toEqual({ eligibleEnvironmentIds: [capable.environmentId], isTargetAvailable: false });
479 });
480
481 it("ignores offline and unloaded targets when all connected targets support the setting", () => {
482 const selected = [capable, offline, loading];
483 expect(
484 getThreadAutoSettlementSearchAvailability(selected, {
485 kind: "all",
486 environmentIds: selected.map((entry) => entry.environmentId),
487 }).isTargetAvailable,
488 ).toBe(true);
489 });
490
491 it("offers no unavailable environments when none can render the setting", () => {
492 const selected = [unsupported, offline, loading];
493 expect(
494 getThreadAutoSettlementSearchAvailability(selected, {
495 kind: "all",
496 environmentIds: selected.map((entry) => entry.environmentId),
497 }),
498 ).toEqual({ eligibleEnvironmentIds: [], isTargetAvailable: false });
499 expect(getThreadAutoSettlementSearchAvailability([]).eligibleEnvironmentIds).toEqual([]);
500 });
501});
502
503describe("settings sidebar scope", () => {
504 it("shows Overview only for project and checkout targets", () => {
505 expect(isSettingsOverviewVisible({})).toBe(false);
506 expect(isSettingsOverviewVisible({ machine: "remote" })).toBe(false);
507 expect(isSettingsOverviewVisible({ project: "project" })).toBe(true);
508 expect(isSettingsOverviewVisible({ project: "project", checkout: "checkout" })).toBe(true);
509 });
510});