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