Open History to see the last commit
1import {
2 DEFAULT_SERVER_SETTINGS,
3 PROJECT_SCOPED_SERVER_SETTING_KEYS,
4 ProjectId,
5 ProviderDriverKind,
6 ProviderInstanceId,
7} from "@t3tools/contracts";
8import { describe, expect, it } from "vite-plus/test";
9import { createModelSelection } from "./model.ts";
10import {
11 clearProjectSettingsOverrides,
12 hasProjectSettingsOverrides,
13 resolveNewThreadEnvMode,
14 resolveProjectFileBackedSetting,
15 resolveProjectSettings,
16 resolveWorktreeCleanup,
17 withProjectSettingsOverrides,
18} from "./projectSettings.ts";
19import { applyServerSettingsPatch } from "./serverSettings.ts";
20
21const projectId = ProjectId.make("project-a");
22const otherProjectId = ProjectId.make("project-b");
23
24describe("resolveProjectSettings", () => {
25 it("inherits every scopable key when the project has no overrides", () => {
26 const resolved = resolveProjectSettings(DEFAULT_SERVER_SETTINGS, projectId);
27 expect(resolved.settings).toBe(DEFAULT_SERVER_SETTINGS);
28 for (const key of PROJECT_SCOPED_SERVER_SETTING_KEYS) {
29 expect(resolved.sources[key]).toBe("environment");
30 }
31 expect(resolveProjectSettings(DEFAULT_SERVER_SETTINGS, null).settings).toBe(
32 DEFAULT_SERVER_SETTINGS,
33 );
34 });
35
36 it("ignores an override left undefined by a forward-compatible decode", () => {
37 const resolved = resolveProjectSettings(
38 {
39 ...DEFAULT_SERVER_SETTINGS,
40 defaultRuntimeMode: "full-access",
41 projectSettingsOverrides: { [projectId]: { defaultRuntimeMode: undefined } as never },
42 },
43 projectId,
44 );
45 expect(resolved.settings.defaultRuntimeMode).toBe("full-access");
46 expect(resolved.sources.defaultRuntimeMode).toBe("environment");
47 expect(
48 hasProjectSettingsOverrides({
49 projectSettingsOverrides: { [projectId]: { defaultRuntimeMode: undefined } as never },
50 }),
51 ).toBe(false);
52 });
53
54 it("treats a null project like an absent one before the shell snapshot arrives", () => {
55 // The mobile new-task flow resolves settings while its selected project is
56 // still null; reading the aggregate's legacy fields off null crashed launch.
57 expect(resolveProjectSettings(DEFAULT_SERVER_SETTINGS, null, null).settings).toBe(
58 DEFAULT_SERVER_SETTINGS,
59 );
60 });
61
62 it("applies overrides per key and reports their source", () => {
63 const settings = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
64 defaultAutoPull: true,
65 sidebarAutoSettleAfterDays: 3,
66 projectSettingsOverrides: {
67 [projectId]: { defaultAutoPull: false, sidebarAutoSettleAfterDays: null },
68 },
69 });
70 const resolved = resolveProjectSettings(settings, projectId);
71 expect(resolved.settings.defaultAutoPull).toBe(false);
72 expect(resolved.settings.sidebarAutoSettleAfterDays).toBeNull();
73 expect(resolved.settings.defaultThreadEnvMode).toBe(settings.defaultThreadEnvMode);
74 expect(resolved.sources.defaultAutoPull).toBe("project");
75 expect(resolved.sources.sidebarAutoSettleAfterDays).toBe("project");
76 expect(resolved.sources.defaultThreadEnvMode).toBe("environment");
77 expect(resolveProjectSettings(settings, otherProjectId).settings.defaultAutoPull).toBe(true);
78 });
79
80 it("keeps the environment text generation model when the override's provider is disabled", () => {
81 const disabledSelection = createModelSelection(ProviderInstanceId.make("claudeAgent"), "opus");
82 const settings = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
83 providerInstances: {
84 [ProviderInstanceId.make("claudeAgent")]: {
85 driver: ProviderDriverKind.make("claudeAgent"),
86 enabled: false,
87 },
88 },
89 projectSettingsOverrides: {
90 [projectId]: { textGenerationModelSelection: disabledSelection },
91 },
92 });
93 const resolved = resolveProjectSettings(settings, projectId);
94 expect(resolved.settings.textGenerationModelSelection).toEqual(
95 settings.textGenerationModelSelection,
96 );
97 expect(resolved.sources.textGenerationModelSelection).toBe("environment");
98 });
99
100 it("honours the aggregate's own fields only until the server has folded them", () => {
101 const aggregateModel = createModelSelection(ProviderInstanceId.make("codex"), "gpt-5.5");
102 const project = {
103 defaultModelSelection: aggregateModel,
104 defaultThreadEnvMode: "local" as const,
105 };
106 const unfolded = resolveProjectSettings(
107 { ...DEFAULT_SERVER_SETTINGS, projectSettingsFolded: false },
108 projectId,
109 project,
110 );
111 expect(unfolded.settings.defaultModelSelection).toEqual(aggregateModel);
112 expect(unfolded.settings.defaultThreadEnvMode).toBe("local");
113 expect(unfolded.sources.defaultModelSelection).toBe("project");
114 // A stored override still beats the aggregate before the fold.
115 const overridden = resolveProjectSettings(
116 {
117 ...DEFAULT_SERVER_SETTINGS,
118 projectSettingsFolded: false,
119 projectSettingsOverrides: { [projectId]: { defaultThreadEnvMode: "worktree" } },
120 },
121 projectId,
122 project,
123 );
124 expect(overridden.settings.defaultThreadEnvMode).toBe("worktree");
125 // After the fold a reset in the record wins over the stale aggregate.
126 const folded = resolveProjectSettings(
127 { ...DEFAULT_SERVER_SETTINGS, projectSettingsFolded: true },
128 projectId,
129 project,
130 );
131 expect(folded.settings.defaultModelSelection).toBeNull();
132 expect(folded.sources.defaultModelSelection).toBe("environment");
133 });
134
135 it("keeps the environment default model when the override's provider is disabled", () => {
136 const disabledSelection = createModelSelection(ProviderInstanceId.make("claudeAgent"), "opus");
137 const settings = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
138 providerInstances: {
139 [ProviderInstanceId.make("claudeAgent")]: {
140 driver: ProviderDriverKind.make("claudeAgent"),
141 enabled: false,
142 },
143 },
144 projectSettingsOverrides: { [projectId]: { defaultModelSelection: disabledSelection } },
145 });
146 const resolved = resolveProjectSettings(settings, projectId);
147 expect(resolved.settings.defaultModelSelection).toBeNull();
148 expect(resolved.sources.defaultModelSelection).toBe("environment");
149 });
150});
151
152describe("resolveProjectSettings with a t3.json", () => {
153 it("walks project override, environment value, file, then built-in for file-backed keys", () => {
154 const file = { defaultThreadEnvMode: "worktree" as const };
155 const fromOverride = resolveProjectSettings(
156 {
157 ...DEFAULT_SERVER_SETTINGS,
158 projectSettingsOverrides: { [projectId]: { defaultThreadEnvMode: "local" } },
159 },
160 projectId,
161 null,
162 file,
163 );
164 expect(fromOverride.settings.defaultThreadEnvMode).toBe("local");
165 expect(fromOverride.sources.defaultThreadEnvMode).toBe("project");
166
167 const fromEnvironment = resolveProjectSettings(
168 { ...DEFAULT_SERVER_SETTINGS, defaultThreadEnvMode: "local" },
169 projectId,
170 null,
171 file,
172 );
173 expect(fromEnvironment.settings.defaultThreadEnvMode).toBe("local");
174 expect(fromEnvironment.sources.defaultThreadEnvMode).toBe("environment");
175
176 const fromFile = resolveProjectSettings(DEFAULT_SERVER_SETTINGS, projectId, null, file);
177 expect(fromFile.settings.defaultThreadEnvMode).toBe("worktree");
178 expect(fromFile.sources.defaultThreadEnvMode).toBe("t3.json");
179
180 const builtIn = resolveProjectSettings(DEFAULT_SERVER_SETTINGS, projectId, null, null);
181 expect(builtIn.settings.defaultThreadEnvMode).toBe("local");
182 expect(builtIn.sources.defaultThreadEnvMode).toBe("environment");
183 // A stored null override defers like an unset one and is not reported
184 // as the project's value.
185 const nullOverride = resolveProjectSettings(
186 {
187 ...DEFAULT_SERVER_SETTINGS,
188 projectSettingsOverrides: { [projectId]: { defaultThreadEnvMode: null } as never },
189 },
190 projectId,
191 null,
192 file,
193 );
194 expect(nullOverride.settings.defaultThreadEnvMode).toBe("worktree");
195 expect(nullOverride.sources.defaultThreadEnvMode).toBe("t3.json");
196 // A file that does not mention the key leaves the source alone too.
197 expect(
198 resolveProjectSettings(DEFAULT_SERVER_SETTINGS, projectId, null, {}).sources
199 .defaultThreadEnvMode,
200 ).toBe("environment");
201 });
202
203 it("resolves one key from the settings tier, then the file, then the built-in", () => {
204 expect(
205 resolveProjectFileBackedSetting("worktreeSubmodules", "none", {
206 worktreeSubmodules: "top-level",
207 }),
208 ).toEqual({ value: "none", source: "environment" });
209 expect(
210 resolveProjectFileBackedSetting("worktreeSubmodules", null, {
211 worktreeSubmodules: "top-level",
212 }),
213 ).toEqual({ value: "top-level", source: "t3.json" });
214 expect(resolveProjectFileBackedSetting("worktreeSubmodules", null, null)).toEqual({
215 value: "recursive",
216 source: "environment",
217 });
218 });
219
220 it("leaves settings untouched when no file is passed", () => {
221 expect(resolveProjectSettings(DEFAULT_SERVER_SETTINGS, projectId).settings).toBe(
222 DEFAULT_SERVER_SETTINGS,
223 );
224 });
225});
226
227describe("resolveNewThreadEnvMode", () => {
228 const worktreeEnvironment = resolveProjectSettings(
229 { ...DEFAULT_SERVER_SETTINGS, defaultThreadEnvMode: "worktree" },
230 projectId,
231 null,
232 null,
233 );
234 const newProject = {
235 projectSettings: worktreeEnvironment,
236 workspaceRoot: "/data/projects/pinball-stats",
237 newProjectsRoot: "/data/projects/",
238 projectHasThreads: false,
239 };
240
241 it("starts a new project's first thread in its checkout", () => {
242 expect(resolveNewThreadEnvMode(newProject)).toBe("local");
243 expect(
244 resolveNewThreadEnvMode({
245 ...newProject,
246 workspaceRoot: "C:\\Data\\Projects\\Pinball-Stats",
247 newProjectsRoot: "c:/data/projects",
248 }),
249 ).toBe("local");
250 });
251
252 it("keeps the configured default once the project has a thread or is not a new project", () => {
253 expect(resolveNewThreadEnvMode({ ...newProject, projectHasThreads: true })).toBe("worktree");
254 expect(resolveNewThreadEnvMode({ ...newProject, workspaceRoot: "/data/projects" })).toBe(
255 "worktree",
256 );
257 expect(
258 resolveNewThreadEnvMode({ ...newProject, workspaceRoot: "/data/projects/a/nested" }),
259 ).toBe("worktree");
260 expect(resolveNewThreadEnvMode({ ...newProject, workspaceRoot: "/data/projects-old/a" })).toBe(
261 "worktree",
262 );
263 expect(resolveNewThreadEnvMode({ ...newProject, newProjectsRoot: undefined })).toBe("worktree");
264 });
265
266 it("lets a project override, or a t3.json the environment defers to, keep worktree mode", () => {
267 for (const projectSettings of [
268 resolveProjectSettings(
269 {
270 ...DEFAULT_SERVER_SETTINGS,
271 projectSettingsOverrides: { [projectId]: { defaultThreadEnvMode: "worktree" } },
272 },
273 projectId,
274 null,
275 null,
276 ),
277 resolveProjectSettings(DEFAULT_SERVER_SETTINGS, projectId, null, {
278 defaultThreadEnvMode: "worktree",
279 }),
280 ]) {
281 expect(resolveNewThreadEnvMode({ ...newProject, projectSettings })).toBe("worktree");
282 }
283 });
284});
285
286describe("projectSettingsOverrides patches", () => {
287 it("replaces a project's entry, removes it with null, and drops empty entries", () => {
288 const first = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
289 projectSettingsOverrides: {
290 [projectId]: { defaultAutoPull: true, enableAgentBrowserAccess: false },
291 [otherProjectId]: { defaultAutoPull: false },
292 },
293 });
294 expect(hasProjectSettingsOverrides(first)).toBe(true);
295 const replaced = applyServerSettingsPatch(first, {
296 projectSettingsOverrides: { [projectId]: { enableAgentBrowserAccess: false } },
297 });
298 expect(replaced.projectSettingsOverrides[projectId]).toEqual({
299 enableAgentBrowserAccess: false,
300 });
301 expect(replaced.projectSettingsOverrides[otherProjectId]).toEqual({ defaultAutoPull: false });
302 const emptied = applyServerSettingsPatch(replaced, {
303 projectSettingsOverrides: { [projectId]: {} },
304 });
305 expect(emptied.projectSettingsOverrides[projectId]).toBeUndefined();
306 const removed = applyServerSettingsPatch(replaced, {
307 projectSettingsOverrides: { [projectId]: null },
308 });
309 expect(removed.projectSettingsOverrides).toEqual({
310 [otherProjectId]: { defaultAutoPull: false },
311 });
312 expect(hasProjectSettingsOverrides(DEFAULT_SERVER_SETTINGS)).toBe(false);
313 });
314
315 it("derives the legacy per-key maps from the generic record", () => {
316 const settings = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
317 projectSettingsOverrides: {
318 [projectId]: { defaultAutoPull: true, enableAgentBrowserAccess: false },
319 [otherProjectId]: { defaultProjectScripts: [] },
320 },
321 });
322 expect(settings.projectAutoPullOverrides).toEqual({ [projectId]: true });
323 expect(settings.projectAgentBrowserAccessOverrides).toEqual({ [projectId]: false });
324 expect(settings.projectScriptOverrides).toEqual({ [otherProjectId]: [] });
325 });
326
327 it("translates legacy per-key patches into the generic record", () => {
328 const written = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
329 projectAutoPullOverrides: { [projectId]: true },
330 projectAgentBrowserAccessOverrides: { [projectId]: false, [otherProjectId]: true },
331 });
332 expect(written.projectSettingsOverrides).toEqual({
333 [projectId]: { defaultAutoPull: true, enableAgentBrowserAccess: false },
334 [otherProjectId]: { enableAgentBrowserAccess: true },
335 });
336 const cleared = applyServerSettingsPatch(written, {
337 projectAgentBrowserAccessOverrides: { [projectId]: null, [otherProjectId]: null },
338 });
339 expect(cleared.projectSettingsOverrides).toEqual({ [projectId]: { defaultAutoPull: true } });
340 });
341
342 it("lets a canonical entry win over a legacy map for the same project", () => {
343 const current = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
344 projectSettingsOverrides: { [projectId]: { defaultAutoPull: true } },
345 });
346 // The canonical entry omits defaultAutoPull to clear it; the stale legacy
347 // map in the same patch must not put it back.
348 const next = applyServerSettingsPatch(current, {
349 projectSettingsOverrides: { [projectId]: { defaultThreadEnvMode: "local" } },
350 projectAutoPullOverrides: { [projectId]: true, [otherProjectId]: false },
351 });
352 expect(next.projectSettingsOverrides).toEqual({
353 [projectId]: { defaultThreadEnvMode: "local" },
354 [otherProjectId]: { defaultAutoPull: false },
355 });
356 });
357
358 it("builds replacement entries and clears individual keys", () => {
359 const settings = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
360 projectSettingsOverrides: {
361 [projectId]: { defaultAutoPull: true, enableAgentBrowserAccess: false },
362 },
363 });
364 expect(clearProjectSettingsOverrides(settings, projectId, ["defaultAutoPull"])).toEqual({
365 enableAgentBrowserAccess: false,
366 });
367 expect(
368 clearProjectSettingsOverrides(settings, projectId, [
369 "defaultAutoPull",
370 "enableAgentBrowserAccess",
371 ]),
372 ).toBeNull();
373 expect(clearProjectSettingsOverrides(settings, otherProjectId, ["defaultAutoPull"])).toBeNull();
374 expect(withProjectSettingsOverrides(settings, projectId, null)).toEqual({});
375 expect(
376 withProjectSettingsOverrides(settings, otherProjectId, { defaultThreadEnvMode: "worktree" }),
377 ).toEqual({
378 ...settings.projectSettingsOverrides,
379 [otherProjectId]: { defaultThreadEnvMode: "worktree" },
380 });
381 });
382});
383
384describe("resolveWorktreeCleanup", () => {
385 it("inherits machine rules, disables one project and keeps custom rules isolated", () => {
386 const machine = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
387 storageCleanup: { worktreeAfterDays: 8, worktreeOnDelete: true, logsAfterDays: 3 },
388 });
389 const inherited = resolveWorktreeCleanup(machine, projectId);
390 const off = applyServerSettingsPatch(machine, {
391 projectSettingsOverrides: {
392 [projectId]: { worktreeCleanup: { mode: "off" } },
393 },
394 });
395 expect(resolveWorktreeCleanup(off, projectId)).toEqual({
396 worktreeAfterDays: null,
397 worktreeOnDelete: false,
398 worktreeOnMerge: false,
399 worktreeUnchanged: false,
400 worktreeKeepWhen: "uncommitted-changes",
401 });
402 expect(resolveWorktreeCleanup(off, otherProjectId)).toEqual(inherited);
403 const custom = applyServerSettingsPatch(off, {
404 projectSettingsOverrides: {
405 [projectId]: {
406 worktreeCleanup: { mode: "custom", rules: { ...inherited, worktreeAfterDays: 15 } },
407 },
408 },
409 });
410 expect(resolveWorktreeCleanup(custom, projectId).worktreeAfterDays).toBe(15);
411 expect(custom.storageCleanup.logsAfterDays).toBe(3);
412 const reset = applyServerSettingsPatch(custom, {
413 projectSettingsOverrides: {
414 [projectId]: clearProjectSettingsOverrides(custom, projectId, ["worktreeCleanup"]),
415 },
416 });
417 expect(resolveWorktreeCleanup(reset, projectId)).toEqual(inherited);
418 });
419 it("completes partial machine custom rules and preserves them across edits", () => {
420 const initial = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
421 storageCleanup: { worktreeAfterDays: 8, worktreeOnDelete: true },
422 });
423 const custom = applyServerSettingsPatch(initial, {
424 worktreeCleanup: { mode: "custom", rules: { worktreeOnMerge: true } },
425 });
426 const edited = applyServerSettingsPatch(custom, {
427 worktreeCleanup: { mode: "custom", rules: { worktreeAfterDays: 15 } },
428 });
429 expect(resolveWorktreeCleanup(edited, null)).toEqual({
430 worktreeAfterDays: 15,
431 worktreeOnDelete: true,
432 worktreeOnMerge: true,
433 worktreeUnchanged: false,
434 worktreeKeepWhen: "uncommitted-changes",
435 });
436 expect(
437 resolveWorktreeCleanup(applyServerSettingsPatch(edited, { worktreeCleanup: null }), null)
438 .worktreeAfterDays,
439 ).toBe(8);
440 });
441});
442
443it("inherits branch naming defaults and applies project overrides independently", () => {
444 const settings = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
445 branchNamingMode: "static",
446 branchNamePrefix: "team/",
447 branchNameInstructions: "Use issue IDs.",
448 projectSettingsOverrides: { [projectId]: { branchNamingMode: "custom" } },
449 });
450 expect(resolveProjectSettings(settings, projectId).settings).toMatchObject({
451 branchNamingMode: "custom",
452 branchNamePrefix: "team/",
453 branchNameInstructions: "Use issue IDs.",
454 });
455 expect(resolveProjectSettings(settings, otherProjectId).settings).toMatchObject({
456 branchNamingMode: "static",
457 branchNamePrefix: "team/",
458 });
459 const cleared = applyServerSettingsPatch(settings, {
460 projectSettingsOverrides: {
461 [projectId]: clearProjectSettingsOverrides(settings, projectId, ["branchNamingMode"]),
462 },
463 });
464 expect(resolveProjectSettings(cleared, projectId).settings.branchNamingMode).toBe("static");
465});