Open History to see the last commit
1import {
2 DEFAULT_SERVER_SETTINGS,
3 EnvironmentId,
4 ProjectId,
5 ProviderDriverKind,
6 ProviderInstanceId,
7} from "@t3tools/contracts";
8import { describe, expect, it } from "@effect/vitest";
9
10import {
11 filterSharedServerPatch,
12 findSharedSettingsMismatches,
13 pickSharedServerSettings,
14 splitSharedServerPatch,
15 supportsSharedSettingsSync,
16} from "./sharedSettings.ts";
17
18const primaryId = EnvironmentId.make("env-primary");
19const laptopId = EnvironmentId.make("env-laptop");
20const boxId = EnvironmentId.make("env-box");
21const restartCapabilities = { threadRestartContinuation: true };
22
23describe("supportsSharedSettingsSync", () => {
24 it("accepts only connected servers that advertise the shared-settings capability", () => {
25 expect(
26 supportsSharedSettingsSync({
27 connection: { phase: "connected" },
28 serverConfig: { environment: { capabilities: { threadAutoSettlement: true } } },
29 }),
30 ).toBe(true);
31 expect(
32 supportsSharedSettingsSync({
33 connection: { phase: "connected" },
34 serverConfig: { environment: { capabilities: {} } },
35 }),
36 ).toBe(false);
37 expect(
38 supportsSharedSettingsSync({
39 connection: { phase: "reconnecting" },
40 serverConfig: { environment: { capabilities: { threadAutoSettlement: true } } },
41 }),
42 ).toBe(false);
43 });
44});
45
46describe("splitSharedServerPatch", () => {
47 it("keeps project overrides local: project ids belong to one environment", () => {
48 const patch = {
49 projectSettingsOverrides: { [ProjectId.make("project")]: { defaultAutoPull: true } },
50 sidebarAutoSettleOnMerge: false,
51 };
52 expect(splitSharedServerPatch(patch)).toEqual({
53 sharedPatch: { sidebarAutoSettleOnMerge: false },
54 localPatch: { projectSettingsOverrides: patch.projectSettingsOverrides },
55 });
56 });
57
58 it.each([
59 {
60 instanceId: ProviderInstanceId.make("codex"),
61 model: "gpt-5.6-sol",
62 options: [{ id: "reasoningEffort", value: "low" }],
63 },
64 {
65 instanceId: ProviderInstanceId.make("claudeAgent"),
66 model: "claude-sonnet-4-6",
67 options: [{ id: "effort", value: "high" }],
68 },
69 DEFAULT_SERVER_SETTINGS.textGenerationModelSelection,
70 ])("shares the text generation model and options, including reset (%j)", (selection) => {
71 const patch = { textGenerationModelSelection: selection };
72 expect(splitSharedServerPatch(patch)).toEqual({ sharedPatch: patch, localPatch: {} });
73 expect(pickSharedServerSettings({ ...DEFAULT_SERVER_SETTINGS, ...patch })).toMatchObject(patch);
74 const environment = {
75 environmentId: boxId,
76 label: "Remote Box",
77 syncEligible: true,
78 settings: {
79 ...DEFAULT_SERVER_SETTINGS,
80 textGenerationModelSelection: { ...selection, model: "different-model" },
81 },
82 };
83 const input = {
84 primaryEnvironmentId: primaryId,
85 primarySettings: { ...DEFAULT_SERVER_SETTINGS, ...patch },
86 environments: [environment],
87 };
88 expect(findSharedSettingsMismatches(input)).toEqual([
89 { environmentId: boxId, label: "Remote Box" },
90 ]);
91 expect(
92 findSharedSettingsMismatches({
93 ...input,
94 environments: [{ ...environment, settings: input.primarySettings }],
95 }),
96 ).toEqual([]);
97 });
98
99 it("routes preference keys to the shared patch and machine keys to the local patch", () => {
100 const { sharedPatch, localPatch } = splitSharedServerPatch({
101 sidebarAutoSettleAfterDays: 7,
102 sidebarAutoSettleOnMerge: false,
103 continueThreadsAfterServerUpdate: true,
104 enableAgentBrowserAccess: false,
105 defaultThreadEnvMode: "worktree",
106 newWorktreesStartFromOrigin: true,
107 });
108 expect(sharedPatch).toEqual({
109 sidebarAutoSettleAfterDays: 7,
110 sidebarAutoSettleOnMerge: false,
111 continueThreadsAfterServerUpdate: true,
112 newWorktreesStartFromOrigin: true,
113 });
114 expect(localPatch).toEqual({
115 enableAgentBrowserAccess: false,
116 defaultThreadEnvMode: "worktree",
117 });
118 });
119});
120
121describe("pickSharedServerSettings", () => {
122 it("returns only the shared keys", () => {
123 expect(
124 Object.keys(pickSharedServerSettings(DEFAULT_SERVER_SETTINGS, restartCapabilities)).sort(),
125 ).toEqual([
126 "continueThreadsAfterServerUpdate",
127 "newWorktreesStartFromOrigin",
128 "sidebarAutoSettleAfterDays",
129 "sidebarAutoSettleOnMerge",
130 "sourceControlWritingStyle",
131 "textGenerationModelSelection",
132 ]);
133 });
134});
135
136describe("filterSharedServerPatch", () => {
137 it.each([true, false])(
138 "resets a disabled default provider only on the originating environment (%s)",
139 (targetIsSource) => {
140 const settings = {
141 ...DEFAULT_SERVER_SETTINGS,
142 providerInstances: {
143 codex: { driver: ProviderDriverKind.make("codex"), enabled: false, config: {} },
144 claudeAgent: {
145 driver: ProviderDriverKind.make("claudeAgent"),
146 enabled: true,
147 config: {},
148 },
149 },
150 textGenerationModelSelection: {
151 instanceId: ProviderInstanceId.make("claudeAgent"),
152 model: "claude-opus-4-6",
153 },
154 };
155 const patch = {
156 textGenerationModelSelection: DEFAULT_SERVER_SETTINGS.textGenerationModelSelection,
157 continueThreadsAfterServerUpdate: true,
158 sidebarAutoSettleAfterDays: 7,
159 };
160 expect(filterSharedServerPatch(patch, undefined, settings, settings, targetIsSource)).toEqual(
161 {
162 ...(targetIsSource
163 ? { textGenerationModelSelection: DEFAULT_SERVER_SETTINGS.textGenerationModelSelection }
164 : {}),
165 sidebarAutoSettleAfterDays: 7,
166 },
167 );
168 },
169 );
170
171 it.each(["missing", "disabled", "different-driver", "enabled"] as const)(
172 "shares a custom model only when its target provider is enabled (%s)",
173 (availability) => {
174 const instanceId = ProviderInstanceId.make("codex_personal");
175 const selection = {
176 instanceId,
177 model: "gpt-5.6-luna",
178 options: [{ id: "reasoningEffort", value: "low" }],
179 };
180 const instance = {
181 driver: ProviderDriverKind.make(
182 availability === "different-driver" ? "claudeAgent" : "codex",
183 ),
184 enabled: availability !== "disabled",
185 config: {},
186 };
187 const settings = {
188 ...DEFAULT_SERVER_SETTINGS,
189 providerInstances: availability === "missing" ? {} : { [instanceId]: instance },
190 };
191 const patch = { sidebarAutoSettleAfterDays: 7, textGenerationModelSelection: selection };
192 const sourceSettings = {
193 ...settings,
194 providerInstances: {
195 [instanceId]: { ...instance, driver: ProviderDriverKind.make("codex"), enabled: true },
196 },
197 };
198 expect(filterSharedServerPatch(patch, restartCapabilities, settings, sourceSettings)).toEqual(
199 availability === "enabled" ? patch : { sidebarAutoSettleAfterDays: 7 },
200 );
201 const primarySettings = {
202 ...sourceSettings,
203 textGenerationModelSelection: selection,
204 };
205 expect(
206 findSharedSettingsMismatches({
207 primaryEnvironmentId: primaryId,
208 primarySettings,
209 environments: [
210 { environmentId: boxId, label: "Remote Box", syncEligible: true, settings },
211 ],
212 }),
213 ).toEqual(availability === "enabled" ? [{ environmentId: boxId, label: "Remote Box" }] : []);
214 },
215 );
216
217 it.each([true, false])("preserves supported restart preference %s", (enabled) => {
218 const patch = { continueThreadsAfterServerUpdate: enabled, sidebarAutoSettleAfterDays: 7 };
219 expect(filterSharedServerPatch(patch, restartCapabilities)).toEqual(patch);
220 });
221
222 it.each([undefined, {}, { threadRestartContinuation: false }])(
223 "omits only the unsupported restart preference with capabilities %j",
224 (capabilities) => {
225 expect(
226 filterSharedServerPatch(
227 { continueThreadsAfterServerUpdate: true, sidebarAutoSettleAfterDays: 7 },
228 capabilities,
229 ),
230 ).toEqual({ sidebarAutoSettleAfterDays: 7 });
231 expect(pickSharedServerSettings(DEFAULT_SERVER_SETTINGS, capabilities)).not.toHaveProperty(
232 "continueThreadsAfterServerUpdate",
233 );
234 },
235 );
236});
237
238describe("findSharedSettingsMismatches", () => {
239 const primarySettings = { ...DEFAULT_SERVER_SETTINGS, sidebarAutoSettleAfterDays: 7 };
240
241 it.each([true, false])(
242 "detects remote restart continuation drift when the preference is %s",
243 (enabled) => {
244 const settings = { ...primarySettings, continueThreadsAfterServerUpdate: enabled };
245 const remoteSettings = { ...settings, continueThreadsAfterServerUpdate: !enabled };
246 const environment = {
247 environmentId: boxId,
248 label: "Remote Box",
249 syncEligible: true,
250 settings: remoteSettings,
251 capabilities: restartCapabilities,
252 };
253 expect(
254 findSharedSettingsMismatches({
255 primaryEnvironmentId: primaryId,
256 primarySettings: settings,
257 primaryCapabilities: restartCapabilities,
258 environments: [environment],
259 }),
260 ).toEqual([{ environmentId: boxId, label: "Remote Box" }]);
261 expect(
262 findSharedSettingsMismatches({
263 primaryEnvironmentId: primaryId,
264 primarySettings: settings,
265 primaryCapabilities: restartCapabilities,
266 environments: [
267 {
268 ...environment,
269 settings: Object.assign(
270 {},
271 remoteSettings,
272 pickSharedServerSettings(settings, restartCapabilities),
273 ),
274 },
275 ],
276 }),
277 ).toEqual([]);
278 },
279 );
280
281 it.each([
282 [undefined, restartCapabilities],
283 [restartCapabilities, undefined],
284 [undefined, undefined],
285 ])(
286 "ignores restart drift unless both servers support it (%j, %j)",
287 (primaryCapabilities, capabilities) => {
288 const environment = {
289 environmentId: boxId,
290 label: "Remote Box",
291 syncEligible: true,
292 capabilities,
293 settings: { ...primarySettings, continueThreadsAfterServerUpdate: true },
294 };
295 const input = {
296 primaryEnvironmentId: primaryId,
297 primarySettings,
298 primaryCapabilities,
299 environments: [environment],
300 };
301 expect(findSharedSettingsMismatches(input)).toEqual([]);
302 expect(
303 findSharedSettingsMismatches({
304 ...input,
305 environments: [
306 {
307 ...environment,
308 settings: { ...environment.settings, sidebarAutoSettleAfterDays: 14 },
309 },
310 ],
311 }),
312 ).toEqual([{ environmentId: boxId, label: "Remote Box" }]);
313 },
314 );
315
316 it("lists sync-eligible environments whose shared settings differ", () => {
317 const mismatches = findSharedSettingsMismatches({
318 primaryEnvironmentId: primaryId,
319 primarySettings,
320 environments: [
321 {
322 environmentId: primaryId,
323 label: "Desktop",
324 syncEligible: true,
325 settings: primarySettings,
326 },
327 {
328 environmentId: laptopId,
329 label: "Laptop",
330 syncEligible: true,
331 settings: primarySettings,
332 },
333 {
334 environmentId: boxId,
335 label: "Remote Box",
336 syncEligible: true,
337 settings: DEFAULT_SERVER_SETTINGS,
338 },
339 ],
340 });
341 expect(mismatches).toEqual([{ environmentId: boxId, label: "Remote Box" }]);
342 });
343
344 it("ignores machine-only differences", () => {
345 const mismatches = findSharedSettingsMismatches({
346 primaryEnvironmentId: primaryId,
347 primarySettings,
348 environments: [
349 {
350 environmentId: boxId,
351 label: "Remote Box",
352 syncEligible: true,
353 settings: {
354 ...primarySettings,
355 enableAgentBrowserAccess: false,
356 defaultThreadEnvMode:
357 primarySettings.defaultThreadEnvMode === "local" ? "worktree" : "local",
358 },
359 },
360 ],
361 });
362 expect(mismatches).toEqual([]);
363 });
364
365 it("reports nothing until the primary environment's settings are loaded", () => {
366 const environments = [
367 {
368 environmentId: boxId,
369 label: "Remote Box",
370 syncEligible: true,
371 settings: primarySettings,
372 },
373 ];
374 expect(
375 findSharedSettingsMismatches({ primaryEnvironmentId: null, primarySettings, environments }),
376 ).toEqual([]);
377 expect(
378 findSharedSettingsMismatches({
379 primaryEnvironmentId: primaryId,
380 primarySettings: null,
381 environments,
382 }),
383 ).toEqual([]);
384 });
385
386 it("skips ineligible environments and environments without a loaded config", () => {
387 const mismatches = findSharedSettingsMismatches({
388 primaryEnvironmentId: primaryId,
389 primarySettings,
390 environments: [
391 {
392 environmentId: laptopId,
393 label: "Laptop",
394 syncEligible: false,
395 settings: DEFAULT_SERVER_SETTINGS,
396 },
397 { environmentId: boxId, label: "Remote Box", syncEligible: true, settings: null },
398 ],
399 });
400 expect(mismatches).toEqual([]);
401 });
402});