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 defaultThreadEnvMode: "worktree",
48 newWorktreesStartFromOrigin: true,
49 });
50 expect(sharedPatch).toEqual({
51 sidebarAutoSettleAfterDays: 7,
52 sidebarAutoSettleOnMerge: false,
53 continueThreadsAfterServerUpdate: true,
54 newWorktreesStartFromOrigin: true,
55 });
56 expect(localPatch).toEqual({
57 enableAgentBrowserAccess: false,
58 defaultThreadEnvMode: "worktree",
59 });
60 });
61});
62
63describe("pickSharedServerSettings", () => {
64 it("returns only the shared keys", () => {
65 expect(
66 Object.keys(pickSharedServerSettings(DEFAULT_SERVER_SETTINGS, restartCapabilities)).sort(),
67 ).toEqual([
68 "continueThreadsAfterServerUpdate",
69 "newWorktreesStartFromOrigin",
70 "sidebarAutoSettleAfterDays",
71 "sidebarAutoSettleOnMerge",
72 "sourceControlWritingStyle",
73 ]);
74 });
75});
76
77describe("filterSharedServerPatch", () => {
78 it.each([true, false])("preserves supported restart preference %s", (enabled) => {
79 const patch = { continueThreadsAfterServerUpdate: enabled, sidebarAutoSettleAfterDays: 7 };
80 expect(filterSharedServerPatch(patch, restartCapabilities)).toEqual(patch);
81 });
82
83 it.each([undefined, {}, { threadRestartContinuation: false }])(
84 "omits only the unsupported restart preference with capabilities %j",
85 (capabilities) => {
86 expect(
87 filterSharedServerPatch(
88 { continueThreadsAfterServerUpdate: true, sidebarAutoSettleAfterDays: 7 },
89 capabilities,
90 ),
91 ).toEqual({ sidebarAutoSettleAfterDays: 7 });
92 expect(pickSharedServerSettings(DEFAULT_SERVER_SETTINGS, capabilities)).not.toHaveProperty(
93 "continueThreadsAfterServerUpdate",
94 );
95 },
96 );
97});
98
99describe("findSharedSettingsMismatches", () => {
100 const primarySettings = { ...DEFAULT_SERVER_SETTINGS, sidebarAutoSettleAfterDays: 7 };
101
102 it.each([true, false])(
103 "detects remote restart continuation drift when the preference is %s",
104 (enabled) => {
105 const settings = { ...primarySettings, continueThreadsAfterServerUpdate: enabled };
106 const remoteSettings = { ...settings, continueThreadsAfterServerUpdate: !enabled };
107 const environment = {
108 environmentId: boxId,
109 label: "Remote Box",
110 syncEligible: true,
111 settings: remoteSettings,
112 capabilities: restartCapabilities,
113 };
114 expect(
115 findSharedSettingsMismatches({
116 primaryEnvironmentId: primaryId,
117 primarySettings: settings,
118 primaryCapabilities: restartCapabilities,
119 environments: [environment],
120 }),
121 ).toEqual([{ environmentId: boxId, label: "Remote Box" }]);
122 expect(
123 findSharedSettingsMismatches({
124 primaryEnvironmentId: primaryId,
125 primarySettings: settings,
126 primaryCapabilities: restartCapabilities,
127 environments: [
128 {
129 ...environment,
130 settings: Object.assign(
131 {},
132 remoteSettings,
133 pickSharedServerSettings(settings, restartCapabilities),
134 ),
135 },
136 ],
137 }),
138 ).toEqual([]);
139 },
140 );
141
142 it.each([
143 [undefined, restartCapabilities],
144 [restartCapabilities, undefined],
145 [undefined, undefined],
146 ])(
147 "ignores restart drift unless both servers support it (%j, %j)",
148 (primaryCapabilities, capabilities) => {
149 const environment = {
150 environmentId: boxId,
151 label: "Remote Box",
152 syncEligible: true,
153 capabilities,
154 settings: { ...primarySettings, continueThreadsAfterServerUpdate: true },
155 };
156 const input = {
157 primaryEnvironmentId: primaryId,
158 primarySettings,
159 primaryCapabilities,
160 environments: [environment],
161 };
162 expect(findSharedSettingsMismatches(input)).toEqual([]);
163 expect(
164 findSharedSettingsMismatches({
165 ...input,
166 environments: [
167 {
168 ...environment,
169 settings: { ...environment.settings, sidebarAutoSettleAfterDays: 14 },
170 },
171 ],
172 }),
173 ).toEqual([{ environmentId: boxId, label: "Remote Box" }]);
174 },
175 );
176
177 it("lists sync-eligible environments whose shared settings differ", () => {
178 const mismatches = findSharedSettingsMismatches({
179 primaryEnvironmentId: primaryId,
180 primarySettings,
181 environments: [
182 {
183 environmentId: primaryId,
184 label: "Desktop",
185 syncEligible: true,
186 settings: primarySettings,
187 },
188 {
189 environmentId: laptopId,
190 label: "Laptop",
191 syncEligible: true,
192 settings: primarySettings,
193 },
194 {
195 environmentId: boxId,
196 label: "Remote Box",
197 syncEligible: true,
198 settings: DEFAULT_SERVER_SETTINGS,
199 },
200 ],
201 });
202 expect(mismatches).toEqual([{ environmentId: boxId, label: "Remote Box" }]);
203 });
204
205 it("ignores machine-only differences", () => {
206 const mismatches = findSharedSettingsMismatches({
207 primaryEnvironmentId: primaryId,
208 primarySettings,
209 environments: [
210 {
211 environmentId: boxId,
212 label: "Remote Box",
213 syncEligible: true,
214 settings: {
215 ...primarySettings,
216 enableAgentBrowserAccess: false,
217 defaultThreadEnvMode:
218 primarySettings.defaultThreadEnvMode === "local" ? "worktree" : "local",
219 },
220 },
221 ],
222 });
223 expect(mismatches).toEqual([]);
224 });
225
226 it("reports nothing until the primary environment's settings are loaded", () => {
227 const environments = [
228 {
229 environmentId: boxId,
230 label: "Remote Box",
231 syncEligible: true,
232 settings: primarySettings,
233 },
234 ];
235 expect(
236 findSharedSettingsMismatches({ primaryEnvironmentId: null, primarySettings, environments }),
237 ).toEqual([]);
238 expect(
239 findSharedSettingsMismatches({
240 primaryEnvironmentId: primaryId,
241 primarySettings: null,
242 environments,
243 }),
244 ).toEqual([]);
245 });
246
247 it("skips ineligible environments and environments without a loaded config", () => {
248 const mismatches = findSharedSettingsMismatches({
249 primaryEnvironmentId: primaryId,
250 primarySettings,
251 environments: [
252 {
253 environmentId: laptopId,
254 label: "Laptop",
255 syncEligible: false,
256 settings: DEFAULT_SERVER_SETTINGS,
257 },
258 { environmentId: boxId, label: "Remote Box", syncEligible: true, settings: null },
259 ],
260 });
261 expect(mismatches).toEqual([]);
262 });
263});