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

projectSettings.test.ts

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