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

settings.test.ts

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