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