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

settingsSearch.test.ts

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