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 defaultEnabledForDriver,
11 resolveProviderInstanceEnabled,
12 ServerSettings,
13 ServerSettingsPatch,
14} from "./settings.ts";
15
16const decodeClientSettings = Schema.decodeUnknownSync(ClientSettingsSchema);
17const decodeClientSettingsPatch = Schema.decodeUnknownSync(ClientSettingsPatch);
18const decodeServerSettings = Schema.decodeUnknownSync(ServerSettings);
19const decodeServerSettingsPatch = Schema.decodeUnknownSync(ServerSettingsPatch);
20const encodeServerSettings = Schema.encodeSync(ServerSettings);
21const decodeClaudeSettings = Schema.decodeUnknownSync(ClaudeSettings);
22
23describe("ClaudeSettings auto-compaction", () => {
24 it("uses Claude's default threshold when no override is configured", () => {
25 expect(decodeClaudeSettings({}).autoCompactWindow).toBe("");
26 });
27
28 it.each(["100000", "300000", "1000000"])(
29 "accepts a supported auto-compaction threshold: %s",
30 (value) => {
31 expect(decodeClaudeSettings({ autoCompactWindow: value }).autoCompactWindow).toBe(value);
32 },
33 );
34
35 it.each(["99999", "1000001", "300k", "invalid"])(
36 "rejects an unsupported auto-compaction threshold: %s",
37 (value) => {
38 expect(() => decodeClaudeSettings({ autoCompactWindow: value })).toThrow();
39 },
40 );
41
42 it("rejects an unsupported threshold at the settings patch boundary", () => {
43 expect(() =>
44 decodeServerSettingsPatch({ providers: { claudeAgent: { autoCompactWindow: "300k" } } }),
45 ).toThrow();
46 expect(
47 decodeServerSettingsPatch({ providers: { claudeAgent: { autoCompactWindow: "300000" } } }),
48 ).toBeDefined();
49 });
50});
51
52describe("ClientSettings composer context strip", () => {
53 it("defaults to draft-only and accepts a persistent strip preference", () => {
54 expect(decodeClientSettings({}).persistComposerContextStrip).toBe(false);
55 expect(
56 decodeClientSettingsPatch({ persistComposerContextStrip: true }).persistComposerContextStrip,
57 ).toBe(true);
58 });
59});
60
61describe("ClientSettings word wrap", () => {
62 it("defaults word wrap on", () => {
63 expect(decodeClientSettings({}).wordWrap).toBe(true);
64 });
65
66 it("ignores obsolete wrapping preferences", () => {
67 const decoded = decodeClientSettings({
68 chatWordWrap: false,
69 diffWordWrap: false,
70 });
71
72 expect(decoded.wordWrap).toBe(true);
73 expect(decoded).not.toHaveProperty("chatWordWrap");
74 expect(decoded).not.toHaveProperty("diffWordWrap");
75 });
76});
77
78describe("ClientSettings browser recording frame rate", () => {
79 it("defaults to 30 fps", () => {
80 expect(decodeClientSettings({}).browserRecordingFrameRate).toBe(30);
81 });
82
83 it.each([30, 60])("accepts a supported frame rate: %s", (frameRate) => {
84 expect(
85 decodeClientSettings({ browserRecordingFrameRate: frameRate }).browserRecordingFrameRate,
86 ).toBe(frameRate);
87 expect(
88 decodeClientSettingsPatch({ browserRecordingFrameRate: frameRate }).browserRecordingFrameRate,
89 ).toBe(frameRate);
90 });
91
92 it.each([24, 59, 120])("rejects an unsupported frame rate: %s", (frameRate) => {
93 expect(() => decodeClientSettings({ browserRecordingFrameRate: frameRate })).toThrow();
94 expect(() => decodeClientSettingsPatch({ browserRecordingFrameRate: frameRate })).toThrow();
95 });
96});
97
98describe("ClientSettings glass opacity", () => {
99 it("defaults to a readable translucent surface", () => {
100 expect(decodeClientSettings({}).glassOpacity).toBe(80);
101 });
102
103 it.each([39, 101, 72.5])("rejects an invalid glass opacity: %s", (value) => {
104 expect(() => decodeClientSettings({ glassOpacity: value })).toThrow();
105 expect(() => decodeClientSettingsPatch({ glassOpacity: value })).toThrow();
106 });
107
108 it.each([40, 75, 100])("accepts a glass opacity within the supported range: %s", (value) => {
109 expect(decodeClientSettings({ glassOpacity: value }).glassOpacity).toBe(value);
110 expect(decodeClientSettingsPatch({ glassOpacity: value }).glassOpacity).toBe(value);
111 });
112});
113
114describe("ClientSettings appearance contrast", () => {
115 it("defaults to the theme's original contrast", () => {
116 expect(decodeClientSettings({}).appearanceContrast).toBe(100);
117 });
118
119 it.each([49, 201, 92.5])("rejects an invalid appearance contrast: %s", (value) => {
120 expect(() => decodeClientSettings({ appearanceContrast: value })).toThrow();
121 expect(() => decodeClientSettingsPatch({ appearanceContrast: value })).toThrow();
122 });
123
124 it.each([50, 100, 150, 200])("accepts an appearance contrast in range: %s", (value) => {
125 expect(decodeClientSettings({ appearanceContrast: value }).appearanceContrast).toBe(value);
126 expect(decodeClientSettingsPatch({ appearanceContrast: value }).appearanceContrast).toBe(value);
127 });
128});
129
130describe("ClientSettings environment identification", () => {
131 it("defaults to artwork and accepts each presentation mode", () => {
132 expect(decodeClientSettings({}).environmentIdentificationMode).toBe("artwork");
133
134 for (const mode of ["artwork", "pill", "none"] as const) {
135 expect(
136 decodeClientSettingsPatch({ environmentIdentificationMode: mode })
137 .environmentIdentificationMode,
138 ).toBe(mode);
139 }
140 });
141
142 it("rejects unsupported presentation modes", () => {
143 expect(() => decodeClientSettings({ environmentIdentificationMode: "badge" })).toThrow();
144 expect(() => decodeClientSettingsPatch({ environmentIdentificationMode: "badge" })).toThrow();
145 });
146});
147
148describe("ClientSettings sidebar", () => {
149 it("defaults to the current sidebar with automatic merge and inactivity settling", () => {
150 const settings = decodeClientSettings({});
151 expect(settings.legacySidebarEnabled).toBe(false);
152 expect(settings.sidebarAutoSettleAfterDays).toBe(3);
153 expect(settings.sidebarAutoSettleOnMerge).toBe(true);
154 });
155
156 it("drops the retired sidebar v2 beta keys, resetting everyone to the default", () => {
157 const decoded = decodeClientSettings({
158 sidebarV2Enabled: false,
159 sidebarV2ConfiguredByUser: true,
160 });
161 expect(decoded.legacySidebarEnabled).toBe(false);
162 expect(decoded).not.toHaveProperty("sidebarV2Enabled");
163 expect(decoded).not.toHaveProperty("sidebarV2ConfiguredByUser");
164 });
165
166 it("preserves an explicit legacy sidebar opt-in", () => {
167 expect(decodeClientSettings({ legacySidebarEnabled: true }).legacySidebarEnabled).toBe(true);
168 expect(decodeClientSettingsPatch({ legacySidebarEnabled: true }).legacySidebarEnabled).toBe(
169 true,
170 );
171 });
172
173 it("keeps unpin confirmation opt-in and patchable", () => {
174 expect(decodeClientSettings({}).confirmThreadUnpin).toBe(false);
175 expect(decodeClientSettingsPatch({ confirmThreadUnpin: true }).confirmThreadUnpin).toBe(true);
176 expect(() => decodeClientSettingsPatch({ confirmThreadUnpin: "yes" })).toThrow();
177 });
178
179 it("allows auto-settle by inactivity to be disabled", () => {
180 expect(
181 decodeClientSettings({ sidebarAutoSettleAfterDays: null }).sidebarAutoSettleAfterDays,
182 ).toBeNull();
183 });
184
185 it("allows auto-settle on merge to be disabled", () => {
186 expect(decodeClientSettings({ sidebarAutoSettleOnMerge: false }).sidebarAutoSettleOnMerge).toBe(
187 false,
188 );
189 expect(
190 decodeClientSettingsPatch({ sidebarAutoSettleOnMerge: false }).sidebarAutoSettleOnMerge,
191 ).toBe(false);
192 });
193
194 it.each([-1, 0, 91])("rejects an auto-settle threshold outside 1..90: %s", (value) => {
195 expect(() => decodeClientSettings({ sidebarAutoSettleAfterDays: value })).toThrow();
196 expect(() => decodeClientSettingsPatch({ sidebarAutoSettleAfterDays: value })).toThrow();
197 });
198});
199
200describe("ServerSettings.providerInstances (slice-2 invariant)", () => {
201 it("defaults text generation to Luna at low reasoning effort", () => {
202 expect(DEFAULT_SERVER_SETTINGS.textGenerationModelSelection).toEqual({
203 instanceId: ProviderInstanceId.make("codex"),
204 model: "gpt-5.6-luna",
205 options: [{ id: "reasoningEffort", value: "low" }],
206 });
207 });
208
209 it("defaults to an empty record so legacy configs without the key still decode", () => {
210 expect(DEFAULT_SERVER_SETTINGS.providerInstances).toEqual({});
211 });
212
213 it("decodes a fully empty config (legacy on-disk shape) without complaint", () => {
214 const decoded = decodeServerSettings({});
215 expect(decoded.providerInstances).toEqual({});
216 // Legacy `providers` struct is still hydrated with its per-driver defaults
217 // so existing call sites keep working through the migration.
218 expect(decoded.providers.codex.enabled).toBe(true);
219 });
220
221 it("decodes a multi-instance map mixing first-party and fork drivers", () => {
222 const decoded = decodeServerSettings({
223 providerInstances: {
224 codex_personal: {
225 driver: "codex",
226 displayName: "Codex (personal)",
227 config: { homePath: "~/.codex_personal" },
228 },
229 codex_work: {
230 driver: "codex",
231 config: { homePath: "~/.codex_work" },
232 },
233 ollama_local: {
234 driver: "ollama",
235 displayName: "Ollama (local)",
236 config: { endpoint: "http://localhost:11434" },
237 },
238 },
239 });
240 const personalId = ProviderInstanceId.make("codex_personal");
241 const workId = ProviderInstanceId.make("codex_work");
242 const ollamaId = ProviderInstanceId.make("ollama_local");
243
244 expect(decoded.providerInstances[personalId]?.driver).toBe("codex");
245 expect(decoded.providerInstances[workId]?.config).toEqual({ homePath: "~/.codex_work" });
246 // Critical: a config naming a driver this build does not know about
247 // (`ollama` is not in `ProviderDriverKind`) must round-trip without loss.
248 // The runtime handles "driver not installed" — the schema must not.
249 expect(decoded.providerInstances[ollamaId]?.driver).toBe("ollama");
250 expect(decoded.providerInstances[ollamaId]?.config).toEqual({
251 endpoint: "http://localhost:11434",
252 });
253 });
254
255 it("rejects instance keys that violate the slug pattern", () => {
256 expect(() =>
257 decodeServerSettings({
258 providerInstances: { "1bad": { driver: "codex" } },
259 }),
260 ).toThrow();
261 });
262});
263
264describe("provider enabled defaults", () => {
265 it("enables only the stable bindings by default", () => {
266 const decoded = decodeServerSettings({});
267 expect(decoded.providers.codex.enabled).toBe(true);
268 expect(decoded.providers.claudeAgent.enabled).toBe(true);
269 expect(decoded.providers.cursor.enabled).toBe(false);
270 expect(decoded.providers.grok.enabled).toBe(false);
271 expect(decoded.providers.opencode.enabled).toBe(false);
272 });
273
274 it("derives per-driver defaults from the settings schemas", () => {
275 expect(defaultEnabledForDriver(ProviderDriverKind.make("codex"))).toBe(true);
276 expect(defaultEnabledForDriver(ProviderDriverKind.make("cursor"))).toBe(false);
277 expect(defaultEnabledForDriver(ProviderDriverKind.make("grok"))).toBe(false);
278 // Unknown fork drivers stay enabled; their own build decides otherwise.
279 expect(defaultEnabledForDriver(ProviderDriverKind.make("ollama"))).toBe(true);
280 });
281
282 it("keeps Cursor enabled when an existing user explicitly opted in", () => {
283 const cursor = ProviderDriverKind.make("cursor");
284 const cursorId = ProviderInstanceId.make("cursor");
285 const decoded = decodeServerSettings({
286 providers: { cursor: { enabled: true } },
287 providerInstances: {
288 [cursorId]: { driver: cursor, enabled: true, config: {} },
289 },
290 });
291
292 expect(decoded.providers.cursor.enabled).toBe(true);
293 expect(resolveProviderInstanceEnabled(decoded.providerInstances[cursorId]!)).toBe(true);
294 });
295
296 it("resolves instance enabled state with explicit false winning", () => {
297 const grok = ProviderDriverKind.make("grok");
298 const codex = ProviderDriverKind.make("codex");
299 // No flags anywhere: driver default applies.
300 expect(resolveProviderInstanceEnabled({ driver: grok, config: {} })).toBe(false);
301 expect(resolveProviderInstanceEnabled({ driver: codex, config: {} })).toBe(true);
302 // Envelope flag wins over the driver default.
303 expect(resolveProviderInstanceEnabled({ driver: grok, enabled: true, config: {} })).toBe(true);
304 expect(resolveProviderInstanceEnabled({ driver: codex, enabled: false, config: {} })).toBe(
305 false,
306 );
307 // Legacy in-config flag fills in when the envelope is silent.
308 expect(resolveProviderInstanceEnabled({ driver: grok, config: { enabled: true } })).toBe(true);
309 // Conflicting flags: the explicit false wins, whichever side it is on.
310 expect(
311 resolveProviderInstanceEnabled({ driver: grok, enabled: true, config: { enabled: false } }),
312 ).toBe(false);
313 expect(
314 resolveProviderInstanceEnabled({ driver: codex, enabled: false, config: { enabled: true } }),
315 ).toBe(false);
316 });
317});
318
319describe("ServerSettings worktree defaults", () => {
320 it("defaults start-from-origin on for legacy configs", () => {
321 expect(decodeServerSettings({}).newWorktreesStartFromOrigin).toBe(true);
322 });
323
324 it("accepts start-from-origin updates", () => {
325 expect(
326 decodeServerSettingsPatch({ newWorktreesStartFromOrigin: false }).newWorktreesStartFromOrigin,
327 ).toBe(false);
328 });
329});
330
331describe("ServerSettings Cursor legacy settings", () => {
332 it("ignores obsolete Cursor CLI settings when reading server settings", () => {
333 const decoded = decodeServerSettings({
334 providers: {
335 cursor: {
336 enabled: true,
337 binaryPath: "cursor-agent",
338 apiEndpoint: "http://127.0.0.1:3774",
339 },
340 },
341 });
342
343 expect(decoded.providers.cursor.enabled).toBe(true);
344 expect(decoded.providers.cursor).not.toHaveProperty("binaryPath");
345 expect(decoded.providers.cursor).not.toHaveProperty("apiEndpoint");
346 });
347
348 it("ignores obsolete Cursor CLI settings in patches", () => {
349 const patch = decodeServerSettingsPatch({
350 providers: {
351 cursor: {
352 enabled: true,
353 binaryPath: "cursor-agent",
354 apiEndpoint: "http://127.0.0.1:3774",
355 },
356 },
357 });
358
359 expect(patch.providers?.cursor?.enabled).toBe(true);
360 expect(patch.providers?.cursor).not.toHaveProperty("binaryPath");
361 expect(patch.providers?.cursor).not.toHaveProperty("apiEndpoint");
362 });
363});
364
365describe("ServerSettings.sourceControlWritingStyle", () => {
366 it("defaults all style settings for legacy configs", () => {
367 const settings = decodeServerSettings({});
368
369 expect(settings.sourceControlWritingStyle).toEqual({
370 mode: "repo_conventions",
371 customInstructions: "",
372 followChangeRequestTemplates: true,
373 });
374 expect(settings.sourceControlWriterModelSelection).toBeNull();
375 });
376
377 it("trims partial style updates", () => {
378 const patch = decodeServerSettingsPatch({
379 sourceControlWritingStyle: {
380 mode: "custom",
381 customInstructions: " Prefer concise wording. ",
382 },
383 });
384
385 expect(patch.sourceControlWritingStyle).toEqual({
386 mode: "custom",
387 customInstructions: "Prefer concise wording.",
388 });
389 });
390});
391
392describe("ServerSettingsPatch.providerInstances", () => {
393 it("treats providerInstances as an optional whole-map replacement", () => {
394 const patch = decodeServerSettingsPatch({});
395 expect(patch.providerInstances).toBeUndefined();
396
397 const replacement = decodeServerSettingsPatch({
398 providerInstances: {
399 codex_personal: { driver: "codex", config: { homePath: "~/.codex" } },
400 },
401 });
402 expect(replacement.providerInstances).toBeDefined();
403 expect(replacement.providerInstances?.[ProviderInstanceId.make("codex_personal")]?.driver).toBe(
404 "codex",
405 );
406 });
407
408 it("preserves a fork-defined driver entry through patch decoding", () => {
409 const patch = decodeServerSettingsPatch({
410 providerInstances: {
411 ollama_local: {
412 driver: "ollama",
413 config: { endpoint: "http://localhost:11434" },
414 },
415 },
416 });
417 const ollamaId = ProviderInstanceId.make("ollama_local");
418 expect(patch.providerInstances?.[ollamaId]?.driver).toBe("ollama");
419 });
420});
421
422describe("ServerSettingsPatch string normalization", () => {
423 it("trims string settings while decoding patches", () => {
424 const patch = decodeServerSettingsPatch({
425 addProjectBaseDirectory: " ~/Development ",
426 textGenerationModelSelection: { model: " gpt-5.4-mini " },
427 observability: {
428 otlpTracesUrl: " http://localhost:4318/v1/traces ",
429 },
430 providers: {
431 codex: {
432 binaryPath: " /opt/homebrew/bin/codex ",
433 homePath: " ~/.codex ",
434 launchArgs: " --strict-config --enable foo ",
435 },
436 },
437 providerInstances: {
438 codex_personal: {
439 driver: " codex ",
440 displayName: " Codex Personal ",
441 config: { homePath: " ~/.codex-personal " },
442 },
443 },
444 });
445
446 expect(patch.addProjectBaseDirectory).toBe("~/Development");
447 expect(patch.textGenerationModelSelection?.model).toBe("gpt-5.4-mini");
448 expect(patch.observability?.otlpTracesUrl).toBe("http://localhost:4318/v1/traces");
449 expect(patch.providers?.codex?.binaryPath).toBe("/opt/homebrew/bin/codex");
450 expect(patch.providers?.codex?.homePath).toBe("~/.codex");
451 expect(patch.providers?.codex?.launchArgs).toBe("--strict-config --enable foo");
452 expect(patch.providerInstances?.[ProviderInstanceId.make("codex_personal")]?.driver).toBe(
453 "codex",
454 );
455 expect(patch.providerInstances?.[ProviderInstanceId.make("codex_personal")]?.displayName).toBe(
456 "Codex Personal",
457 );
458 expect(patch.providerInstances?.[ProviderInstanceId.make("codex_personal")]?.config).toEqual({
459 homePath: " ~/.codex-personal ",
460 });
461 });
462
463 it("trims encoded server settings values before validation", () => {
464 const defaultSettings = decodeServerSettings({});
465 const encoded = encodeServerSettings({
466 ...defaultSettings,
467 addProjectBaseDirectory: " ~/Development ",
468 providers: {
469 ...defaultSettings.providers,
470 codex: {
471 ...defaultSettings.providers.codex,
472 binaryPath: " /opt/homebrew/bin/codex ",
473 launchArgs: " --strict-config ",
474 },
475 },
476 });
477
478 expect(encoded.addProjectBaseDirectory).toBe("~/Development");
479 expect(encoded.providers?.codex?.binaryPath).toBe("/opt/homebrew/bin/codex");
480 expect(encoded.providers?.codex?.launchArgs).toBe("--strict-config");
481 });
482});