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