Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

settings.test.ts

482 lines18.3 KB

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