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 resolveProviderInstanceEnabled,
11 ServerSettings,
12 ServerSettingsPatch,
13} from "./settings.ts";
14
15const decodeClientSettings = Schema.decodeUnknownSync(ClientSettingsSchema);
16const decodeClientSettingsPatch = Schema.decodeUnknownSync(ClientSettingsPatch);
17const encodeClientSettings = Schema.encodeSync(ClientSettingsSchema);
18const decodeServerSettings = Schema.decodeUnknownSync(ServerSettings);
19const decodeServerSettingsPatch = Schema.decodeUnknownSync(ServerSettingsPatch);
20const encodeServerSettings = Schema.encodeSync(ServerSettings);
21const decodeClaudeSettings = Schema.decodeUnknownSync(ClaudeSettings);
22
23describe("storage cleanup settings", () => {
24 it("keeps cleanup disabled for existing installations", () => {
25 expect(decodeServerSettings({}).worktreeCleanup).toBeNull();
26 expect(decodeServerSettings({}).storageCleanup).toEqual({
27 worktreeAfterDays: null,
28 worktreeOnMerge: false,
29 worktreeOnDelete: false,
30 worktreeUnchanged: false,
31 browserArtifactsAfterDays: null,
32 logsAfterDays: null,
33 });
34 });
35
36 it("accepts eight-day retention and disabling one rule without resetting others", () => {
37 expect(decodeServerSettingsPatch({ storageCleanup: { worktreeAfterDays: 8 } })).toEqual({
38 storageCleanup: { worktreeAfterDays: 8 },
39 });
40 expect(decodeServerSettingsPatch({ storageCleanup: { worktreeAfterDays: null } })).toEqual({
41 storageCleanup: { worktreeAfterDays: null },
42 });
43 });
44
45 it("accepts partial custom patches but requires complete stored project rules", () => {
46 expect(
47 decodeServerSettingsPatch({
48 worktreeCleanup: { mode: "custom", rules: { worktreeAfterDays: 8 } },
49 }),
50 ).toEqual({ worktreeCleanup: { mode: "custom", rules: { worktreeAfterDays: 8 } } });
51 expect(() =>
52 decodeServerSettings({
53 projectSettingsOverrides: {
54 project: { worktreeCleanup: { mode: "custom", rules: { worktreeAfterDays: 8 } } },
55 },
56 }),
57 ).toThrow();
58 });
59
60 it.each([0, -1, 1.5, 3651])("rejects invalid retention %s", (days) => {
61 expect(() =>
62 decodeServerSettingsPatch({ storageCleanup: { browserArtifactsAfterDays: days } }),
63 ).toThrow();
64 });
65});
66
67describe("ClientSettings rich text composer", () => {
68 it("enables rich text for new and existing settings without a saved preference", () => {
69 expect(decodeClientSettings({}).composerRichTextEnabled).toBe(true);
70 expect(decodeClientSettings({ sendShortcut: "mod-enter" }).composerRichTextEnabled).toBe(true);
71 });
72
73 it("preserves an explicit opt-out through patches and persistence", () => {
74 const preference = { composerRichTextEnabled: false };
75 expect(decodeClientSettingsPatch(preference)).toEqual(preference);
76 expect(encodeClientSettings(decodeClientSettings(preference))).toMatchObject(preference);
77 });
78});
79
80describe("ServerSettings default permissions", () => {
81 it("keeps full access for settings saved before a default was configured", () => {
82 expect(decodeServerSettings({}).defaultRuntimeMode).toBe("full-access");
83 expect(DEFAULT_SERVER_SETTINGS.defaultRuntimeMode).toBe("full-access");
84 });
85
86 it.each(["approval-required", "auto-accept-edits", "auto", "full-access"])(
87 "round-trips %s as an environment default and project override",
88 (defaultRuntimeMode) => {
89 const input = {
90 defaultRuntimeMode,
91 projectSettingsOverrides: { project: { defaultRuntimeMode } },
92 };
93 expect(encodeServerSettings(decodeServerSettings(input))).toMatchObject(input);
94 expect(decodeServerSettingsPatch(input)).toEqual(input);
95 },
96 );
97
98 it("rejects unsupported permission defaults", () => {
99 expect(() => decodeServerSettings({ defaultRuntimeMode: "unsupported" })).toThrow();
100 expect(() => decodeServerSettingsPatch({ defaultRuntimeMode: "unsupported" })).toThrow();
101 expect(() =>
102 decodeServerSettingsPatch({
103 projectSettingsOverrides: { project: { defaultRuntimeMode: "unsupported" } },
104 }),
105 ).toThrow();
106 });
107});
108
109describe("ServerSettings usage price overrides", () => {
110 const prices = { inputCostPerMillionTokens: 2, outputCostPerMillionTokens: 8 };
111
112 it("defaults to automatic pricing and round-trips arbitrary model IDs", () => {
113 expect(decodeServerSettings({}).usagePriceOverrides).toEqual({});
114 const settings = decodeServerSettings({
115 usagePriceOverrides: { " vendor/example-model ": prices },
116 });
117 expect(encodeServerSettings(settings).usagePriceOverrides).toEqual({
118 "vendor/example-model": prices,
119 });
120 });
121
122 it("accepts zero rates, optional cache rates, and per-model deletion", () => {
123 const overrides = {
124 "example-model": {
125 inputCostPerMillionTokens: 0,
126 outputCostPerMillionTokens: 0,
127 cacheReadCostPerMillionTokens: 0.5,
128 cacheWriteCostPerMillionTokens: 3,
129 },
130 "removed-model": null,
131 };
132 expect(
133 decodeServerSettingsPatch({ usagePriceOverrides: overrides }).usagePriceOverrides,
134 ).toEqual(overrides);
135 });
136
137 it.each([
138 "inputCostPerMillionTokens",
139 "outputCostPerMillionTokens",
140 "cacheReadCostPerMillionTokens",
141 "cacheWriteCostPerMillionTokens",
142 ])("rejects invalid %s rates at the settings boundary", (field) => {
143 for (const value of [-1, Number.NaN, Number.POSITIVE_INFINITY, "2"]) {
144 const usagePriceOverrides = { "example-model": { ...prices, [field]: value } };
145 expect(() => decodeServerSettings({ usagePriceOverrides })).toThrow();
146 expect(() => decodeServerSettingsPatch({ usagePriceOverrides })).toThrow();
147 }
148 });
149
150 it("rejects empty model IDs and incomplete input/output pricing", () => {
151 for (const usagePriceOverrides of [
152 { " ": prices },
153 { "example-model": { inputCostPerMillionTokens: 2 } },
154 { "example-model": { outputCostPerMillionTokens: 8 } },
155 ]) {
156 expect(() => decodeServerSettingsPatch({ usagePriceOverrides })).toThrow();
157 }
158 });
159});
160
161describe("custom model settings", () => {
162 const capabilities = {
163 optionDescriptors: [
164 {
165 id: "effort",
166 label: "Reasoning",
167 type: "select",
168 options: [{ id: "high", label: "High", isDefault: true }],
169 },
170 ],
171 };
172
173 it("accepts legacy bare slugs alongside full entries", () => {
174 const decoded = decodeClaudeSettings({
175 customModels: ["bare-slug", { slug: "named", name: "Named", capabilities }],
176 });
177 expect(decoded.customModels).toEqual([
178 "bare-slug",
179 { slug: "named", name: "Named", capabilities },
180 ]);
181 });
182
183 it("accepts entries at the settings patch boundary", () => {
184 expect(
185 decodeServerSettingsPatch({
186 providers: { codex: { customModels: [{ slug: "x", capabilities }] } },
187 }).providers?.codex?.customModels,
188 ).toEqual([{ slug: "x", capabilities }]);
189 expect(() =>
190 decodeServerSettingsPatch({ providers: { codex: { customModels: [{ name: "no slug" }] } } }),
191 ).toThrow();
192 });
193});
194
195describe("ClaudeSettings auto-compaction", () => {
196 it("uses Claude's default threshold when no override is configured", () => {
197 expect(decodeClaudeSettings({}).autoCompactWindow).toBe("");
198 });
199
200 it.each(["100000", "300000", "1000000"])(
201 "accepts a supported auto-compaction threshold: %s",
202 (value) => {
203 expect(decodeClaudeSettings({ autoCompactWindow: value }).autoCompactWindow).toBe(value);
204 },
205 );
206
207 it.each(["99999", "1000001", "300k", "invalid"])(
208 "rejects an unsupported auto-compaction threshold: %s",
209 (value) => {
210 expect(() => decodeClaudeSettings({ autoCompactWindow: value })).toThrow();
211 },
212 );
213
214 it("rejects an unsupported threshold at the settings patch boundary", () => {
215 expect(() =>
216 decodeServerSettingsPatch({ providers: { claudeAgent: { autoCompactWindow: "300k" } } }),
217 ).toThrow();
218 expect(
219 decodeServerSettingsPatch({ providers: { claudeAgent: { autoCompactWindow: "300000" } } }),
220 ).toBeDefined();
221 });
222});
223
224describe("ClientSettings notifications", () => {
225 it("requires opt-in when existing settings omit notification preferences", () => {
226 expect(decodeClientSettings({}).notificationMode).toBe("off");
227 expect(decodeClientSettings({}).inAppNotificationsEnabled).toBe(false);
228 expect(decodeClientSettingsPatch({})).not.toHaveProperty("inAppNotificationsEnabled");
229 expect(decodeClientSettingsPatch({})).not.toHaveProperty("notificationMode");
230 });
231
232 it.each([true, false])(
233 "round-trips in-app notifications set to %s",
234 (inAppNotificationsEnabled) => {
235 const settings = decodeClientSettings({ inAppNotificationsEnabled });
236 expect(encodeClientSettings(settings).inAppNotificationsEnabled).toBe(
237 inAppNotificationsEnabled,
238 );
239 expect(
240 decodeClientSettingsPatch({ inAppNotificationsEnabled }).inAppNotificationsEnabled,
241 ).toBe(inAppNotificationsEnabled);
242 },
243 );
244
245 it.each(["true", 1, null])(
246 "rejects an invalid in-app notification preference %s",
247 (inAppNotificationsEnabled) => {
248 expect(() => decodeClientSettings({ inAppNotificationsEnabled })).toThrow();
249 expect(() => decodeClientSettingsPatch({ inAppNotificationsEnabled })).toThrow();
250 },
251 );
252
253 it.each(["off", "notifications", "sound", "notifications-and-sound"])(
254 "round-trips the %s mode",
255 (notificationMode) => {
256 const settings = decodeClientSettings({ notificationMode });
257 expect(encodeClientSettings(settings).notificationMode).toBe(notificationMode);
258 expect(decodeClientSettingsPatch({ notificationMode }).notificationMode).toBe(
259 notificationMode,
260 );
261 },
262 );
263
264 it.each(["always", true, null])(
265 "rejects unsupported notification mode %s",
266 (notificationMode) => {
267 expect(() => decodeClientSettings({ notificationMode })).toThrow();
268 expect(() => decodeClientSettingsPatch({ notificationMode })).toThrow();
269 },
270 );
271});
272
273describe("ClientSettings default diff file state", () => {
274 it("keeps files collapsed when existing settings omit the preference", () => {
275 expect(decodeClientSettings({}).diffFilesCollapsed).toBe(true);
276 });
277
278 it.each([true, false])("preserves a saved collapsed preference of %s", (diffFilesCollapsed) => {
279 const settings = decodeClientSettings({ diffFilesCollapsed });
280 expect(encodeClientSettings(settings).diffFilesCollapsed).toBe(diffFilesCollapsed);
281 expect(decodeClientSettingsPatch({ diffFilesCollapsed }).diffFilesCollapsed).toBe(
282 diffFilesCollapsed,
283 );
284 });
285});
286
287describe("ClientSettings diff colors", () => {
288 it("keeps red and green for existing settings without a saved palette", () => {
289 expect(decodeClientSettings({}).diffColorScheme).toBe("red-green");
290 });
291
292 it.each(["red-green", "blue-orange"])("round-trips the %s palette", (diffColorScheme) => {
293 const settings = decodeClientSettings({ diffColorScheme });
294 expect(encodeClientSettings(settings).diffColorScheme).toBe(diffColorScheme);
295 expect(decodeClientSettingsPatch({ diffColorScheme }).diffColorScheme).toBe(diffColorScheme);
296 });
297
298 it("rejects unsupported palettes", () => {
299 expect(() => decodeClientSettings({ diffColorScheme: "purple-yellow" })).toThrow();
300 expect(() => decodeClientSettingsPatch({ diffColorScheme: "purple-yellow" })).toThrow();
301 });
302});
303
304describe("ClientSettings chat width", () => {
305 it("keeps the comfortable width for existing settings without a saved width", () => {
306 expect(decodeClientSettings({}).chatWidth).toBe("comfortable");
307 });
308
309 it.each(["comfortable", "wide", "full"])("round-trips the %s width", (chatWidth) => {
310 const settings = decodeClientSettings({ chatWidth });
311 expect(encodeClientSettings(settings).chatWidth).toBe(chatWidth);
312 expect(decodeClientSettingsPatch({ chatWidth }).chatWidth).toBe(chatWidth);
313 });
314
315 it("rejects unsupported widths", () => {
316 expect(() => decodeClientSettings({ chatWidth: "huge" })).toThrow();
317 expect(() => decodeClientSettingsPatch({ chatWidth: "huge" })).toThrow();
318 });
319});
320
321describe("ClientSettings load balancing", () => {
322 it("requires opt-in when settings are new or omit load balancing", () => {
323 expect(decodeClientSettings({}).loadBalancingEnabled).toBe(false);
324 expect(decodeClientSettings({ loadBalancingWeights: {} }).loadBalancingEnabled).toBe(false);
325 });
326
327 it.each([true, false])("preserves a saved choice of %s", (loadBalancingEnabled) => {
328 const settings = decodeClientSettings({ loadBalancingEnabled });
329 expect(encodeClientSettings(settings).loadBalancingEnabled).toBe(loadBalancingEnabled);
330 expect(decodeClientSettingsPatch({ loadBalancingEnabled }).loadBalancingEnabled).toBe(
331 loadBalancingEnabled,
332 );
333 });
334});
335
336describe("ClientSettings word wrap", () => {
337 it("defaults word wrap on", () => {
338 expect(decodeClientSettings({}).wordWrap).toBe(true);
339 });
340
341 it("ignores obsolete wrapping preferences", () => {
342 const decoded = decodeClientSettings({
343 chatWordWrap: false,
344 diffWordWrap: false,
345 });
346
347 expect(decoded.wordWrap).toBe(true);
348 expect(decoded).not.toHaveProperty("chatWordWrap");
349 expect(decoded).not.toHaveProperty("diffWordWrap");
350 });
351});
352
353describe("ClientSettings window capture", () => {
354 it("defaults capture off while keeping its feedback enabled", () => {
355 const settings = decodeClientSettings({});
356
357 expect(settings.snapShotEnabled).toBe(false);
358 expect(settings.snapShotIncludeAccessibility).toBe(true);
359 expect(settings.snapShotShortcut).toEqual({ kind: "both-shift-keys" });
360 expect(settings.snapShotPlaySound).toBe(true);
361 expect(settings.snapShotSound).toBe("soft-pop");
362 expect(settings.snapShotFlash).toBe(true);
363 expect(settings.snapShotAnimations).toBe(true);
364 });
365
366 it("accepts capture preference updates", () => {
367 expect(
368 decodeClientSettingsPatch({
369 snapShotEnabled: true,
370 snapShotIncludeAccessibility: false,
371 snapShotShortcut: {
372 key: "w",
373 metaKey: false,
374 ctrlKey: false,
375 shiftKey: true,
376 altKey: true,
377 modKey: false,
378 },
379 snapShotPlaySound: false,
380 snapShotSound: "camera-shutter",
381 snapShotFlash: false,
382 snapShotAnimations: false,
383 }),
384 ).toEqual({
385 snapShotEnabled: true,
386 snapShotIncludeAccessibility: false,
387 snapShotShortcut: {
388 key: "w",
389 metaKey: false,
390 ctrlKey: false,
391 shiftKey: true,
392 altKey: true,
393 modKey: false,
394 },
395 snapShotPlaySound: false,
396 snapShotSound: "camera-shutter",
397 snapShotFlash: false,
398 snapShotAnimations: false,
399 });
400 });
401
402 it("rejects unknown capture sounds", () => {
403 expect(() => decodeClientSettingsPatch({ snapShotSound: "doorbell" })).toThrow();
404 });
405
406 it("accepts modifier pair shortcuts", () => {
407 expect(
408 decodeClientSettingsPatch({
409 snapShotShortcut: { kind: "modifier-pair", modifier: "meta" },
410 }),
411 ).toEqual({
412 snapShotShortcut: { kind: "modifier-pair", modifier: "meta" },
413 });
414 expect(() =>
415 decodeClientSettingsPatch({
416 snapShotShortcut: { kind: "modifier-pair", modifier: "hyper" },
417 }),
418 ).toThrow();
419 });
420
421 it("rejects a capture shortcut with no modifier", () => {
422 expect(() =>
423 decodeClientSettingsPatch({
424 snapShotShortcut: {
425 key: "w",
426 metaKey: false,
427 ctrlKey: false,
428 shiftKey: false,
429 altKey: false,
430 modKey: false,
431 },
432 }),
433 ).toThrow();
434 });
435});
436
437describe("ClientSettings proactive panels", () => {
438 it("is opt-in and accepts client-local updates", () => {
439 expect(decodeClientSettings({}).proactivePanelsEnabled).toBe(false);
440 expect(decodeClientSettingsPatch({ proactivePanelsEnabled: true }).proactivePanelsEnabled).toBe(
441 true,
442 );
443 });
444});
445
446describe("ClientSettings quit confirmation", () => {
447 it("defaults to hold", () => {
448 expect(decodeClientSettings({}).confirmQuit).toBe("hold");
449 });
450
451 it.each(["direct", "hold", "double-click"] as const)("accepts the %s mode", (mode) => {
452 expect(decodeClientSettings({ confirmQuit: mode }).confirmQuit).toBe(mode);
453 expect(decodeClientSettingsPatch({ confirmQuit: mode }).confirmQuit).toBe(mode);
454 });
455
456 it.each([
457 [true, "hold"],
458 [false, "direct"],
459 ] as const)("migrates the legacy %s value to %s", (legacyValue, mode) => {
460 const settings = decodeClientSettings({ confirmQuit: legacyValue });
461
462 expect(settings.confirmQuit).toBe(mode);
463 expect(encodeClientSettings(settings).confirmQuit).toBe(mode);
464 });
465
466 it("rejects legacy booleans at the patch boundary", () => {
467 expect(() => decodeClientSettingsPatch({ confirmQuit: true })).toThrow();
468 });
469});
470
471describe("ClientSettings browser recording frame rate", () => {
472 it("defaults to 30 fps", () => {
473 expect(decodeClientSettings({}).browserRecordingFrameRate).toBe(30);
474 });
475
476 it.each([30, 60])("accepts a supported frame rate: %s", (frameRate) => {
477 expect(
478 decodeClientSettings({ browserRecordingFrameRate: frameRate }).browserRecordingFrameRate,
479 ).toBe(frameRate);
480 expect(
481 decodeClientSettingsPatch({ browserRecordingFrameRate: frameRate }).browserRecordingFrameRate,
482 ).toBe(frameRate);
483 });
484
485 it.each([24, 59, 120])("rejects an unsupported frame rate: %s", (frameRate) => {
486 expect(() => decodeClientSettings({ browserRecordingFrameRate: frameRate })).toThrow();
487 expect(() => decodeClientSettingsPatch({ browserRecordingFrameRate: frameRate })).toThrow();
488 });
489});
490
491describe("ClientSettings recording input overlays", () => {
492 it("defaults both overlays off and accepts independent opt-ins", () => {
493 const settings = decodeClientSettings({});
494 expect(settings.browserRecordingShowKeyPresses).toBe(false);
495 expect(settings.browserRecordingShowMousePresses).toBe(false);
496 expect(
497 decodeClientSettingsPatch({
498 browserRecordingShowKeyPresses: true,
499 browserRecordingShowMousePresses: false,
500 }),
501 ).toMatchObject({
502 browserRecordingShowKeyPresses: true,
503 browserRecordingShowMousePresses: false,
504 });
505 });
506});
507
508describe("ClientSettings glass opacity", () => {
509 it("defaults to a readable translucent surface", () => {
510 expect(decodeClientSettings({}).glassOpacity).toBe(80);
511 });
512
513 it.each([39, 101, 72.5])("rejects an invalid glass opacity: %s", (value) => {
514 expect(() => decodeClientSettings({ glassOpacity: value })).toThrow();
515 expect(() => decodeClientSettingsPatch({ glassOpacity: value })).toThrow();
516 });
517
518 it.each([40, 75, 100])("accepts a glass opacity within the supported range: %s", (value) => {
519 expect(decodeClientSettings({ glassOpacity: value }).glassOpacity).toBe(value);
520 expect(decodeClientSettingsPatch({ glassOpacity: value }).glassOpacity).toBe(value);
521 });
522});
523
524describe("ClientSettings appearance contrast", () => {
525 it("defaults to the theme's original contrast", () => {
526 expect(decodeClientSettings({}).appearanceContrast).toBe(100);
527 });
528
529 it.each([49, 201, 92.5])("rejects an invalid appearance contrast: %s", (value) => {
530 expect(() => decodeClientSettings({ appearanceContrast: value })).toThrow();
531 expect(() => decodeClientSettingsPatch({ appearanceContrast: value })).toThrow();
532 });
533
534 it.each([50, 100, 150, 200])("accepts an appearance contrast in range: %s", (value) => {
535 expect(decodeClientSettings({ appearanceContrast: value }).appearanceContrast).toBe(value);
536 expect(decodeClientSettingsPatch({ appearanceContrast: value }).appearanceContrast).toBe(value);
537 });
538});
539
540describe("ClientSettings panel animations", () => {
541 it("defaults to instant changes", () => {
542 expect(decodeClientSettings({}).panelAnimationDurationMs).toBe(0);
543 });
544
545 it.each([0, 400])("accepts a panel animation duration: %s", (value) => {
546 expect(decodeClientSettingsPatch({ panelAnimationDurationMs: value })).toEqual({
547 panelAnimationDurationMs: value,
548 });
549 });
550
551 it.each([-1, 401, 150.5])("rejects an invalid panel animation duration: %s", (value) => {
552 expect(() => decodeClientSettingsPatch({ panelAnimationDurationMs: value })).toThrow();
553 });
554});
555
556describe("ClientSettings environment identification", () => {
557 it("defaults to artwork and accepts each presentation mode", () => {
558 expect(decodeClientSettings({}).environmentIdentificationMode).toBe("artwork");
559
560 for (const mode of ["artwork", "pill", "none"] as const) {
561 expect(
562 decodeClientSettingsPatch({ environmentIdentificationMode: mode })
563 .environmentIdentificationMode,
564 ).toBe(mode);
565 }
566 });
567
568 it("rejects unsupported presentation modes", () => {
569 expect(() => decodeClientSettings({ environmentIdentificationMode: "badge" })).toThrow();
570 expect(() => decodeClientSettingsPatch({ environmentIdentificationMode: "badge" })).toThrow();
571 });
572});
573
574describe("ClientSettings sidebar", () => {
575 it("defaults to the current sidebar", () => {
576 expect(decodeClientSettings({}).legacySidebarEnabled).toBe(false);
577 });
578
579 it("drops the retired sidebar v2 beta keys, resetting everyone to the default", () => {
580 const decoded = decodeClientSettings({
581 sidebarV2Enabled: false,
582 sidebarV2ConfiguredByUser: true,
583 });
584 expect(decoded.legacySidebarEnabled).toBe(false);
585 expect(decoded).not.toHaveProperty("sidebarV2Enabled");
586 expect(decoded).not.toHaveProperty("sidebarV2ConfiguredByUser");
587 });
588
589 it("drops the retired compact sidebar keys for users who opted in", () => {
590 const stored = { compactSidebarEnabled: true, sidebarCompactThreadRows: true };
591 const decoded = decodeClientSettings(stored);
592 expect(decoded).not.toHaveProperty("compactSidebarEnabled");
593 expect(decoded).not.toHaveProperty("sidebarCompactThreadRows");
594 expect(decodeClientSettingsPatch(stored)).toEqual({});
595 });
596
597 it("preserves an explicit legacy sidebar opt-in", () => {
598 expect(decodeClientSettings({ legacySidebarEnabled: true }).legacySidebarEnabled).toBe(true);
599 expect(decodeClientSettingsPatch({ legacySidebarEnabled: true }).legacySidebarEnabled).toBe(
600 true,
601 );
602 });
603
604 it("keeps unpin confirmation opt-in and patchable", () => {
605 expect(decodeClientSettings({}).confirmThreadUnpin).toBe(false);
606 expect(decodeClientSettingsPatch({ confirmThreadUnpin: true }).confirmThreadUnpin).toBe(true);
607 expect(() => decodeClientSettingsPatch({ confirmThreadUnpin: "yes" })).toThrow();
608 });
609});
610
611describe("ClientSettings context window meter", () => {
612 it("defaults off and preserves an explicit legacy opt-in", () => {
613 expect(decodeClientSettings({}).contextWindowMeterEnabled).toBe(false);
614 expect(
615 decodeClientSettings({ contextWindowMeterEnabled: true }).contextWindowMeterEnabled,
616 ).toBe(true);
617 expect(
618 decodeClientSettingsPatch({ contextWindowMeterEnabled: true }).contextWindowMeterEnabled,
619 ).toBe(true);
620 });
621});
622
623describe("ClientSettings send shortcut", () => {
624 it("defaults to Enter and validates the supported choices", () => {
625 expect(decodeClientSettings({}).sendShortcut).toBe("enter");
626 for (const sendShortcut of ["enter", "mod-enter-multiline", "mod-enter"]) {
627 expect(decodeClientSettings({ sendShortcut }).sendShortcut).toBe(sendShortcut);
628 expect(decodeClientSettingsPatch({ sendShortcut }).sendShortcut).toBe(sendShortcut);
629 }
630 expect(() => decodeClientSettingsPatch({ sendShortcut: "invalid" })).toThrow();
631 });
632});
633
634describe("ClientSettings follow-up behavior", () => {
635 it("defaults to queue and accepts either behavior", () => {
636 expect(decodeClientSettings({}).followUpBehavior).toBe("queue");
637 for (const followUpBehavior of ["queue", "steer"]) {
638 expect(decodeClientSettings({ followUpBehavior }).followUpBehavior).toBe(followUpBehavior);
639 expect(decodeClientSettingsPatch({ followUpBehavior }).followUpBehavior).toBe(
640 followUpBehavior,
641 );
642 }
643 expect(() => decodeClientSettingsPatch({ followUpBehavior: "invalid" })).toThrow();
644 });
645});
646
647describe("ClientSettings composer collapse", () => {
648 it("collapses on scroll by default and accepts opting out", () => {
649 expect(decodeClientSettings({}).composerCollapseOnScroll).toBe(true);
650 expect(
651 decodeClientSettingsPatch({ composerCollapseOnScroll: false }).composerCollapseOnScroll,
652 ).toBe(false);
653 });
654
655 it("drops the retired blur trigger key", () => {
656 const decoded = decodeClientSettings({ composerCollapseOnBlur: false });
657 expect(decoded.composerCollapseOnScroll).toBe(true);
658 expect(decoded).not.toHaveProperty("composerCollapseOnBlur");
659 });
660});
661
662describe("ServerSettings thread settlement", () => {
663 it("defaults merge settlement on and inactivity settlement to three days", () => {
664 const settings = decodeServerSettings({});
665 expect(settings.sidebarAutoSettleAfterDays).toBe(3);
666 expect(settings.sidebarAutoSettleOnMerge).toBe(true);
667 });
668
669 it("allows both automatic rules to be disabled", () => {
670 expect(
671 decodeServerSettings({
672 sidebarAutoSettleAfterDays: null,
673 sidebarAutoSettleOnMerge: false,
674 }),
675 ).toMatchObject({ sidebarAutoSettleAfterDays: null, sidebarAutoSettleOnMerge: false });
676 expect(
677 decodeServerSettingsPatch({
678 sidebarAutoSettleAfterDays: null,
679 sidebarAutoSettleOnMerge: false,
680 }),
681 ).toMatchObject({ sidebarAutoSettleAfterDays: null, sidebarAutoSettleOnMerge: false });
682 });
683
684 it.each([-1, 0, 91])("rejects an auto-settle threshold outside 1..90: %s", (value) => {
685 expect(() => decodeServerSettings({ sidebarAutoSettleAfterDays: value })).toThrow();
686 expect(() => decodeServerSettingsPatch({ sidebarAutoSettleAfterDays: value })).toThrow();
687 });
688});
689
690describe("ClientSettings pull request merge methods", () => {
691 it("defaults to no project overrides and accepts supported methods", () => {
692 expect(decodeClientSettings({}).pullRequestMergeMethodOverrides).toEqual({});
693 expect(
694 decodeClientSettingsPatch({
695 pullRequestMergeMethodOverrides: { project: "squash" },
696 }).pullRequestMergeMethodOverrides,
697 ).toEqual({ project: "squash" });
698 });
699
700 it("rejects unsupported project merge methods", () => {
701 expect(() =>
702 decodeClientSettingsPatch({
703 pullRequestMergeMethodOverrides: { project: "fast-forward" },
704 }),
705 ).toThrow();
706 });
707});
708
709describe("ServerSettings.providerInstances (slice-2 invariant)", () => {
710 it("defaults to an empty record so legacy configs without the key still decode", () => {
711 expect(DEFAULT_SERVER_SETTINGS.providerInstances).toEqual({});
712 });
713
714 it("decodes a fully empty config (legacy on-disk shape) without complaint", () => {
715 const decoded = decodeServerSettings({});
716 expect(decoded.providerInstances).toEqual({});
717 // Legacy `providers` struct is still hydrated with its per-driver defaults
718 // so existing call sites keep working through the migration.
719 expect(decoded.providers.codex.enabled).toBe(true);
720 });
721
722 it("decodes a multi-instance map mixing first-party and fork drivers", () => {
723 const decoded = decodeServerSettings({
724 providerInstances: {
725 codex_personal: {
726 driver: "codex",
727 displayName: "Codex (personal)",
728 config: { homePath: "~/.codex_personal" },
729 },
730 codex_work: {
731 driver: "codex",
732 config: { homePath: "~/.codex_work" },
733 },
734 ollama_local: {
735 driver: "ollama",
736 displayName: "Ollama (local)",
737 config: { endpoint: "http://localhost:11434" },
738 },
739 },
740 });
741 const personalId = ProviderInstanceId.make("codex_personal");
742 const workId = ProviderInstanceId.make("codex_work");
743 const ollamaId = ProviderInstanceId.make("ollama_local");
744
745 expect(decoded.providerInstances[personalId]?.driver).toBe("codex");
746 expect(decoded.providerInstances[workId]?.config).toEqual({ homePath: "~/.codex_work" });
747 // Critical: a config naming a driver this build does not know about
748 // (`ollama` is not in `ProviderDriverKind`) must round-trip without loss.
749 // The runtime handles "driver not installed" — the schema must not.
750 expect(decoded.providerInstances[ollamaId]?.driver).toBe("ollama");
751 expect(decoded.providerInstances[ollamaId]?.config).toEqual({
752 endpoint: "http://localhost:11434",
753 });
754 });
755
756 it("rejects instance keys that violate the slug pattern", () => {
757 expect(() =>
758 decodeServerSettings({
759 providerInstances: { "1bad": { driver: "codex" } },
760 }),
761 ).toThrow();
762 });
763});
764
765describe("provider enabled defaults", () => {
766 it("enables only the stable bindings by default", () => {
767 const decoded = decodeServerSettings({});
768 expect(decoded.providers.codex.enabled).toBe(true);
769 expect(decoded.providers.claudeAgent.enabled).toBe(true);
770 expect(decoded.providers.cursor.enabled).toBe(false);
771 expect(decoded.providers.grok.enabled).toBe(false);
772 expect(decoded.providers.opencode.enabled).toBe(false);
773 });
774
775 it("keeps Cursor enabled when an existing user explicitly opted in", () => {
776 const cursor = ProviderDriverKind.make("cursor");
777 const cursorId = ProviderInstanceId.make("cursor");
778 const decoded = decodeServerSettings({
779 providers: { cursor: { enabled: true } },
780 providerInstances: {
781 [cursorId]: { driver: cursor, enabled: true, config: {} },
782 },
783 });
784
785 expect(decoded.providers.cursor.enabled).toBe(true);
786 expect(resolveProviderInstanceEnabled(decoded.providerInstances[cursorId]!)).toBe(true);
787 });
788
789 it("resolves instance enabled state with explicit false winning", () => {
790 const grok = ProviderDriverKind.make("grok");
791 const codex = ProviderDriverKind.make("codex");
792 // No flags anywhere: driver default applies.
793 expect(resolveProviderInstanceEnabled({ driver: grok, config: {} })).toBe(false);
794 expect(resolveProviderInstanceEnabled({ driver: codex, config: {} })).toBe(true);
795 // Unknown fork drivers stay enabled.
796 expect(
797 resolveProviderInstanceEnabled({ driver: ProviderDriverKind.make("ollama"), config: {} }),
798 ).toBe(true);
799 // Envelope flag wins over the driver default.
800 expect(resolveProviderInstanceEnabled({ driver: grok, enabled: true, config: {} })).toBe(true);
801 expect(resolveProviderInstanceEnabled({ driver: codex, enabled: false, config: {} })).toBe(
802 false,
803 );
804 // Legacy in-config flag fills in when the envelope is silent.
805 expect(resolveProviderInstanceEnabled({ driver: grok, config: { enabled: true } })).toBe(true);
806 // Conflicting flags: the explicit false wins, whichever side it is on.
807 expect(
808 resolveProviderInstanceEnabled({ driver: grok, enabled: true, config: { enabled: false } }),
809 ).toBe(false);
810 expect(
811 resolveProviderInstanceEnabled({ driver: codex, enabled: false, config: { enabled: true } }),
812 ).toBe(false);
813 });
814});
815
816describe("ServerSettings worktree defaults", () => {
817 it("defaults the thread env mode to inherit and keeps stored values", () => {
818 expect(decodeServerSettings({}).defaultThreadEnvMode).toBeNull();
819 expect(decodeServerSettings({ defaultThreadEnvMode: "worktree" }).defaultThreadEnvMode).toBe(
820 "worktree",
821 );
822 expect(
823 decodeServerSettings({ defaultThreadEnvMode: "remote" }).defaultThreadEnvMode,
824 ).toBeNull();
825 expect(
826 decodeServerSettingsPatch({ defaultThreadEnvMode: null }).defaultThreadEnvMode,
827 ).toBeNull();
828 });
829
830 it("keeps an inherited thread env mode off the wire for older clients", () => {
831 const encode = Schema.encodeSync(ServerSettings);
832 expect("defaultThreadEnvMode" in encode(decodeServerSettings({}))).toBe(false);
833 expect(
834 encode(decodeServerSettings({ defaultThreadEnvMode: "worktree" })).defaultThreadEnvMode,
835 ).toBe("worktree");
836 });
837
838 it("defaults start-from-origin on for legacy configs", () => {
839 expect(decodeServerSettings({}).newWorktreesStartFromOrigin).toBe(true);
840 });
841
842 it("accepts start-from-origin updates", () => {
843 expect(
844 decodeServerSettingsPatch({ newWorktreesStartFromOrigin: false }).newWorktreesStartFromOrigin,
845 ).toBe(false);
846 });
847
848 it("defaults worktree submodules to inherit and tolerates unknown modes", () => {
849 expect(decodeServerSettings({}).worktreeSubmodules).toBeNull();
850 expect(decodeServerSettings({ worktreeSubmodules: "top-level" }).worktreeSubmodules).toBe(
851 "top-level",
852 );
853 expect(decodeServerSettings({ worktreeSubmodules: "shallow" }).worktreeSubmodules).toBeNull();
854 expect(decodeServerSettingsPatch({ worktreeSubmodules: null }).worktreeSubmodules).toBeNull();
855 });
856});
857
858describe("ServerSettings.sourceControlWritingStyle", () => {
859 it("defaults all style settings for legacy configs", () => {
860 const settings = decodeServerSettings({});
861
862 expect(settings.sourceControlWritingStyle).toEqual({
863 mode: "repo_conventions",
864 customInstructions: "",
865 followChangeRequestTemplates: true,
866 });
867 expect(settings.sourceControlWriterModelSelection).toBeNull();
868 });
869
870 it("trims partial style updates", () => {
871 const patch = decodeServerSettingsPatch({
872 sourceControlWritingStyle: {
873 mode: "custom",
874 customInstructions: " Prefer concise wording. ",
875 },
876 });
877
878 expect(patch.sourceControlWritingStyle).toEqual({
879 mode: "custom",
880 customInstructions: "Prefer concise wording.",
881 });
882 });
883});
884
885describe("ServerSettingsPatch.providerInstances", () => {
886 it("treats providerInstances as an optional whole-map replacement", () => {
887 const patch = decodeServerSettingsPatch({});
888 expect(patch.providerInstances).toBeUndefined();
889
890 const replacement = decodeServerSettingsPatch({
891 providerInstances: {
892 codex_personal: { driver: "codex", config: { homePath: "~/.codex" } },
893 },
894 });
895 expect(replacement.providerInstances).toBeDefined();
896 expect(replacement.providerInstances?.[ProviderInstanceId.make("codex_personal")]?.driver).toBe(
897 "codex",
898 );
899 });
900
901 it("preserves a fork-defined driver entry through patch decoding", () => {
902 const patch = decodeServerSettingsPatch({
903 providerInstances: {
904 ollama_local: {
905 driver: "ollama",
906 config: { endpoint: "http://localhost:11434" },
907 },
908 },
909 });
910 const ollamaId = ProviderInstanceId.make("ollama_local");
911 expect(patch.providerInstances?.[ollamaId]?.driver).toBe("ollama");
912 });
913});
914
915describe("ServerSettingsPatch string normalization", () => {
916 it("trims string settings while decoding patches", () => {
917 const patch = decodeServerSettingsPatch({
918 addProjectBaseDirectory: " ~/Development ",
919 textGenerationModelSelection: { model: " gpt-5.4-mini " },
920 observability: {
921 otlpTracesUrl: " http://localhost:4318/v1/traces ",
922 },
923 providers: {
924 codex: {
925 binaryPath: " /opt/homebrew/bin/codex ",
926 homePath: " ~/.codex ",
927 launchArgs: " --strict-config --enable foo ",
928 },
929 },
930 providerInstances: {
931 codex_personal: {
932 driver: " codex ",
933 displayName: " Codex Personal ",
934 config: { homePath: " ~/.codex-personal " },
935 },
936 },
937 });
938
939 expect(patch.addProjectBaseDirectory).toBe("~/Development");
940 expect(patch.textGenerationModelSelection?.model).toBe("gpt-5.4-mini");
941 expect(patch.observability?.otlpTracesUrl).toBe("http://localhost:4318/v1/traces");
942 expect(patch.providers?.codex?.binaryPath).toBe("/opt/homebrew/bin/codex");
943 expect(patch.providers?.codex?.homePath).toBe("~/.codex");
944 expect(patch.providers?.codex?.launchArgs).toBe("--strict-config --enable foo");
945 expect(patch.providerInstances?.[ProviderInstanceId.make("codex_personal")]?.driver).toBe(
946 "codex",
947 );
948 expect(patch.providerInstances?.[ProviderInstanceId.make("codex_personal")]?.displayName).toBe(
949 "Codex Personal",
950 );
951 expect(patch.providerInstances?.[ProviderInstanceId.make("codex_personal")]?.config).toEqual({
952 homePath: " ~/.codex-personal ",
953 });
954 });
955
956 it("trims encoded server settings values before validation", () => {
957 const defaultSettings = decodeServerSettings({});
958 const encoded = encodeServerSettings({
959 ...defaultSettings,
960 addProjectBaseDirectory: " ~/Development ",
961 providers: {
962 ...defaultSettings.providers,
963 codex: {
964 ...defaultSettings.providers.codex,
965 binaryPath: " /opt/homebrew/bin/codex ",
966 launchArgs: " --strict-config ",
967 },
968 },
969 });
970
971 expect(encoded.addProjectBaseDirectory).toBe("~/Development");
972 expect(encoded.providers?.codex?.binaryPath).toBe("/opt/homebrew/bin/codex");
973 expect(encoded.providers?.codex?.launchArgs).toBe("--strict-config");
974 });
975});
976
977describe("ServerSettings environment icon", () => {
978 it("defaults to null", () => {
979 expect(decodeServerSettings({}).environmentIcon).toBeNull();
980 });
981
982 it("keeps a kind this build knows", () => {
983 expect(decodeServerSettings({ environmentIcon: "mac-mini" }).environmentIcon).toBe("mac-mini");
984 expect(decodeServerSettings({ environmentIcon: "linux" }).environmentIcon).toBe("linux");
985 });
986
987 it("decodes a kind from a newer server as null instead of failing the snapshot", () => {
988 expect(decodeServerSettings({ environmentIcon: "toaster" }).environmentIcon).toBeNull();
989 });
990
991 it("round-trips through encode", () => {
992 const settings = decodeServerSettings({ environmentIcon: "laptop" });
993 expect(encodeServerSettings(settings).environmentIcon).toBe("laptop");
994
995 const linuxSettings = decodeServerSettings({ environmentIcon: "linux" });
996 expect(encodeServerSettings(linuxSettings).environmentIcon).toBe("linux");
997 });
998});
999
1000const decodeDeviceHostSettings = Schema.decodeSync(ServerSettings);
1001
1002it("validates remote device hosts and rejects ambiguous host ids", () => {
1003 const host = { id: "mini", label: "Mac mini", target: "user@mini", port: 2222 };
1004 expect(decodeDeviceHostSettings({ deviceHosts: [host] }).deviceHosts).toEqual([host]);
1005 expect(() => decodeDeviceHostSettings({ deviceHosts: [host, host] })).toThrow();
1006 expect(() => decodeDeviceHostSettings({ deviceHosts: [{ ...host, id: "local" }] })).toThrow();
1007 expect(() =>
1008 decodeDeviceHostSettings({ deviceHosts: [{ ...host, target: "-oProxyCommand=bad" }] }),
1009 ).toThrow();
1010 expect(() => decodeDeviceHostSettings({ deviceHosts: [{ ...host, port: 0 }] })).toThrow();
1011});