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