Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2import * as Schema from "effect/Schema";
3
4import { ProviderDriverKind, ProviderInstanceId } from "./providerInstance.ts";
5import {
6 ClientSettingsSchema,
7 ClientSettingsPatch,
8 ClaudeSettings,
9 DEFAULT_SERVER_SETTINGS,
10 defaultEnabledForDriver,
11 resolveProviderInstanceEnabled,
12 ServerSettings,
13 ServerSettingsPatch,
14} from "./settings.ts";
15
16const decodeClientSettings = Schema.decodeUnknownSync(ClientSettingsSchema);
17const decodeClientSettingsPatch = Schema.decodeUnknownSync(ClientSettingsPatch);
18const encodeClientSettings = Schema.encodeSync(ClientSettingsSchema);
19const decodeServerSettings = Schema.decodeUnknownSync(ServerSettings);
20const decodeServerSettingsPatch = Schema.decodeUnknownSync(ServerSettingsPatch);
21const encodeServerSettings = Schema.encodeSync(ServerSettings);
22const decodeClaudeSettings = Schema.decodeUnknownSync(ClaudeSettings);
23
24describe("ClaudeSettings auto-compaction", () => {
25 it("uses Claude's default threshold when no override is configured", () => {
26 expect(decodeClaudeSettings({}).autoCompactWindow).toBe("");
27 });
28
29 it.each(["100000", "300000", "1000000"])(
30 "accepts a supported auto-compaction threshold: %s",
31 (value) => {
32 expect(decodeClaudeSettings({ autoCompactWindow: value }).autoCompactWindow).toBe(value);
33 },
34 );
35
36 it.each(["99999", "1000001", "300k", "invalid"])(
37 "rejects an unsupported auto-compaction threshold: %s",
38 (value) => {
39 expect(() => decodeClaudeSettings({ autoCompactWindow: value })).toThrow();
40 },
41 );
42
43 it("rejects an unsupported threshold at the settings patch boundary", () => {
44 expect(() =>
45 decodeServerSettingsPatch({ providers: { claudeAgent: { autoCompactWindow: "300k" } } }),
46 ).toThrow();
47 expect(
48 decodeServerSettingsPatch({ providers: { claudeAgent: { autoCompactWindow: "300000" } } }),
49 ).toBeDefined();
50 });
51});
52
53describe("ClientSettings word wrap", () => {
54 it("defaults word wrap on", () => {
55 expect(decodeClientSettings({}).wordWrap).toBe(true);
56 });
57
58 it("ignores obsolete wrapping preferences", () => {
59 const decoded = decodeClientSettings({
60 chatWordWrap: false,
61 diffWordWrap: false,
62 });
63
64 expect(decoded.wordWrap).toBe(true);
65 expect(decoded).not.toHaveProperty("chatWordWrap");
66 expect(decoded).not.toHaveProperty("diffWordWrap");
67 });
68});
69
70describe("ClientSettings proactive panels", () => {
71 it("is opt-in and accepts client-local updates", () => {
72 expect(decodeClientSettings({}).proactivePanelsEnabled).toBe(false);
73 expect(decodeClientSettingsPatch({ proactivePanelsEnabled: true }).proactivePanelsEnabled).toBe(
74 true,
75 );
76 });
77});
78
79describe("ClientSettings quit confirmation", () => {
80 it("defaults to hold", () => {
81 expect(decodeClientSettings({}).confirmQuit).toBe("hold");
82 });
83
84 it.each(["direct", "hold", "double-click"] as const)("accepts the %s mode", (mode) => {
85 expect(decodeClientSettings({ confirmQuit: mode }).confirmQuit).toBe(mode);
86 expect(decodeClientSettingsPatch({ confirmQuit: mode }).confirmQuit).toBe(mode);
87 });
88
89 it.each([
90 [true, "hold"],
91 [false, "direct"],
92 ] as const)("migrates the legacy %s value to %s", (legacyValue, mode) => {
93 const settings = decodeClientSettings({ confirmQuit: legacyValue });
94
95 expect(settings.confirmQuit).toBe(mode);
96 expect(encodeClientSettings(settings).confirmQuit).toBe(mode);
97 });
98
99 it("rejects legacy booleans at the patch boundary", () => {
100 expect(() => decodeClientSettingsPatch({ confirmQuit: true })).toThrow();
101 });
102});
103
104describe("ClientSettings browser recording frame rate", () => {
105 it("defaults to 30 fps", () => {
106 expect(decodeClientSettings({}).browserRecordingFrameRate).toBe(30);
107 });
108
109 it.each([30, 60])("accepts a supported frame rate: %s", (frameRate) => {
110 expect(
111 decodeClientSettings({ browserRecordingFrameRate: frameRate }).browserRecordingFrameRate,
112 ).toBe(frameRate);
113 expect(
114 decodeClientSettingsPatch({ browserRecordingFrameRate: frameRate }).browserRecordingFrameRate,
115 ).toBe(frameRate);
116 });
117
118 it.each([24, 59, 120])("rejects an unsupported frame rate: %s", (frameRate) => {
119 expect(() => decodeClientSettings({ browserRecordingFrameRate: frameRate })).toThrow();
120 expect(() => decodeClientSettingsPatch({ browserRecordingFrameRate: frameRate })).toThrow();
121 });
122});
123
124describe("ClientSettings glass opacity", () => {
125 it("defaults to a readable translucent surface", () => {
126 expect(decodeClientSettings({}).glassOpacity).toBe(80);
127 });
128
129 it.each([39, 101, 72.5])("rejects an invalid glass opacity: %s", (value) => {
130 expect(() => decodeClientSettings({ glassOpacity: value })).toThrow();
131 expect(() => decodeClientSettingsPatch({ glassOpacity: value })).toThrow();
132 });
133
134 it.each([40, 75, 100])("accepts a glass opacity within the supported range: %s", (value) => {
135 expect(decodeClientSettings({ glassOpacity: value }).glassOpacity).toBe(value);
136 expect(decodeClientSettingsPatch({ glassOpacity: value }).glassOpacity).toBe(value);
137 });
138});
139
140describe("ClientSettings appearance contrast", () => {
141 it("defaults to the theme's original contrast", () => {
142 expect(decodeClientSettings({}).appearanceContrast).toBe(100);
143 });
144
145 it.each([49, 201, 92.5])("rejects an invalid appearance contrast: %s", (value) => {
146 expect(() => decodeClientSettings({ appearanceContrast: value })).toThrow();
147 expect(() => decodeClientSettingsPatch({ appearanceContrast: value })).toThrow();
148 });
149
150 it.each([50, 100, 150, 200])("accepts an appearance contrast in range: %s", (value) => {
151 expect(decodeClientSettings({ appearanceContrast: value }).appearanceContrast).toBe(value);
152 expect(decodeClientSettingsPatch({ appearanceContrast: value }).appearanceContrast).toBe(value);
153 });
154});
155
156describe("ClientSettings panel animations", () => {
157 it("defaults to instant changes", () => {
158 expect(decodeClientSettings({}).panelAnimationDurationMs).toBe(0);
159 });
160
161 it.each([0, 400])("accepts a panel animation duration: %s", (value) => {
162 expect(decodeClientSettingsPatch({ panelAnimationDurationMs: value })).toEqual({
163 panelAnimationDurationMs: value,
164 });
165 });
166
167 it.each([-1, 401, 150.5])("rejects an invalid panel animation duration: %s", (value) => {
168 expect(() => decodeClientSettingsPatch({ panelAnimationDurationMs: value })).toThrow();
169 });
170});
171
172describe("ClientSettings environment identification", () => {
173 it("defaults to artwork and accepts each presentation mode", () => {
174 expect(decodeClientSettings({}).environmentIdentificationMode).toBe("artwork");
175
176 for (const mode of ["artwork", "pill", "none"] as const) {
177 expect(
178 decodeClientSettingsPatch({ environmentIdentificationMode: mode })
179 .environmentIdentificationMode,
180 ).toBe(mode);
181 }
182 });
183
184 it("rejects unsupported presentation modes", () => {
185 expect(() => decodeClientSettings({ environmentIdentificationMode: "badge" })).toThrow();
186 expect(() => decodeClientSettingsPatch({ environmentIdentificationMode: "badge" })).toThrow();
187 });
188});
189
190describe("ClientSettings sidebar", () => {
191 it("defaults to the current sidebar", () => {
192 expect(decodeClientSettings({}).legacySidebarEnabled).toBe(false);
193 });
194
195 it("drops the retired sidebar v2 beta keys, resetting everyone to the default", () => {
196 const decoded = decodeClientSettings({
197 sidebarV2Enabled: false,
198 sidebarV2ConfiguredByUser: true,
199 });
200 expect(decoded.legacySidebarEnabled).toBe(false);
201 expect(decoded).not.toHaveProperty("sidebarV2Enabled");
202 expect(decoded).not.toHaveProperty("sidebarV2ConfiguredByUser");
203 });
204
205 it("preserves an explicit legacy sidebar opt-in", () => {
206 expect(decodeClientSettings({ legacySidebarEnabled: true }).legacySidebarEnabled).toBe(true);
207 expect(decodeClientSettingsPatch({ legacySidebarEnabled: true }).legacySidebarEnabled).toBe(
208 true,
209 );
210 });
211
212 it("keeps unpin confirmation opt-in and patchable", () => {
213 expect(decodeClientSettings({}).confirmThreadUnpin).toBe(false);
214 expect(decodeClientSettingsPatch({ confirmThreadUnpin: true }).confirmThreadUnpin).toBe(true);
215 expect(() => decodeClientSettingsPatch({ confirmThreadUnpin: "yes" })).toThrow();
216 });
217});
218
219describe("ClientSettings context window meter", () => {
220 it("defaults off and preserves an explicit legacy opt-in", () => {
221 expect(decodeClientSettings({}).contextWindowMeterEnabled).toBe(false);
222 expect(
223 decodeClientSettings({ contextWindowMeterEnabled: true }).contextWindowMeterEnabled,
224 ).toBe(true);
225 expect(
226 decodeClientSettingsPatch({ contextWindowMeterEnabled: true }).contextWindowMeterEnabled,
227 ).toBe(true);
228 });
229});
230
231describe("ClientSettings composer collapse", () => {
232 it("collapses on blur and scroll by default and accepts opting out of each", () => {
233 const defaults = decodeClientSettings({});
234 expect(defaults.composerCollapseOnBlur).toBe(true);
235 expect(defaults.composerCollapseOnScroll).toBe(true);
236
237 const blurOff = decodeClientSettings({ composerCollapseOnBlur: false });
238 expect(blurOff.composerCollapseOnBlur).toBe(false);
239 expect(blurOff.composerCollapseOnScroll).toBe(true);
240
241 expect(
242 decodeClientSettingsPatch({ composerCollapseOnScroll: false }).composerCollapseOnScroll,
243 ).toBe(false);
244 });
245});
246
247describe("ServerSettings thread settlement", () => {
248 it("defaults merge settlement on and inactivity settlement to three days", () => {
249 const settings = decodeServerSettings({});
250 expect(settings.sidebarAutoSettleAfterDays).toBe(3);
251 expect(settings.sidebarAutoSettleOnMerge).toBe(true);
252 });
253
254 it("allows both automatic rules to be disabled", () => {
255 expect(
256 decodeServerSettings({
257 sidebarAutoSettleAfterDays: null,
258 sidebarAutoSettleOnMerge: false,
259 }),
260 ).toMatchObject({ sidebarAutoSettleAfterDays: null, sidebarAutoSettleOnMerge: false });
261 expect(
262 decodeServerSettingsPatch({
263 sidebarAutoSettleAfterDays: null,
264 sidebarAutoSettleOnMerge: false,
265 }),
266 ).toMatchObject({ sidebarAutoSettleAfterDays: null, sidebarAutoSettleOnMerge: false });
267 });
268
269 it.each([-1, 0, 91])("rejects an auto-settle threshold outside 1..90: %s", (value) => {
270 expect(() => decodeServerSettings({ sidebarAutoSettleAfterDays: value })).toThrow();
271 expect(() => decodeServerSettingsPatch({ sidebarAutoSettleAfterDays: value })).toThrow();
272 });
273});
274
275describe("ServerSettings.providerInstances (slice-2 invariant)", () => {
276 it("defaults text generation to Luna at low reasoning effort", () => {
277 expect(DEFAULT_SERVER_SETTINGS.textGenerationModelSelection).toEqual({
278 instanceId: ProviderInstanceId.make("codex"),
279 model: "gpt-5.6-luna",
280 options: [{ id: "reasoningEffort", value: "low" }],
281 });
282 });
283
284 it("defaults to an empty record so legacy configs without the key still decode", () => {
285 expect(DEFAULT_SERVER_SETTINGS.providerInstances).toEqual({});
286 });
287
288 it("decodes a fully empty config (legacy on-disk shape) without complaint", () => {
289 const decoded = decodeServerSettings({});
290 expect(decoded.providerInstances).toEqual({});
291 // Legacy `providers` struct is still hydrated with its per-driver defaults
292 // so existing call sites keep working through the migration.
293 expect(decoded.providers.codex.enabled).toBe(true);
294 });
295
296 it("decodes a multi-instance map mixing first-party and fork drivers", () => {
297 const decoded = decodeServerSettings({
298 providerInstances: {
299 codex_personal: {
300 driver: "codex",
301 displayName: "Codex (personal)",
302 config: { homePath: "~/.codex_personal" },
303 },
304 codex_work: {
305 driver: "codex",
306 config: { homePath: "~/.codex_work" },
307 },
308 ollama_local: {
309 driver: "ollama",
310 displayName: "Ollama (local)",
311 config: { endpoint: "http://localhost:11434" },
312 },
313 },
314 });
315 const personalId = ProviderInstanceId.make("codex_personal");
316 const workId = ProviderInstanceId.make("codex_work");
317 const ollamaId = ProviderInstanceId.make("ollama_local");
318
319 expect(decoded.providerInstances[personalId]?.driver).toBe("codex");
320 expect(decoded.providerInstances[workId]?.config).toEqual({ homePath: "~/.codex_work" });
321 // Critical: a config naming a driver this build does not know about
322 // (`ollama` is not in `ProviderDriverKind`) must round-trip without loss.
323 // The runtime handles "driver not installed" — the schema must not.
324 expect(decoded.providerInstances[ollamaId]?.driver).toBe("ollama");
325 expect(decoded.providerInstances[ollamaId]?.config).toEqual({
326 endpoint: "http://localhost:11434",
327 });
328 });
329
330 it("rejects instance keys that violate the slug pattern", () => {
331 expect(() =>
332 decodeServerSettings({
333 providerInstances: { "1bad": { driver: "codex" } },
334 }),
335 ).toThrow();
336 });
337});
338
339describe("provider enabled defaults", () => {
340 it("enables only the stable bindings by default", () => {
341 const decoded = decodeServerSettings({});
342 expect(decoded.providers.codex.enabled).toBe(true);
343 expect(decoded.providers.claudeAgent.enabled).toBe(true);
344 expect(decoded.providers.cursor.enabled).toBe(false);
345 expect(decoded.providers.grok.enabled).toBe(false);
346 expect(decoded.providers.opencode.enabled).toBe(false);
347 });
348
349 it("derives per-driver defaults from the settings schemas", () => {
350 expect(defaultEnabledForDriver(ProviderDriverKind.make("codex"))).toBe(true);
351 expect(defaultEnabledForDriver(ProviderDriverKind.make("cursor"))).toBe(false);
352 expect(defaultEnabledForDriver(ProviderDriverKind.make("grok"))).toBe(false);
353 // Unknown fork drivers stay enabled; their own build decides otherwise.
354 expect(defaultEnabledForDriver(ProviderDriverKind.make("ollama"))).toBe(true);
355 });
356
357 it("keeps Cursor enabled when an existing user explicitly opted in", () => {
358 const cursor = ProviderDriverKind.make("cursor");
359 const cursorId = ProviderInstanceId.make("cursor");
360 const decoded = decodeServerSettings({
361 providers: { cursor: { enabled: true } },
362 providerInstances: {
363 [cursorId]: { driver: cursor, enabled: true, config: {} },
364 },
365 });
366
367 expect(decoded.providers.cursor.enabled).toBe(true);
368 expect(resolveProviderInstanceEnabled(decoded.providerInstances[cursorId]!)).toBe(true);
369 });
370
371 it("resolves instance enabled state with explicit false winning", () => {
372 const grok = ProviderDriverKind.make("grok");
373 const codex = ProviderDriverKind.make("codex");
374 // No flags anywhere: driver default applies.
375 expect(resolveProviderInstanceEnabled({ driver: grok, config: {} })).toBe(false);
376 expect(resolveProviderInstanceEnabled({ driver: codex, config: {} })).toBe(true);
377 // Envelope flag wins over the driver default.
378 expect(resolveProviderInstanceEnabled({ driver: grok, enabled: true, config: {} })).toBe(true);
379 expect(resolveProviderInstanceEnabled({ driver: codex, enabled: false, config: {} })).toBe(
380 false,
381 );
382 // Legacy in-config flag fills in when the envelope is silent.
383 expect(resolveProviderInstanceEnabled({ driver: grok, config: { enabled: true } })).toBe(true);
384 // Conflicting flags: the explicit false wins, whichever side it is on.
385 expect(
386 resolveProviderInstanceEnabled({ driver: grok, enabled: true, config: { enabled: false } }),
387 ).toBe(false);
388 expect(
389 resolveProviderInstanceEnabled({ driver: codex, enabled: false, config: { enabled: true } }),
390 ).toBe(false);
391 });
392});
393
394describe("ServerSettings worktree defaults", () => {
395 it("defaults start-from-origin on for legacy configs", () => {
396 expect(decodeServerSettings({}).newWorktreesStartFromOrigin).toBe(true);
397 });
398
399 it("accepts start-from-origin updates", () => {
400 expect(
401 decodeServerSettingsPatch({ newWorktreesStartFromOrigin: false }).newWorktreesStartFromOrigin,
402 ).toBe(false);
403 });
404});
405
406describe("ServerSettings.sourceControlWritingStyle", () => {
407 it("defaults all style settings for legacy configs", () => {
408 const settings = decodeServerSettings({});
409
410 expect(settings.sourceControlWritingStyle).toEqual({
411 mode: "repo_conventions",
412 customInstructions: "",
413 followChangeRequestTemplates: true,
414 });
415 expect(settings.sourceControlWriterModelSelection).toBeNull();
416 });
417
418 it("trims partial style updates", () => {
419 const patch = decodeServerSettingsPatch({
420 sourceControlWritingStyle: {
421 mode: "custom",
422 customInstructions: " Prefer concise wording. ",
423 },
424 });
425
426 expect(patch.sourceControlWritingStyle).toEqual({
427 mode: "custom",
428 customInstructions: "Prefer concise wording.",
429 });
430 });
431});
432
433describe("ServerSettingsPatch.providerInstances", () => {
434 it("treats providerInstances as an optional whole-map replacement", () => {
435 const patch = decodeServerSettingsPatch({});
436 expect(patch.providerInstances).toBeUndefined();
437
438 const replacement = decodeServerSettingsPatch({
439 providerInstances: {
440 codex_personal: { driver: "codex", config: { homePath: "~/.codex" } },
441 },
442 });
443 expect(replacement.providerInstances).toBeDefined();
444 expect(replacement.providerInstances?.[ProviderInstanceId.make("codex_personal")]?.driver).toBe(
445 "codex",
446 );
447 });
448
449 it("preserves a fork-defined driver entry through patch decoding", () => {
450 const patch = decodeServerSettingsPatch({
451 providerInstances: {
452 ollama_local: {
453 driver: "ollama",
454 config: { endpoint: "http://localhost:11434" },
455 },
456 },
457 });
458 const ollamaId = ProviderInstanceId.make("ollama_local");
459 expect(patch.providerInstances?.[ollamaId]?.driver).toBe("ollama");
460 });
461});
462
463describe("ServerSettingsPatch string normalization", () => {
464 it("trims string settings while decoding patches", () => {
465 const patch = decodeServerSettingsPatch({
466 addProjectBaseDirectory: " ~/Development ",
467 textGenerationModelSelection: { model: " gpt-5.4-mini " },
468 observability: {
469 otlpTracesUrl: " http://localhost:4318/v1/traces ",
470 },
471 providers: {
472 codex: {
473 binaryPath: " /opt/homebrew/bin/codex ",
474 homePath: " ~/.codex ",
475 launchArgs: " --strict-config --enable foo ",
476 },
477 },
478 providerInstances: {
479 codex_personal: {
480 driver: " codex ",
481 displayName: " Codex Personal ",
482 config: { homePath: " ~/.codex-personal " },
483 },
484 },
485 });
486
487 expect(patch.addProjectBaseDirectory).toBe("~/Development");
488 expect(patch.textGenerationModelSelection?.model).toBe("gpt-5.4-mini");
489 expect(patch.observability?.otlpTracesUrl).toBe("http://localhost:4318/v1/traces");
490 expect(patch.providers?.codex?.binaryPath).toBe("/opt/homebrew/bin/codex");
491 expect(patch.providers?.codex?.homePath).toBe("~/.codex");
492 expect(patch.providers?.codex?.launchArgs).toBe("--strict-config --enable foo");
493 expect(patch.providerInstances?.[ProviderInstanceId.make("codex_personal")]?.driver).toBe(
494 "codex",
495 );
496 expect(patch.providerInstances?.[ProviderInstanceId.make("codex_personal")]?.displayName).toBe(
497 "Codex Personal",
498 );
499 expect(patch.providerInstances?.[ProviderInstanceId.make("codex_personal")]?.config).toEqual({
500 homePath: " ~/.codex-personal ",
501 });
502 });
503
504 it("trims encoded server settings values before validation", () => {
505 const defaultSettings = decodeServerSettings({});
506 const encoded = encodeServerSettings({
507 ...defaultSettings,
508 addProjectBaseDirectory: " ~/Development ",
509 providers: {
510 ...defaultSettings.providers,
511 codex: {
512 ...defaultSettings.providers.codex,
513 binaryPath: " /opt/homebrew/bin/codex ",
514 launchArgs: " --strict-config ",
515 },
516 },
517 });
518
519 expect(encoded.addProjectBaseDirectory).toBe("~/Development");
520 expect(encoded.providers?.codex?.binaryPath).toBe("/opt/homebrew/bin/codex");
521 expect(encoded.providers?.codex?.launchArgs).toBe("--strict-config");
522 });
523});
524
525describe("ServerSettings environment icon", () => {
526 it("defaults to null", () => {
527 expect(decodeServerSettings({}).environmentIcon).toBeNull();
528 });
529
530 it("keeps a kind this build knows", () => {
531 expect(decodeServerSettings({ environmentIcon: "mac-mini" }).environmentIcon).toBe("mac-mini");
532 });
533
534 it("decodes a kind from a newer server as null instead of failing the snapshot", () => {
535 expect(decodeServerSettings({ environmentIcon: "toaster" }).environmentIcon).toBeNull();
536 });
537
538 it("round-trips through encode", () => {
539 const settings = decodeServerSettings({ environmentIcon: "laptop" });
540 expect(encodeServerSettings(settings).environmentIcon).toBe("laptop");
541 });
542});