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

settingsSearch.test.ts

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