- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/worker/project/test/project.test.ts?format=json
406 linesFirst import 95% · speed 3% · rename 2% · infra-split 1%Last change speed in version 160, 4 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";
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 };
16}
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 }
24}
26test("a first pull 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, push adds it to shared, and pull 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 push 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, `push 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 push, sends news, and pull 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 pushed "Mark three for a" (version 1), which changes notes.md lines 3-3, where your draft also changes lines; run abso pull 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 // and what its held push said.
209 project.edit("b", { file: "notes.md", old: block, new: "three (a and b)\n" }, 7);
210 const settled = project.publish("b", "Settle three", 8);
211 assert.deepEqual(settled.ok && "version" in settled && [settled.version, settled.settled], [
212 2,
213 [{ file: "notes.md", kept: "rewrote", theirs: { agent: "a", version: 1 }, held: "Mark three for b" }],
214 ]);
215 assert.equal(project.files()["notes.md"], "one\ntwo\nthree (a and b)\nfour\nfive\n");
216 assert.equal(project.files()["engine.ts"], "export function match(p, s) {\n return void 0;\n}\n");
217 // Shared, not every atom: a sync that writes markers (#11) discards the draft change it replaces.
218 const shared = project.sharedView().set;
219 assert.deepEqual(unresolvedConflicts(project.graph, shared), []);
220});
222test("pushing with the markers still in is refused; asIs pushes both and settles it (#37, #11)", () => {
223 const { project } = setup();
224 project.sync("a");
225 project.sync("b");
226 project.edit("a", { file: "notes.md", old: "two\n", new: "two-a\n" }, 2);
227 project.edit("b", { file: "notes.md", old: "two\n", new: "two-b\n" }, 2);
228 project.publish("a", "a", 3);
229 assert.equal(project.publish("b", "b", 4).ok, false);
230 assert.equal(project.sync("b").collisions.length, 1);
231 const marked = project.publish("b", "Keep both lines", 5);
232 assert.deepEqual(!marked.ok && marked.collisions.map((c) => [c.kind, c.marked]), [["lines", true]]);
233 assert.equal(project.publish("b", "Keep both lines", 5, { asIs: true }).ok, true);
234 assert.equal(project.files()["notes.md"], "one\ntwo-b\ntwo-a\nthree\nfour\nfive\n");
235 const shared = project.sharedView().set;
236 assert.deepEqual(unresolvedConflicts(project.graph, shared), []);
237});
239test("after pushing without pulling, the agent keeps editing its own folder", () => {
240 const { project } = setup();
241 project.sync("a");
242 project.sync("b");
243 project.edit("b", { file: "notes.md", old: "five\n", new: "five-b\n" }, 2);
244 project.publish("b", "b first", 3);
245 project.edit("a", { file: "notes.md", old: "one\n", new: "one-a\n" }, 4);
246 assert.equal(project.publish("a", "a after b", 5).ok, true);
247 // a's folder still says "five": an edit against it must match.
248 assert.deepEqual(project.edit("a", { file: "notes.md", old: "one-a\ntwo\n", new: "one-a\n2\n" }, 6), {
249 ok: true,
250 news: [],
251 });
252 assert.equal(project.status().agents[0]!.base, 0);
253 const s = project.sync("a");
254 assert.deepEqual(s.files, { "notes.md": "one-a\n2\nthree\nfour\nfive-b\n" });
255 // With nobody else in between, publishing moves the base along.
256 project.sync("b");
257 project.edit("b", { file: "notes.md", old: "three\n", new: "3\n" }, 7);
258 project.publish("b", "b again", 8);
259 assert.equal(project.status().agents[1]!.base, project.version);
260});
262test("a reloaded project is the same project and keeps going", () => {
263 const { project, store } = setup();
264 project.sync("a");
265 project.sync("b");
266 project.edit("a", { file: "notes.md", old: "two\n", new: "two-a\n" }, 2);
267 project.edit("b", { file: "notes.md", old: "two\n", new: "two-b\n" }, 2);
268 project.publish("a", "a", 3);
269 const again = Project.load(store.entries, new MemoryStore());
270 assert.deepEqual(again.status(), project.status());
271 assert.deepEqual(again.files(), project.files());
272 // The news b hasn't heard yet survives the reload.
273 const heard = again.record("b", {}, 4);
274 assert.equal(heard.news.length, 1);
275 assert.equal(again.publish("b", "b", 5).ok, false);
276});
278test("bad input is a ProjectError", () => {
279 const { project } = setup();
280 assert.throws(
281 () => project.sync("bad name"),
282 (e) => e instanceof ProjectError && e.code === "bad-agent",
283 );
284 assert.throws(
285 () => project.init({}, 1),
286 (e) => e instanceof ProjectError && e.code === "already-initialized",
287 );
288 assert.throws(
289 () => new Project(new MemoryStore()).status(),
290 (e) => e instanceof ProjectError && e.code === "not-initialized",
291 );
292 assert.throws(
293 () => project.edit("a", { file: "", content: "x" }, 1),
294 (e) => e instanceof ProjectError && e.code === "bad-request",
295 );
296});
298test("each call stores what it changed in one append", () => {
299 const store = new MemoryStore();
300 const calls: number[] = [];
301 const counting = {
302 append: (e: readonly unknown[]) => {
303 calls.push(e.length);
304 store.append(e as never);
305 },
306 };
307 const project = new Project(counting);
308 project.init(START, 1);
309 project.sync("a");
310 assert.deepEqual(project.status().version, 0); // status writes nothing
311 project.edit("a", { file: "notes.md", old: "one\n", new: "1\n" }, 2);
312 project.publish("a", "m", 3);
313 assert.deepEqual(calls, [1, 2, 1, 1]);
314 assert.deepEqual(
315 store.entries.map((e) => e.type),
316 ["init", "join", "sync", "draft", "publish"],
317 );
318});
320test("a call that joins an agent and then throws still stores the join, so the log loads", () => {
321 // From the live drafts-smoke project: a new agent's first call was a switch
322 // to a bad draft name; its join was applied but never stored, and the
323 // agent's next sync made a log that no longer loaded.
324 const store = new MemoryStore();
325 const project = new Project(store);
326 project.init({ "a.txt": "a\n" }, 0);
327 assert.throws(() => project.switchTo("newbie", "bad name!", 1));
328 project.sync("newbie");
329 assert.deepEqual(
330 store.entries.map((e) => e.type),
331 ["init", "join", "sync"],
332 );
333 assert.deepEqual(Project.load(store.entries, new MemoryStore()).status(), project.status());
334});
336test("a log already missing a join loads: the join is put back at the version it had", () => {
337 const store = new MemoryStore();
338 const project = new Project(store);
339 project.init({ "a.txt": "a\n" }, 0);
340 project.edit("ann", { file: "a.txt", old: "a", new: "b" }, 1);
341 project.publish("ann", "b", 2);
342 project.sync("bob");
343 const broken = store.entries.filter((e) => !(e.type === "join" && e.agent === "bob"));
344 const loaded = Project.load(broken, new MemoryStore());
345 assert.deepEqual(loaded.status(), project.status());
346 assert.deepEqual(loaded.files(), project.files());
347});
349test("a refused push is logged and counted, and the counts survive a reload", () => {
350 const store = new MemoryStore();
351 const project = new Project(store);
352 project.init({ "a.txt": "a\n" }, 0);
353 project.sync("ann");
354 project.sync("bob");
355 project.edit("ann", { file: "a.txt", old: "a", new: "b" }, 1);
356 project.edit("bob", { file: "a.txt", old: "a", new: "c" }, 1);
357 project.publish("ann", "b", 2);
358 assert.equal(project.publish("bob", "c", 3).ok, false);
359 assert.deepEqual(project.status().refusals, { total: 1, lines: 1, definition: 0, attribute: 0, duplicate: 0 });
360 assert.deepEqual(project.refusals().refusals, [
361 {
362 agent: "bob",
363 draft: null,
364 via: "publish",
365 time: 3,
366 collisions: [{ kind: "lines", file: "a.txt" }],
367 message: "c",
368 },
369 ]);
370 assert.deepEqual(Project.load(store.entries, new MemoryStore()).refusals(), project.refusals());
371});
373test("a full pull answers every file in the view, even for an agent that pulled before; file listings change nothing (#37)", () => {
374 const store = new MemoryStore();
375 const project = new Project(store);
376 project.init({ "a.txt": "a\n", "src/b.ts": "b\n" }, 0);
377 project.sync("artist1");
378 project.edit("lead", { file: "src/c.ts", content: "c\n" }, 1);
379 project.publish("lead", "c", 2);
380 // A plain sync gives only what changed since; a full one, everything.
381 assert.deepEqual(Object.keys(project.sync("artist1").files), ["src/c.ts"]);
382 assert.deepEqual(Object.keys(project.sync("artist1", { full: true }).files), ["a.txt", "src/b.ts", "src/c.ts"]);
383 const before = store.entries.length;
384 assert.deepEqual(project.agentPaths("artist1"), {
385 version: 1,
386 draft: null,
387 files: ["a.txt", "src/b.ts", "src/c.ts"],
388 });
389 assert.deepEqual(project.pathsAt(0), { version: 0, draft: null, files: ["a.txt", "src/b.ts"] });
390 assert.equal(store.entries.length, before, "reading the listings logs nothing");
391 assert.throws(() => project.agentPaths("nobody"), /join first/);
392});
394test("a log entry of a kind this engine doesn't know stops the load instead of being skipped", () => {
395 const store = new MemoryStore();
396 const project = new Project(store);
397 project.init({ "a.txt": "a\n" }, 0);
398 const entries = [
399 ...store.entries,
400 { type: "from-the-future", agent: "x", time: 1 } as unknown as (typeof store.entries)[number],
401 ];
402 assert.throws(
403 () => Project.load(entries, new MemoryStore()),
404 /log entry 1 is of kind "from-the-future", which this version of abso doesn't know/,
405 );
406});