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