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