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

settings.test.ts

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