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

projectSettings.test.ts

403 lines16.5 KB

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