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 expect(resolveProjectSettings(DEFAULT_SERVER_SETTINGS, null, null).settings).toBe(
56 DEFAULT_SERVER_SETTINGS,
57 );
58 });
59
60 it("applies overrides per key and reports their source", () => {
61 const settings = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
62 defaultAutoPull: true,
63 sidebarAutoSettleAfterDays: 3,
64 projectSettingsOverrides: {
65 [projectId]: { defaultAutoPull: false, sidebarAutoSettleAfterDays: null },
66 },
67 });
68 const resolved = resolveProjectSettings(settings, projectId);
69 expect(resolved.settings.defaultAutoPull).toBe(false);
70 expect(resolved.settings.sidebarAutoSettleAfterDays).toBeNull();
71 expect(resolved.settings.defaultThreadEnvMode).toBe(settings.defaultThreadEnvMode);
72 expect(resolved.sources.defaultAutoPull).toBe("project");
73 expect(resolved.sources.sidebarAutoSettleAfterDays).toBe("project");
74 expect(resolved.sources.defaultThreadEnvMode).toBe("environment");
75 expect(resolveProjectSettings(settings, otherProjectId).settings.defaultAutoPull).toBe(true);
76 });
77
78 it("keeps the environment text generation model when the override's provider is disabled", () => {
79 const disabledSelection = createModelSelection(ProviderInstanceId.make("claudeAgent"), "opus");
80 const settings = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
81 providerInstances: {
82 [ProviderInstanceId.make("claudeAgent")]: {
83 driver: ProviderDriverKind.make("claudeAgent"),
84 enabled: false,
85 },
86 },
87 projectSettingsOverrides: {
88 [projectId]: { textGenerationModelSelection: disabledSelection },
89 },
90 });
91 const resolved = resolveProjectSettings(settings, projectId);
92 expect(resolved.settings.textGenerationModelSelection).toEqual(
93 settings.textGenerationModelSelection,
94 );
95 expect(resolved.sources.textGenerationModelSelection).toBe("environment");
96 });
97
98 it("honours the aggregate's own fields only until the server has folded them", () => {
99 const aggregateModel = createModelSelection(ProviderInstanceId.make("codex"), "gpt-5.5");
100 const project = {
101 defaultModelSelection: aggregateModel,
102 defaultThreadEnvMode: "local" as const,
103 };
104 const unfolded = resolveProjectSettings(
105 { ...DEFAULT_SERVER_SETTINGS, projectSettingsFolded: false },
106 projectId,
107 project,
108 );
109 expect(unfolded.settings.defaultModelSelection).toEqual(aggregateModel);
110 expect(unfolded.settings.defaultThreadEnvMode).toBe("local");
111 expect(unfolded.sources.defaultModelSelection).toBe("project");
112 const overridden = resolveProjectSettings(
113 {
114 ...DEFAULT_SERVER_SETTINGS,
115 projectSettingsFolded: false,
116 projectSettingsOverrides: { [projectId]: { defaultThreadEnvMode: "worktree" } },
117 },
118 projectId,
119 project,
120 );
121 expect(overridden.settings.defaultThreadEnvMode).toBe("worktree");
122 const folded = resolveProjectSettings(
123 { ...DEFAULT_SERVER_SETTINGS, projectSettingsFolded: true },
124 projectId,
125 project,
126 );
127 expect(folded.settings.defaultModelSelection).toBeNull();
128 expect(folded.sources.defaultModelSelection).toBe("environment");
129 });
130
131 it("keeps the environment default model when the override's provider is disabled", () => {
132 const disabledSelection = createModelSelection(ProviderInstanceId.make("claudeAgent"), "opus");
133 const settings = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
134 providerInstances: {
135 [ProviderInstanceId.make("claudeAgent")]: {
136 driver: ProviderDriverKind.make("claudeAgent"),
137 enabled: false,
138 },
139 },
140 projectSettingsOverrides: { [projectId]: { defaultModelSelection: disabledSelection } },
141 });
142 const resolved = resolveProjectSettings(settings, projectId);
143 expect(resolved.settings.defaultModelSelection).toBeNull();
144 expect(resolved.sources.defaultModelSelection).toBe("environment");
145 });
146
147 it("moves text generation off an instance the project turned off", () => {
148 const codex = ProviderInstanceId.make("codex");
149 const settings = {
150 ...DEFAULT_SERVER_SETTINGS,
151 textGenerationModelSelection: createModelSelection(codex, "gpt-5"),
152 projectSettingsOverrides: {
153 [projectId]: { providerInstances: { [codex]: { enabled: false } } },
154 },
155 };
156 expect(
157 resolveProjectSettings(settings, projectId).settings.textGenerationModelSelection.instanceId,
158 ).toBe(ProviderInstanceId.make("claudeAgent"));
159 expect(
160 resolveProjectSettings(settings, otherProjectId).settings.textGenerationModelSelection,
161 ).toEqual(settings.textGenerationModelSelection);
162 });
163
164 it("falls back to a configured instance when every built-in one is off for the project", () => {
165 const codex = ProviderInstanceId.make("codex");
166 const codexWork = ProviderInstanceId.make("codex_work");
167 const settings = {
168 ...DEFAULT_SERVER_SETTINGS,
169 textGenerationModelSelection: createModelSelection(codex, "gpt-5"),
170 providerInstances: { [codexWork]: { driver: ProviderDriverKind.make("codex"), config: {} } },
171 projectSettingsOverrides: {
172 [projectId]: {
173 providerInstances: {
174 [codex]: { enabled: false },
175 [ProviderInstanceId.make("claudeAgent")]: { enabled: false },
176 },
177 },
178 },
179 };
180 expect(
181 resolveProjectSettings(settings, projectId).settings.textGenerationModelSelection.instanceId,
182 ).toBe(codexWork);
183 });
184});
185
186describe("resolveProjectSettings with a t3.json", () => {
187 it("walks project override, environment value, file, then built-in for file-backed keys", () => {
188 const file = { defaultThreadEnvMode: "worktree" as const };
189 const fromOverride = resolveProjectSettings(
190 {
191 ...DEFAULT_SERVER_SETTINGS,
192 projectSettingsOverrides: { [projectId]: { defaultThreadEnvMode: "local" } },
193 },
194 projectId,
195 null,
196 file,
197 );
198 expect(fromOverride.settings.defaultThreadEnvMode).toBe("local");
199 expect(fromOverride.sources.defaultThreadEnvMode).toBe("project");
200
201 const fromEnvironment = resolveProjectSettings(
202 { ...DEFAULT_SERVER_SETTINGS, defaultThreadEnvMode: "local" },
203 projectId,
204 null,
205 file,
206 );
207 expect(fromEnvironment.settings.defaultThreadEnvMode).toBe("local");
208 expect(fromEnvironment.sources.defaultThreadEnvMode).toBe("environment");
209
210 const fromFile = resolveProjectSettings(DEFAULT_SERVER_SETTINGS, projectId, null, file);
211 expect(fromFile.settings.defaultThreadEnvMode).toBe("worktree");
212 expect(fromFile.sources.defaultThreadEnvMode).toBe("t3.json");
213
214 const builtIn = resolveProjectSettings(DEFAULT_SERVER_SETTINGS, projectId, null, null);
215 expect(builtIn.settings.defaultThreadEnvMode).toBe("local");
216 expect(builtIn.sources.defaultThreadEnvMode).toBe("environment");
217 const nullOverride = resolveProjectSettings(
218 {
219 ...DEFAULT_SERVER_SETTINGS,
220 projectSettingsOverrides: { [projectId]: { defaultThreadEnvMode: null } as never },
221 },
222 projectId,
223 null,
224 file,
225 );
226 expect(nullOverride.settings.defaultThreadEnvMode).toBe("worktree");
227 expect(nullOverride.sources.defaultThreadEnvMode).toBe("t3.json");
228 expect(
229 resolveProjectSettings(DEFAULT_SERVER_SETTINGS, projectId, null, {}).sources
230 .defaultThreadEnvMode,
231 ).toBe("environment");
232 });
233
234 it("resolves one key from the settings tier, then the file, then the built-in", () => {
235 expect(
236 resolveProjectFileBackedSetting("worktreeSubmodules", "none", {
237 worktreeSubmodules: "top-level",
238 }),
239 ).toEqual({ value: "none", source: "environment" });
240 expect(
241 resolveProjectFileBackedSetting("worktreeSubmodules", null, {
242 worktreeSubmodules: "top-level",
243 }),
244 ).toEqual({ value: "top-level", source: "t3.json" });
245 expect(resolveProjectFileBackedSetting("worktreeSubmodules", null, null)).toEqual({
246 value: "recursive",
247 source: "environment",
248 });
249 });
250
251 it("leaves settings untouched when no file is passed", () => {
252 expect(resolveProjectSettings(DEFAULT_SERVER_SETTINGS, projectId).settings).toBe(
253 DEFAULT_SERVER_SETTINGS,
254 );
255 });
256});
257
258describe("resolveNewThreadEnvMode", () => {
259 const worktreeEnvironment = resolveProjectSettings(
260 { ...DEFAULT_SERVER_SETTINGS, defaultThreadEnvMode: "worktree" },
261 projectId,
262 null,
263 null,
264 );
265 const newProject = {
266 projectSettings: worktreeEnvironment,
267 workspaceRoot: "/data/projects/pinball-stats",
268 newProjectsRoot: "/data/projects/",
269 projectHasThreads: false,
270 };
271
272 it("starts a new project's first thread in its checkout", () => {
273 expect(resolveNewThreadEnvMode(newProject)).toBe("local");
274 expect(
275 resolveNewThreadEnvMode({
276 ...newProject,
277 workspaceRoot: "C:\\Data\\Projects\\Pinball-Stats",
278 newProjectsRoot: "c:/data/projects",
279 }),
280 ).toBe("local");
281 });
282
283 it("keeps the configured default once the project has a thread or is not a new project", () => {
284 expect(resolveNewThreadEnvMode({ ...newProject, projectHasThreads: true })).toBe("worktree");
285 expect(resolveNewThreadEnvMode({ ...newProject, workspaceRoot: "/data/projects" })).toBe(
286 "worktree",
287 );
288 expect(
289 resolveNewThreadEnvMode({ ...newProject, workspaceRoot: "/data/projects/a/nested" }),
290 ).toBe("worktree");
291 expect(resolveNewThreadEnvMode({ ...newProject, workspaceRoot: "/data/projects-old/a" })).toBe(
292 "worktree",
293 );
294 expect(resolveNewThreadEnvMode({ ...newProject, newProjectsRoot: undefined })).toBe("worktree");
295 });
296
297 it("lets a project override, or a t3.json the environment defers to, keep worktree mode", () => {
298 for (const projectSettings of [
299 resolveProjectSettings(
300 {
301 ...DEFAULT_SERVER_SETTINGS,
302 projectSettingsOverrides: { [projectId]: { defaultThreadEnvMode: "worktree" } },
303 },
304 projectId,
305 null,
306 null,
307 ),
308 resolveProjectSettings(DEFAULT_SERVER_SETTINGS, projectId, null, {
309 defaultThreadEnvMode: "worktree",
310 }),
311 ]) {
312 expect(resolveNewThreadEnvMode({ ...newProject, projectSettings })).toBe("worktree");
313 }
314 });
315});
316
317describe("projectSettingsOverrides patches", () => {
318 it("replaces a project's entry, removes it with null, and drops empty entries", () => {
319 const first = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
320 projectSettingsOverrides: {
321 [projectId]: { defaultAutoPull: true, enableAgentBrowserAccess: false },
322 [otherProjectId]: { defaultAutoPull: false },
323 },
324 });
325 expect(hasProjectSettingsOverrides(first)).toBe(true);
326 const replaced = applyServerSettingsPatch(first, {
327 projectSettingsOverrides: { [projectId]: { enableAgentBrowserAccess: false } },
328 });
329 expect(replaced.projectSettingsOverrides[projectId]).toEqual({
330 enableAgentBrowserAccess: false,
331 });
332 expect(replaced.projectSettingsOverrides[otherProjectId]).toEqual({ defaultAutoPull: false });
333 const emptied = applyServerSettingsPatch(replaced, {
334 projectSettingsOverrides: { [projectId]: {} },
335 });
336 expect(emptied.projectSettingsOverrides[projectId]).toBeUndefined();
337 const removed = applyServerSettingsPatch(replaced, {
338 projectSettingsOverrides: { [projectId]: null },
339 });
340 expect(removed.projectSettingsOverrides).toEqual({
341 [otherProjectId]: { defaultAutoPull: false },
342 });
343 expect(hasProjectSettingsOverrides(DEFAULT_SERVER_SETTINGS)).toBe(false);
344 });
345
346 it("derives the legacy per-key maps from the generic record", () => {
347 const settings = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
348 projectSettingsOverrides: {
349 [projectId]: { defaultAutoPull: true, enableAgentBrowserAccess: false },
350 [otherProjectId]: { defaultProjectScripts: [] },
351 },
352 });
353 expect(settings.projectAutoPullOverrides).toEqual({ [projectId]: true });
354 expect(settings.projectAgentBrowserAccessOverrides).toEqual({ [projectId]: false });
355 expect(settings.projectScriptOverrides).toEqual({ [otherProjectId]: [] });
356 });
357
358 it("translates legacy per-key patches into the generic record", () => {
359 const written = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
360 projectAutoPullOverrides: { [projectId]: true },
361 projectAgentBrowserAccessOverrides: { [projectId]: false, [otherProjectId]: true },
362 });
363 expect(written.projectSettingsOverrides).toEqual({
364 [projectId]: { defaultAutoPull: true, enableAgentBrowserAccess: false },
365 [otherProjectId]: { enableAgentBrowserAccess: true },
366 });
367 const cleared = applyServerSettingsPatch(written, {
368 projectAgentBrowserAccessOverrides: { [projectId]: null, [otherProjectId]: null },
369 });
370 expect(cleared.projectSettingsOverrides).toEqual({ [projectId]: { defaultAutoPull: true } });
371 });
372
373 it("lets a canonical entry win over a legacy map for the same project", () => {
374 const current = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
375 projectSettingsOverrides: { [projectId]: { defaultAutoPull: true } },
376 });
377 const next = applyServerSettingsPatch(current, {
378 projectSettingsOverrides: { [projectId]: { defaultThreadEnvMode: "local" } },
379 projectAutoPullOverrides: { [projectId]: true, [otherProjectId]: false },
380 });
381 expect(next.projectSettingsOverrides).toEqual({
382 [projectId]: { defaultThreadEnvMode: "local" },
383 [otherProjectId]: { defaultAutoPull: false },
384 });
385 });
386
387 it("builds replacement entries and clears individual keys", () => {
388 const settings = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
389 projectSettingsOverrides: {
390 [projectId]: { defaultAutoPull: true, enableAgentBrowserAccess: false },
391 },
392 });
393 expect(clearProjectSettingsOverrides(settings, projectId, ["defaultAutoPull"])).toEqual({
394 enableAgentBrowserAccess: false,
395 });
396 expect(
397 clearProjectSettingsOverrides(settings, projectId, [
398 "defaultAutoPull",
399 "enableAgentBrowserAccess",
400 ]),
401 ).toBeNull();
402 expect(clearProjectSettingsOverrides(settings, otherProjectId, ["defaultAutoPull"])).toBeNull();
403 expect(withProjectSettingsOverrides(settings, projectId, null)).toEqual({});
404 expect(
405 withProjectSettingsOverrides(settings, otherProjectId, { defaultThreadEnvMode: "worktree" }),
406 ).toEqual({
407 ...settings.projectSettingsOverrides,
408 [otherProjectId]: { defaultThreadEnvMode: "worktree" },
409 });
410 });
411});
412
413describe("resolveWorktreeCleanup", () => {
414 it("inherits machine rules, disables one project and keeps custom rules isolated", () => {
415 const machine = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
416 storageCleanup: {
417 worktreeAfterDays: 8,
418 worktreeSettledAfterDays: 2,
419 worktreeKeepWhen: "any-local-files",
420 worktreeOnDelete: true,
421 logsAfterDays: 3,
422 },
423 });
424 const inherited = resolveWorktreeCleanup(machine, projectId);
425 expect(inherited).toMatchObject({
426 worktreeSettledAfterDays: 2,
427 worktreeKeepWhen: "any-local-files",
428 });
429 const off = applyServerSettingsPatch(machine, {
430 projectSettingsOverrides: {
431 [projectId]: { worktreeCleanup: { mode: "off" } },
432 },
433 });
434 expect(resolveWorktreeCleanup(off, projectId)).toEqual({
435 worktreeAfterDays: null,
436 worktreeSettledAfterDays: null,
437 worktreeOnDelete: false,
438 worktreeOnMerge: false,
439 worktreeUnchanged: false,
440 worktreeKeepWhen: "any-local-files",
441 });
442 expect(resolveWorktreeCleanup(off, otherProjectId)).toEqual(inherited);
443 const custom = applyServerSettingsPatch(off, {
444 projectSettingsOverrides: {
445 [projectId]: {
446 worktreeCleanup: {
447 mode: "custom",
448 rules: { ...inherited, worktreeAfterDays: 15, worktreeSettledAfterDays: 0 },
449 },
450 },
451 },
452 });
453 expect(resolveWorktreeCleanup(custom, projectId).worktreeAfterDays).toBe(15);
454 expect(resolveWorktreeCleanup(custom, projectId).worktreeSettledAfterDays).toBe(0);
455 expect(resolveWorktreeCleanup(custom, otherProjectId)).toEqual(inherited);
456 expect(custom.storageCleanup.logsAfterDays).toBe(3);
457 const reset = applyServerSettingsPatch(custom, {
458 projectSettingsOverrides: {
459 [projectId]: clearProjectSettingsOverrides(custom, projectId, ["worktreeCleanup"]),
460 },
461 });
462 expect(resolveWorktreeCleanup(reset, projectId)).toEqual(inherited);
463 });
464 it("completes partial machine custom rules and preserves them across edits", () => {
465 const initial = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
466 storageCleanup: { worktreeAfterDays: 8, worktreeSettledAfterDays: 0, worktreeOnDelete: true },
467 });
468 const custom = applyServerSettingsPatch(initial, {
469 worktreeCleanup: { mode: "custom", rules: { worktreeOnMerge: true } },
470 });
471 const edited = applyServerSettingsPatch(custom, {
472 worktreeCleanup: { mode: "custom", rules: { worktreeAfterDays: 15 } },
473 });
474 expect(resolveWorktreeCleanup(edited, null)).toEqual({
475 worktreeAfterDays: 15,
476 worktreeSettledAfterDays: 0,
477 worktreeOnDelete: true,
478 worktreeOnMerge: true,
479 worktreeUnchanged: false,
480 worktreeKeepWhen: "uncommitted-changes",
481 });
482 expect(
483 resolveWorktreeCleanup(applyServerSettingsPatch(edited, { worktreeCleanup: null }), null)
484 .worktreeAfterDays,
485 ).toBe(8);
486 });
487});
488
489it("inherits branch naming defaults and applies project overrides independently", () => {
490 const settings = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
491 branchNamingMode: "static",
492 branchNamePrefix: "team/",
493 branchNameInstructions: "Use issue IDs.",
494 projectSettingsOverrides: { [projectId]: { branchNamingMode: "custom" } },
495 });
496 expect(resolveProjectSettings(settings, projectId).settings).toMatchObject({
497 branchNamingMode: "custom",
498 branchNamePrefix: "team/",
499 branchNameInstructions: "Use issue IDs.",
500 });
501 expect(resolveProjectSettings(settings, otherProjectId).settings).toMatchObject({
502 branchNamingMode: "static",
503 branchNamePrefix: "team/",
504 });
505 const cleared = applyServerSettingsPatch(settings, {
506 projectSettingsOverrides: {
507 [projectId]: clearProjectSettingsOverrides(settings, projectId, ["branchNamingMode"]),
508 },
509 });
510 expect(resolveProjectSettings(cleared, projectId).settings.branchNamingMode).toBe("static");
511});