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

settingsSearch.test.ts

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