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