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

settingsSearch.test.ts

236 lines9.0 KB

1import { describe, expect, it, vi } from "vite-plus/test";
2import { EnvironmentId } from "@t3tools/contracts";
3​
4import {
5 filterAvailableSettingsSearchItems,
6 getSettingsSearchTargetScope,
7 getThreadAutoSettlementSearchAvailability,
8 isSettingsSearchScopeAvailable,
9 searchSettings,
10 SETTINGS_SEARCH_ITEMS,
11 type SettingsSearchItem,
12} from "./settingsSearch";
13​
14const ITEMS: ReadonlyArray<SettingsSearchItem> = [
15 {
16 id: "word-wrap",
17 title: "Word wrap",
18 to: "/settings/general",
19 searchTerms: ["long lines in code previews"],
20 },
21 {
22 id: "network-access",
23 title: "Network access",
24 to: "/settings/connections",
25 searchTerms: ["remote pairing backend"],
26 },
27 {
28 id: "providers",
29 title: "Providers",
30 to: "/settings/providers",
31 searchTerms: ["claude codex agents"],
32 },
33 {
34 id: "provider-updates",
35 title: "Update checks",
36 to: "/settings/general",
37 },
38 {
39 id: "automatic-updates",
40 title: "Automatic updates",
41 to: "/settings/general",
42 },
43];
44​
45describe("searchSettings", () => {
46 it("matches titles, sections, and remembered setting details", () => {
47 expect(searchSettings("word", ITEMS).map((item) => item.id)).toEqual(["word-wrap"]);
48 expect(searchSettings("network", ITEMS).map((item) => item.id)).toEqual(["network-access"]);
49 expect(searchSettings("connections", ITEMS).map((item) => item.id)).toEqual(["network-access"]);
50 expect(searchSettings("claude", ITEMS).map((item) => item.id)).toEqual(["providers"]);
51 expect(searchSettings("long lines", ITEMS).map((item) => item.id)).toEqual(["word-wrap"]);
52 });
53​
54 it("matches normalized title substrings", () => {
55 expect(searchSettings(" WORD WRAP ", ITEMS).map((item) => item.id)).toEqual(["word-wrap"]);
56 expect(searchSettings("glass").map((item) => item.id)).toEqual(["setting-glass-opacity"]);
57 expect(searchSettings("panel animations").map((item) => item.id)).toEqual(["panel-animations"]);
58 expect(searchSettings("thè\u{1ab0}mes")[0]?.id).toBe("theme");
59 const localeLowerCase = vi.spyOn(String.prototype, "toLocaleLowerCase").mockReturnValue("gıt");
60 try {
61 expect(searchSettings("GIT")[0]?.id).toBe("git-fetch-interval");
62 expect(localeLowerCase).not.toHaveBeenCalled();
63 } finally {
64 localeLowerCase.mockRestore();
65 }
66 expect(searchSettings("xyzzy")).toEqual([]);
67 });
68​
69 it("matches query words across fields and ranks the strongest result first", () => {
70 expect(searchSettings("pairing remote", ITEMS).map((item) => item.id)).toEqual([
71 "network-access",
72 ]);
73 expect(
74 searchSettings("remote pairing")
75 .slice(0, 2)
76 .map((item) => item.id),
77 ).toEqual(["network-access", "connections-environment"]);
78 });
79​
80 it("hides desktop-only settings from browser search", () => {
81 expect(SETTINGS_SEARCH_ITEMS.some((item) => item.id === "quit-confirmation")).toBe(true);
82 expect(searchSettings("hold to quit")).toEqual([]);
83 expect(searchSettings("wsl")).toEqual([]);
84 expect(searchSettings("update track")).toEqual([]);
85 });
86​
87 it("hides settings whose controls are unavailable", () => {
88 const available = filterAvailableSettingsSearchItems({
89 hasCloudPublicConfig: false,
90 hasEnvironment: false,
91 hasProviderSettingsEnvironment: false,
92 hasMacProviderSettingsEnvironment: false,
93 canManageLocalBackend: false,
94 isWslSettingsRowVisible: false,
95 hasThreadAutoSettlement: false,
96 });
97​
98 const gatedIds = new Set<string>([
99 "follow-change-request-templates",
100 "git-fetch-interval",
101 "network-access",
102 "publish-agent-activity",
103 "provider-health-check-interval",
104 "cursor-keychain-usage",
105 "source-control-writer-model",
106 "review-guide-model",
107 "review-guide-instructions",
108 "source-control-writing-style",
109 "t3-connect",
110 "hold-webhooks-while-offline",
111 "tailscale-https",
112 "wsl-backend",
113 "auto-settle-inactive-threads",
114 "auto-settle-merged-threads",
115 "days-before-auto-settle",
116 ]);
117 expect(available.map((item) => item.id).filter((id) => gatedIds.has(id))).toEqual([]);
118 });
119​
120 it("finds keybinding commands by label, command id, and default key", () => {
121 expect(searchSettings("toggle sidebar")[0]?.id).toBe("keybinding-sidebar.toggle");
122 expect(searchSettings("sidebar.toggle")[0]?.id).toBe("keybinding-sidebar.toggle");
123 expect(searchSettings("mod+b")[0]?.id).toBe("keybinding-sidebar.toggle");
124 expect(searchSettings("copy link")[0]).toMatchObject({
125 id: "keybinding-thread.copyReference",
126 to: "/settings/keybindings",
127 });
128 });
129​
130 it("sends commands without a default binding to the section", () => {
131 expect(searchSettings("thread.stop")[0]).toMatchObject({
132 id: "keybinding-thread.stop",
133 targetId: "keybindings",
134 });
135 expect(searchSettings("sidebar.toggle")[0]?.targetId).toBeUndefined();
136 });
137​
138 it("keeps catalog result ids unique", () => {
139 const ids = SETTINGS_SEARCH_ITEMS.map((item) => item.id);
140 expect(new Set(ids).size).toBe(ids.length);
141 });
142});
143​
144describe("settings search targets", () => {
145 it("requires an environment for the resolved telemetry export destination", () => {
146 const result = searchSettings("OpenTelemetry export").find(
147 (item) => item.id === "telemetry-export",
148 )!;
149 const target = getSettingsSearchTargetScope(result.targetId ?? result.id)!;
150 expect(isSettingsSearchScopeAvailable(target.scope, "all")).toBe(false);
151 expect(isSettingsSearchScopeAvailable(target.scope, "project")).toBe(false);
152 expect(isSettingsSearchScopeAvailable(target.scope, "environment")).toBe(true);
153 });
154​
155 it("keeps environment-wide settings out of project scopes", () => {
156 const updates = getSettingsSearchTargetScope("provider-update-checks")!;
157 expect(updates.scope).toBe("environment-defaults");
158 expect(isSettingsSearchScopeAvailable(updates.scope, "environment")).toBe(true);
159 expect(isSettingsSearchScopeAvailable(updates.scope, "all")).toBe(true);
160 expect(isSettingsSearchScopeAvailable(updates.scope, "project")).toBe(false);
161 const streaming = getSettingsSearchTargetScope("response-streaming")!;
162 expect(streaming.scope).toBe("project-defaults");
163 expect(isSettingsSearchScopeAvailable(streaming.scope, "project")).toBe(true);
164 for (const id of ["legacy-plan-mode", "legacy-context-window-indicator", "legacy-sidebar"]) {
165 expect(getSettingsSearchTargetScope(id)!.scope).toBeNull();
166 }
167 });
168});
169​
170describe("auto-settlement search availability", () => {
171 function environment(id: string, { connected = true, loaded = true, supported = true } = {}) {
172 return {
173 environmentId: EnvironmentId.make(id),
174 connection: { phase: connected ? ("connected" as const) : ("offline" as const) },
175 serverConfig: loaded
176 ? { environment: { capabilities: { threadAutoSettlement: supported } } }
177 : null,
178 };
179 }
180​
181 const capable = environment("capable");
182 const unsupported = environment("unsupported", { supported: false });
183 const offline = environment("offline", { connected: false });
184 const loading = environment("loading", { loaded: false });
185 const environments = [capable, unsupported, offline, loading];
186​
187 it("keeps results discoverable when one connected environment supports them", () => {
188 const availability = getThreadAutoSettlementSearchAvailability(environments);
189 expect(availability.eligibleEnvironmentIds).toEqual([capable.environmentId]);
190 const items = filterAvailableSettingsSearchItems({
191 hasCloudPublicConfig: false,
192 hasEnvironment: true,
193 hasProviderSettingsEnvironment: true,
194 hasMacProviderSettingsEnvironment: false,
195 canManageLocalBackend: false,
196 isWslSettingsRowVisible: false,
197 hasThreadAutoSettlement: availability.eligibleEnvironmentIds.length > 0,
198 });
199 expect(searchSettings("auto-settle", items).map((item) => item.id)).toEqual([
200 "auto-settle-inactive-threads",
201 "auto-settle-merged-threads",
202 "days-before-auto-settle",
203 ]);
204 });
205​
206 it("offers only capable environments when an aggregate has mixed capabilities", () => {
207 expect(
208 getThreadAutoSettlementSearchAvailability(environments, {
209 kind: "all",
210 environmentIds: environments.map((entry) => entry.environmentId),
211 }),
212 ).toEqual({ eligibleEnvironmentIds: [capable.environmentId], isTargetAvailable: false });
213 });
214​
215 it.each([unsupported, offline, loading])(
216 "does not render the target on $environmentId or silently fall back",
217 (selected) => {
218 expect(
219 getThreadAutoSettlementSearchAvailability(environments, {
220 kind: "environment",
221 environmentIds: [selected.environmentId],
222 }),
223 ).toEqual({ eligibleEnvironmentIds: [capable.environmentId], isTargetAvailable: false });
224 },
225 );
226​
227 it("ignores offline and unloaded targets when all connected targets support the setting", () => {
228 const selected = [capable, offline, loading];
229 expect(
230 getThreadAutoSettlementSearchAvailability(selected, {
231 kind: "all",
232 environmentIds: selected.map((entry) => entry.environmentId),
233 }).isTargetAvailable,
234 ).toBe(true);
235 });
236});