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