Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2import * as Schema from "effect/Schema";
3
4import { ProviderInstanceId } from "./providerInstance.ts";
5import {
6 ClientSettingsSchema,
7 ClientSettingsPatch,
8 DEFAULT_SERVER_SETTINGS,
9 ServerSettings,
10 ServerSettingsPatch,
11} from "./settings.ts";
12
13const decodeClientSettings = Schema.decodeUnknownSync(ClientSettingsSchema);
14const decodeClientSettingsPatch = Schema.decodeUnknownSync(ClientSettingsPatch);
15const decodeServerSettings = Schema.decodeUnknownSync(ServerSettings);
16const decodeServerSettingsPatch = Schema.decodeUnknownSync(ServerSettingsPatch);
17const encodeServerSettings = Schema.encodeSync(ServerSettings);
18
19describe("ClientSettings Cua integration", () => {
20 it("defaults computer use off", () => {
21 expect(decodeClientSettings({}).enableCua).toBe(false);
22 });
23
24 it("accepts computer use updates", () => {
25 expect(decodeClientSettingsPatch({ enableCua: true }).enableCua).toBe(true);
26 });
27});
28
29describe("ClientSettings word wrap", () => {
30 it("defaults word wrap on", () => {
31 expect(decodeClientSettings({}).wordWrap).toBe(true);
32 });
33
34 it("ignores obsolete wrapping preferences", () => {
35 const decoded = decodeClientSettings({
36 chatWordWrap: false,
37 diffWordWrap: false,
38 });
39
40 expect(decoded.wordWrap).toBe(true);
41 expect(decoded).not.toHaveProperty("chatWordWrap");
42 expect(decoded).not.toHaveProperty("diffWordWrap");
43 });
44});
45
46describe("ClientSettings glass opacity", () => {
47 it("defaults to a readable translucent surface", () => {
48 expect(decodeClientSettings({}).glassOpacity).toBe(80);
49 });
50
51 it.each([39, 101, 72.5])("rejects an invalid glass opacity: %s", (value) => {
52 expect(() => decodeClientSettings({ glassOpacity: value })).toThrow();
53 expect(() => decodeClientSettingsPatch({ glassOpacity: value })).toThrow();
54 });
55
56 it.each([40, 75, 100])("accepts a glass opacity within the supported range: %s", (value) => {
57 expect(decodeClientSettings({ glassOpacity: value }).glassOpacity).toBe(value);
58 expect(decodeClientSettingsPatch({ glassOpacity: value }).glassOpacity).toBe(value);
59 });
60});
61
62describe("ClientSettings environment identification", () => {
63 it("defaults to artwork and accepts each presentation mode", () => {
64 expect(decodeClientSettings({}).environmentIdentificationMode).toBe("artwork");
65
66 for (const mode of ["artwork", "pill", "none"] as const) {
67 expect(
68 decodeClientSettingsPatch({ environmentIdentificationMode: mode })
69 .environmentIdentificationMode,
70 ).toBe(mode);
71 }
72 });
73
74 it("rejects unsupported presentation modes", () => {
75 expect(() => decodeClientSettings({ environmentIdentificationMode: "badge" })).toThrow();
76 expect(() => decodeClientSettingsPatch({ environmentIdentificationMode: "badge" })).toThrow();
77 });
78});
79
80describe("ClientSettings sidebar v2", () => {
81 it("defaults the beta off with a three-day auto-settle threshold", () => {
82 const settings = decodeClientSettings({});
83 expect(settings.sidebarV2Enabled).toBe(false);
84 expect(settings.sidebarAutoSettleAfterDays).toBe(3);
85 });
86
87 it("treats settings written before the beta had a per-channel default as unconfigured", () => {
88 // The stored blob always carries `sidebarV2Enabled`, so only the companion
89 // flag can distinguish "user opted out" from "never touched it".
90 expect(decodeClientSettings({ sidebarV2Enabled: false }).sidebarV2ConfiguredByUser).toBe(false);
91 expect(decodeClientSettings({ sidebarV2Enabled: true }).sidebarV2ConfiguredByUser).toBe(false);
92 });
93
94 it("preserves an explicit beta choice", () => {
95 const settings = decodeClientSettings({
96 sidebarV2Enabled: false,
97 sidebarV2ConfiguredByUser: true,
98 });
99 expect(settings.sidebarV2Enabled).toBe(false);
100 expect(settings.sidebarV2ConfiguredByUser).toBe(true);
101 });
102
103 it("carries an explicit beta opt-out through the patch the beta toggle writes", () => {
104 const patch = decodeClientSettingsPatch({
105 sidebarV2Enabled: false,
106 sidebarV2ConfiguredByUser: true,
107 });
108 expect(patch.sidebarV2Enabled).toBe(false);
109 expect(patch.sidebarV2ConfiguredByUser).toBe(true);
110 });
111
112 it("allows auto-settle by inactivity to be disabled", () => {
113 expect(
114 decodeClientSettings({ sidebarAutoSettleAfterDays: null }).sidebarAutoSettleAfterDays,
115 ).toBeNull();
116 });
117
118 it.each([-1, 0, 91])("rejects an auto-settle threshold outside 1..90: %s", (value) => {
119 expect(() => decodeClientSettings({ sidebarAutoSettleAfterDays: value })).toThrow();
120 expect(() => decodeClientSettingsPatch({ sidebarAutoSettleAfterDays: value })).toThrow();
121 });
122});
123
124describe("ServerSettings.providerInstances (slice-2 invariant)", () => {
125 it("defaults to an empty record so legacy configs without the key still decode", () => {
126 expect(DEFAULT_SERVER_SETTINGS.providerInstances).toEqual({});
127 });
128
129 it("decodes a fully empty config (legacy on-disk shape) without complaint", () => {
130 const decoded = decodeServerSettings({});
131 expect(decoded.providerInstances).toEqual({});
132 // Legacy `providers` struct is still hydrated with its per-driver defaults
133 // so existing call sites keep working through the migration.
134 expect(decoded.providers.codex.enabled).toBe(true);
135 });
136
137 it("decodes a multi-instance map mixing first-party and fork drivers", () => {
138 const decoded = decodeServerSettings({
139 providerInstances: {
140 codex_personal: {
141 driver: "codex",
142 displayName: "Codex (personal)",
143 config: { homePath: "~/.codex_personal" },
144 },
145 codex_work: {
146 driver: "codex",
147 config: { homePath: "~/.codex_work" },
148 },
149 ollama_local: {
150 driver: "ollama",
151 displayName: "Ollama (local)",
152 config: { endpoint: "http://localhost:11434" },
153 },
154 },
155 });
156 const personalId = ProviderInstanceId.make("codex_personal");
157 const workId = ProviderInstanceId.make("codex_work");
158 const ollamaId = ProviderInstanceId.make("ollama_local");
159
160 expect(decoded.providerInstances[personalId]?.driver).toBe("codex");
161 expect(decoded.providerInstances[workId]?.config).toEqual({ homePath: "~/.codex_work" });
162 // Critical: a config naming a driver this build does not know about
163 // (`ollama` is not in `ProviderDriverKind`) must round-trip without loss.
164 // The runtime handles "driver not installed" — the schema must not.
165 expect(decoded.providerInstances[ollamaId]?.driver).toBe("ollama");
166 expect(decoded.providerInstances[ollamaId]?.config).toEqual({
167 endpoint: "http://localhost:11434",
168 });
169 });
170
171 it("rejects instance keys that violate the slug pattern", () => {
172 expect(() =>
173 decodeServerSettings({
174 providerInstances: { "1bad": { driver: "codex" } },
175 }),
176 ).toThrow();
177 });
178});
179
180describe("ServerSettings worktree defaults", () => {
181 it("defaults start-from-origin on for legacy configs", () => {
182 expect(decodeServerSettings({}).newWorktreesStartFromOrigin).toBe(true);
183 });
184
185 it("accepts start-from-origin updates", () => {
186 expect(
187 decodeServerSettingsPatch({ newWorktreesStartFromOrigin: false }).newWorktreesStartFromOrigin,
188 ).toBe(false);
189 });
190});
191
192describe("ServerSettings Cursor legacy settings", () => {
193 it("ignores obsolete Cursor CLI settings when reading server settings", () => {
194 const decoded = decodeServerSettings({
195 providers: {
196 cursor: {
197 enabled: true,
198 binaryPath: "cursor-agent",
199 apiEndpoint: "http://127.0.0.1:3774",
200 },
201 },
202 });
203
204 expect(decoded.providers.cursor.enabled).toBe(true);
205 expect(decoded.providers.cursor).not.toHaveProperty("binaryPath");
206 expect(decoded.providers.cursor).not.toHaveProperty("apiEndpoint");
207 });
208
209 it("ignores obsolete Cursor CLI settings in patches", () => {
210 const patch = decodeServerSettingsPatch({
211 providers: {
212 cursor: {
213 enabled: true,
214 binaryPath: "cursor-agent",
215 apiEndpoint: "http://127.0.0.1:3774",
216 },
217 },
218 });
219
220 expect(patch.providers?.cursor?.enabled).toBe(true);
221 expect(patch.providers?.cursor).not.toHaveProperty("binaryPath");
222 expect(patch.providers?.cursor).not.toHaveProperty("apiEndpoint");
223 });
224});
225
226describe("ServerSettings.sourceControlWritingStyle", () => {
227 it("defaults all style settings for legacy configs", () => {
228 const settings = decodeServerSettings({});
229
230 expect(settings.sourceControlWritingStyle).toEqual({
231 mode: "repo_conventions",
232 customInstructions: "",
233 followChangeRequestTemplates: true,
234 });
235 expect(settings.sourceControlWriterModelSelection).toBeNull();
236 });
237
238 it("trims partial style updates", () => {
239 const patch = decodeServerSettingsPatch({
240 sourceControlWritingStyle: {
241 mode: "custom",
242 customInstructions: " Prefer concise wording. ",
243 },
244 });
245
246 expect(patch.sourceControlWritingStyle).toEqual({
247 mode: "custom",
248 customInstructions: "Prefer concise wording.",
249 });
250 });
251});
252
253describe("ServerSettingsPatch.providerInstances", () => {
254 it("treats providerInstances as an optional whole-map replacement", () => {
255 const patch = decodeServerSettingsPatch({});
256 expect(patch.providerInstances).toBeUndefined();
257
258 const replacement = decodeServerSettingsPatch({
259 providerInstances: {
260 codex_personal: { driver: "codex", config: { homePath: "~/.codex" } },
261 },
262 });
263 expect(replacement.providerInstances).toBeDefined();
264 expect(replacement.providerInstances?.[ProviderInstanceId.make("codex_personal")]?.driver).toBe(
265 "codex",
266 );
267 });
268
269 it("preserves a fork-defined driver entry through patch decoding", () => {
270 const patch = decodeServerSettingsPatch({
271 providerInstances: {
272 ollama_local: {
273 driver: "ollama",
274 config: { endpoint: "http://localhost:11434" },
275 },
276 },
277 });
278 const ollamaId = ProviderInstanceId.make("ollama_local");
279 expect(patch.providerInstances?.[ollamaId]?.driver).toBe("ollama");
280 });
281});
282
283describe("ServerSettingsPatch string normalization", () => {
284 it("trims string settings while decoding patches", () => {
285 const patch = decodeServerSettingsPatch({
286 addProjectBaseDirectory: " ~/Development ",
287 textGenerationModelSelection: { model: " gpt-5.4-mini " },
288 observability: {
289 otlpTracesUrl: " http://localhost:4318/v1/traces ",
290 },
291 providers: {
292 codex: {
293 binaryPath: " /opt/homebrew/bin/codex ",
294 homePath: " ~/.codex ",
295 launchArgs: " --strict-config --enable foo ",
296 },
297 },
298 providerInstances: {
299 codex_personal: {
300 driver: " codex ",
301 displayName: " Codex Personal ",
302 config: { homePath: " ~/.codex-personal " },
303 },
304 },
305 });
306
307 expect(patch.addProjectBaseDirectory).toBe("~/Development");
308 expect(patch.textGenerationModelSelection?.model).toBe("gpt-5.4-mini");
309 expect(patch.observability?.otlpTracesUrl).toBe("http://localhost:4318/v1/traces");
310 expect(patch.providers?.codex?.binaryPath).toBe("/opt/homebrew/bin/codex");
311 expect(patch.providers?.codex?.homePath).toBe("~/.codex");
312 expect(patch.providers?.codex?.launchArgs).toBe("--strict-config --enable foo");
313 expect(patch.providerInstances?.[ProviderInstanceId.make("codex_personal")]?.driver).toBe(
314 "codex",
315 );
316 expect(patch.providerInstances?.[ProviderInstanceId.make("codex_personal")]?.displayName).toBe(
317 "Codex Personal",
318 );
319 expect(patch.providerInstances?.[ProviderInstanceId.make("codex_personal")]?.config).toEqual({
320 homePath: " ~/.codex-personal ",
321 });
322 });
323
324 it("trims encoded server settings values before validation", () => {
325 const defaultSettings = decodeServerSettings({});
326 const encoded = encodeServerSettings({
327 ...defaultSettings,
328 addProjectBaseDirectory: " ~/Development ",
329 providers: {
330 ...defaultSettings.providers,
331 codex: {
332 ...defaultSettings.providers.codex,
333 binaryPath: " /opt/homebrew/bin/codex ",
334 launchArgs: " --strict-config ",
335 },
336 },
337 });
338
339 expect(encoded.addProjectBaseDirectory).toBe("~/Development");
340 expect(encoded.providers?.codex?.binaryPath).toBe("/opt/homebrew/bin/codex");
341 expect(encoded.providers?.codex?.launchArgs).toBe("--strict-config");
342 });
343});