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

settingsSearch.test.ts

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