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 ]);
126 expect(available.map((item) => item.id).filter((id) => gatedIds.has(id))).toEqual([]);
127 });
128
129 it("finds keybinding commands by label, command id, and default key", () => {
130 expect(searchSettings("toggle sidebar")[0]?.id).toBe("keybinding-sidebar.toggle");
131 expect(searchSettings("sidebar.toggle")[0]?.id).toBe("keybinding-sidebar.toggle");
132 expect(searchSettings("mod+b")[0]?.id).toBe("keybinding-sidebar.toggle");
133 expect(searchSettings("copy link")[0]).toMatchObject({
134 id: "keybinding-thread.copyReference",
135 to: "/settings/keybindings",
136 });
137 });
138
139 it("sends commands without a default binding to the section", () => {
140 expect(searchSettings("thread.stop")[0]).toMatchObject({
141 id: "keybinding-thread.stop",
142 targetId: "keybindings",
143 });
144 expect(searchSettings("sidebar.toggle")[0]?.targetId).toBeUndefined();
145 });
146
147 it("keeps catalog result ids unique", () => {
148 const ids = SETTINGS_SEARCH_ITEMS.map((item) => item.id);
149 expect(new Set(ids).size).toBe(ids.length);
150 });
151});
152
153describe("settings search targets", () => {
154 it("requires an environment for the resolved telemetry export destination", () => {
155 const result = searchSettings("OpenTelemetry export").find(
156 (item) => item.id === "telemetry-export",
157 )!;
158 const target = getSettingsSearchTargetScope(result.targetId ?? result.id)!;
159 expect(isSettingsSearchScopeAvailable(target.scope, "all")).toBe(false);
160 expect(isSettingsSearchScopeAvailable(target.scope, "project")).toBe(false);
161 expect(isSettingsSearchScopeAvailable(target.scope, "environment")).toBe(true);
162 });
163
164 it("keeps environment-wide settings out of project scopes", () => {
165 const updates = getSettingsSearchTargetScope("provider-update-checks")!;
166 expect(updates.scope).toBe("environment-defaults");
167 expect(isSettingsSearchScopeAvailable(updates.scope, "environment")).toBe(true);
168 expect(isSettingsSearchScopeAvailable(updates.scope, "all")).toBe(true);
169 expect(isSettingsSearchScopeAvailable(updates.scope, "project")).toBe(false);
170 const streaming = getSettingsSearchTargetScope("response-streaming")!;
171 expect(streaming.scope).toBe("project-defaults");
172 expect(isSettingsSearchScopeAvailable(streaming.scope, "project")).toBe(true);
173 for (const id of ["legacy-plan-mode", "legacy-context-window-indicator", "legacy-sidebar"]) {
174 expect(getSettingsSearchTargetScope(id)!.scope).toBeNull();
175 }
176 });
177});
178
179describe("auto-settlement search availability", () => {
180 function environment(id: string, { connected = true, loaded = true, supported = true } = {}) {
181 return {
182 environmentId: EnvironmentId.make(id),
183 connection: { phase: connected ? ("connected" as const) : ("offline" as const) },
184 serverConfig: loaded
185 ? { environment: { capabilities: { threadAutoSettlement: supported } } }
186 : null,
187 };
188 }
189
190 const capable = environment("capable");
191 const unsupported = environment("unsupported", { supported: false });
192 const offline = environment("offline", { connected: false });
193 const loading = environment("loading", { loaded: false });
194 const environments = [capable, unsupported, offline, loading];
195
196 it("keeps results discoverable when one connected environment supports them", () => {
197 const availability = getThreadAutoSettlementSearchAvailability(environments);
198 expect(availability.eligibleEnvironmentIds).toEqual([capable.environmentId]);
199 const items = filterAvailableSettingsSearchItems({
200 hasCloudPublicConfig: false,
201 hasEnvironment: true,
202 hasProviderSettingsEnvironment: true,
203 hasMacProviderSettingsEnvironment: false,
204 canManageLocalBackend: false,
205 isWslSettingsRowVisible: false,
206 hasThreadAutoSettlement: availability.eligibleEnvironmentIds.length > 0,
207 });
208 expect(searchSettings("auto-settle", items).map((item) => item.id)).toEqual([
209 "auto-settle-inactive-threads",
210 "auto-settle-merged-threads",
211 "days-before-auto-settle",
212 "keep-threads-with-pull-requests",
213 ]);
214 });
215
216 it("offers only capable environments when an aggregate has mixed capabilities", () => {
217 expect(
218 getThreadAutoSettlementSearchAvailability(environments, {
219 kind: "all",
220 environmentIds: environments.map((entry) => entry.environmentId),
221 }),
222 ).toEqual({ eligibleEnvironmentIds: [capable.environmentId], isTargetAvailable: false });
223 });
224
225 it.each([unsupported, offline, loading])(
226 "does not render the target on $environmentId or silently fall back",
227 (selected) => {
228 expect(
229 getThreadAutoSettlementSearchAvailability(environments, {
230 kind: "environment",
231 environmentIds: [selected.environmentId],
232 }),
233 ).toEqual({ eligibleEnvironmentIds: [capable.environmentId], isTargetAvailable: false });
234 },
235 );
236
237 it("ignores offline and unloaded targets when all connected targets support the setting", () => {
238 const selected = [capable, offline, loading];
239 expect(
240 getThreadAutoSettlementSearchAvailability(selected, {
241 kind: "all",
242 environmentIds: selected.map((entry) => entry.environmentId),
243 }).isTargetAvailable,
244 ).toBe(true);
245 });
246});
247
248describe("source control host search items", () => {
249 const definition = {
250 ...UNKNOWN_SOURCE_CONTROL_CLIENT,
251 kind: SourceControlProviderKind.make("example"),
252 label: "Example",
253 searchKeywords: ["sample"],
254 settings: makeProviderSettingsSchema({ apiToken: Schema.String }),
255 };
256
257 it("lists a host by its label, its keywords, and its settings fields", () => {
258 const items = sourceControlHostSearchItems([UNKNOWN_SOURCE_CONTROL_CLIENT, definition]);
259 expect(items.map((item) => item.title)).toEqual(["Example credentials"]);
260 expect(searchSettings("sample apitoken", items).map((item) => item.id)).toEqual([
261 sourceControlHostSettingsSearchId("example"),
262 ]);
263 });
264
265 it("lists a host with credentials by its accounts, token and CLI", () => {
266 const items = sourceControlHostSearchItems([
267 {
268 ...definition,
269 credentials: {
270 cliName: "ex",
271 cliLabel: "Example CLI",
272 loginCommand: "ex login",
273 tokenEnvVar: "EX_TOKEN",
274 tokenAccess: "access",
275 newTokenUrl: (host) => `https://${host}/tokens`,
276 },
277 },
278 ]);
279 expect(items.map((item) => item.title)).toEqual(["Example accounts and token"]);
280 expect(searchSettings("ex switch accounts", items)).toHaveLength(1);
281 });
282
283 it("finds the built-in hosts by their own words", () => {
284 expect(searchSettings("codeberg").map((item) => item.id)).toContain("source-control");
285 expect(searchSettings("ghes").map((item) => item.id)).toContain(
286 sourceControlHostSettingsSearchId("github"),
287 );
288 });
289});
290
291describe("movedSettingsPath", () => {
292 it("sends old links to the page their row moved to", () => {
293 expect(movedSettingsPath("/settings/general", "text-generation-model")).toBe(
294 "/settings/threads",
295 );
296 expect(movedSettingsPath("/settings/connections", "authorized-clients")).toBe(
297 "/settings/access",
298 );
299 expect(movedSettingsPath("/settings/source-control", "worktree-branch-naming")).toBe(
300 "/settings/worktrees",
301 );
302 expect(movedSettingsPath("/settings/appearance", "diff-color-scheme")).toBe("/settings/diffs");
303 expect(movedSettingsPath("/settings/connections", "network-access")).toBe("/settings/access");
304 expect(movedSettingsPath("/settings/storage", "storage-worktrees")).toBe("/settings/worktrees");
305 expect(movedSettingsPath("/settings/storage", "")).toBe("/settings/maintenance");
306 expect(movedSettingsPath("/settings/integrations", "devices")).toBe("/settings/devices");
307 expect(movedSettingsPath("/settings/beta", "")).toBe("/settings/labs");
308 });
309
310 it("leaves current pages alone", () => {
311 expect(movedSettingsPath("/settings/general", "time-format")).toBeNull();
312 expect(movedSettingsPath("/settings/projects", "project-defaults")).toBeNull();
313 expect(movedSettingsPath("/settings/connections", "remote-environments")).toBeNull();
314 expect(movedSettingsPath("/settings/appearance", "theme")).toBeNull();
315 });
316});