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

settingsSearch.test.ts

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