- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/worker/project/test/ingest.test.ts?version=296&format=json
128 linesFirst import 100%Last change First import in version 0, 1 version changed it
You're reading version 296. The newest is 318, and no version since then changed this file.
1// Taking in a plain-git push (demo moment 5): a person cloned shared from
2// Artifacts, committed with ordinary git, and pushed. The commits join the
3// graph as diffs; they land in shared unless they collide, and then they wait
4// in their own named draft.
6import assert from "node:assert/strict";
7import { test } from "node:test";
8import { MemoryStore, Project, ProjectError } from "../project.ts";
10const START = { "a.txt": "one\ntwo\nthree\n", "b.txt": "x\n" };
12function setup() {
13 const store = new MemoryStore();
14 const project = new Project(store);
15 project.init(START, 0);
16 project.sync("agent");
17 return { project, store };
18}
20test("commits that collide with nothing land in shared, one diff each", () => {
21 const { project } = setup();
22 const answer = project.ingest(
23 {
24 author: "Robin",
25 base: 0,
26 commits: [
27 { sha: "aaaaaaaa1111", message: "Shout one", files: { "a.txt": "ONE\ntwo\nthree\n", "b.txt": "x\n" } },
28 { sha: "bbbbbbbb2222", message: "Add c, drop b", files: { "a.txt": "ONE\ntwo\nthree\n", "c.txt": "new\n" } },
29 ],
30 },
31 1,
32 );
33 assert.deepEqual(answer, { landed: true, version: 1, draft: "git-aaaaaaaa1111", changes: 5 });
34 assert.deepEqual(project.files(), { "a.txt": "ONE\ntwo\nthree\n", "c.txt": "new\n" });
35 assert.match(project.published(1)!.message, /^Add c, drop b\n\nGit-Commit: bbbbbbbb2222\nAuthor: Robin$/);
36 // Agents get it the usual way.
37 assert.deepEqual(project.sync("agent").files, { "a.txt": "ONE\ntwo\nthree\n", "b.txt": null, "c.txt": "new\n" });
38});
40test("a push built on an older version still lands when it touches other lines", () => {
41 const { project } = setup();
42 project.edit("agent", { file: "a.txt", old: "three\n", new: "THREE\n" }, 1);
43 project.publish("agent", "Agent shouts three", 2);
44 const answer = project.ingest(
45 {
46 author: "p",
47 base: 0,
48 commits: [{ sha: "cccccccc", message: "One", files: { "a.txt": "uno\ntwo\nthree\n", "b.txt": "x\n" } }],
49 },
50 3,
51 );
52 assert.equal(answer.landed, true);
53 assert.equal(project.files()["a.txt"], "uno\ntwo\nTHREE\n");
54});
56test("a push that collides waits in its named draft, and can be settled and landed there", () => {
57 const { project } = setup();
58 project.edit("agent", { file: "a.txt", old: "two\n", new: "two (agent)\n" }, 1);
59 project.publish("agent", "Agent edits two", 2);
60 const answer = project.ingest(
61 {
62 author: "p",
63 base: 0,
64 commits: [
65 {
66 sha: "dddddddd",
67 message: "Person edits two",
68 files: { "a.txt": "one\ntwo (person)\nthree\n", "b.txt": "x\n" },
69 },
70 ],
71 },
72 3,
73 );
74 assert.equal(answer.landed, false);
75 assert.equal(!answer.landed && answer.draft, "git-dddddddd");
76 assert.equal(!answer.landed && answer.collisions[0]!.theirs.agent, "agent");
77 assert.equal(project.version, 1, "shared kept the agent version");
78 assert.deepEqual(
79 project.status().drafts.map((d) => [d.name, d.changes]),
80 [["git-dddddddd", 2]],
81 );
83 // Anyone can settle it: switch to the draft, move it onto shared, edit, land.
84 project.switchTo("fixer", "git-dddddddd", 4);
85 project.syncDraft("fixer", 5);
86 const synced = project.sync("fixer");
87 assert.equal(synced.collisions.length, 1);
88 project.record("fixer", { "a.txt": "one\ntwo (agent and person)\nthree\n" }, 6);
89 project.publish("fixer", "Settle two", 7);
90 assert.equal(project.land("fixer", "Land the person push", 8).ok, true);
91 assert.equal(project.files()["a.txt"], "one\ntwo (agent and person)\nthree\n");
92});
94test("the same push is taken in once, and bad input is refused", () => {
95 const { project, store } = setup();
96 const push = {
97 author: "p",
98 base: 0,
99 commits: [{ sha: "eeeeeeee", message: "m", files: { "a.txt": "one\n", "b.txt": "x\n" } }],
100 };
101 project.ingest(push, 1);
102 assert.throws(
103 () => project.ingest(push, 2),
104 (e) => e instanceof ProjectError && e.code === "bad-request",
105 );
106 assert.throws(
107 () => project.ingest({ ...push, base: 99 }, 2),
108 (e) => e instanceof ProjectError,
109 );
110 assert.throws(
111 () => project.ingest({ ...push, commits: [] }, 2),
112 (e) => e instanceof ProjectError,
113 );
114 assert.throws(
115 () => project.ingest({ ...push, commits: [{ sha: "not hex", message: "m", files: {} }] }, 2),
116 (e) => e instanceof ProjectError,
117 );
118 // A refused ingest wrote nothing.
119 const before = store.entries.length;
120 assert.throws(() =>
121 project.ingest(
122 { author: "p", base: 0, commits: [{ sha: "ffffffff", message: "m", files: { ".avcs/x": "y" } }] },
123 3,
124 ),
125 );
126 assert.equal(store.entries.length, before);
127 assert.deepEqual(Project.load(store.entries, new MemoryStore()).status(), project.status());
128});