Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2import * as Schema from "effect/Schema";
3
4import { ProviderInstanceId } from "./providerInstance.ts";
5import {
6 ClientSettingsSchema,
7 ClientSettingsPatch,
8 DEFAULT_SERVER_SETTINGS,
9 ServerSettings,
10 ServerSettingsPatch,
11} from "./settings.ts";
12
13const decodeClientSettings = Schema.decodeUnknownSync(ClientSettingsSchema);
14const decodeClientSettingsPatch = Schema.decodeUnknownSync(ClientSettingsPatch);
15const decodeServerSettings = Schema.decodeUnknownSync(ServerSettings);
16const decodeServerSettingsPatch = Schema.decodeUnknownSync(ServerSettingsPatch);
17const encodeServerSettings = Schema.encodeSync(ServerSettings);
18
19describe("ClientSettings word wrap", () => {
20 it("defaults word wrap on", () => {
21 expect(decodeClientSettings({}).wordWrap).toBe(true);
22 });
23
24 it("ignores obsolete wrapping preferences", () => {
25 const decoded = decodeClientSettings({
26 chatWordWrap: false,
27 diffWordWrap: false,
28 });
29
30 expect(decoded.wordWrap).toBe(true);
31 expect(decoded).not.toHaveProperty("chatWordWrap");
32 expect(decoded).not.toHaveProperty("diffWordWrap");
33 });
34});
35
36describe("ClientSettings glass opacity", () => {
37 it("defaults to a readable translucent surface", () => {
38 expect(decodeClientSettings({}).glassOpacity).toBe(80);
39 });
40
41 it.each([39, 101, 72.5])("rejects an invalid glass opacity: %s", (value) => {
42 expect(() => decodeClientSettings({ glassOpacity: value })).toThrow();
43 expect(() => decodeClientSettingsPatch({ glassOpacity: value })).toThrow();
44 });
45
46 it.each([40, 75, 100])("accepts a glass opacity within the supported range: %s", (value) => {
47 expect(decodeClientSettings({ glassOpacity: value }).glassOpacity).toBe(value);
48 expect(decodeClientSettingsPatch({ glassOpacity: value }).glassOpacity).toBe(value);
49 });
50});
51
52describe("ClientSettings environment identification", () => {
53 it("defaults to artwork and accepts each presentation mode", () => {
54 expect(decodeClientSettings({}).environmentIdentificationMode).toBe("artwork");
55
56 for (const mode of ["artwork", "pill", "none"] as const) {
57 expect(
58 decodeClientSettingsPatch({ environmentIdentificationMode: mode })
59 .environmentIdentificationMode,
60 ).toBe(mode);
61 }
62 });
63
64 it("rejects unsupported presentation modes", () => {
65 expect(() => decodeClientSettings({ environmentIdentificationMode: "badge" })).toThrow();
66 expect(() => decodeClientSettingsPatch({ environmentIdentificationMode: "badge" })).toThrow();
67 });
68});
69
70describe("ClientSettings sidebar v2", () => {
71 it("defaults the beta off with a three-day auto-settle threshold", () => {
72 const settings = decodeClientSettings({});
73 expect(settings.sidebarV2Enabled).toBe(false);
74 expect(settings.sidebarAutoSettleAfterDays).toBe(3);
75 });
76
77 it("treats settings written before the beta had a per-channel default as unconfigured", () => {
78 // The stored blob always carries `sidebarV2Enabled`, so only the companion
79 // flag can distinguish "user opted out" from "never touched it".
80 expect(decodeClientSettings({ sidebarV2Enabled: false }).sidebarV2ConfiguredByUser).toBe(false);
81 expect(decodeClientSettings({ sidebarV2Enabled: true }).sidebarV2ConfiguredByUser).toBe(false);
82 });
83
84 it("preserves an explicit beta choice", () => {
85 const settings = decodeClientSettings({
86 sidebarV2Enabled: false,
87 sidebarV2ConfiguredByUser: true,
88 });
89 expect(settings.sidebarV2Enabled).toBe(false);
90 expect(settings.sidebarV2ConfiguredByUser).toBe(true);
91 });
92
93 it("carries an explicit beta opt-out through the patch the beta toggle writes", () => {
94 const patch = decodeClientSettingsPatch({
95 sidebarV2Enabled: false,
96 sidebarV2ConfiguredByUser: true,
97 });
98 expect(patch.sidebarV2Enabled).toBe(false);
99 expect(patch.sidebarV2ConfiguredByUser).toBe(true);
100 });
101
102 it("allows auto-settle by inactivity to be disabled", () => {
103 expect(
104 decodeClientSettings({ sidebarAutoSettleAfterDays: null }).sidebarAutoSettleAfterDays,
105 ).toBeNull();
106 });
107
108 it.each([-1, 0, 91])("rejects an auto-settle threshold outside 1..90: %s", (value) => {
109 expect(() => decodeClientSettings({ sidebarAutoSettleAfterDays: value })).toThrow();
110 expect(() => decodeClientSettingsPatch({ sidebarAutoSettleAfterDays: value })).toThrow();
111 });
112});
113
114describe("ServerSettings.providerInstances (slice-2 invariant)", () => {
115 it("defaults text generation to Luna at low reasoning effort", () => {
116 expect(DEFAULT_SERVER_SETTINGS.textGenerationModelSelection).toEqual({
117 instanceId: ProviderInstanceId.make("codex"),
118 model: "gpt-5.6-luna",
119 options: [{ id: "reasoningEffort", value: "low" }],
120 });
121 });
122
123 it("defaults to an empty record so legacy configs without the key still decode", () => {
124 expect(DEFAULT_SERVER_SETTINGS.providerInstances).toEqual({});
125 });
126
127 it("decodes a fully empty config (legacy on-disk shape) without complaint", () => {
128 const decoded = decodeServerSettings({});
129 expect(decoded.providerInstances).toEqual({});
130 // Legacy `providers` struct is still hydrated with its per-driver defaults
131 // so existing call sites keep working through the migration.
132 expect(decoded.providers.codex.enabled).toBe(true);
133 });
134
135 it("decodes a multi-instance map mixing first-party and fork drivers", () => {
136 const decoded = decodeServerSettings({
137 providerInstances: {
138 codex_personal: {
139 driver: "codex",
140 displayName: "Codex (personal)",
141 config: { homePath: "~/.codex_personal" },
142 },
143 codex_work: {
144 driver: "codex",
145 config: { homePath: "~/.codex_work" },
146 },
147 ollama_local: {
148 driver: "ollama",
149 displayName: "Ollama (local)",
150 config: { endpoint: "http://localhost:11434" },
151 },
152 },
153 });
154 const personalId = ProviderInstanceId.make("codex_personal");
155 const workId = ProviderInstanceId.make("codex_work");
156 const ollamaId = ProviderInstanceId.make("ollama_local");
157
158 expect(decoded.providerInstances[personalId]?.driver).toBe("codex");
159 expect(decoded.providerInstances[workId]?.config).toEqual({ homePath: "~/.codex_work" });
160 // Critical: a config naming a driver this build does not know about
161 // (`ollama` is not in `ProviderDriverKind`) must round-trip without loss.
162 // The runtime handles "driver not installed" — the schema must not.
163 expect(decoded.providerInstances[ollamaId]?.driver).toBe("ollama");
164 expect(decoded.providerInstances[ollamaId]?.config).toEqual({
165 endpoint: "http://localhost:11434",
166 });
167 });
168
169 it("rejects instance keys that violate the slug pattern", () => {
170 expect(() =>
171 decodeServerSettings({
172 providerInstances: { "1bad": { driver: "codex" } },
173 }),
174 ).toThrow();
175 });
176});
177
178describe("ServerSettings worktree defaults", () => {
179 it("defaults start-from-origin on for legacy configs", () => {
180 expect(decodeServerSettings({}).newWorktreesStartFromOrigin).toBe(true);
181 });
182
183 it("accepts start-from-origin updates", () => {
184 expect(
185 decodeServerSettingsPatch({ newWorktreesStartFromOrigin: false }).newWorktreesStartFromOrigin,
186 ).toBe(false);
187 });
188});
189
190describe("ServerSettings.sourceControlWritingStyle", () => {
191 it("defaults all style settings for legacy configs", () => {
192 const settings = decodeServerSettings({});
193
194 expect(settings.sourceControlWritingStyle).toEqual({
195 mode: "repo_conventions",
196 customInstructions: "",
197 followChangeRequestTemplates: true,
198 });
199 expect(settings.sourceControlWriterModelSelection).toBeNull();
200 });
201
202 it("trims partial style updates", () => {
203 const patch = decodeServerSettingsPatch({
204 sourceControlWritingStyle: {
205 mode: "custom",
206 customInstructions: " Prefer concise wording. ",
207 },
208 });
209
210 expect(patch.sourceControlWritingStyle).toEqual({
211 mode: "custom",
212 customInstructions: "Prefer concise wording.",
213 });
214 });
215});
216
217describe("ServerSettingsPatch.providerInstances", () => {
218 it("treats providerInstances as an optional whole-map replacement", () => {
219 const patch = decodeServerSettingsPatch({});
220 expect(patch.providerInstances).toBeUndefined();
221
222 const replacement = decodeServerSettingsPatch({
223 providerInstances: {
224 codex_personal: { driver: "codex", config: { homePath: "~/.codex" } },
225 },
226 });
227 expect(replacement.providerInstances).toBeDefined();
228 expect(replacement.providerInstances?.[ProviderInstanceId.make("codex_personal")]?.driver).toBe(
229 "codex",
230 );
231 });
232
233 it("preserves a fork-defined driver entry through patch decoding", () => {
234 const patch = decodeServerSettingsPatch({
235 providerInstances: {
236 ollama_local: {
237 driver: "ollama",
238 config: { endpoint: "http://localhost:11434" },
239 },
240 },
241 });
242 const ollamaId = ProviderInstanceId.make("ollama_local");
243 expect(patch.providerInstances?.[ollamaId]?.driver).toBe("ollama");
244 });
245});
246
247describe("ServerSettingsPatch string normalization", () => {
248 it("trims string settings while decoding patches", () => {
249 const patch = decodeServerSettingsPatch({
250 addProjectBaseDirectory: " ~/Development ",
251 textGenerationModelSelection: { model: " gpt-5.4-mini " },
252 observability: {
253 otlpTracesUrl: " http://localhost:4318/v1/traces ",
254 },
255 providers: {
256 codex: {
257 binaryPath: " /opt/homebrew/bin/codex ",
258 homePath: " ~/.codex ",
259 launchArgs: " --strict-config --enable foo ",
260 },
261 },
262 providerInstances: {
263 codex_personal: {
264 driver: " codex ",
265 displayName: " Codex Personal ",
266 config: { homePath: " ~/.codex-personal " },
267 },
268 },
269 });
270
271 expect(patch.addProjectBaseDirectory).toBe("~/Development");
272 expect(patch.textGenerationModelSelection?.model).toBe("gpt-5.4-mini");
273 expect(patch.observability?.otlpTracesUrl).toBe("http://localhost:4318/v1/traces");
274 expect(patch.providers?.codex?.binaryPath).toBe("/opt/homebrew/bin/codex");
275 expect(patch.providers?.codex?.homePath).toBe("~/.codex");
276 expect(patch.providers?.codex?.launchArgs).toBe("--strict-config --enable foo");
277 expect(patch.providerInstances?.[ProviderInstanceId.make("codex_personal")]?.driver).toBe(
278 "codex",
279 );
280 expect(patch.providerInstances?.[ProviderInstanceId.make("codex_personal")]?.displayName).toBe(
281 "Codex Personal",
282 );
283 expect(patch.providerInstances?.[ProviderInstanceId.make("codex_personal")]?.config).toEqual({
284 homePath: " ~/.codex-personal ",
285 });
286 });
287
288 it("trims encoded server settings values before validation", () => {
289 const defaultSettings = decodeServerSettings({});
290 const encoded = encodeServerSettings({
291 ...defaultSettings,
292 addProjectBaseDirectory: " ~/Development ",
293 providers: {
294 ...defaultSettings.providers,
295 codex: {
296 ...defaultSettings.providers.codex,
297 binaryPath: " /opt/homebrew/bin/codex ",
298 launchArgs: " --strict-config ",
299 },
300 },
301 });
302
303 expect(encoded.addProjectBaseDirectory).toBe("~/Development");
304 expect(encoded.providers?.codex?.binaryPath).toBe("/opt/homebrew/bin/codex");
305 expect(encoded.providers?.codex?.launchArgs).toBe("--strict-config");
306 });
307});