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