Skip to content

abso-core

Public

abso is version control for agents: the client, the engine and a server you can run on your own Cloudflare account.

Check passed
398 linesFirst import 100% · infra-split 1%0 versions changed it
1import assert from "node:assert/strict";
2import { test } from "node:test";
3import { unresolvedConflicts } from "../../../src/engine/index.ts";
4import { type Files, MemoryStore, Project, ProjectError } from "../project.ts";
5
6const START = {
7 "engine.ts": "export function match(p, s) {\n return null;\n}\n",
8 "notes.md": "one\ntwo\nthree\nfour\nfive\n",
9};
11function setup(files: Record<string, string> = START): { project: Project; store: MemoryStore } {
12 const store = new MemoryStore();
13 const project = new Project(store);
14 project.init(files, 1);
15 return { project, store };
18/** A folder that follows what the server hands back, like the client does. */
19function apply(folder: Record<string, string>, files: Files): void {
20 for (const [path, text] of Object.entries(files)) {
21 if (text === null) delete folder[path];
22 else folder[path] = text;
23 }
26test("a first sync hands back every file, empty ones included", () => {
27 const store = new MemoryStore();
28 const project = new Project(store);
29 assert.deepEqual(project.init({ ...START, "empty.txt": "" }, 1), { version: 0, skipped: [] });
30 const answer = project.sync("backtracker");
31 assert.equal(answer.version, 0);
32 assert.deepEqual(answer.files, { ...START, "empty.txt": "" });
33 assert.deepEqual(answer.collisions, []);
34 assert.deepEqual(project.sync("backtracker").files, {});
35});
37test("an edit joins the draft, publish adds it to shared, and sync brings it to others", () => {
38 const { project } = setup();
39 project.sync("a");
40 project.sync("b");
41 assert.deepEqual(project.edit("a", { file: "notes.md", old: "two\n", new: "TWO\n" }, 2), { ok: true, news: [] });
42 assert.deepEqual(project.status().agents, [
43 {
44 agent: "a",
45 draft: null,
46 base: 0,
47 draftChanges: 1,
48 files: ["notes.md"],
49 parked: [],
50 edits: "commands",
51 modCurrent: null,
52 lastEdit: 2,
53 joined: 1,
54 },
55 {
56 agent: "b",
57 draft: null,
58 base: 0,
59 draftChanges: 0,
60 files: [],
61 parked: [],
62 edits: "none",
63 modCurrent: null,
64 lastEdit: null,
65 joined: 3,
66 },
67 ]);
68 assert.deepEqual(project.publish("a", "Shout two", 3), { ok: true, version: 1, changes: 1 });
69 assert.equal(project.files()["notes.md"], "one\nTWO\nthree\nfour\nfive\n");
70 assert.deepEqual(project.published(1), { agent: "a", message: "Shout two", time: 3 });
71 const b = project.sync("b");
72 assert.deepEqual(b, { version: 1, files: { "notes.md": "one\nTWO\nthree\nfour\nfive\n" }, collisions: [] });
73 assert.equal(project.status().agents[0]!.base, 1);
74});
76test("an edit whose old text is not there exactly once is out of step", () => {
77 const { project } = setup();
78 project.sync("a");
79 assert.deepEqual(project.edit("a", { file: "notes.md", old: "six\n", new: "x\n" }, 2), {
80 ok: false,
81 reason: "out of step",
82 });
83 assert.deepEqual(project.edit("a", { file: "engine.ts", old: "n", new: "x" }, 2), {
84 ok: false,
85 reason: "out of step",
86 });
87 assert.deepEqual(project.edit("a", { file: "missing.ts", old: "x", new: "y" }, 2), {
88 ok: false,
89 reason: "out of step",
90 });
91 assert.equal(project.status().agents[0]!.draftChanges, 0);
92});
94test("Write and MultiEdit, and record creates, changes and deletes files", () => {
95 const { project } = setup();
96 project.sync("a");
97 project.edit("a", { file: "new.ts", content: "export const x = 1;\n" }, 2);
98 project.edit(
99 "a",
100 {
101 file: "notes.md",
102 edits: [
103 { old: "one\n", new: "ONE\n" },
104 { old: "ONE\ntwo", new: "ONE\n2" },
105 ],
106 },
107 3,
108 );
109 assert.equal(project.edit("a", { file: "notes.md", edits: [{ old: "zzz", new: "y" }] }, 3).ok, false);
110 const r = project.record(
111 "a",
112 { "engine.ts": null, "other.ts": "let y\r\n", "notes.md": "ONE\n2\nthree\nfour\nfive\n" },
113 4,
114 );
115 assert.deepEqual(r, { ok: true, changed: ["engine.ts", "other.ts"], news: [] });
116 project.publish("a", "Rework", 5);
117 assert.deepEqual(project.files(), {
118 "new.ts": "export const x = 1;\n",
119 "notes.md": "ONE\n2\nthree\nfour\nfive\n",
120 "other.ts": "let y\n",
121 });
122});
124test("changes that do not collide publish in any order and give the same files", () => {
125 const edits: [string, string, string, string][] = [
126 ["a", "notes.md", "one\n", "uno\n"],
127 ["b", "notes.md", "four\n", "cuatro\n"],
128 ["c", "engine.ts", " return null;\n", " return [];\n"],
129 ["d", "added.ts", "", "export {};\n"],
130 ];
131 const orders = [
132 [0, 1, 2, 3],
133 [3, 2, 1, 0],
134 [1, 3, 0, 2],
135 [2, 0, 3, 1],
136 ];
137 const results = orders.map((order) => {
138 const { project } = setup();
139 for (const [agent] of edits) project.sync(agent);
140 for (const [agent, file, old, nu] of edits) {
141 if (old === "") project.edit(agent, { file, content: nu }, 2);
142 // No collision news; b hears only that a is in notes.md too (#37).
143 else {
144 const a = project.edit(agent, { file, old, new: nu }, 2);
145 assert.deepEqual(a.ok && a.news.filter((n) => !n.includes("which you just started changing")), []);
146 }
147 }
148 for (const i of order) {
149 const [agent] = edits[i]!;
150 assert.equal(project.publish(agent, `from ${agent}`, 3).ok, true, `publish by ${agent}`);
151 }
152 return project.files();
153 });
154 for (const r of results) assert.deepEqual(r, results[0]);
155 assert.deepEqual(results[0], {
156 "added.ts": "export {};\n",
157 "engine.ts": "export function match(p, s) {\n return [];\n}\n",
158 "notes.md": "uno\ntwo\nthree\ncuatro\nfive\n",
159 });
160});
162test("a collision refuses the publish, sends news, and sync then an edit settles it", () => {
163 const { project } = setup();
164 const folderB: Record<string, string> = {};
165 apply(folderB, project.sync("a").files);
166 apply(folderB, project.sync("b").files);
168 project.edit("b", { file: "notes.md", old: "three\n", new: "THREE (b)\n" }, 2);
169 project.edit("a", { file: "notes.md", old: "three\n", new: "three (a)\n" }, 3);
170 assert.deepEqual(project.publish("a", "Mark three for a", 4), { ok: true, version: 1, changes: 1 });
172 // b hears about it with its next edit.
173 const heard = project.edit("b", { file: "engine.ts", old: "return null", new: "return undefined" }, 5);
174 assert.equal(heard.ok, true);
175 assert.deepEqual(heard.ok && heard.news, [
176 'a published "Mark three for a" (version 1), which changes notes.md lines 3-3, where your draft also changes lines; run abso sync to see both versions.',
177 ]);
178 assert.deepEqual(project.edit("b", { file: "engine.ts", old: "undefined", new: "void 0" }, 5), {
179 ok: true,
180 news: [],
181 });
183 const refused = project.publish("b", "Mark three for b", 6);
184 assert.equal(refused.ok, false);
185 assert.deepEqual(!refused.ok && refused.collisions, [
186 {
187 kind: "lines",
188 file: "notes.md",
189 yours: { lines: [3, 3], text: "THREE (b)\n" },
190 theirs: { lines: [3, 3], text: "three (a)\n", agent: "a", message: "Mark three for a", version: 1 },
191 },
192 ]);
193 assert.equal(project.version, 1);
195 // Sync brings both versions into b's file between markers (#11), and lists the collision.
196 const synced = project.sync("b");
197 apply(folderB, synced.files);
198 assert.deepEqual(
199 synced.collisions.map((c) => [c.kind, c.marked, c.yours.lines]),
200 [["lines", true, [3, 9]]],
201 );
202 const block =
203 '<<<<<<< yours (unpublished)\nTHREE (b)\n||||||| before (version 0)\nthree\n=======\nthree (a)\n>>>>>>> theirs: a, version 1 "Mark three for a"\n';
204 assert.equal(folderB["notes.md"], `one\ntwo\n${block}four\nfive\n`);
205 assert.deepEqual(Object.keys(synced.files), ["notes.md"]); // b's own engine.ts edit was already in its folder
207 // b settles it with an edit that takes the markers out, and publish goes through, saying what b kept.
208 project.edit("b", { file: "notes.md", old: block, new: "three (a and b)\n" }, 7);
209 const settled = project.publish("b", "Settle three", 8);
210 assert.deepEqual(settled.ok && "version" in settled && [settled.version, settled.settled], [
211 2,
212 [{ file: "notes.md", kept: "rewrote", theirs: { agent: "a", version: 1 } }],
213 ]);
214 assert.equal(project.files()["notes.md"], "one\ntwo\nthree (a and b)\nfour\nfive\n");
215 assert.equal(project.files()["engine.ts"], "export function match(p, s) {\n return void 0;\n}\n");
216 // Shared, not every atom: a sync that writes markers (#11) discards the draft change it replaces.
217 const shared = project.sharedView().set;
218 assert.deepEqual(unresolvedConflicts(project.graph, shared), []);
219});
221test("publishing with the markers still in is refused; asIs publishes both and settles it (#37, #11)", () => {
222 const { project } = setup();
223 project.sync("a");
224 project.sync("b");
225 project.edit("a", { file: "notes.md", old: "two\n", new: "two-a\n" }, 2);
226 project.edit("b", { file: "notes.md", old: "two\n", new: "two-b\n" }, 2);
227 project.publish("a", "a", 3);
228 assert.equal(project.publish("b", "b", 4).ok, false);
229 assert.equal(project.sync("b").collisions.length, 1);
230 const marked = project.publish("b", "Keep both lines", 5);
231 assert.deepEqual(!marked.ok && marked.collisions.map((c) => [c.kind, c.marked]), [["lines", true]]);
232 assert.equal(project.publish("b", "Keep both lines", 5, { asIs: true }).ok, true);
233 assert.equal(project.files()["notes.md"], "one\ntwo-b\ntwo-a\nthree\nfour\nfive\n");
234 const shared = project.sharedView().set;
235 assert.deepEqual(unresolvedConflicts(project.graph, shared), []);
236});
238test("after publishing without syncing, the agent keeps editing its own folder", () => {
239 const { project } = setup();
240 project.sync("a");
241 project.sync("b");
242 project.edit("b", { file: "notes.md", old: "five\n", new: "five-b\n" }, 2);
243 project.publish("b", "b first", 3);
244 project.edit("a", { file: "notes.md", old: "one\n", new: "one-a\n" }, 4);
245 assert.equal(project.publish("a", "a after b", 5).ok, true);
246 // a's folder still says "five": an edit against it must match.
247 assert.deepEqual(project.edit("a", { file: "notes.md", old: "one-a\ntwo\n", new: "one-a\n2\n" }, 6), {
248 ok: true,
249 news: [],
250 });
251 assert.equal(project.status().agents[0]!.base, 0);
252 const s = project.sync("a");
253 assert.deepEqual(s.files, { "notes.md": "one-a\n2\nthree\nfour\nfive-b\n" });
254 // With nobody else in between, publishing moves the base along.
255 project.sync("b");
256 project.edit("b", { file: "notes.md", old: "three\n", new: "3\n" }, 7);
257 project.publish("b", "b again", 8);
258 assert.equal(project.status().agents[1]!.base, project.version);
259});
261test("a reloaded project is the same project and keeps going", () => {
262 const { project, store } = setup();
263 project.sync("a");
264 project.sync("b");
265 project.edit("a", { file: "notes.md", old: "two\n", new: "two-a\n" }, 2);
266 project.edit("b", { file: "notes.md", old: "two\n", new: "two-b\n" }, 2);
267 project.publish("a", "a", 3);
268 const again = Project.load(store.entries, new MemoryStore());
269 assert.deepEqual(again.status(), project.status());
270 assert.deepEqual(again.files(), project.files());
271 // The news b hasn't heard yet survives the reload.
272 const heard = again.record("b", {}, 4);
273 assert.equal(heard.news.length, 1);
274 assert.equal(again.publish("b", "b", 5).ok, false);
275});
277test("bad input is a ProjectError", () => {
278 const { project } = setup();
279 assert.throws(
280 () => project.sync("bad name"),
281 (e) => e instanceof ProjectError && e.code === "bad-agent",
282 );
283 assert.throws(
284 () => project.init({}, 1),
285 (e) => e instanceof ProjectError && e.code === "already-initialized",
286 );
287 assert.throws(
288 () => new Project(new MemoryStore()).status(),
289 (e) => e instanceof ProjectError && e.code === "not-initialized",
290 );
291 assert.throws(
292 () => project.edit("a", { file: "", content: "x" }, 1),
293 (e) => e instanceof ProjectError && e.code === "bad-request",
294 );
295});
297test("each call stores what it changed in one append", () => {
298 const store = new MemoryStore();
299 const calls: number[] = [];
300 const counting = {
301 append: (e: readonly unknown[]) => {
302 calls.push(e.length);
303 store.append(e as never);
304 },
305 };
306 const project = new Project(counting);
307 project.init(START, 1);
308 project.sync("a");
309 assert.deepEqual(project.status().version, 0); // status writes nothing
310 project.edit("a", { file: "notes.md", old: "one\n", new: "1\n" }, 2);
311 project.publish("a", "m", 3);
312 assert.deepEqual(calls, [1, 2, 1, 1]);
313 assert.deepEqual(
314 store.entries.map((e) => e.type),
315 ["init", "join", "sync", "draft", "publish"],
316 );
317});
319test("a call that joins an agent and then throws still stores the join, so the log loads", () => {
320 // From the live drafts-smoke project: a new agent's first call was a switch
321 // to a bad draft name; its join was applied but never stored, and the
322 // agent's next sync made a log that no longer loaded.
323 const store = new MemoryStore();
324 const project = new Project(store);
325 project.init({ "a.txt": "a\n" }, 0);
326 assert.throws(() => project.switchTo("newbie", "bad name!", 1));
327 project.sync("newbie");
328 assert.deepEqual(
329 store.entries.map((e) => e.type),
330 ["init", "join", "sync"],
331 );
332 assert.deepEqual(Project.load(store.entries, new MemoryStore()).status(), project.status());
333});
335test("a log already missing a join loads: the join is put back at the version it had", () => {
336 const store = new MemoryStore();
337 const project = new Project(store);
338 project.init({ "a.txt": "a\n" }, 0);
339 project.edit("ann", { file: "a.txt", old: "a", new: "b" }, 1);
340 project.publish("ann", "b", 2);
341 project.sync("bob");
342 const broken = store.entries.filter((e) => !(e.type === "join" && e.agent === "bob"));
343 const loaded = Project.load(broken, new MemoryStore());
344 assert.deepEqual(loaded.status(), project.status());
345 assert.deepEqual(loaded.files(), project.files());
346});
348test("a refused publish is logged and counted, and the counts survive a reload", () => {
349 const store = new MemoryStore();
350 const project = new Project(store);
351 project.init({ "a.txt": "a\n" }, 0);
352 project.sync("ann");
353 project.sync("bob");
354 project.edit("ann", { file: "a.txt", old: "a", new: "b" }, 1);
355 project.edit("bob", { file: "a.txt", old: "a", new: "c" }, 1);
356 project.publish("ann", "b", 2);
357 assert.equal(project.publish("bob", "c", 3).ok, false);
358 assert.deepEqual(project.status().refusals, { total: 1, lines: 1, definition: 0, attribute: 0, duplicate: 0 });
359 assert.deepEqual(project.refusals().refusals, [
360 { agent: "bob", draft: null, via: "publish", time: 3, collisions: [{ kind: "lines", file: "a.txt" }] },
361 ]);
362 assert.deepEqual(Project.load(store.entries, new MemoryStore()).refusals(), project.refusals());
363});
365test("a full sync answers every file in the view, even for an agent that synced before; file listings change nothing (#37)", () => {
366 const store = new MemoryStore();
367 const project = new Project(store);
368 project.init({ "a.txt": "a\n", "src/b.ts": "b\n" }, 0);
369 project.sync("artist1");
370 project.edit("lead", { file: "src/c.ts", content: "c\n" }, 1);
371 project.publish("lead", "c", 2);
372 // A plain sync gives only what changed since; a full one, everything.
373 assert.deepEqual(Object.keys(project.sync("artist1").files), ["src/c.ts"]);
374 assert.deepEqual(Object.keys(project.sync("artist1", { full: true }).files), ["a.txt", "src/b.ts", "src/c.ts"]);
375 const before = store.entries.length;
376 assert.deepEqual(project.agentPaths("artist1"), {
377 version: 1,
378 draft: null,
379 files: ["a.txt", "src/b.ts", "src/c.ts"],
380 });
381 assert.deepEqual(project.pathsAt(0), { version: 0, draft: null, files: ["a.txt", "src/b.ts"] });
382 assert.equal(store.entries.length, before, "reading the listings logs nothing");
383 assert.throws(() => project.agentPaths("nobody"), /join first/);
384});
386test("a log entry of a kind this engine doesn't know stops the load instead of being skipped", () => {
387 const store = new MemoryStore();
388 const project = new Project(store);
389 project.init({ "a.txt": "a\n" }, 0);
390 const entries = [
391 ...store.entries,
392 { type: "from-the-future", agent: "x", time: 1 } as unknown as (typeof store.entries)[number],
393 ];
394 assert.throws(
395 () => Project.load(entries, new MemoryStore()),
396 /log entry 1 is of kind "from-the-future", which this version of abso doesn't know/,
397 );
398});