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