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