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

settings.test.ts

320 lines11.6 KB

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