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

projectSettings.test.ts

274 lines11.7 KB

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 resolveProjectSettings,
13 resolveWorktreeCleanup,
14 withProjectSettingsOverrides,
15} from "./projectSettings.ts";
16import { applyServerSettingsPatch } from "./serverSettings.ts";
17​
18const projectId = ProjectId.make("project-a");
19const otherProjectId = ProjectId.make("project-b");
20​
21describe("resolveProjectSettings", () => {
22 it("inherits every scopable key when the project has no overrides", () => {
23 const resolved = resolveProjectSettings(DEFAULT_SERVER_SETTINGS, projectId);
24 expect(resolved.settings).toBe(DEFAULT_SERVER_SETTINGS);
25 for (const key of PROJECT_SCOPED_SERVER_SETTING_KEYS) {
26 expect(resolved.sources[key]).toBe("environment");
27 }
28 expect(resolveProjectSettings(DEFAULT_SERVER_SETTINGS, null).settings).toBe(
29 DEFAULT_SERVER_SETTINGS,
30 );
31 });
32​
33 it("treats a null project like an absent one before the shell snapshot arrives", () => {
34 // The mobile new-task flow resolves settings while its selected project is
35 // still null; reading the aggregate's legacy fields off null crashed launch.
36 expect(resolveProjectSettings(DEFAULT_SERVER_SETTINGS, null, null).settings).toBe(
37 DEFAULT_SERVER_SETTINGS,
38 );
39 });
40​
41 it("applies overrides per key and reports their source", () => {
42 const settings = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
43 defaultAutoPull: true,
44 sidebarAutoSettleAfterDays: 3,
45 projectSettingsOverrides: {
46 [projectId]: { defaultAutoPull: false, sidebarAutoSettleAfterDays: null },
47 },
48 });
49 const resolved = resolveProjectSettings(settings, projectId);
50 expect(resolved.settings.defaultAutoPull).toBe(false);
51 expect(resolved.settings.sidebarAutoSettleAfterDays).toBeNull();
52 expect(resolved.settings.defaultThreadEnvMode).toBe(settings.defaultThreadEnvMode);
53 expect(resolved.sources.defaultAutoPull).toBe("project");
54 expect(resolved.sources.sidebarAutoSettleAfterDays).toBe("project");
55 expect(resolved.sources.defaultThreadEnvMode).toBe("environment");
56 expect(resolveProjectSettings(settings, otherProjectId).settings.defaultAutoPull).toBe(true);
57 });
58​
59 it("keeps the environment text generation model when the override's provider is disabled", () => {
60 const disabledSelection = createModelSelection(ProviderInstanceId.make("claudeAgent"), "opus");
61 const settings = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
62 providers: { claudeAgent: { enabled: false } },
63 projectSettingsOverrides: {
64 [projectId]: { textGenerationModelSelection: disabledSelection },
65 },
66 });
67 const resolved = resolveProjectSettings(settings, projectId);
68 expect(resolved.settings.textGenerationModelSelection).toEqual(
69 settings.textGenerationModelSelection,
70 );
71 expect(resolved.sources.textGenerationModelSelection).toBe("environment");
72 });
73​
74 it("honours the aggregate's own fields only until the server has folded them", () => {
75 const aggregateModel = createModelSelection(ProviderInstanceId.make("codex"), "gpt-5.5");
76 const project = {
77 defaultModelSelection: aggregateModel,
78 defaultThreadEnvMode: "local" as const,
79 };
80 const unfolded = resolveProjectSettings(
81 { ...DEFAULT_SERVER_SETTINGS, projectSettingsFolded: false },
82 projectId,
83 project,
84 );
85 expect(unfolded.settings.defaultModelSelection).toEqual(aggregateModel);
86 expect(unfolded.settings.defaultThreadEnvMode).toBe("local");
87 expect(unfolded.sources.defaultModelSelection).toBe("project");
88 // A stored override still beats the aggregate before the fold.
89 const overridden = resolveProjectSettings(
90 {
91 ...DEFAULT_SERVER_SETTINGS,
92 projectSettingsFolded: false,
93 projectSettingsOverrides: { [projectId]: { defaultThreadEnvMode: "worktree" } },
94 },
95 projectId,
96 project,
97 );
98 expect(overridden.settings.defaultThreadEnvMode).toBe("worktree");
99 // After the fold a reset in the record wins over the stale aggregate.
100 const folded = resolveProjectSettings(
101 { ...DEFAULT_SERVER_SETTINGS, projectSettingsFolded: true },
102 projectId,
103 project,
104 );
105 expect(folded.settings.defaultModelSelection).toBeNull();
106 expect(folded.sources.defaultModelSelection).toBe("environment");
107 });
108​
109 it("keeps the environment default model when the override's provider is disabled", () => {
110 const disabledSelection = createModelSelection(ProviderInstanceId.make("claudeAgent"), "opus");
111 const settings = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
112 providers: { claudeAgent: { enabled: false } },
113 projectSettingsOverrides: { [projectId]: { defaultModelSelection: disabledSelection } },
114 });
115 const resolved = resolveProjectSettings(settings, projectId);
116 expect(resolved.settings.defaultModelSelection).toBeNull();
117 expect(resolved.sources.defaultModelSelection).toBe("environment");
118 });
119});
120​
121describe("projectSettingsOverrides patches", () => {
122 it("replaces a project's entry, removes it with null, and drops empty entries", () => {
123 const first = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
124 projectSettingsOverrides: {
125 [projectId]: { defaultAutoPull: true, enableAgentBrowserAccess: false },
126 [otherProjectId]: { defaultAutoPull: false },
127 },
128 });
129 expect(hasProjectSettingsOverrides(first)).toBe(true);
130 const replaced = applyServerSettingsPatch(first, {
131 projectSettingsOverrides: { [projectId]: { enableAgentBrowserAccess: false } },
132 });
133 expect(replaced.projectSettingsOverrides[projectId]).toEqual({
134 enableAgentBrowserAccess: false,
135 });
136 expect(replaced.projectSettingsOverrides[otherProjectId]).toEqual({ defaultAutoPull: false });
137 const emptied = applyServerSettingsPatch(replaced, {
138 projectSettingsOverrides: { [projectId]: {} },
139 });
140 expect(emptied.projectSettingsOverrides[projectId]).toBeUndefined();
141 const removed = applyServerSettingsPatch(replaced, {
142 projectSettingsOverrides: { [projectId]: null },
143 });
144 expect(removed.projectSettingsOverrides).toEqual({
145 [otherProjectId]: { defaultAutoPull: false },
146 });
147 expect(hasProjectSettingsOverrides(DEFAULT_SERVER_SETTINGS)).toBe(false);
148 });
149​
150 it("derives the legacy per-key maps from the generic record", () => {
151 const settings = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
152 projectSettingsOverrides: {
153 [projectId]: { defaultAutoPull: true, enableAgentBrowserAccess: false },
154 [otherProjectId]: { defaultProjectScripts: [] },
155 },
156 });
157 expect(settings.projectAutoPullOverrides).toEqual({ [projectId]: true });
158 expect(settings.projectAgentBrowserAccessOverrides).toEqual({ [projectId]: false });
159 expect(settings.projectScriptOverrides).toEqual({ [otherProjectId]: [] });
160 });
161​
162 it("translates legacy per-key patches into the generic record", () => {
163 const written = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
164 projectAutoPullOverrides: { [projectId]: true },
165 projectAgentBrowserAccessOverrides: { [projectId]: false, [otherProjectId]: true },
166 });
167 expect(written.projectSettingsOverrides).toEqual({
168 [projectId]: { defaultAutoPull: true, enableAgentBrowserAccess: false },
169 [otherProjectId]: { enableAgentBrowserAccess: true },
170 });
171 const cleared = applyServerSettingsPatch(written, {
172 projectAgentBrowserAccessOverrides: { [projectId]: null, [otherProjectId]: null },
173 });
174 expect(cleared.projectSettingsOverrides).toEqual({ [projectId]: { defaultAutoPull: true } });
175 });
176​
177 it("lets a canonical entry win over a legacy map for the same project", () => {
178 const current = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
179 projectSettingsOverrides: { [projectId]: { defaultAutoPull: true } },
180 });
181 // The canonical entry omits defaultAutoPull to clear it; the stale legacy
182 // map in the same patch must not put it back.
183 const next = applyServerSettingsPatch(current, {
184 projectSettingsOverrides: { [projectId]: { defaultThreadEnvMode: "local" } },
185 projectAutoPullOverrides: { [projectId]: true, [otherProjectId]: false },
186 });
187 expect(next.projectSettingsOverrides).toEqual({
188 [projectId]: { defaultThreadEnvMode: "local" },
189 [otherProjectId]: { defaultAutoPull: false },
190 });
191 });
192​
193 it("builds replacement entries and clears individual keys", () => {
194 const settings = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
195 projectSettingsOverrides: {
196 [projectId]: { defaultAutoPull: true, enableAgentBrowserAccess: false },
197 },
198 });
199 expect(clearProjectSettingsOverrides(settings, projectId, ["defaultAutoPull"])).toEqual({
200 enableAgentBrowserAccess: false,
201 });
202 expect(
203 clearProjectSettingsOverrides(settings, projectId, [
204 "defaultAutoPull",
205 "enableAgentBrowserAccess",
206 ]),
207 ).toBeNull();
208 expect(clearProjectSettingsOverrides(settings, otherProjectId, ["defaultAutoPull"])).toBeNull();
209 expect(withProjectSettingsOverrides(settings, projectId, null)).toEqual({});
210 expect(
211 withProjectSettingsOverrides(settings, otherProjectId, { defaultThreadEnvMode: "worktree" }),
212 ).toEqual({
213 ...settings.projectSettingsOverrides,
214 [otherProjectId]: { defaultThreadEnvMode: "worktree" },
215 });
216 });
217});
218​
219describe("resolveWorktreeCleanup", () => {
220 it("inherits machine rules, disables one project and keeps custom rules isolated", () => {
221 const machine = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
222 storageCleanup: { worktreeAfterDays: 8, worktreeOnDelete: true, logsAfterDays: 3 },
223 });
224 const inherited = resolveWorktreeCleanup(machine, projectId);
225 const off = applyServerSettingsPatch(machine, {
226 projectSettingsOverrides: {
227 [projectId]: { worktreeCleanup: { mode: "off" } },
228 },
229 });
230 expect(resolveWorktreeCleanup(off, projectId)).toEqual({
231 worktreeAfterDays: null,
232 worktreeOnDelete: false,
233 worktreeOnMerge: false,
234 worktreeUnchanged: false,
235 });
236 expect(resolveWorktreeCleanup(off, otherProjectId)).toEqual(inherited);
237 const custom = applyServerSettingsPatch(off, {
238 projectSettingsOverrides: {
239 [projectId]: {
240 worktreeCleanup: { mode: "custom", rules: { ...inherited, worktreeAfterDays: 15 } },
241 },
242 },
243 });
244 expect(resolveWorktreeCleanup(custom, projectId).worktreeAfterDays).toBe(15);
245 expect(custom.storageCleanup.logsAfterDays).toBe(3);
246 const reset = applyServerSettingsPatch(custom, {
247 projectSettingsOverrides: {
248 [projectId]: clearProjectSettingsOverrides(custom, projectId, ["worktreeCleanup"]),
249 },
250 });
251 expect(resolveWorktreeCleanup(reset, projectId)).toEqual(inherited);
252 });
253 it("completes partial machine custom rules and preserves them across edits", () => {
254 const initial = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
255 storageCleanup: { worktreeAfterDays: 8, worktreeOnDelete: true },
256 });
257 const custom = applyServerSettingsPatch(initial, {
258 worktreeCleanup: { mode: "custom", rules: { worktreeOnMerge: true } },
259 });
260 const edited = applyServerSettingsPatch(custom, {
261 worktreeCleanup: { mode: "custom", rules: { worktreeAfterDays: 15 } },
262 });
263 expect(resolveWorktreeCleanup(edited, null)).toEqual({
264 worktreeAfterDays: 15,
265 worktreeOnDelete: true,
266 worktreeOnMerge: true,
267 worktreeUnchanged: false,
268 });
269 expect(
270 resolveWorktreeCleanup(applyServerSettingsPatch(edited, { worktreeCleanup: null }), null)
271 .worktreeAfterDays,
272 ).toBe(8);
273 });
274});