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