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 searchSettings,
16 SETTINGS_SEARCH_ITEMS,
17 sourceControlHostSearchItems,
18 sourceControlHostSettingsSearchId,
19 type SettingsSearchItem,
20} from "./settingsSearch";
21
22const ITEMS: ReadonlyArray<SettingsSearchItem> = [
23 {
24 id: "word-wrap",
25 title: "Word wrap",
26 to: "/settings/general",
27 searchTerms: ["long lines in code previews"],
28 },
29 {
30 id: "network-access",
31 title: "Network access",
32 to: "/settings/connections",
33 searchTerms: ["remote pairing backend"],
34 },
35 {
36 id: "providers",
37 title: "Providers",
38 to: "/settings/providers",
39 searchTerms: ["claude codex agents"],
40 },
41 {
42 id: "provider-updates",
43 title: "Update checks",
44 to: "/settings/general",
45 },
46 {
47 id: "automatic-updates",
48 title: "Automatic updates",
49 to: "/settings/general",
50 },
51];
52
53describe("searchSettings", () => {
54 it("matches titles, sections, and remembered setting details", () => {
55 expect(searchSettings("word", ITEMS).map((item) => item.id)).toEqual(["word-wrap"]);
56 expect(searchSettings("network", ITEMS).map((item) => item.id)).toEqual(["network-access"]);
57 expect(searchSettings("connections", ITEMS).map((item) => item.id)).toEqual(["network-access"]);
58 expect(searchSettings("claude", ITEMS).map((item) => item.id)).toEqual(["providers"]);
59 expect(searchSettings("long lines", ITEMS).map((item) => item.id)).toEqual(["word-wrap"]);
60 });
61
62 it("matches normalized title substrings", () => {
63 expect(searchSettings(" WORD WRAP ", ITEMS).map((item) => item.id)).toEqual(["word-wrap"]);
64 expect(searchSettings("glass").map((item) => item.id)).toEqual(["setting-glass-opacity"]);
65 expect(searchSettings("panel animations").map((item) => item.id)).toEqual(["panel-animations"]);
66 expect(searchSettings("thè\u{1ab0}mes")[0]?.id).toBe("theme");
67 const localeLowerCase = vi.spyOn(String.prototype, "toLocaleLowerCase").mockReturnValue("gıt");
68 try {
69 expect(searchSettings("GIT")[0]?.id).toBe("git-fetch-interval");
70 expect(localeLowerCase).not.toHaveBeenCalled();
71 } finally {
72 localeLowerCase.mockRestore();
73 }
74 expect(searchSettings("xyzzy")).toEqual([]);
75 });
76
77 it("matches query words across fields and ranks the strongest result first", () => {
78 expect(searchSettings("pairing remote", ITEMS).map((item) => item.id)).toEqual([
79 "network-access",
80 ]);
81 expect(
82 searchSettings("remote pairing")
83 .slice(0, 2)
84 .map((item) => item.id),
85 ).toEqual(["network-access", "connections-environment"]);
86 });
87
88 it("hides desktop-only settings from browser search", () => {
89 expect(SETTINGS_SEARCH_ITEMS.some((item) => item.id === "quit-confirmation")).toBe(true);
90 expect(searchSettings("hold to quit")).toEqual([]);
91 expect(searchSettings("wsl")).toEqual([]);
92 expect(searchSettings("update track")).toEqual([]);
93 });
94
95 it("hides settings whose controls are unavailable", () => {
96 const available = filterAvailableSettingsSearchItems({
97 hasCloudPublicConfig: false,
98 hasEnvironment: false,
99 hasProviderSettingsEnvironment: false,
100 hasMacProviderSettingsEnvironment: false,
101 canManageLocalBackend: false,
102 isWslSettingsRowVisible: false,
103 hasThreadAutoSettlement: false,
104 });
105
106 const gatedIds = new Set<string>([
107 "follow-change-request-templates",
108 "git-fetch-interval",
109 "network-access",
110 "publish-agent-activity",
111 "provider-health-check-interval",
112 "cursor-keychain-usage",
113 "source-control-writer-model",
114 "review-guide-model",
115 "review-guide-instructions",
116 "source-control-writing-style",
117 "t3-connect",
118 "hold-webhooks-while-offline",
119 "tailscale-https",
120 "wsl-backend",
121 "auto-settle-inactive-threads",
122 "auto-settle-merged-threads",
123 "days-before-auto-settle",
124 "sidebar-project-icon-colors",
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 ]);
213 });
214
215 it("offers only capable environments when an aggregate has mixed capabilities", () => {
216 expect(
217 getThreadAutoSettlementSearchAvailability(environments, {
218 kind: "all",
219 environmentIds: environments.map((entry) => entry.environmentId),
220 }),
221 ).toEqual({ eligibleEnvironmentIds: [capable.environmentId], isTargetAvailable: false });
222 });
223
224 it.each([unsupported, offline, loading])(
225 "does not render the target on $environmentId or silently fall back",
226 (selected) => {
227 expect(
228 getThreadAutoSettlementSearchAvailability(environments, {
229 kind: "environment",
230 environmentIds: [selected.environmentId],
231 }),
232 ).toEqual({ eligibleEnvironmentIds: [capable.environmentId], isTargetAvailable: false });
233 },
234 );
235
236 it("ignores offline and unloaded targets when all connected targets support the setting", () => {
237 const selected = [capable, offline, loading];
238 expect(
239 getThreadAutoSettlementSearchAvailability(selected, {
240 kind: "all",
241 environmentIds: selected.map((entry) => entry.environmentId),
242 }).isTargetAvailable,
243 ).toBe(true);
244 });
245});
246
247describe("source control host search items", () => {
248 const definition = {
249 ...UNKNOWN_SOURCE_CONTROL_CLIENT,
250 kind: SourceControlProviderKind.make("example"),
251 label: "Example",
252 searchKeywords: ["sample"],
253 settings: makeProviderSettingsSchema({ apiToken: Schema.String }),
254 };
255
256 it("lists a host by its label, its keywords, and its settings fields", () => {
257 const items = sourceControlHostSearchItems([UNKNOWN_SOURCE_CONTROL_CLIENT, definition]);
258 expect(items.map((item) => item.title)).toEqual(["Example credentials"]);
259 expect(searchSettings("sample apitoken", items).map((item) => item.id)).toEqual([
260 sourceControlHostSettingsSearchId("example"),
261 ]);
262 });
263
264 it("lists a host with credentials by its accounts, token and CLI", () => {
265 const items = sourceControlHostSearchItems([
266 {
267 ...definition,
268 credentials: {
269 cliName: "ex",
270 cliLabel: "Example CLI",
271 loginCommand: "ex login",
272 tokenEnvVar: "EX_TOKEN",
273 tokenAccess: "access",
274 newTokenUrl: (host) => `https://${host}/tokens`,
275 },
276 },
277 ]);
278 expect(items.map((item) => item.title)).toEqual(["Example accounts and token"]);
279 expect(searchSettings("ex switch accounts", items)).toHaveLength(1);
280 });
281
282 it("finds the built-in hosts by their own words", () => {
283 expect(searchSettings("codeberg").map((item) => item.id)).toContain("source-control");
284 expect(searchSettings("ghes").map((item) => item.id)).toContain(
285 sourceControlHostSettingsSearchId("github"),
286 );
287 });
288});