Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

settingsSearch.test.ts

343 lines13.2 KB

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