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