- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/worker/project/test/appends.test.ts?version=295&format=json
128 linesFirst import 97% · rename 3%Last change rename in version 100, 2 versions changed it
You're reading version 295. The newest is 318, and no version since then changed this file.
1// Insertions at the same point (#37, the game trial): five builders appended
2// their pitches to notes/pitches.md at once, and publishes were refused for
3// what was never a real collision. Two pure insertions after the same line
4// lose nothing, so they are kept side by side; they still collide when they
5// share a real line, create the same file, or one changed what the other
6// inserted next to.
8import assert from "node:assert/strict";
9import { test } from "node:test";
10import { MemoryStore, Project } from "../project.ts";
12const PITCHES = "# Pitches\n";
14function setup(files: Record<string, string> = { "notes/pitches.md": PITCHES }) {
15 const project = new Project(new MemoryStore());
16 project.init(files, 0);
17 const builders = ["b1", "b2", "b3", "b4", "b5"];
18 for (const b of builders) project.sync(b);
19 return { project, builders };
20}
22test("five builders appending to the same file at once all push on the first try, and nothing is lost", () => {
23 const { project, builders } = setup();
24 builders.forEach((b, i) =>
25 project.edit(
26 b,
27 { file: "notes/pitches.md", old: "# Pitches\n", new: `# Pitches\n\n## ${b}'s pitch\nIdea number ${i + 1}.\n` },
28 1,
29 ),
30 );
31 builders.forEach((b, i) => assert.equal(project.publish(b, `${b} pitch`, 2 + i).ok, true, b));
32 const text = project.files()["notes/pitches.md"]!;
33 for (const [i, b] of builders.entries()) assert.ok(text.includes(`## ${b}'s pitch\nIdea number ${i + 1}.\n`), b);
34 assert.equal(text.split("\n").filter((l) => l === "# Pitches").length, 1);
35 // Everyone syncs to the same file, with nothing to settle.
36 for (const b of builders) assert.deepEqual(project.sync(b).collisions, [], b);
37});
39test("a push says what it went on top of that the folder did not have yet", () => {
40 const { project } = setup();
41 project.edit("b5", { file: "notes/pitches.md", old: "# Pitches\n", new: "# Pitches\nfive\n" }, 1);
42 project.publish("b5", "Pitch five", 2);
43 project.edit("b3", { file: "notes/pitches.md", old: "# Pitches\n", new: "# Pitches\nthree\n" }, 3);
44 assert.deepEqual(project.publish("b3", "Pitch three", 4), {
45 ok: true,
46 version: 2,
47 changes: 1,
48 mergedWith: [{ version: 1, agent: "b5", message: "Pitch five" }],
49 });
50 assert.deepEqual(project.sync("b3").files, { "notes/pitches.md": "# Pitches\nthree\nfive\n" });
51});
53test("insertions at the same point still collide when they share a real line", () => {
54 const { project } = setup({ "a.ts": 'import { x } from "./x.ts";\nrun();\n' });
55 project.edit(
56 "b1",
57 {
58 file: "a.ts",
59 old: 'import { x } from "./x.ts";\n',
60 new: 'import { x } from "./x.ts";\nimport { y } from "./y.ts";\n',
61 },
62 1,
63 );
64 project.edit(
65 "b2",
66 {
67 file: "a.ts",
68 old: 'import { x } from "./x.ts";\n',
69 new: 'import { x } from "./x.ts";\nimport { y } from "./y.ts";\nimport { z } from "./z.ts";\n',
70 },
71 1,
72 );
73 project.publish("b1", "y", 2);
74 assert.equal(project.publish("b2", "y and z", 3).ok, false);
75});
77test("two agents creating the same new file still collide", () => {
78 const { project } = setup();
79 project.edit("b1", { file: "src/game.ts", content: "export const a = 1;\n" }, 1);
80 project.edit("b2", { file: "src/game.ts", content: "export const b = 2;\n" }, 1);
81 project.publish("b1", "game a", 2);
82 assert.equal(project.publish("b2", "game b", 3).ok, false);
83});
85test("an insertion next to a line the other side changed still collides", () => {
86 const { project } = setup({ "notes/pitches.md": "# Pitches\nend\n" });
87 project.edit("b1", { file: "notes/pitches.md", old: "# Pitches\n", new: "# Game pitches\n" }, 1);
88 project.edit("b2", { file: "notes/pitches.md", old: "# Pitches\n", new: "# Pitches\nmine\n" }, 1);
89 project.publish("b1", "Retitle", 2);
90 assert.equal(project.publish("b2", "Mine", 3).ok, false);
91});
93test("a one-line list both sides added to is merged at pull, told, and pushes; no doubled declaration", () => {
94 const { project } = setup({ "src/main.ts": "const SYSTEMS = [input, physics];\nrun(SYSTEMS);\n" });
95 project.edit("b1", { file: "src/main.ts", old: "[input, physics]", new: "[input, physics, audio]" }, 1);
96 project.publish("b1", "Audio system", 2);
97 project.edit("b2", { file: "src/main.ts", old: "[input, physics]", new: "[input, physics, render]" }, 3);
98 assert.equal(project.publish("b2", "Render system", 4).ok, false);
99 const s = project.sync("b2");
100 assert.deepEqual(s.collisions, []);
101 assert.deepEqual(s.merged, [
102 {
103 file: "src/main.ts",
104 line: 1,
105 text: "const SYSTEMS = [input, physics, audio, render];",
106 theirs: { agent: "b1", message: "Audio system", version: 1 },
107 },
108 ]);
109 assert.equal(project.publish("b2", "Render system", 5).ok, true);
110 assert.equal(project.files()["src/main.ts"], "const SYSTEMS = [input, physics, audio, render];\nrun(SYSTEMS);\n");
111});
113test("news about a collision is dropped once the agent pulls past it; announcements stay", () => {
114 const { project } = setup({ "a.ts": "x\n" });
115 project.edit("b2", { file: "a.ts", old: "x", new: "y" }, 1);
116 project.edit("b4", { file: "a.ts", old: "x", new: "z" }, 1);
117 project.publish("b4", "z", 2);
118 project.announce("lead", "Stand-up at 16:00.", ["b2"], 3);
119 // b2 syncs (the collision is listed there), settles it, and publishes.
120 assert.equal(project.sync("b2").collisions.length, 1);
121 // The next answer with news has the announcement, and not the collision it already synced past.
122 assert.deepEqual(project.record("b2", { "a.ts": "y and z\n" }, 4).news, [
123 "Announcement from lead (as claimed): Stand-up at 16:00.",
124 ]);
125 assert.equal(project.publish("b2", "Both", 5).ok, true);
126 const next = project.edit("b2", { file: "a.ts", old: "y and z", new: "yz" }, 6);
127 assert.deepEqual(next.ok && next.news, []);
128});