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