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

sharedSettings.test.ts

403 lines13.0 KB

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 "autoResumeLimitedThreads",
127 "continueThreadsAfterServerUpdate",
128 "newWorktreesStartFromOrigin",
129 "sidebarAutoSettleAfterDays",
130 "sidebarAutoSettleOnMerge",
131 "sourceControlWritingStyle",
132 "textGenerationModelSelection",
133 ]);
134 });
135});
136​
137describe("filterSharedServerPatch", () => {
138 it.each([true, false])(
139 "resets a disabled default provider only on the originating environment (%s)",
140 (targetIsSource) => {
141 const settings = {
142 ...DEFAULT_SERVER_SETTINGS,
143 providerInstances: {
144 codex: { driver: ProviderDriverKind.make("codex"), enabled: false, config: {} },
145 claudeAgent: {
146 driver: ProviderDriverKind.make("claudeAgent"),
147 enabled: true,
148 config: {},
149 },
150 },
151 textGenerationModelSelection: {
152 instanceId: ProviderInstanceId.make("claudeAgent"),
153 model: "claude-opus-4-6",
154 },
155 };
156 const patch = {
157 textGenerationModelSelection: DEFAULT_SERVER_SETTINGS.textGenerationModelSelection,
158 continueThreadsAfterServerUpdate: true,
159 sidebarAutoSettleAfterDays: 7,
160 };
161 expect(filterSharedServerPatch(patch, undefined, settings, settings, targetIsSource)).toEqual(
162 {
163 ...(targetIsSource
164 ? { textGenerationModelSelection: DEFAULT_SERVER_SETTINGS.textGenerationModelSelection }
165 : {}),
166 sidebarAutoSettleAfterDays: 7,
167 },
168 );
169 },
170 );
171​
172 it.each(["missing", "disabled", "different-driver", "enabled"] as const)(
173 "shares a custom model only when its target provider is enabled (%s)",
174 (availability) => {
175 const instanceId = ProviderInstanceId.make("codex_personal");
176 const selection = {
177 instanceId,
178 model: "gpt-5.6-luna",
179 options: [{ id: "reasoningEffort", value: "low" }],
180 };
181 const instance = {
182 driver: ProviderDriverKind.make(
183 availability === "different-driver" ? "claudeAgent" : "codex",
184 ),
185 enabled: availability !== "disabled",
186 config: {},
187 };
188 const settings = {
189 ...DEFAULT_SERVER_SETTINGS,
190 providerInstances: availability === "missing" ? {} : { [instanceId]: instance },
191 };
192 const patch = { sidebarAutoSettleAfterDays: 7, textGenerationModelSelection: selection };
193 const sourceSettings = {
194 ...settings,
195 providerInstances: {
196 [instanceId]: { ...instance, driver: ProviderDriverKind.make("codex"), enabled: true },
197 },
198 };
199 expect(filterSharedServerPatch(patch, restartCapabilities, settings, sourceSettings)).toEqual(
200 availability === "enabled" ? patch : { sidebarAutoSettleAfterDays: 7 },
201 );
202 const primarySettings = {
203 ...sourceSettings,
204 textGenerationModelSelection: selection,
205 };
206 expect(
207 findSharedSettingsMismatches({
208 primaryEnvironmentId: primaryId,
209 primarySettings,
210 environments: [
211 { environmentId: boxId, label: "Remote Box", syncEligible: true, settings },
212 ],
213 }),
214 ).toEqual(availability === "enabled" ? [{ environmentId: boxId, label: "Remote Box" }] : []);
215 },
216 );
217​
218 it.each([true, false])("preserves supported restart preference %s", (enabled) => {
219 const patch = { continueThreadsAfterServerUpdate: enabled, sidebarAutoSettleAfterDays: 7 };
220 expect(filterSharedServerPatch(patch, restartCapabilities)).toEqual(patch);
221 });
222​
223 it.each([undefined, {}, { threadRestartContinuation: false }])(
224 "omits only the unsupported restart preference with capabilities %j",
225 (capabilities) => {
226 expect(
227 filterSharedServerPatch(
228 { continueThreadsAfterServerUpdate: true, sidebarAutoSettleAfterDays: 7 },
229 capabilities,
230 ),
231 ).toEqual({ sidebarAutoSettleAfterDays: 7 });
232 expect(pickSharedServerSettings(DEFAULT_SERVER_SETTINGS, capabilities)).not.toHaveProperty(
233 "continueThreadsAfterServerUpdate",
234 );
235 },
236 );
237});
238​
239describe("findSharedSettingsMismatches", () => {
240 const primarySettings = { ...DEFAULT_SERVER_SETTINGS, sidebarAutoSettleAfterDays: 7 };
241​
242 it.each([true, false])(
243 "detects remote restart continuation drift when the preference is %s",
244 (enabled) => {
245 const settings = { ...primarySettings, continueThreadsAfterServerUpdate: enabled };
246 const remoteSettings = { ...settings, continueThreadsAfterServerUpdate: !enabled };
247 const environment = {
248 environmentId: boxId,
249 label: "Remote Box",
250 syncEligible: true,
251 settings: remoteSettings,
252 capabilities: restartCapabilities,
253 };
254 expect(
255 findSharedSettingsMismatches({
256 primaryEnvironmentId: primaryId,
257 primarySettings: settings,
258 primaryCapabilities: restartCapabilities,
259 environments: [environment],
260 }),
261 ).toEqual([{ environmentId: boxId, label: "Remote Box" }]);
262 expect(
263 findSharedSettingsMismatches({
264 primaryEnvironmentId: primaryId,
265 primarySettings: settings,
266 primaryCapabilities: restartCapabilities,
267 environments: [
268 {
269 ...environment,
270 settings: Object.assign(
271 {},
272 remoteSettings,
273 pickSharedServerSettings(settings, restartCapabilities),
274 ),
275 },
276 ],
277 }),
278 ).toEqual([]);
279 },
280 );
281​
282 it.each([
283 [undefined, restartCapabilities],
284 [restartCapabilities, undefined],
285 [undefined, undefined],
286 ])(
287 "ignores restart drift unless both servers support it (%j, %j)",
288 (primaryCapabilities, capabilities) => {
289 const environment = {
290 environmentId: boxId,
291 label: "Remote Box",
292 syncEligible: true,
293 capabilities,
294 settings: { ...primarySettings, continueThreadsAfterServerUpdate: true },
295 };
296 const input = {
297 primaryEnvironmentId: primaryId,
298 primarySettings,
299 primaryCapabilities,
300 environments: [environment],
301 };
302 expect(findSharedSettingsMismatches(input)).toEqual([]);
303 expect(
304 findSharedSettingsMismatches({
305 ...input,
306 environments: [
307 {
308 ...environment,
309 settings: { ...environment.settings, sidebarAutoSettleAfterDays: 14 },
310 },
311 ],
312 }),
313 ).toEqual([{ environmentId: boxId, label: "Remote Box" }]);
314 },
315 );
316​
317 it("lists sync-eligible environments whose shared settings differ", () => {
318 const mismatches = findSharedSettingsMismatches({
319 primaryEnvironmentId: primaryId,
320 primarySettings,
321 environments: [
322 {
323 environmentId: primaryId,
324 label: "Desktop",
325 syncEligible: true,
326 settings: primarySettings,
327 },
328 {
329 environmentId: laptopId,
330 label: "Laptop",
331 syncEligible: true,
332 settings: primarySettings,
333 },
334 {
335 environmentId: boxId,
336 label: "Remote Box",
337 syncEligible: true,
338 settings: DEFAULT_SERVER_SETTINGS,
339 },
340 ],
341 });
342 expect(mismatches).toEqual([{ environmentId: boxId, label: "Remote Box" }]);
343 });
344​
345 it("ignores machine-only differences", () => {
346 const mismatches = findSharedSettingsMismatches({
347 primaryEnvironmentId: primaryId,
348 primarySettings,
349 environments: [
350 {
351 environmentId: boxId,
352 label: "Remote Box",
353 syncEligible: true,
354 settings: {
355 ...primarySettings,
356 enableAgentBrowserAccess: false,
357 defaultThreadEnvMode:
358 primarySettings.defaultThreadEnvMode === "local" ? "worktree" : "local",
359 },
360 },
361 ],
362 });
363 expect(mismatches).toEqual([]);
364 });
365​
366 it("reports nothing until the primary environment's settings are loaded", () => {
367 const environments = [
368 {
369 environmentId: boxId,
370 label: "Remote Box",
371 syncEligible: true,
372 settings: primarySettings,
373 },
374 ];
375 expect(
376 findSharedSettingsMismatches({ primaryEnvironmentId: null, primarySettings, environments }),
377 ).toEqual([]);
378 expect(
379 findSharedSettingsMismatches({
380 primaryEnvironmentId: primaryId,
381 primarySettings: null,
382 environments,
383 }),
384 ).toEqual([]);
385 });
386​
387 it("skips ineligible environments and environments without a loaded config", () => {
388 const mismatches = findSharedSettingsMismatches({
389 primaryEnvironmentId: primaryId,
390 primarySettings,
391 environments: [
392 {
393 environmentId: laptopId,
394 label: "Laptop",
395 syncEligible: false,
396 settings: DEFAULT_SERVER_SETTINGS,
397 },
398 { environmentId: boxId, label: "Remote Box", syncEligible: true, settings: null },
399 ],
400 });
401 expect(mismatches).toEqual([]);
402 });
403});