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