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

scopedSettings.test.ts

793 lines28.0 KB

1import {
2 DEFAULT_SERVER_SETTINGS,
3 EnvironmentAuthorizationError,
4 EnvironmentId,
5 ProjectId,
6 ProviderInstanceId,
7 type ServerSettings,
8} from "@t3tools/contracts";
9import { describe, expect, it, vi } from "vite-plus/test";
10import { applyServerSettingsPatch } from "@t3tools/shared/serverSettings";
11import * as Cause from "effect/Cause";
12import { resolveWorktreeCleanup } from "@t3tools/shared/projectSettings";
13​
14import type { SidebarProjectSnapshot } from "../../sidebarProjectGrouping";
15import {
16 persistScopedSettingsPatch,
17 planProjectOverridesClear,
18 planScopedSettingsClear,
19 planScopedSettingsPatch,
20 scopedIdleCompaction,
21 resolveScopedSettingsTargets,
22 scopedSettingsAreMixed,
23 scopedSettingsSource,
24 selectScopedSettingsEnvironments,
25} from "./scopedSettings";
26import { resolveSettingsScope } from "./settingsScope";
27​
28function environment(
29 id: string,
30 options: {
31 connected?: boolean;
32 loaded?: boolean;
33 settings?: Partial<ServerSettings>;
34 projectOverrides?: boolean;
35 } = {},
36) {
37 return {
38 environmentId: EnvironmentId.make(id),
39 label: id,
40 connection: {
41 phase: options.connected === false ? ("offline" as const) : ("connected" as const),
42 },
43 serverConfig:
44 options.loaded === false
45 ? null
46 : {
47 settings: { ...DEFAULT_SERVER_SETTINGS, ...options.settings },
48 environment: {
49 capabilities: { projectSettingsOverrides: options.projectOverrides !== false },
50 },
51 },
52 };
54​
55const laptop = environment("Laptop");
56const server = environment("Server");
57const offline = environment("Offline", { connected: false });
58const loading = environment("Loading", { loaded: false });
59const environments = [laptop, server, offline, loading];
60const all = resolveSettingsScope({}, [], environments);
61const named = resolveSettingsScope({ machine: server.environmentId }, [], environments);
62​
63const projectId = ProjectId.make("project");
64const laptopProjectId = ProjectId.make("laptop-project");
65const member = {
66 id: projectId,
67 environmentId: server.environmentId,
68 title: "Project",
69 workspaceRoot: "/repo",
70 physicalProjectKey: `${server.environmentId}:/repo`,
71 environmentLabel: server.label,
72 defaultModelSelection: null,
73 scripts: [],
74 createdAt: "2026-09-07T00:00:00.000Z",
75 updatedAt: "2026-09-07T00:00:00.000Z",
76};
77const laptopMember = {
78 ...member,
79 id: laptopProjectId,
80 environmentId: laptop.environmentId,
81 physicalProjectKey: `${laptop.environmentId}:/repo`,
82 environmentLabel: laptop.label,
83};
84const group: SidebarProjectSnapshot = {
85 ...member,
86 projectKey: "project-group",
87 displayName: "Project",
88 memberProjects: [member, laptopMember],
89 memberProjectRefs: [
90 { environmentId: server.environmentId, projectId },
91 { environmentId: laptop.environmentId, projectId: laptopProjectId },
92 ],
93 groupedProjectCount: 2,
94 environmentPresence: "remote-only",
95 allRemoteMembersAreDesktopLocal: false,
96 allRemoteMembersAreWsl: false,
97 remoteEnvironmentLabels: [server.label, laptop.label],
98};
99const project = resolveSettingsScope({ project: group.projectKey }, [group], environments);
100const checkout = resolveSettingsScope(
101 { project: group.projectKey, machine: server.environmentId, checkout: member.physicalProjectKey },
102 [group],
103 environments,
104);
105​
106describe("scoped settings targets", () => {
107 it("uses the named environment even when a different primary is available", () => {
108 const selected = selectScopedSettingsEnvironments(named, environments, laptop.environmentId);
109 expect(selected.environments).toEqual([server]);
110 expect(selected.environment).toBe(server);
111 });
112​
113 it("keeps an offline named environment selected without falling back to primary", () => {
114 const scope = resolveSettingsScope({ machine: offline.environmentId }, [], environments);
115 const selected = selectScopedSettingsEnvironments(scope, environments, laptop.environmentId);
116 expect(selected.environments).toEqual([offline]);
117 expect(selected.connectedEnvironments).toEqual([]);
118 expect(selected.environment).toBeNull();
119 expect(
120 planScopedSettingsPatch(scope, environments, { enableProviderUpdateChecks: false }),
121 ).toMatchObject({
122 serverWrites: [],
123 unavailableReason: "Connect Offline to save this setting.",
124 });
125 });
126​
127 it("resolves each member's effective settings and source at project scope", () => {
128 const overridden = environment("Server", {
129 settings: {
130 defaultAutoPull: false,
131 projectSettingsOverrides: { [projectId]: { defaultAutoPull: true } },
132 },
133 });
134 const targets = resolveScopedSettingsTargets(project, [laptop, overridden]);
135 expect(targets.map((target) => [target.projectId, target.settings.defaultAutoPull])).toEqual([
136 [projectId, true],
137 [laptopProjectId, false],
138 ]);
139 expect(scopedSettingsSource(targets, ["defaultAutoPull"])).toBe("mixed");
140 expect(scopedSettingsSource([targets[0]!], ["defaultAutoPull"])).toBe("project");
141 expect(scopedSettingsSource(targets, ["enableProviderUpdateChecks"])).toBe("environment");
142 expect(scopedSettingsAreMixed(targets, ["defaultAutoPull"])).toBe(true);
143 });
144});
145​
146describe("scoped settings writes", () => {
147 it("edits the effective machine policy without changing other machines' rules", () => {
148 const custom = environment("Laptop", {
149 settings: {
150 worktreeCleanup: {
151 mode: "custom",
152 rules: {
153 worktreeAfterDays: 12,
154 worktreeSettledAfterDays: null,
155 worktreeOnDelete: true,
156 worktreeOnMerge: true,
157 worktreeUnchanged: false,
158 worktreeKeepWhen: "uncommitted-changes",
159 },
160 },
161 },
162 });
163 const disabled = environment("Server", {
164 settings: { worktreeCleanup: { mode: "off" } },
165 });
166 const targets = [custom, disabled];
167 const plan = planScopedSettingsPatch(all, targets, {
168 storageCleanup: { worktreeOnDelete: false },
169 });
170 const policies = plan.serverWrites.map((write, index) =>
171 resolveWorktreeCleanup(
172 applyServerSettingsPatch(targets[index]!.serverConfig!.settings, write.patch),
173 null,
174 ),
175 );
176 expect(policies).toEqual([
177 {
178 worktreeAfterDays: 12,
179 worktreeSettledAfterDays: null,
180 worktreeOnDelete: false,
181 worktreeOnMerge: true,
182 worktreeUnchanged: false,
183 worktreeKeepWhen: "uncommitted-changes",
184 },
185 {
186 worktreeAfterDays: null,
187 worktreeSettledAfterDays: null,
188 worktreeOnDelete: false,
189 worktreeOnMerge: false,
190 worktreeUnchanged: false,
191 worktreeKeepWhen: "uncommitted-changes",
192 },
193 ]);
194 });
195​
196 it("compares idle compaction by mode, and offers Never only where every server stores it", () => {
197 const fixed = {
198 settings: {
199 ...DEFAULT_SERVER_SETTINGS,
200 compactIdleThreads: false,
201 idleCompaction: "onReturn" as const,
202 },
203 };
204 const modes = {
205 settings: { ...DEFAULT_SERVER_SETTINGS, compactIdleThreads: "onReturn" as const },
206 };
207 const legacy = { settings: { ...DEFAULT_SERVER_SETTINGS, compactIdleThreads: false } };
208 expect(scopedIdleCompaction([fixed, modes])).toEqual({ mixed: false, neverAvailable: true });
209 expect(scopedIdleCompaction([fixed, legacy])).toEqual({ mixed: false, neverAvailable: false });
210 expect(
211 scopedIdleCompaction([fixed, { settings: { ...fixed.settings, idleCompaction: "off" } }])
212 .mixed,
213 ).toBe(true);
214 });
215​
216 it("sends an idle compaction mode in the form each environment's server reads", () => {
217 const targets = [
218 environment("Fixed", { settings: { compactIdleThreads: false, idleCompaction: "onReturn" } }),
219 environment("Modes", { settings: { compactIdleThreads: "onReturn" } }),
220 environment("Switch", { settings: { compactIdleThreads: false } }),
221 ];
222 const plan = planScopedSettingsPatch(resolveSettingsScope({}, [], targets), targets, {
223 idleCompaction: "beforeCacheExpires",
224 });
225 expect(plan.serverWrites.map((write) => write.patch)).toEqual([
226 { idleCompaction: "beforeCacheExpires" },
227 { compactIdleThreads: "beforeCacheExpires" },
228 { compactIdleThreads: true },
229 ]);
230 });
231​
232 it("writes an aggregate preference only to connected environments with loaded configuration", async () => {
233 const persistServer = vi.fn().mockResolvedValue({ _tag: "Success" });
234 const persistClient = vi.fn();
235 await persistScopedSettingsPatch(
236 planScopedSettingsPatch(all, environments, { enableProviderUpdateChecks: false }),
237 persistServer,
238 persistClient,
239 );
240 expect(persistServer.mock.calls.map(([input]) => input.environmentId)).toEqual([
241 laptop.environmentId,
242 server.environmentId,
243 ]);
244 expect(persistClient).not.toHaveBeenCalled();
245 });
246​
247 describe("an environment that saves but drops keys it does not know", () => {
248 const upgradedLaptop = (saved: Partial<ServerSettings>) =>
249 vi.fn(async ({ environmentId }: { environmentId: EnvironmentId }) => ({
250 _tag: "Success" as const,
251 value: {
252 ...DEFAULT_SERVER_SETTINGS,
253 ...(environmentId === laptop.environmentId ? saved : {}),
254 },
255 }));
256 const outcome = async (
257 plan: ReturnType<typeof planScopedSettingsPatch>,
258 persistServer: ReturnType<typeof upgradedLaptop>,
259 ) => {
260 const result = await persistScopedSettingsPatch(plan, persistServer, vi.fn());
261 return {
262 saved: result.savedEnvironments.map(({ label }) => label),
263 failed: result.failedEnvironments.map(({ label, message }) => ({ label, message })),
264 };
265 };
266 const unsupported = [
267 {
268 label: server.label,
269 message: "This environment does not support this setting. Update T3 Code on it.",
270 },
271 ];
272​
273 it("reports a dropped toggle", async () => {
274 expect(
275 await outcome(
276 planScopedSettingsPatch(all, environments, { reviewGuidesEnabled: true }),
277 upgradedLaptop({ reviewGuidesEnabled: true }),
278 ),
279 ).toEqual({ saved: [laptop.label], failed: unsupported });
280 });
281​
282 it("reports dropped text but accepts the server trimming it", async () => {
283 expect(
284 await outcome(
285 planScopedSettingsPatch(all, environments, { reviewGuideInstructions: " Read tests " }),
286 upgradedLaptop({ reviewGuideInstructions: "Read tests" }),
287 ),
288 ).toEqual({ saved: [laptop.label], failed: unsupported });
289 });
290​
291 // regression: #18334 an environment silently dropped a setting with no warning
292 it("reports a dropped key inside a project override", async () => {
293 expect(
294 await outcome(
295 planScopedSettingsPatch(project, environments, { defaultAutoPull: true }),
296 upgradedLaptop({
297 projectSettingsOverrides: { [laptopProjectId]: { defaultAutoPull: true } },
298 }),
299 ),
300 ).toEqual({ saved: [laptop.label], failed: unsupported });
301 });
302​
303 it("accepts a write that matches what the environment already had", async () => {
304 expect(
305 await outcome(
306 planScopedSettingsPatch(all, environments, { reviewGuidesEnabled: false }),
307 upgradedLaptop({}),
308 ),
309 ).toEqual({ saved: [laptop.label, server.label], failed: [] });
310 });
311 });
312​
313 it("persists client keys locally at any scope alongside server keys", async () => {
314 const persistServer = vi.fn().mockResolvedValue({ _tag: "Success" });
315 const persistClient = vi.fn();
316 await persistScopedSettingsPatch(
317 planScopedSettingsPatch(named, environments, {
318 diffIgnoreWhitespace: false,
319 enableProviderUpdateChecks: false,
320 }),
321 persistServer,
322 persistClient,
323 );
324 expect(persistClient).toHaveBeenCalledExactlyOnceWith({ diffIgnoreWhitespace: false });
325 expect(persistServer).toHaveBeenCalledExactlyOnceWith({
326 environmentId: server.environmentId,
327 input: { patch: { enableProviderUpdateChecks: false } },
328 });
329 });
330​
331 it("writes project overrides into each member's entry on its environment", () => {
332 const withExisting = environment("Server", {
333 settings: {
334 projectSettingsOverrides: { [projectId]: { enableAgentBrowserAccess: false } },
335 },
336 });
337 const plan = planScopedSettingsPatch(project, [laptop, withExisting], {
338 defaultAutoPull: true,
339 });
340 expect(plan.unavailableReason).toBeNull();
341 expect(plan.serverWrites).toEqual([
342 {
343 environmentId: server.environmentId,
344 label: server.label,
345 previous: expect.anything(),
346 patch: {
347 projectSettingsOverrides: {
348 [projectId]: { enableAgentBrowserAccess: false, defaultAutoPull: true },
349 },
350 },
351 },
352 {
353 environmentId: laptop.environmentId,
354 label: laptop.label,
355 previous: expect.anything(),
356 patch: { projectSettingsOverrides: { [laptopProjectId]: { defaultAutoPull: true } } },
357 },
358 ]);
359 expect(
360 planScopedSettingsPatch(checkout, [laptop, server], { defaultAutoPull: true }),
361 ).toMatchObject({
362 serverWrites: [{ environmentId: server.environmentId }],
363 });
364 });
365​
366 it("keeps each project's other cleanup rules when changing one rule across machines", () => {
367 const machine = environment("Laptop", {
368 settings: {
369 storageCleanup: { ...DEFAULT_SERVER_SETTINGS.storageCleanup, worktreeAfterDays: 30 },
370 },
371 });
372 const customized = environment("Server", {
373 settings: {
374 projectSettingsOverrides: {
375 [projectId]: {
376 defaultAutoPull: true,
377 worktreeCleanup: {
378 mode: "custom",
379 rules: {
380 worktreeAfterDays: 8,
381 worktreeSettledAfterDays: null,
382 worktreeOnDelete: false,
383 worktreeOnMerge: true,
384 worktreeUnchanged: false,
385 worktreeKeepWhen: "uncommitted-changes",
386 },
387 },
388 },
389 },
390 },
391 });
392 const plan = planScopedSettingsPatch(project, [machine, customized], {
393 worktreeCleanup: { mode: "custom", rules: { worktreeOnDelete: true } },
394 });
395 const settledPlan = planScopedSettingsPatch(project, [machine, customized], {
396 worktreeCleanup: { mode: "custom", rules: { worktreeSettledAfterDays: 0 } },
397 });
398 expect(
399 settledPlan.serverWrites.map((write) => write.patch.projectSettingsOverrides),
400 ).toMatchObject([
401 { [projectId]: { worktreeCleanup: { rules: { worktreeSettledAfterDays: 0 } } } },
402 { [laptopProjectId]: { worktreeCleanup: { rules: { worktreeSettledAfterDays: 0 } } } },
403 ]);
404 expect(plan.serverWrites.map((write) => write.patch.projectSettingsOverrides)).toEqual([
405 {
406 [projectId]: {
407 defaultAutoPull: true,
408 worktreeCleanup: {
409 mode: "custom",
410 rules: {
411 worktreeAfterDays: 8,
412 worktreeSettledAfterDays: null,
413 worktreeOnDelete: true,
414 worktreeOnMerge: true,
415 worktreeUnchanged: false,
416 worktreeKeepWhen: "uncommitted-changes",
417 },
418 },
419 },
420 },
421 {
422 [laptopProjectId]: {
423 worktreeCleanup: {
424 mode: "custom",
425 rules: {
426 worktreeAfterDays: 30,
427 worktreeSettledAfterDays: null,
428 worktreeOnDelete: true,
429 worktreeOnMerge: false,
430 worktreeUnchanged: false,
431 worktreeKeepWhen: "uncommitted-changes",
432 },
433 },
434 },
435 },
436 ]);
437 expect(
438 planScopedSettingsClear(checkout, [customized], ["worktreeCleanup"]).serverWrites[0]?.patch,
439 ).toEqual({
440 projectSettingsOverrides: { [projectId]: { defaultAutoPull: true } },
441 });
442 expect(
443 planScopedSettingsPatch(project, [machine, customized], {
444 storageCleanup: { browserArtifactsAfterDays: 8 },
445 }).serverWrites,
446 ).toEqual([]);
447 });
448​
449 // regression: #11541 device settings were written to the wrong scope
450 it("scopes agent device access to projects while keeping hub and hosts environment-wide", () => {
451 const plan = planScopedSettingsPatch(project, [laptop, server], {
452 enableAgentDeviceAccess: true,
453 });
454 expect(plan.serverWrites.map((write) => write.patch)).toEqual([
455 { projectSettingsOverrides: { [projectId]: { enableAgentDeviceAccess: true } } },
456 { projectSettingsOverrides: { [laptopProjectId]: { enableAgentDeviceAccess: true } } },
457 ]);
458 for (const patch of [{ enableDeviceSupport: true }, { deviceHosts: [] }]) {
459 expect(planScopedSettingsPatch(project, environments, patch).serverWrites).toEqual([]);
460 }
461 });
462​
463 it("refuses environment-wide keys and older servers at project scope", () => {
464 expect(
465 planScopedSettingsPatch(project, environments, { enableProviderUpdateChecks: false }),
466 ).toMatchObject({
467 serverWrites: [],
468 unavailableReason: "This setting is environment-wide and cannot be overridden by a project.",
469 });
470 const legacy = environment("Server", { projectOverrides: false });
471 expect(
472 planScopedSettingsPatch(checkout, [laptop, legacy], { defaultAutoPull: true }),
473 ).toMatchObject({ serverWrites: [], unavailableReason: expect.stringContaining("update") });
474 });
475​
476 it("clears overrides per member and removes an emptied entry", () => {
477 const withOverrides = environment("Server", {
478 settings: {
479 projectSettingsOverrides: {
480 [projectId]: { defaultAutoPull: true, enableAgentBrowserAccess: false },
481 },
482 },
483 });
484 const plan = planScopedSettingsClear(checkout, [laptop, withOverrides], ["defaultAutoPull"]);
485 expect(plan.serverWrites).toEqual([
486 {
487 environmentId: server.environmentId,
488 label: server.label,
489 previous: expect.anything(),
490 patch: { projectSettingsOverrides: { [projectId]: { enableAgentBrowserAccess: false } } },
491 },
492 ]);
493 expect(
494 planScopedSettingsClear(
495 checkout,
496 [laptop, withOverrides],
497 ["defaultAutoPull", "enableAgentBrowserAccess"],
498 ).serverWrites[0]?.patch,
499 ).toEqual({ projectSettingsOverrides: { [projectId]: null } });
500 });
501​
502 it("never substitutes an environment-default write for an invalid scope", () => {
503 const scope = resolveSettingsScope({ machine: "removed" }, [group], environments);
504 const plan = planScopedSettingsPatch(scope, environments, { enableAgentBrowserAccess: false });
505 expect(plan.serverWrites).toEqual([]);
506 expect(plan.hasClientWrite).toBe(false);
507 expect(plan.unavailableReason).not.toBeNull();
508 });
509​
510 it("waits for every target and identifies both RPC failures and rejected writes", async () => {
511 const third = environment("Third");
512 const fourth = environment("Fourth");
513 const selected = [...environments, third, fourth];
514 const scope = resolveSettingsScope({}, [], selected);
515 const persistServer = vi
516 .fn()
517 .mockResolvedValueOnce({ _tag: "Success" })
518 .mockResolvedValueOnce({
519 _tag: "Failure",
520 cause: Cause.fail(
521 new EnvironmentAuthorizationError({
522 requiredScope: "settings:write",
523 message: "This connection lacks permission to change settings.",
524 }),
525 ),
526 })
527 .mockRejectedValueOnce(new Error("Disconnected during save"))
528 .mockResolvedValueOnce({ _tag: "Success" });
529 const result = await persistScopedSettingsPatch(
530 planScopedSettingsPatch(scope, selected, { enableAgentBrowserAccess: false }),
531 persistServer,
532 vi.fn(),
533 );
534 expect(result.savedEnvironmentCount).toBe(2);
535 expect(result.savedEnvironments.map(({ label }) => label)).toEqual([
536 laptop.label,
537 fourth.label,
538 ]);
539 expect(result.failedEnvironments.map(({ message }) => message)).toEqual([
540 "This connection lacks permission to change settings.",
541 "Disconnected during save",
542 ]);
543 expect(result.failedEnvironments.map(({ label }) => label)).toEqual([
544 server.label,
545 third.label,
546 ]);
547 expect(persistServer).toHaveBeenCalledTimes(4);
548 });
549});
550​
551describe("scoped settings mixed values", () => {
552 it("treats independently decoded equal nested settings as the same value", () => {
553 const style = {
554 ...DEFAULT_SERVER_SETTINGS.sourceControlWritingStyle,
555 mode: "custom" as const,
556 customInstructions: "Use plain language",
557 };
558 const first = environment("First", { settings: { sourceControlWritingStyle: { ...style } } });
559 const second = environment("Second", { settings: { sourceControlWritingStyle: { ...style } } });
560 const targets = resolveScopedSettingsTargets(all, [first, second]);
561 expect(scopedSettingsAreMixed(targets, ["sourceControlWritingStyle"])).toBe(false);
562 });
563});
564​
565describe("project overrides at environment scope", () => {
566 const laptop = EnvironmentId.make("laptop");
567 const fleet = ProjectId.make("fleet");
568 const t3 = ProjectId.make("t3");
569 const environment = (
570 environmentId: EnvironmentId,
571 overrides: ServerSettings["projectSettingsOverrides"],
572 ) => ({
573 environmentId,
574 label: environmentId,
575 connection: { phase: "connected" as const },
576 serverConfig: {
577 settings: { ...DEFAULT_SERVER_SETTINGS, projectSettingsOverrides: overrides },
578 environment: { capabilities: { projectSettingsOverrides: true } },
579 },
580 });
581​
582 it("clears only those keys and drops entries that become empty", () => {
583 const plan = planProjectOverridesClear(
584 [
585 environment(laptop, {
586 [fleet]: { defaultAutoPull: true, defaultThreadEnvMode: "local" },
587 [t3]: { defaultAutoPull: true },
588 }),
589 ],
590 [
591 { environmentId: laptop, projectId: fleet },
592 { environmentId: laptop, projectId: t3 },
593 ],
594 ["defaultAutoPull"],
595 );
596 expect(plan.serverWrites).toEqual([
597 {
598 environmentId: laptop,
599 label: laptop,
600 previous: expect.anything(),
601 patch: {
602 projectSettingsOverrides: { [fleet]: { defaultThreadEnvMode: "local" }, [t3]: null },
603 },
604 },
605 ]);
606 });
607});
608​
609describe("null patches at project scope", () => {
610 it("removes the override for keys that cannot store null and keeps it for keys that can", () => {
611 const environmentId = EnvironmentId.make("laptop");
612 const projectId = ProjectId.make("fleet");
613 const scope = {
614 kind: "project" as const,
615 group: {} as never,
616 environmentId: null,
617 label: "fleet",
618 members: [{ id: projectId, environmentId } as never],
619 environmentIds: [environmentId],
620 };
621 const environments = [
622 {
623 environmentId,
624 label: "Laptop",
625 connection: { phase: "connected" as const },
626 serverConfig: {
627 settings: {
628 ...DEFAULT_SERVER_SETTINGS,
629 projectSettingsOverrides: {
630 [projectId]: { defaultThreadEnvMode: "worktree" as const, defaultAutoPull: true },
631 },
632 },
633 environment: { capabilities: { projectSettingsOverrides: true } },
634 },
635 },
636 ];
637 expect(
638 planScopedSettingsPatch(scope, environments, { defaultThreadEnvMode: null }).serverWrites[0]
639 ?.patch,
640 ).toEqual({ projectSettingsOverrides: { [projectId]: { defaultAutoPull: true } } });
641 expect(
642 planScopedSettingsPatch(scope, environments, { defaultModelSelection: null }).serverWrites[0]
643 ?.patch,
644 ).toEqual({
645 projectSettingsOverrides: {
646 [projectId]: {
647 defaultThreadEnvMode: "worktree",
648 defaultAutoPull: true,
649 defaultModelSelection: null,
650 },
651 },
652 });
653 });
654});
655​
656describe("partial object patches at project scope", () => {
657 it("completes a writing style field patch from the target's effective value", () => {
658 const environmentId = EnvironmentId.make("laptop");
659 const projectId = ProjectId.make("fleet");
660 const member = {
661 id: projectId,
662 environmentId,
663 physicalProjectKey: "laptop:/repo",
664 environmentLabel: "Laptop",
665 title: "fleet",
666 workspaceRoot: "/repo",
667 defaultModelSelection: null,
668 scripts: [],
669 createdAt: "2026-01-01T00:00:00.000Z",
670 updatedAt: "2026-01-01T00:00:00.000Z",
671 };
672 const settings = {
673 ...DEFAULT_SERVER_SETTINGS,
674 projectSettingsOverrides: {
675 [projectId]: {
676 sourceControlWritingStyle: {
677 mode: "custom" as const,
678 customInstructions: "Keep it short.",
679 followChangeRequestTemplates: false,
680 },
681 },
682 },
683 };
684 const plan = planScopedSettingsPatch(
685 {
686 kind: "project",
687 group: {} as never,
688 environmentId: null,
689 label: "fleet",
690 members: [member as never],
691 environmentIds: [environmentId],
692 },
693 [
694 {
695 environmentId,
696 label: "Laptop",
697 connection: { phase: "connected" },
698 serverConfig: {
699 settings,
700 environment: { capabilities: { projectSettingsOverrides: true } },
701 },
702 },
703 ],
704 { sourceControlWritingStyle: { customInstructions: "Be terse." } },
705 );
706 expect(plan.serverWrites[0]?.patch).toEqual({
707 projectSettingsOverrides: {
708 [projectId]: {
709 sourceControlWritingStyle: {
710 mode: "custom",
711 customInstructions: "Be terse.",
712 followChangeRequestTemplates: false,
713 },
714 },
715 },
716 });
717 });
718});
719​
720it("routes legacy model preference keys exclusively to the selected environment", () => {
721 const patch = {
722 favorites: [{ provider: ProviderInstanceId.make("codex"), model: "sol" }],
723 providerModelPreferences: {},
724 };
725 const plan = planScopedSettingsPatch(named, environments, patch);
726 expect(plan.clientPatch).toEqual({});
727 expect(plan.hasClientWrite).toBe(false);
728 expect(plan.serverWrites).toEqual([
729 {
730 environmentId: server.environmentId,
731 label: server.label,
732 previous: expect.anything(),
733 patch,
734 },
735 ]);
736});
737​
738describe("model selection patches at project scope", () => {
739 const codex = ProviderInstanceId.make("codex");
740 const claude = ProviderInstanceId.make("claudeAgent");
741 const saved = {
742 instanceId: codex,
743 model: "gpt-5.6",
744 options: [{ id: "reasoningEffort", value: "high" }],
745 };
746 const customized = environment("Server", {
747 settings: {
748 projectSettingsOverrides: {
749 [projectId]: {
750 defaultModelSelection: saved,
751 sourceControlWriterModelSelection: saved,
752 reviewGuideModelSelection: saved,
753 textGenerationModelSelection: saved,
754 },
755 },
756 },
757 });
758​
759 // regression: #16581 changing a project's model kept the old model's options
760 it("replaces old options when changing model selections", () => {
761 const plan = planScopedSettingsPatch(checkout, [customized], {
762 defaultModelSelection: { instanceId: claude, model: "claude-opus-4-8" },
763 sourceControlWriterModelSelection: { instanceId: codex, model: "gpt-5.6-mini" },
764 reviewGuideModelSelection: { instanceId: codex, model: "gpt-5.6-mini" },
765 textGenerationModelSelection: { instanceId: claude, model: "claude-sonnet-4-6" },
766 });
767 expect(plan.serverWrites[0]?.patch.projectSettingsOverrides?.[projectId]).toEqual({
768 defaultModelSelection: { instanceId: claude, model: "claude-opus-4-8" },
769 sourceControlWriterModelSelection: { instanceId: codex, model: "gpt-5.6-mini" },
770 reviewGuideModelSelection: { instanceId: codex, model: "gpt-5.6-mini" },
771 textGenerationModelSelection: { instanceId: claude, model: "claude-sonnet-4-6" },
772 });
773 });
774​
775 it("completes partial text-generation selections without retaining old options", () => {
776 const modelPlan = planScopedSettingsPatch(checkout, [customized], {
777 textGenerationModelSelection: { model: "gpt-5.6-mini" },
778 });
779 expect(
780 modelPlan.serverWrites[0]?.patch.projectSettingsOverrides?.[projectId]
781 ?.textGenerationModelSelection,
782 ).toEqual({ instanceId: codex, model: "gpt-5.6-mini" });
783​
784 const options = [{ id: "reasoningEffort", value: "low" }];
785 const optionsPlan = planScopedSettingsPatch(checkout, [customized], {
786 textGenerationModelSelection: { options },
787 });
788 expect(
789 optionsPlan.serverWrites[0]?.patch.projectSettingsOverrides?.[projectId]
790 ?.textGenerationModelSelection,
791 ).toEqual({ ...saved, options });
792 });
793});