Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2import * as Schema from "effect/Schema";
3
4import { type CommandId, ProjectId } from "./baseSchemas.ts";
5import { ProviderDriverKind, ProviderInstanceId } from "./providerInstance.ts";
6import {
7 ClientSettingsSchema,
8 ClientSettingsPatch,
9 ClaudeSettings,
10 DEFAULT_SERVER_SETTINGS,
11 globalIdleCompaction,
12 idleCompactionPatch,
13 requiredScopesForProjectMutation,
14 resolveIdleCompaction,
15 resolveProjectProviderInstanceEnabled,
16 resolveProviderInstanceEnabled,
17 ServerSettings,
18 ServerSettingsPatch,
19 withIdleCompaction,
20} from "./settings.ts";
21
22const decodeClientSettings = Schema.decodeUnknownSync(ClientSettingsSchema);
23const decodeClientSettingsPatch = Schema.decodeUnknownSync(ClientSettingsPatch);
24const encodeClientSettings = Schema.encodeSync(ClientSettingsSchema);
25const decodeServerSettings = Schema.decodeUnknownSync(ServerSettings);
26const decodeServerSettingsPatch = Schema.decodeUnknownSync(ServerSettingsPatch);
27const encodeServerSettings = Schema.encodeSync(ServerSettings);
28const decodeClaudeSettings = Schema.decodeUnknownSync(ClaudeSettings);
29
30describe("storage cleanup settings", () => {
31 it("keeps cleanup disabled for existing installations", () => {
32 expect(decodeServerSettings({}).worktreeCleanup).toBeNull();
33 expect(decodeServerSettings({}).storageCleanup).toEqual({
34 worktreeAfterDays: null,
35 worktreeSettledAfterDays: null,
36 worktreeOnMerge: false,
37 worktreeOnDelete: false,
38 worktreeUnchanged: false,
39 worktreeKeepWhen: "uncommitted-changes",
40 browserArtifactsAfterDays: null,
41 logsAfterDays: null,
42 });
43 });
44
45 it("accepts partial custom patches but requires complete stored project rules", () => {
46 expect(
47 decodeServerSettingsPatch({
48 worktreeCleanup: { mode: "custom", rules: { worktreeAfterDays: 8 } },
49 }),
50 ).toEqual({ worktreeCleanup: { mode: "custom", rules: { worktreeAfterDays: 8 } } });
51 expect(() =>
52 decodeServerSettings({
53 projectSettingsOverrides: {
54 project: { worktreeCleanup: { mode: "custom", rules: { worktreeAfterDays: 8 } } },
55 },
56 }),
57 ).toThrow();
58 expect(
59 decodeServerSettings({
60 projectSettingsOverrides: {
61 project: {
62 worktreeCleanup: {
63 mode: "custom",
64 rules: {
65 worktreeAfterDays: null,
66 worktreeOnMerge: false,
67 worktreeOnDelete: false,
68 worktreeUnchanged: false,
69 },
70 },
71 },
72 },
73 }).projectSettingsOverrides[ProjectId.make("project")]?.worktreeCleanup,
74 ).toMatchObject({
75 mode: "custom",
76 rules: { worktreeSettledAfterDays: null, worktreeKeepWhen: "uncommitted-changes" },
77 });
78 });
79
80 it("decodes older custom rules with the default local-file policy", () => {
81 const settings = decodeServerSettings({
82 worktreeCleanup: {
83 mode: "custom",
84 rules: {
85 worktreeAfterDays: 8,
86 worktreeOnMerge: false,
87 worktreeOnDelete: false,
88 worktreeUnchanged: false,
89 },
90 },
91 });
92 expect(settings.worktreeCleanup).toMatchObject({
93 rules: { worktreeKeepWhen: "uncommitted-changes" },
94 });
95 expect(
96 decodeServerSettingsPatch({ storageCleanup: { worktreeKeepWhen: "tracked-changes" } }),
97 ).toEqual({ storageCleanup: { worktreeKeepWhen: "tracked-changes" } });
98 expect(() =>
99 decodeServerSettingsPatch({ storageCleanup: { worktreeKeepWhen: "unknown" } }),
100 ).toThrow();
101 });
102});
103
104describe("ServerSettings usage price overrides", () => {
105 const prices = { inputCostPerMillionTokens: 2, outputCostPerMillionTokens: 8 };
106
107 it("rejects empty model IDs and incomplete input/output pricing", () => {
108 for (const usagePriceOverrides of [
109 { " ": prices },
110 { "example-model": { inputCostPerMillionTokens: 2 } },
111 { "example-model": { outputCostPerMillionTokens: 8 } },
112 ]) {
113 expect(() => decodeServerSettingsPatch({ usagePriceOverrides })).toThrow();
114 }
115 });
116});
117
118describe("custom model settings", () => {
119 const capabilities = {
120 optionDescriptors: [
121 {
122 id: "effort",
123 label: "Reasoning",
124 type: "select",
125 options: [{ id: "high", label: "High", isDefault: true }],
126 },
127 ],
128 };
129
130 it("accepts legacy bare slugs alongside full entries", () => {
131 const decoded = decodeClaudeSettings({
132 customModels: ["bare-slug", { slug: "named", name: "Named", capabilities }],
133 });
134 expect(decoded.customModels).toEqual([
135 "bare-slug",
136 { slug: "named", name: "Named", capabilities },
137 ]);
138 });
139});
140
141describe("resolveIdleCompaction", () => {
142 it("lets a Claude instance override the global default", () => {
143 const settings = decodeServerSettings({
144 idleCompaction: "off",
145 providerInstances: {
146 claude_work: { driver: "claudeAgent", config: { idleCompaction: "onReturn" } },
147 claude_personal: { driver: "claudeAgent" },
148 },
149 });
150 expect(resolveIdleCompaction(settings, ProviderInstanceId.make("claude_work"))).toBe(
151 "onReturn",
152 );
153 expect(resolveIdleCompaction(settings, ProviderInstanceId.make("claude_personal"))).toBe("off");
154 expect(resolveIdleCompaction(settings, ProviderInstanceId.make("claudeAgent"))).toBe("off");
155 });
156
157 it("reads the mode from the legacy switch, in either form servers saved it", () => {
158 expect(globalIdleCompaction(decodeServerSettings({ compactIdleThreads: true }))).toBe(
159 "beforeCacheExpires",
160 );
161 expect(globalIdleCompaction(decodeServerSettings({ compactIdleThreads: "off" }))).toBe("off");
162 expect(globalIdleCompaction(decodeServerSettings({}))).toBe("onReturn");
163 });
164
165 it("keeps the legacy switch a boolean, as shipped clients decode it", () => {
166 const encode = Schema.encodeSync(Schema.toCodecJson(ServerSettings));
167 for (const mode of ["onReturn", "beforeCacheExpires", "off"] as const) {
168 const encoded = encode(withIdleCompaction(DEFAULT_SERVER_SETTINGS, mode)) as {
169 readonly compactIdleThreads: unknown;
170 readonly idleCompaction: unknown;
171 };
172 expect(encoded.compactIdleThreads).toBe(mode === "beforeCacheExpires");
173 expect(encoded.idleCompaction).toBe(mode);
174 }
175 });
176
177 it("patches the field the server reads", () => {
178 expect(
179 idleCompactionPatch({ compactIdleThreads: false, idleCompaction: "onReturn" }, "off"),
180 ).toEqual({
181 idleCompaction: "off",
182 });
183 expect(idleCompactionPatch({ compactIdleThreads: "onReturn" }, "off")).toEqual({
184 compactIdleThreads: "off",
185 });
186 expect(idleCompactionPatch({ compactIdleThreads: false }, "beforeCacheExpires")).toEqual({
187 compactIdleThreads: true,
188 });
189 });
190});
191
192describe("ClientSettings notifications", () => {
193 it("requires opt-in when existing settings omit notification preferences", () => {
194 expect(decodeClientSettings({}).notificationMode).toBe("off");
195 expect(decodeClientSettings({}).inAppNotificationsEnabled).toBe(false);
196 expect(decodeClientSettingsPatch({})).not.toHaveProperty("inAppNotificationsEnabled");
197 expect(decodeClientSettingsPatch({})).not.toHaveProperty("notificationMode");
198 });
199});
200
201describe("ClientSettings quit confirmation", () => {
202 it.each([
203 [true, "hold"],
204 [false, "direct"],
205 ] as const)("migrates the legacy %s value to %s", (legacyValue, mode) => {
206 const settings = decodeClientSettings({ confirmQuit: legacyValue });
207
208 expect(settings.confirmQuit).toBe(mode);
209 expect(encodeClientSettings(settings).confirmQuit).toBe(mode);
210 });
211});
212
213describe("provider enabled defaults", () => {
214 it("resolves instance enabled state with explicit false winning", () => {
215 const grok = ProviderDriverKind.make("grok");
216 const codex = ProviderDriverKind.make("codex");
217 expect(resolveProviderInstanceEnabled({ driver: grok, config: {} })).toBe(false);
218 expect(resolveProviderInstanceEnabled({ driver: codex, config: {} })).toBe(true);
219 expect(
220 resolveProviderInstanceEnabled({ driver: ProviderDriverKind.make("ollama"), config: {} }),
221 ).toBe(true);
222 expect(resolveProviderInstanceEnabled({ driver: grok, enabled: true, config: {} })).toBe(true);
223 expect(resolveProviderInstanceEnabled({ driver: codex, enabled: false, config: {} })).toBe(
224 false,
225 );
226 expect(resolveProviderInstanceEnabled({ driver: grok, config: { enabled: true } })).toBe(true);
227 expect(
228 resolveProviderInstanceEnabled({ driver: grok, enabled: true, config: { enabled: false } }),
229 ).toBe(false);
230 expect(
231 resolveProviderInstanceEnabled({ driver: codex, enabled: false, config: { enabled: true } }),
232 ).toBe(false);
233 });
234});
235
236describe("ServerSettings worktree defaults", () => {
237 it("defaults the thread env mode to inherit and keeps stored values", () => {
238 expect(decodeServerSettings({}).defaultThreadEnvMode).toBeNull();
239 expect(decodeServerSettings({ defaultThreadEnvMode: "worktree" }).defaultThreadEnvMode).toBe(
240 "worktree",
241 );
242 expect(
243 decodeServerSettings({ defaultThreadEnvMode: "remote" }).defaultThreadEnvMode,
244 ).toBeNull();
245 expect(
246 decodeServerSettingsPatch({ defaultThreadEnvMode: null }).defaultThreadEnvMode,
247 ).toBeNull();
248 });
249
250 it("keeps an inherited thread env mode off the wire for older clients", () => {
251 const encode = Schema.encodeSync(ServerSettings);
252 expect("defaultThreadEnvMode" in encode(decodeServerSettings({}))).toBe(false);
253 expect(
254 encode(decodeServerSettings({ defaultThreadEnvMode: "worktree" })).defaultThreadEnvMode,
255 ).toBe("worktree");
256 });
257});
258
259describe("ServerSettings.sourceControlWritingStyle", () => {
260 it.each([
261 { legacy: "", mode: "repo_conventions", customInstructions: "" },
262 { legacy: " \t\n ", mode: "repo_conventions", customInstructions: "" },
263 {
264 legacy: " Prefer concise wording.\nKeep issue references. ",
265 mode: "custom",
266 customInstructions: "Prefer concise wording.\nKeep issue references.",
267 },
268 ])("normalizes legacy writing style $legacy in snapshots only", ({ legacy, ...expected }) => {
269 const style = { ...expected, followChangeRequestTemplates: true };
270 for (const [input, normalized] of [
271 [{ sourceControlWritingStyle: legacy }, { sourceControlWritingStyle: style }],
272 [
273 { projectSettingsOverrides: { project: { sourceControlWritingStyle: legacy } } },
274 { projectSettingsOverrides: { project: { sourceControlWritingStyle: style } } },
275 ],
276 ] as const) {
277 const settings = decodeServerSettings(input);
278 expect(settings).toMatchObject(normalized);
279 expect(encodeServerSettings(settings)).toMatchObject(normalized);
280 expect(() => decodeServerSettingsPatch(input)).toThrow();
281 }
282 });
283});
284
285describe("ServerSettings environment icon", () => {
286 it("decodes a kind from a newer server as null instead of failing the snapshot", () => {
287 expect(decodeServerSettings({ environmentIcon: "toaster" }).environmentIcon).toBeNull();
288 });
289});
290
291const decodeDeviceHostSettings = Schema.decodeSync(ServerSettings);
292
293// `deviceHosts` as it was decoded before EAS hosts existed: an SSH host needs a target.
294const decodeOlderDeviceHostSettings = Schema.decodeUnknownSync(
295 Schema.Struct({
296 deviceHosts: Schema.Array(Schema.Struct({ id: Schema.String, target: Schema.String })),
297 }),
298);
299
300it("keeps EAS hosts out of the SSH host list servers and clients before EAS hosts decode", () => {
301 const settings = decodeDeviceHostSettings({
302 deviceHosts: [{ id: "mini", label: "Mac mini", target: "user@mini" }],
303 easDeviceHosts: [{ kind: "eas", id: "eas", label: "EAS Simulator" }],
304 });
305 const wire = encodeServerSettings(settings);
306 expect(decodeOlderDeviceHostSettings(wire).deviceHosts).toEqual([
307 { id: "mini", target: "user@mini" },
308 ]);
309 expect(settings.easDeviceHosts.map((host) => host.id)).toEqual(["eas"]);
310});
311
312it("validates remote device hosts and rejects ambiguous host ids", () => {
313 const host = { id: "mini", label: "Mac mini", target: "user@mini", port: 2222 };
314 // Hosts saved before EAS hosts existed have no `kind`; they are SSH hosts.
315 expect(decodeDeviceHostSettings({ deviceHosts: [host] }).deviceHosts).toEqual([
316 { kind: "ssh", ...host },
317 ]);
318 expect(() => decodeDeviceHostSettings({ deviceHosts: [host, host] })).toThrow();
319 expect(() => decodeDeviceHostSettings({ deviceHosts: [{ ...host, id: "local" }] })).toThrow();
320 expect(() =>
321 decodeDeviceHostSettings({ deviceHosts: [{ ...host, target: "-oProxyCommand=bad" }] }),
322 ).toThrow();
323 expect(() => decodeDeviceHostSettings({ deviceHosts: [{ ...host, port: 0 }] })).toThrow();
324});
325
326describe("resolveProjectProviderInstanceEnabled", () => {
327 const projectId = "project-a" as ProjectId;
328 const instanceId = ProviderInstanceId.make("codex_work");
329
330 it("lets a project disable an instance the machine enabled", () => {
331 const settings: ServerSettings = {
332 ...DEFAULT_SERVER_SETTINGS,
333 providerInstances: {
334 [instanceId]: { driver: ProviderDriverKind.make("codex"), enabled: true },
335 },
336 projectSettingsOverrides: {
337 [projectId]: { providerInstances: { [instanceId]: { enabled: false } } },
338 },
339 };
340 expect(resolveProjectProviderInstanceEnabled(settings, projectId, instanceId)).toBe(false);
341 });
342});
343
344describe("requiredScopesForProjectMutation", () => {
345 it("adds the settings scope only when a mutation carries scripts", () => {
346 const projectId = "project-1" as ProjectId;
347 const commandId = "command-1" as CommandId;
348 expect(
349 requiredScopesForProjectMutation({ type: "project.update", commandId, projectId }),
350 ).toEqual(["orchestration:operate"]);
351 expect(
352 requiredScopesForProjectMutation({
353 type: "project.update",
354 commandId,
355 projectId,
356 scripts: [],
357 }),
358 ).toEqual(["orchestration:operate", "settings:write"]);
359 });
360});
361
362describe("server model preferences", () => {
363 it("distinguishes unmigrated lists from deliberately cleared lists", () => {
364 expect(decodeServerSettings({})).toMatchObject({
365 favorites: null,
366 providerModelPreferences: null,
367 });
368 const input = { favorites: [], providerModelPreferences: {} };
369 expect(encodeServerSettings(decodeServerSettings(input))).toMatchObject(input);
370 expect(decodeServerSettingsPatch(input)).toEqual(input);
371 });
372});