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

settingsSearch.test.ts

548 lines21.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";
9import { mFallback, type Msg } from "gt-react";
10​
11import fr from "../../i18n/translations/fr.json";
12import ja from "../../i18n/translations/ja.json";
13​
14import {
15 filterAvailableSettingsSearchItems,
16 getSettingsSearchTargetScope,
17 getThreadAutoSettlementSearchAvailability,
18 isSettingsSearchScopeAvailable,
19 movedSettingsPath,
20 searchSettings,
21 settingsNavGroups,
22 SETTINGS_NAV_LABELS,
23 SETTINGS_SECTION_LABELS,
24 SETTINGS_SEARCH_ITEMS,
25 sourceControlHostSearchItems,
26 sourceControlHostSettingsSearchId,
27 type SettingsSearchItem,
28} from "./settingsSearch";
29​
30const ITEMS: ReadonlyArray<SettingsSearchItem> = [
31 {
32 id: "word-wrap",
33 title: "Word wrap",
34 to: "/settings/general",
35 searchTerms: ["long lines in code previews"],
36 },
37 {
38 id: "network-access",
39 title: "Network access",
40 to: "/settings/access",
41 searchTerms: ["remote pairing backend"],
42 },
43 {
44 id: "providers",
45 title: "Providers",
46 to: "/settings/providers",
47 searchTerms: ["claude codex agents"],
48 },
49 {
50 id: "provider-updates",
51 title: "Update checks",
52 to: "/settings/general",
53 },
54 {
55 id: "automatic-updates",
56 title: "Automatic updates",
57 to: "/settings/general",
58 },
59];
60​
61describe("searchSettings", () => {
62 it("matches titles, sections, and remembered setting details", () => {
63 expect(searchSettings("word", ITEMS).map((item) => item.id)).toEqual(["word-wrap"]);
64 expect(searchSettings("network", ITEMS).map((item) => item.id)).toEqual(["network-access"]);
65 expect(searchSettings("sharing", ITEMS).map((item) => item.id)).toEqual(["network-access"]);
66 expect(searchSettings("claude", ITEMS).map((item) => item.id)).toEqual(["providers"]);
67 expect(searchSettings("long lines", ITEMS).map((item) => item.id)).toEqual(["word-wrap"]);
68 const spanish = (message: string | Msg) =>
69 message === "Word wrap" ? "Ajuste de línea" : mFallback(message);
70 expect(searchSettings("ajuste", ITEMS, spanish).map((item) => item.id)).toEqual(["word-wrap"]);
71 const sourceControlItems: ReadonlyArray<SettingsSearchItem> = [
72 { id: "commit-style", title: "Commit style", to: "/settings/source-control" },
73 ];
74 for (const [section, nav] of [
75 ["Versionshantering och ärenden", "Versioner och ärenden"],
76 ["Page title", "Navigation: localized suffix"],
77 ] as const) {
78 const translate = (message: string | Msg) =>
79 message === SETTINGS_NAV_LABELS["/settings/source-control"]
80 ? nav
81 : message === SETTINGS_SECTION_LABELS["/settings/source-control"]
82 ? section
83 : mFallback(message);
84 for (const query of [section, nav]) {
85 expect(
86 searchSettings(query, sourceControlItems, translate).map((item) => item.id),
87 query,
88 ).toEqual(["commit-style"]);
89 }
90 }
91 for (const query of ["fallback", "fallback agent", "fallback model"]) {
92 expect(searchSettings(query)[0]).toMatchObject({
93 id: "fallback-model",
94 to: "/settings/threads",
95 scope: "environment-defaults",
96 });
97 }
98 });
99​
100 it("ranks a setting by its displayed title in the active language", () => {
101 const items: ReadonlyArray<SettingsSearchItem> = [
102 { id: "dictation", title: "Dictation", to: "/settings/general", searchTerms: ["language"] },
103 {
104 id: "speech-language",
105 title: "Speech language",
106 to: "/settings/general",
107 searchTerms: ["dictation"],
108 },
109 { id: "language", title: "Language", to: "/settings/general" },
110 ];
111 const german: Record<string, string> = {
112 "Speech language": "Diktiersprache",
113 language: "Sprache",
114 Language: "Sprache",
115 };
116 const ids = (query: string) =>
117 searchSettings(
118 query,
119 items,
120 (message) => german[mFallback(message)] ?? mFallback(message),
121 ).map((item) => item.id);
122​
123 expect(ids("Sprache")).toEqual(["language", "speech-language", "dictation"]);
124 expect(ids("Language")).toEqual(["language", "speech-language", "dictation"]);
125 });
126​
127 it("shows the Secrets page in nav and search only when an environment enables secret providers", () => {
128 const availability = {
129 hasCloudPublicConfig: false,
130 hasEnvironment: true,
131 hasProviderSettingsEnvironment: true,
132 canManageLocalBackend: false,
133 isWslSettingsRowVisible: false,
134 hasThreadAutoSettlement: false,
135 };
136 for (const enabled of [false, true]) {
137 const navPaths = settingsNavGroups(enabled).flatMap((group) => group.paths);
138 const searchPaths = searchSettings(
139 "1password",
140 filterAvailableSettingsSearchItems({
141 ...availability,
142 hasSecretProvidersEnvironment: enabled,
143 }),
144 ).map((item) => item.to);
145 expect(navPaths.includes("/settings/secrets")).toBe(enabled);
146 expect(searchPaths.includes("/settings/secrets")).toBe(enabled);
147 }
148 });
149​
150 it("finds where subagents and side chats open by any of its names", () => {
151 for (const query of ["side panel", "subagent", "side chat", "linked thread", "middle click"]) {
152 expect(searchSettings(query).map((item) => item.id)).toContain("side-thread-open-target");
153 }
154 });
155​
156 it("finds the Scheduled tasks page", () => {
157 expect(searchSettings("scheduled tasks")[0]).toMatchObject({
158 id: "scheduled-tasks",
159 to: "/settings/scheduled-tasks",
160 });
161 });
162​
163 it("matches normalized title substrings", () => {
164 expect(searchSettings(" WORD WRAP ", ITEMS).map((item) => item.id)).toEqual(["word-wrap"]);
165 expect(searchSettings("glass").map((item) => item.id)).toEqual(["setting-glass-opacity"]);
166 expect(searchSettings("panel animations").map((item) => item.id)).toEqual(["panel-animations"]);
167 expect(searchSettings("thè\u{1ab0}mes")[0]?.id).toBe("theme");
168 const localeLowerCase = vi.spyOn(String.prototype, "toLocaleLowerCase").mockReturnValue("gıt");
169 try {
170 expect(searchSettings("GIT")[0]?.id).toBe("git-fetch-interval");
171 expect(localeLowerCase).not.toHaveBeenCalled();
172 } finally {
173 localeLowerCase.mockRestore();
174 }
175 expect(searchSettings("xyzzy")).toEqual([]);
176 const contextualItems: ReadonlyArray<SettingsSearchItem> = [
177 { id: "source", title: "Example", to: "/settings/source-control" },
178 { id: "access", title: "Example", to: "/settings/access" },
179 ];
180 for (const query of ["ci", "md", "min", "io", "eyik"]) {
181 expect(searchSettings(query, contextualItems), query).toEqual([]);
182 }
183 });
184​
185 it("matches query words across fields and ranks the strongest result first", () => {
186 expect(searchSettings("pairing remote", ITEMS).map((item) => item.id)).toEqual([
187 "network-access",
188 ]);
189 expect(
190 searchSettings("remote pairing")
191 .slice(0, 2)
192 .map((item) => item.id),
193 ).toEqual(["network-access", "connections-environment"]);
194 });
195​
196 it("hides desktop-only settings from browser search", () => {
197 expect(SETTINGS_SEARCH_ITEMS.some((item) => item.id === "quit-confirmation")).toBe(true);
198 expect(searchSettings("hold to quit")).toEqual([]);
199 expect(searchSettings("wsl")).toEqual([]);
200 expect(searchSettings("update track")).toEqual([]);
201 });
202​
203 it("hides settings whose controls are unavailable", () => {
204 const available = filterAvailableSettingsSearchItems({
205 hasCloudPublicConfig: false,
206 hasEnvironment: false,
207 hasProviderSettingsEnvironment: false,
208 canManageLocalBackend: false,
209 isWslSettingsRowVisible: false,
210 hasThreadAutoSettlement: false,
211 });
212​
213 const gatedIds = new Set<string>([
214 "follow-change-request-templates",
215 "git-fetch-interval",
216 "network-access",
217 "publish-agent-activity",
218 "provider-health-check-interval",
219 "source-control-writer-model",
220 "review-guide-model",
221 "review-guide-instructions",
222 "source-control-writing-style",
223 "t3-connect",
224 "hold-webhooks-while-offline",
225 "tailscale-https",
226 "wsl-backend",
227 "auto-settle-inactive-threads",
228 "auto-settle-merged-threads",
229 "days-before-auto-settle",
230 "linked-environments",
231 "agent-feedback",
232 ]);
233 expect(available.map((item) => item.id).filter((id) => gatedIds.has(id))).toEqual([]);
234 });
235​
236 it("finds linked environments on a remote host without local maintenance access", () => {
237 const availability = {
238 hasCloudPublicConfig: false,
239 hasEnvironment: true,
240 hasProviderSettingsEnvironment: true,
241 canManageLocalBackend: false,
242 localEnvironmentDisabled: true,
243 isWslSettingsRowVisible: false,
244 hasThreadAutoSettlement: false,
245 };
246 const results = (hasPeerLinkHost: boolean) =>
247 searchSettings(
248 "linked environments",
249 filterAvailableSettingsSearchItems({ ...availability, hasPeerLinkHost }),
250 );
251 expect(results(false).map((item) => item.id)).not.toContain("linked-environments");
252 expect(results(true)).toContainEqual(
253 expect.objectContaining({
254 id: "linked-environments",
255 to: "/settings/connections",
256 targetId: "linked-environments",
257 }),
258 );
259 });
260​
261 it("finds the Welcome page only where a primary computer can open it", () => {
262 const availability = {
263 hasCloudPublicConfig: true,
264 hasEnvironment: true,
265 hasProviderSettingsEnvironment: true,
266 canManageLocalBackend: false,
267 isWslSettingsRowVisible: false,
268 hasThreadAutoSettlement: false,
269 };
270 const ids = (hasPrimaryEnvironment: boolean) =>
271 searchSettings(
272 "welcome page",
273 filterAvailableSettingsSearchItems({ ...availability, hasPrimaryEnvironment }),
274 ).map((item) => item.id);
275 // Hosted web reaches remote computers but has no primary one to set up.
276 expect(ids(false)).not.toContain("welcome-page");
277 expect(ids(true)).toContain("welcome-page");
278 });
279​
280 it("finds keybinding commands by label, command id, and default key", () => {
281 expect(searchSettings("toggle sidebar")[0]?.id).toBe("keybinding-sidebar.toggle");
282 expect(searchSettings("sidebar.toggle")[0]?.id).toBe("keybinding-sidebar.toggle");
283 expect(searchSettings("mod+b")[0]?.id).toBe("keybinding-sidebar.toggle");
284 expect(searchSettings("copy link")[0]).toMatchObject({
285 id: "keybinding-thread.copyReference",
286 to: "/settings/keybindings",
287 });
288 });
289​
290 it("sends commands without a default binding to the section", () => {
291 expect(searchSettings("thread.stop")[0]).toMatchObject({
292 id: "keybinding-thread.stop",
293 targetId: "keybindings",
294 });
295 expect(searchSettings("sidebar.toggle")[0]?.targetId).toBeUndefined();
296 });
297​
298 it("finds rows by the titles shown on their pages", () => {
299 for (const [title, id] of [
300 ["Automatic worktree cleanup", "storage-worktrees"],
301 ["Delete settled worktrees", "storage-worktrees"],
302 ["Delete worktrees with deleted threads", "storage-worktrees"],
303 ["Delete merged worktrees", "storage-worktrees"],
304 ["Delete old browser artifacts", "storage-artifacts"],
305 ["Delete old rotated logs", "storage-artifacts"],
306 ["Custom search URL", "browser-search-engine"],
307 ["Native sessions", "providers"],
308 ["CODEX_HOME path", "providers"],
309 ["Shadow home path", "providers"],
310 ["ChatGPT account", "providers"],
311 ["Live Processes", "diagnostics"],
312 ["Most Common Failures", "diagnostics"],
313 ["Top Span Names", "diagnostics"],
314 ["Slowest Spans", "diagnostics"],
315 ["Third-party notices", "open-source-licenses"],
316 ["Your dictionary", "speech-dictionary"],
317 ] as const) {
318 expect(searchSettings(title)[0]?.id, title).toBe(id);
319 }
320 });
321​
322 it("finds voice cleanup by its displayed French heading", () => {
323 const french: Record<string, string> = {
324 Cleanup: fr["9672487a555c8e95"],
325 "Clean up transcripts": fr["745975a3581da25c"],
326 "speech transcription polish clean provider prompt post-processing": fr["f9c66e42006b8ced"],
327 };
328​
329 expect(
330 searchSettings(
331 "Nettoyage",
332 SETTINGS_SEARCH_ITEMS,
333 (message) => french[mFallback(message)] ?? mFallback(message),
334 ).map((item) => item.id),
335 ).toContain("speech-post-processing");
336 });
337​
338 it("finds the shared dictionary by its displayed Japanese heading", () => {
339 const japanese: Record<string, string> = {
340 "Shared dictionary": ja["4cc78a4b83d91915"],
341 "speech voice transcription vocabulary shared dictionary team environment glossary":
342 ja["85ee6e84044167f7"],
343 };
344​
345 expect(
346 searchSettings(
347 "共有辞書",
348 SETTINGS_SEARCH_ITEMS,
349 (message) => japanese[mFallback(message)] ?? mFallback(message),
350 ).map((item) => item.id),
351 ).toContain("speech-project-dictionary");
352 });
353​
354 it.each(["バージョン管理", "サーバー環境"])(
355 "finds source control by the Japanese heading %s",
356 (query) => {
357 const japanese: Record<string, string> = {
358 "Version Control": ja["6cfcdea92cfd773f"],
359 "Server environment": ja["92a25b63cc3bf98d"],
360 };
361​
362 const results = searchSettings(
363 query,
364 SETTINGS_SEARCH_ITEMS,
365 (message) => japanese[mFallback(message)] ?? mFallback(message),
366 );
367 expect(results.map((item) => item.id)).toContain("source-control");
368 },
369 );
370​
371 it("finds the project page for its Pin switch", () => {
372 for (const query of ["pin", "pin project", "pinned projects"]) {
373 expect(
374 searchSettings(query).map((item) => item.id),
375 query,
376 ).toContain("project-overview");
377 }
378 });
379​
380 it("keeps catalog result ids unique", () => {
381 const ids = SETTINGS_SEARCH_ITEMS.map((item) => item.id);
382 expect(new Set(ids).size).toBe(ids.length);
383 });
384});
385​
386describe("settings search targets", () => {
387 it("requires an environment for the resolved telemetry export destination", () => {
388 const result = searchSettings("OpenTelemetry export").find(
389 (item) => item.id === "telemetry-export",
390 )!;
391 const target = getSettingsSearchTargetScope(result.targetId ?? result.id)!;
392 expect(isSettingsSearchScopeAvailable(target.scope, "all")).toBe(false);
393 expect(isSettingsSearchScopeAvailable(target.scope, "project")).toBe(false);
394 expect(isSettingsSearchScopeAvailable(target.scope, "environment")).toBe(true);
395 });
396​
397 it("keeps environment-wide settings out of project scopes", () => {
398 const updates = getSettingsSearchTargetScope("provider-update-checks")!;
399 expect(updates.scope).toBe("environment-defaults");
400 expect(isSettingsSearchScopeAvailable(updates.scope, "environment")).toBe(true);
401 expect(isSettingsSearchScopeAvailable(updates.scope, "all")).toBe(true);
402 expect(isSettingsSearchScopeAvailable(updates.scope, "project")).toBe(false);
403 const fallback = getSettingsSearchTargetScope("fallback-model")!;
404 expect(fallback.scope).toBe("environment-defaults");
405 expect(isSettingsSearchScopeAvailable(fallback.scope, "environment")).toBe(true);
406 expect(isSettingsSearchScopeAvailable(fallback.scope, "all")).toBe(true);
407 expect(isSettingsSearchScopeAvailable(fallback.scope, "project")).toBe(false);
408 for (const id of ["response-streaming", "speech-project-dictionary", "project-actions"]) {
409 expect(getSettingsSearchTargetScope(id)?.scope).toBe("project-defaults");
410 }
411 for (const scope of ["all", "environment", "project"] as const) {
412 expect(isSettingsSearchScopeAvailable("project-defaults", scope)).toBe(true);
413 }
414 for (const id of ["tool-call-visibility", "boring-sidebar"]) {
415 expect(getSettingsSearchTargetScope(id)!.scope).toBeNull();
416 }
417 });
418});
419​
420describe("auto-settlement search availability", () => {
421 function environment(id: string, { connected = true, loaded = true } = {}) {
422 return {
423 environmentId: EnvironmentId.make(id),
424 connection: { phase: connected ? ("connected" as const) : ("offline" as const) },
425 serverConfig: loaded ? { environment: { capabilities: {} } } : null,
426 };
427 }
428​
429 const capable = environment("capable");
430 const offline = environment("offline", { connected: false });
431 const loading = environment("loading", { loaded: false });
432 const environments = [capable, offline, loading];
433​
434 it("keeps results discoverable when one connected environment supports them", () => {
435 const availability = getThreadAutoSettlementSearchAvailability(environments);
436 expect(availability.eligibleEnvironmentIds).toEqual([capable.environmentId]);
437 const items = filterAvailableSettingsSearchItems({
438 hasCloudPublicConfig: false,
439 hasEnvironment: true,
440 hasProviderSettingsEnvironment: true,
441 canManageLocalBackend: false,
442 isWslSettingsRowVisible: false,
443 hasThreadAutoSettlement: availability.eligibleEnvironmentIds.length > 0,
444 });
445 expect(searchSettings("auto-settle", items).map((item) => item.id)).toEqual([
446 "auto-settle-inactive-threads",
447 "auto-settle-merged-threads",
448 "days-before-auto-settle",
449 "keep-threads-with-pull-requests",
450 ]);
451 });
452​
453 it.each([offline, loading])(
454 "does not render the target on $environmentId or silently fall back",
455 (selected) => {
456 expect(
457 getThreadAutoSettlementSearchAvailability(environments, {
458 kind: "environment",
459 environmentIds: [selected.environmentId],
460 }),
461 ).toEqual({ eligibleEnvironmentIds: [capable.environmentId], isTargetAvailable: false });
462 },
463 );
464​
465 it("ignores offline and unloaded targets when all connected targets support the setting", () => {
466 const selected = [capable, offline, loading];
467 expect(
468 getThreadAutoSettlementSearchAvailability(selected, {
469 kind: "all",
470 environmentIds: selected.map((entry) => entry.environmentId),
471 }).isTargetAvailable,
472 ).toBe(true);
473 });
474});
475​
476describe("source control host search items", () => {
477 const definition = {
478 ...UNKNOWN_SOURCE_CONTROL_CLIENT,
479 kind: SourceControlProviderKind.make("example"),
480 label: "Example",
481 searchKeywords: ["sample"],
482 settings: makeProviderSettingsSchema({ apiToken: Schema.String }),
483 };
484​
485 it("lists a host by its label, its keywords, and its settings fields", () => {
486 const items = sourceControlHostSearchItems([UNKNOWN_SOURCE_CONTROL_CLIENT, definition]);
487 expect(items.map((item) => mFallback(item.title))).toEqual(["Example credentials"]);
488 expect(searchSettings("sample apitoken", items).map((item) => item.id)).toEqual([
489 sourceControlHostSettingsSearchId("example"),
490 ]);
491 });
492​
493 it("lists a host with credentials by its accounts, token and CLI", () => {
494 const items = sourceControlHostSearchItems([
495 {
496 ...definition,
497 credentials: {
498 cliName: "ex",
499 cliLabel: "Example CLI",
500 loginCommand: "ex login",
501 tokenEnvVar: "EX_TOKEN",
502 tokenAccess: "access",
503 newTokenUrl: (host) => `https://${host}/tokens`,
504 },
505 },
506 ]);
507 expect(items.map((item) => mFallback(item.title))).toEqual(["Example accounts and token"]);
508 expect(searchSettings("ex switch accounts", items)).toHaveLength(1);
509 });
510​
511 it("finds the built-in hosts by their own words", () => {
512 expect(searchSettings("codeberg").map((item) => item.id)).toContain("source-control");
513 expect(searchSettings("ghes").map((item) => item.id)).toContain(
514 sourceControlHostSettingsSearchId("github"),
515 );
516 });
517});
518​
519describe("movedSettingsPath", () => {
520 it("sends old links to the page their row moved to", () => {
521 expect(movedSettingsPath("/settings/general", "text-generation-model")).toBe(
522 "/settings/threads",
523 );
524 expect(movedSettingsPath("/settings/connections", "authorized-clients")).toBe(
525 "/settings/access",
526 );
527 expect(movedSettingsPath("/settings/source-control", "worktree-branch-naming")).toBe(
528 "/settings/worktrees",
529 );
530 expect(movedSettingsPath("/settings/appearance", "diff-color-scheme")).toBe("/settings/diffs");
531 expect(movedSettingsPath("/settings/connections", "network-access")).toBe("/settings/access");
532 expect(movedSettingsPath("/settings/storage", "storage-worktrees")).toBe("/settings/worktrees");
533 expect(movedSettingsPath("/settings/storage", "")).toBe("/settings/maintenance");
534 expect(movedSettingsPath("/settings/integrations", "devices")).toBe("/settings/devices");
535 expect(movedSettingsPath("/settings/beta", "")).toBe("/settings/labs");
536 expect(movedSettingsPath("/settings/beta", "toString")).toBe("/settings/labs");
537 });
538​
539 it("leaves current pages alone", () => {
540 expect(movedSettingsPath("/settings/general", "time-format")).toBeNull();
541 expect(movedSettingsPath("/settings/general", "toString")).toBeNull();
542 expect(movedSettingsPath("/settings/general", "constructor")).toBeNull();
543 expect(movedSettingsPath("/settings/general", "__proto__")).toBeNull();
544 expect(movedSettingsPath("/settings/projects", "project-defaults")).toBeNull();
545 expect(movedSettingsPath("/settings/connections", "remote-environments")).toBeNull();
546 expect(movedSettingsPath("/settings/appearance", "theme")).toBeNull();
547 });
548});