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

scopedSettings.test.ts

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