Open History to see the last commit
1import { DEFAULT_SERVER_SETTINGS, EnvironmentId } from "@t3tools/contracts";
2import { describe, expect, it } from "@effect/vitest";
3
4import {
5 filterSharedServerPatch,
6 findSharedSettingsMismatches,
7 pickSharedServerSettings,
8 splitSharedServerPatch,
9 supportsSharedSettingsSync,
10} from "./sharedSettings.ts";
11
12const primaryId = EnvironmentId.make("env-primary");
13const laptopId = EnvironmentId.make("env-laptop");
14const boxId = EnvironmentId.make("env-box");
15const restartCapabilities = { threadRestartContinuation: true };
16
17describe("supportsSharedSettingsSync", () => {
18 it("accepts only connected servers that advertise the shared-settings capability", () => {
19 expect(
20 supportsSharedSettingsSync({
21 connection: { phase: "connected" },
22 serverConfig: { environment: { capabilities: { threadAutoSettlement: true } } },
23 }),
24 ).toBe(true);
25 expect(
26 supportsSharedSettingsSync({
27 connection: { phase: "connected" },
28 serverConfig: { environment: { capabilities: {} } },
29 }),
30 ).toBe(false);
31 expect(
32 supportsSharedSettingsSync({
33 connection: { phase: "reconnecting" },
34 serverConfig: { environment: { capabilities: { threadAutoSettlement: true } } },
35 }),
36 ).toBe(false);
37 });
38});
39
40describe("splitSharedServerPatch", () => {
41 it("routes preference keys to the shared patch and machine keys to the local patch", () => {
42 const { sharedPatch, localPatch } = splitSharedServerPatch({
43 sidebarAutoSettleAfterDays: 7,
44 sidebarAutoSettleOnMerge: false,
45 continueThreadsAfterServerUpdate: true,
46 enableAgentBrowserAccess: false,
47 });
48 expect(sharedPatch).toEqual({
49 sidebarAutoSettleAfterDays: 7,
50 sidebarAutoSettleOnMerge: false,
51 continueThreadsAfterServerUpdate: true,
52 });
53 expect(localPatch).toEqual({ enableAgentBrowserAccess: false });
54 });
55});
56
57describe("pickSharedServerSettings", () => {
58 it("returns only the shared keys", () => {
59 expect(
60 Object.keys(pickSharedServerSettings(DEFAULT_SERVER_SETTINGS, restartCapabilities)).sort(),
61 ).toEqual([
62 "continueThreadsAfterServerUpdate",
63 "defaultThreadEnvMode",
64 "newWorktreesStartFromOrigin",
65 "sidebarAutoSettleAfterDays",
66 "sidebarAutoSettleOnMerge",
67 "sourceControlWritingStyle",
68 ]);
69 });
70});
71
72describe("filterSharedServerPatch", () => {
73 it.each([true, false])("preserves supported restart preference %s", (enabled) => {
74 const patch = { continueThreadsAfterServerUpdate: enabled, sidebarAutoSettleAfterDays: 7 };
75 expect(filterSharedServerPatch(patch, restartCapabilities)).toEqual(patch);
76 });
77
78 it.each([undefined, {}, { threadRestartContinuation: false }])(
79 "omits only the unsupported restart preference with capabilities %j",
80 (capabilities) => {
81 expect(
82 filterSharedServerPatch(
83 { continueThreadsAfterServerUpdate: true, sidebarAutoSettleAfterDays: 7 },
84 capabilities,
85 ),
86 ).toEqual({ sidebarAutoSettleAfterDays: 7 });
87 expect(pickSharedServerSettings(DEFAULT_SERVER_SETTINGS, capabilities)).not.toHaveProperty(
88 "continueThreadsAfterServerUpdate",
89 );
90 },
91 );
92});
93
94describe("findSharedSettingsMismatches", () => {
95 const primarySettings = { ...DEFAULT_SERVER_SETTINGS, sidebarAutoSettleAfterDays: 7 };
96
97 it.each([true, false])(
98 "detects remote restart continuation drift when the preference is %s",
99 (enabled) => {
100 const settings = { ...primarySettings, continueThreadsAfterServerUpdate: enabled };
101 const remoteSettings = { ...settings, continueThreadsAfterServerUpdate: !enabled };
102 const environment = {
103 environmentId: boxId,
104 label: "Remote Box",
105 syncEligible: true,
106 settings: remoteSettings,
107 capabilities: restartCapabilities,
108 };
109 expect(
110 findSharedSettingsMismatches({
111 primaryEnvironmentId: primaryId,
112 primarySettings: settings,
113 primaryCapabilities: restartCapabilities,
114 environments: [environment],
115 }),
116 ).toEqual([{ environmentId: boxId, label: "Remote Box" }]);
117 expect(
118 findSharedSettingsMismatches({
119 primaryEnvironmentId: primaryId,
120 primarySettings: settings,
121 primaryCapabilities: restartCapabilities,
122 environments: [
123 {
124 ...environment,
125 settings: Object.assign(
126 {},
127 remoteSettings,
128 pickSharedServerSettings(settings, restartCapabilities),
129 ),
130 },
131 ],
132 }),
133 ).toEqual([]);
134 },
135 );
136
137 it.each([
138 [undefined, restartCapabilities],
139 [restartCapabilities, undefined],
140 [undefined, undefined],
141 ])(
142 "ignores restart drift unless both servers support it (%j, %j)",
143 (primaryCapabilities, capabilities) => {
144 const environment = {
145 environmentId: boxId,
146 label: "Remote Box",
147 syncEligible: true,
148 capabilities,
149 settings: { ...primarySettings, continueThreadsAfterServerUpdate: true },
150 };
151 const input = {
152 primaryEnvironmentId: primaryId,
153 primarySettings,
154 primaryCapabilities,
155 environments: [environment],
156 };
157 expect(findSharedSettingsMismatches(input)).toEqual([]);
158 expect(
159 findSharedSettingsMismatches({
160 ...input,
161 environments: [
162 {
163 ...environment,
164 settings: { ...environment.settings, sidebarAutoSettleAfterDays: 14 },
165 },
166 ],
167 }),
168 ).toEqual([{ environmentId: boxId, label: "Remote Box" }]);
169 },
170 );
171
172 it("lists sync-eligible environments whose shared settings differ", () => {
173 const mismatches = findSharedSettingsMismatches({
174 primaryEnvironmentId: primaryId,
175 primarySettings,
176 environments: [
177 {
178 environmentId: primaryId,
179 label: "Desktop",
180 syncEligible: true,
181 settings: primarySettings,
182 },
183 {
184 environmentId: laptopId,
185 label: "Laptop",
186 syncEligible: true,
187 settings: primarySettings,
188 },
189 {
190 environmentId: boxId,
191 label: "Remote Box",
192 syncEligible: true,
193 settings: DEFAULT_SERVER_SETTINGS,
194 },
195 ],
196 });
197 expect(mismatches).toEqual([{ environmentId: boxId, label: "Remote Box" }]);
198 });
199
200 it("ignores machine-only differences", () => {
201 const mismatches = findSharedSettingsMismatches({
202 primaryEnvironmentId: primaryId,
203 primarySettings,
204 environments: [
205 {
206 environmentId: boxId,
207 label: "Remote Box",
208 syncEligible: true,
209 settings: { ...primarySettings, enableAgentBrowserAccess: false },
210 },
211 ],
212 });
213 expect(mismatches).toEqual([]);
214 });
215
216 it("reports nothing until the primary environment's settings are loaded", () => {
217 const environments = [
218 {
219 environmentId: boxId,
220 label: "Remote Box",
221 syncEligible: true,
222 settings: primarySettings,
223 },
224 ];
225 expect(
226 findSharedSettingsMismatches({ primaryEnvironmentId: null, primarySettings, environments }),
227 ).toEqual([]);
228 expect(
229 findSharedSettingsMismatches({
230 primaryEnvironmentId: primaryId,
231 primarySettings: null,
232 environments,
233 }),
234 ).toEqual([]);
235 });
236
237 it("skips ineligible environments and environments without a loaded config", () => {
238 const mismatches = findSharedSettingsMismatches({
239 primaryEnvironmentId: primaryId,
240 primarySettings,
241 environments: [
242 {
243 environmentId: laptopId,
244 label: "Laptop",
245 syncEligible: false,
246 settings: DEFAULT_SERVER_SETTINGS,
247 },
248 { environmentId: boxId, label: "Remote Box", syncEligible: true, settings: null },
249 ],
250 });
251 expect(mismatches).toEqual([]);
252 });
253});