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

settingsSearch.test.ts

369 lines14.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 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 const streaming = getSettingsSearchTargetScope("response-streaming")!;
236 expect(streaming.scope).toBe("project-defaults");
237 expect(isSettingsSearchScopeAvailable(streaming.scope, "project")).toBe(true);
238 for (const id of ["legacy-plan-mode", "legacy-context-window-indicator", "boring-sidebar"]) {
239 expect(getSettingsSearchTargetScope(id)!.scope).toBeNull();
240 }
241 });
242});
243​
244describe("auto-settlement search availability", () => {
245 function environment(id: string, { connected = true, loaded = true } = {}) {
246 return {
247 environmentId: EnvironmentId.make(id),
248 connection: { phase: connected ? ("connected" as const) : ("offline" as const) },
249 serverConfig: loaded ? { environment: { capabilities: {} } } : null,
250 };
251 }
252​
253 const capable = environment("capable");
254 const offline = environment("offline", { connected: false });
255 const loading = environment("loading", { loaded: false });
256 const environments = [capable, offline, loading];
257​
258 it("keeps results discoverable when one connected environment supports them", () => {
259 const availability = getThreadAutoSettlementSearchAvailability(environments);
260 expect(availability.eligibleEnvironmentIds).toEqual([capable.environmentId]);
261 const items = filterAvailableSettingsSearchItems({
262 hasCloudPublicConfig: false,
263 hasEnvironment: true,
264 hasProviderSettingsEnvironment: true,
265 hasMacProviderSettingsEnvironment: false,
266 canManageLocalBackend: false,
267 isWslSettingsRowVisible: false,
268 hasThreadAutoSettlement: availability.eligibleEnvironmentIds.length > 0,
269 });
270 expect(searchSettings("auto-settle", items).map((item) => item.id)).toEqual([
271 "auto-settle-inactive-threads",
272 "auto-settle-merged-threads",
273 "days-before-auto-settle",
274 "keep-threads-with-pull-requests",
275 ]);
276 });
277​
278 it.each([offline, loading])(
279 "does not render the target on $environmentId or silently fall back",
280 (selected) => {
281 expect(
282 getThreadAutoSettlementSearchAvailability(environments, {
283 kind: "environment",
284 environmentIds: [selected.environmentId],
285 }),
286 ).toEqual({ eligibleEnvironmentIds: [capable.environmentId], isTargetAvailable: false });
287 },
288 );
289​
290 it("ignores offline and unloaded targets when all connected targets support the setting", () => {
291 const selected = [capable, offline, loading];
292 expect(
293 getThreadAutoSettlementSearchAvailability(selected, {
294 kind: "all",
295 environmentIds: selected.map((entry) => entry.environmentId),
296 }).isTargetAvailable,
297 ).toBe(true);
298 });
299});
300​
301describe("source control host search items", () => {
302 const definition = {
303 ...UNKNOWN_SOURCE_CONTROL_CLIENT,
304 kind: SourceControlProviderKind.make("example"),
305 label: "Example",
306 searchKeywords: ["sample"],
307 settings: makeProviderSettingsSchema({ apiToken: Schema.String }),
308 };
309​
310 it("lists a host by its label, its keywords, and its settings fields", () => {
311 const items = sourceControlHostSearchItems([UNKNOWN_SOURCE_CONTROL_CLIENT, definition]);
312 expect(items.map((item) => item.title)).toEqual(["Example credentials"]);
313 expect(searchSettings("sample apitoken", items).map((item) => item.id)).toEqual([
314 sourceControlHostSettingsSearchId("example"),
315 ]);
316 });
317​
318 it("lists a host with credentials by its accounts, token and CLI", () => {
319 const items = sourceControlHostSearchItems([
320 {
321 ...definition,
322 credentials: {
323 cliName: "ex",
324 cliLabel: "Example CLI",
325 loginCommand: "ex login",
326 tokenEnvVar: "EX_TOKEN",
327 tokenAccess: "access",
328 newTokenUrl: (host) => `https://${host}/tokens`,
329 },
330 },
331 ]);
332 expect(items.map((item) => item.title)).toEqual(["Example accounts and token"]);
333 expect(searchSettings("ex switch accounts", items)).toHaveLength(1);
334 });
335​
336 it("finds the built-in hosts by their own words", () => {
337 expect(searchSettings("codeberg").map((item) => item.id)).toContain("source-control");
338 expect(searchSettings("ghes").map((item) => item.id)).toContain(
339 sourceControlHostSettingsSearchId("github"),
340 );
341 });
342});
343​
344describe("movedSettingsPath", () => {
345 it("sends old links to the page their row moved to", () => {
346 expect(movedSettingsPath("/settings/general", "text-generation-model")).toBe(
347 "/settings/threads",
348 );
349 expect(movedSettingsPath("/settings/connections", "authorized-clients")).toBe(
350 "/settings/access",
351 );
352 expect(movedSettingsPath("/settings/source-control", "worktree-branch-naming")).toBe(
353 "/settings/worktrees",
354 );
355 expect(movedSettingsPath("/settings/appearance", "diff-color-scheme")).toBe("/settings/diffs");
356 expect(movedSettingsPath("/settings/connections", "network-access")).toBe("/settings/access");
357 expect(movedSettingsPath("/settings/storage", "storage-worktrees")).toBe("/settings/worktrees");
358 expect(movedSettingsPath("/settings/storage", "")).toBe("/settings/maintenance");
359 expect(movedSettingsPath("/settings/integrations", "devices")).toBe("/settings/devices");
360 expect(movedSettingsPath("/settings/beta", "")).toBe("/settings/labs");
361 });
362​
363 it("leaves current pages alone", () => {
364 expect(movedSettingsPath("/settings/general", "time-format")).toBeNull();
365 expect(movedSettingsPath("/settings/projects", "project-defaults")).toBeNull();
366 expect(movedSettingsPath("/settings/connections", "remote-environments")).toBeNull();
367 expect(movedSettingsPath("/settings/appearance", "theme")).toBeNull();
368 });
369});