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