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