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