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

settings.test.ts

317 lines12.0 KB

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