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