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 resolveProjectSettings,
13 withProjectSettingsOverrides,
14} from "./projectSettings.ts";
15import { applyServerSettingsPatch } from "./serverSettings.ts";
16
17const projectId = ProjectId.make("project-a");
18const otherProjectId = ProjectId.make("project-b");
19
20describe("resolveProjectSettings", () => {
21 it("inherits every scopable key when the project has no overrides", () => {
22 const resolved = resolveProjectSettings(DEFAULT_SERVER_SETTINGS, projectId);
23 expect(resolved.settings).toBe(DEFAULT_SERVER_SETTINGS);
24 for (const key of PROJECT_SCOPED_SERVER_SETTING_KEYS) {
25 expect(resolved.sources[key]).toBe("environment");
26 }
27 expect(resolveProjectSettings(DEFAULT_SERVER_SETTINGS, null).settings).toBe(
28 DEFAULT_SERVER_SETTINGS,
29 );
30 });
31
32 it("applies overrides per key and reports their source", () => {
33 const settings = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
34 defaultAutoPull: true,
35 sidebarAutoSettleAfterDays: 3,
36 projectSettingsOverrides: {
37 [projectId]: { defaultAutoPull: false, sidebarAutoSettleAfterDays: null },
38 },
39 });
40 const resolved = resolveProjectSettings(settings, projectId);
41 expect(resolved.settings.defaultAutoPull).toBe(false);
42 expect(resolved.settings.sidebarAutoSettleAfterDays).toBeNull();
43 expect(resolved.settings.defaultThreadEnvMode).toBe(settings.defaultThreadEnvMode);
44 expect(resolved.sources.defaultAutoPull).toBe("project");
45 expect(resolved.sources.sidebarAutoSettleAfterDays).toBe("project");
46 expect(resolved.sources.defaultThreadEnvMode).toBe("environment");
47 expect(resolveProjectSettings(settings, otherProjectId).settings.defaultAutoPull).toBe(true);
48 });
49
50 it("keeps the environment text generation model when the override's provider is disabled", () => {
51 const disabledSelection = createModelSelection(ProviderInstanceId.make("claudeAgent"), "opus");
52 const settings = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
53 providers: { claudeAgent: { enabled: false } },
54 projectSettingsOverrides: {
55 [projectId]: { textGenerationModelSelection: disabledSelection },
56 },
57 });
58 const resolved = resolveProjectSettings(settings, projectId);
59 expect(resolved.settings.textGenerationModelSelection).toEqual(
60 settings.textGenerationModelSelection,
61 );
62 expect(resolved.sources.textGenerationModelSelection).toBe("environment");
63 });
64
65 it("honours the aggregate's own fields only until the server has folded them", () => {
66 const aggregateModel = createModelSelection(ProviderInstanceId.make("codex"), "gpt-5.5");
67 const project = {
68 defaultModelSelection: aggregateModel,
69 defaultThreadEnvMode: "local" as const,
70 };
71 const unfolded = resolveProjectSettings(
72 { ...DEFAULT_SERVER_SETTINGS, projectSettingsFolded: false },
73 projectId,
74 project,
75 );
76 expect(unfolded.settings.defaultModelSelection).toEqual(aggregateModel);
77 expect(unfolded.settings.defaultThreadEnvMode).toBe("local");
78 expect(unfolded.sources.defaultModelSelection).toBe("project");
79 // A stored override still beats the aggregate before the fold.
80 const overridden = resolveProjectSettings(
81 {
82 ...DEFAULT_SERVER_SETTINGS,
83 projectSettingsFolded: false,
84 projectSettingsOverrides: { [projectId]: { defaultThreadEnvMode: "worktree" } },
85 },
86 projectId,
87 project,
88 );
89 expect(overridden.settings.defaultThreadEnvMode).toBe("worktree");
90 // After the fold a reset in the record wins over the stale aggregate.
91 const folded = resolveProjectSettings(
92 { ...DEFAULT_SERVER_SETTINGS, projectSettingsFolded: true },
93 projectId,
94 project,
95 );
96 expect(folded.settings.defaultModelSelection).toBeNull();
97 expect(folded.sources.defaultModelSelection).toBe("environment");
98 });
99
100 it("keeps the environment default model when the override's provider is disabled", () => {
101 const disabledSelection = createModelSelection(ProviderInstanceId.make("claudeAgent"), "opus");
102 const settings = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
103 providers: { claudeAgent: { enabled: false } },
104 projectSettingsOverrides: { [projectId]: { defaultModelSelection: disabledSelection } },
105 });
106 const resolved = resolveProjectSettings(settings, projectId);
107 expect(resolved.settings.defaultModelSelection).toBeNull();
108 expect(resolved.sources.defaultModelSelection).toBe("environment");
109 });
110});
111
112describe("projectSettingsOverrides patches", () => {
113 it("replaces a project's entry, removes it with null, and drops empty entries", () => {
114 const first = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
115 projectSettingsOverrides: {
116 [projectId]: { defaultAutoPull: true, enableAgentBrowserAccess: false },
117 [otherProjectId]: { defaultAutoPull: false },
118 },
119 });
120 expect(hasProjectSettingsOverrides(first)).toBe(true);
121 const replaced = applyServerSettingsPatch(first, {
122 projectSettingsOverrides: { [projectId]: { enableAgentBrowserAccess: false } },
123 });
124 expect(replaced.projectSettingsOverrides[projectId]).toEqual({
125 enableAgentBrowserAccess: false,
126 });
127 expect(replaced.projectSettingsOverrides[otherProjectId]).toEqual({ defaultAutoPull: false });
128 const emptied = applyServerSettingsPatch(replaced, {
129 projectSettingsOverrides: { [projectId]: {} },
130 });
131 expect(emptied.projectSettingsOverrides[projectId]).toBeUndefined();
132 const removed = applyServerSettingsPatch(replaced, {
133 projectSettingsOverrides: { [projectId]: null },
134 });
135 expect(removed.projectSettingsOverrides).toEqual({
136 [otherProjectId]: { defaultAutoPull: false },
137 });
138 expect(hasProjectSettingsOverrides(DEFAULT_SERVER_SETTINGS)).toBe(false);
139 });
140
141 it("derives the legacy per-key maps from the generic record", () => {
142 const settings = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
143 projectSettingsOverrides: {
144 [projectId]: { defaultAutoPull: true, enableAgentBrowserAccess: false },
145 [otherProjectId]: { defaultProjectScripts: [] },
146 },
147 });
148 expect(settings.projectAutoPullOverrides).toEqual({ [projectId]: true });
149 expect(settings.projectAgentBrowserAccessOverrides).toEqual({ [projectId]: false });
150 expect(settings.projectScriptOverrides).toEqual({ [otherProjectId]: [] });
151 });
152
153 it("translates legacy per-key patches into the generic record", () => {
154 const written = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
155 projectAutoPullOverrides: { [projectId]: true },
156 projectAgentBrowserAccessOverrides: { [projectId]: false, [otherProjectId]: true },
157 });
158 expect(written.projectSettingsOverrides).toEqual({
159 [projectId]: { defaultAutoPull: true, enableAgentBrowserAccess: false },
160 [otherProjectId]: { enableAgentBrowserAccess: true },
161 });
162 const cleared = applyServerSettingsPatch(written, {
163 projectAgentBrowserAccessOverrides: { [projectId]: null, [otherProjectId]: null },
164 });
165 expect(cleared.projectSettingsOverrides).toEqual({ [projectId]: { defaultAutoPull: true } });
166 });
167
168 it("lets a canonical entry win over a legacy map for the same project", () => {
169 const current = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
170 projectSettingsOverrides: { [projectId]: { defaultAutoPull: true } },
171 });
172 // The canonical entry omits defaultAutoPull to clear it; the stale legacy
173 // map in the same patch must not put it back.
174 const next = applyServerSettingsPatch(current, {
175 projectSettingsOverrides: { [projectId]: { defaultThreadEnvMode: "local" } },
176 projectAutoPullOverrides: { [projectId]: true, [otherProjectId]: false },
177 });
178 expect(next.projectSettingsOverrides).toEqual({
179 [projectId]: { defaultThreadEnvMode: "local" },
180 [otherProjectId]: { defaultAutoPull: false },
181 });
182 });
183
184 it("builds replacement entries and clears individual keys", () => {
185 const settings = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
186 projectSettingsOverrides: {
187 [projectId]: { defaultAutoPull: true, enableAgentBrowserAccess: false },
188 },
189 });
190 expect(clearProjectSettingsOverrides(settings, projectId, ["defaultAutoPull"])).toEqual({
191 enableAgentBrowserAccess: false,
192 });
193 expect(
194 clearProjectSettingsOverrides(settings, projectId, [
195 "defaultAutoPull",
196 "enableAgentBrowserAccess",
197 ]),
198 ).toBeNull();
199 expect(clearProjectSettingsOverrides(settings, otherProjectId, ["defaultAutoPull"])).toBeNull();
200 expect(withProjectSettingsOverrides(settings, projectId, null)).toEqual({});
201 expect(
202 withProjectSettingsOverrides(settings, otherProjectId, { defaultThreadEnvMode: "worktree" }),
203 ).toEqual({
204 ...settings.projectSettingsOverrides,
205 [otherProjectId]: { defaultThreadEnvMode: "worktree" },
206 });
207 });
208});