- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/src/gitview/test/ingest.test.ts?format=json
433 linesFirst import 100% · pinch 1%Last change pinch in version 78, 2 versions changed it
1// Plain git in: a person clones a view, commits with the git CLI, and the
2// repo is ingested. One atom per hunk, a tie and a label per commit, the
3// state after each commit equal to the commit's tree, and conflicts with
4// live atoms reported with both sides.
6import assert from "node:assert/strict";
7import { after, describe, it } from "node:test";
8import { type AtomId, Graph, independent, labelContents, project, stateOf } from "../../engine/index.ts";
9import { lines, Repo } from "../../engine/test/helpers.ts";
10import { GitViewError, type IngestResult, ingest, parseTrailers, writeView } from "../index.ts";
11import { cleanup, commitFiles, freshPath, git, numbered, Person, sorted, workingFiles } from "./helpers.ts";
13after(cleanup);
15const APP = numbered("app line", 12);
16const NOTES = lines("# Notes", "one", "two");
18function start(): Repo {
19 return Repo.init({ "app.txt": APP, "notes.md": NOTES });
20}
22async function viewAndClone(repo: Repo, who = "Pat"): Promise<{ viewDir: string; person: Person }> {
23 const viewDir = freshPath("view");
24 await writeView(repo.graph, repo.shared, viewDir);
25 return { viewDir, person: Person.clone(who, viewDir) };
26}
28describe("ingest round trip", () => {
29 const repo = start();
30 let person: Person;
31 const shas: string[] = [];
32 let result: IngestResult;
34 it("cuts each commit into one atom per hunk plus a tie, and labels each commit", async () => {
35 ({ person } = await viewAndClone(repo));
36 person.setLine("app.txt", 3, "app line three");
37 shas.push(person.commit("Spell out line three"));
38 person.write("lib/new.txt", lines("new one", "new two", "new three"));
39 shas.push(person.commit("Add a new file"));
40 person.remove("notes.md");
41 shas.push(person.commit("Drop the notes"));
42 person.setLine("app.txt", 1, "APP LINE 1");
43 person.setLine("app.txt", 6, "APP LINE 6");
44 const text = person.read("app.txt").replace("app line 10\n", "app line 10\ninserted a\ninserted b\n");
45 person.write("app.txt", text);
46 shas.push(person.commit("Three changes at once\n\nTop, middle, and an insert near the end."));
48 const before = new Set(repo.shared);
49 result = await ingest(repo.graph, repo.shared, person.dir);
50 assert.equal(result.base.kind, "view");
51 assert.deepEqual(result.base.heads, ["a0"]);
52 assert.equal(result.base.atom, "a0");
53 assert.deepEqual(
54 result.commits.map((c) => c.commit),
55 shas,
56 );
57 assert.deepEqual(
58 result.commits.map((c) => c.hunks.length),
59 [1, 1, 1, 3],
60 );
61 assert.deepEqual(
62 result.labels.map((l) => l.name),
63 shas.map((s) => `git-${s.slice(0, 7)}`),
64 );
65 assert.equal(result.atoms.length, 6 + 4);
66 assert.deepEqual(result.conflicts, []);
67 assert.deepEqual(result.revived, []);
68 assert.deepEqual(result.skipped, []);
69 // The shared set passed in is untouched; the graph has the new atoms and labels.
70 assert.deepEqual(repo.shared, before);
71 for (const atom of result.atoms) assert.equal(repo.graph.atom(atom.id), atom);
72 for (const label of result.labels) assert.deepEqual(repo.graph.label(label.name), label);
73 repo.shared = result.shared;
74 });
76 it("makes the shared projection equal the working tree", () => {
77 assert.deepEqual(sorted(project(repo.graph, repo.shared)), workingFiles(person.dir));
78 });
80 it("gives every label exactly its commit: hunks plus tie, and state equal to the commit tree", async () => {
81 for (const [i, c] of result.commits.entries()) {
82 const label = repo.graph.label(c.label)!;
83 assert.equal(label.head, c.tie);
84 assert.deepEqual(labelContents(repo.graph, label), [...c.hunks, c.tie]);
85 assert.equal(label.base, i === 0 ? "a0" : result.commits[i - 1]!.label);
86 assert.deepEqual(label.follows, i === 0 ? undefined : [result.commits[i - 1]!.label]);
87 assert.deepEqual(
88 sorted(project(repo.graph, stateOf(repo.graph, c.label))),
89 await commitFiles(person.dir, c.commit),
90 );
91 }
92 assert.equal(repo.graph.label(result.commits[3]!.label)!.title, "Three changes at once");
93 assert.equal(repo.graph.label(result.commits[3]!.label)!.message, "Top, middle, and an insert near the end.");
94 });
96 it("writes hunks as parallel atoms with the git author, time and message", () => {
97 const last = result.commits[3]!;
98 const [h1, h2, h3] = last.hunks as [AtomId, AtomId, AtomId];
99 assert.ok(independent(repo.graph, h1, h2) && independent(repo.graph, h2, h3) && independent(repo.graph, h1, h3));
100 // None of them needed the earlier commits' hunks: they touched other lines.
101 for (const h of last.hunks)
102 for (const earlier of result.commits[0]!.hunks) assert.ok(!repo.graph.dependsOn(h, earlier));
103 for (const id of last.hunks) {
104 const atom = repo.graph.atom(id);
105 assert.equal(atom.author, "Pat");
106 assert.equal(atom.time, person.lastTime());
107 assert.ok(
108 atom.message.startsWith(
109 "Three changes at once\n\nTop, middle, and an insert near the end.\n\nHunk: app.txt @@",
110 ),
111 );
112 }
113 assert.deepEqual(
114 last.hunks.map((id) => parseTrailers(repo.graph.atom(id).message).find((t) => t.key === "Hunk")!.value),
115 ["app.txt @@ -1,1 +1,1 @@", "app.txt @@ -6,1 +6,1 @@", "app.txt @@ -10,0 +11,2 @@"],
116 );
117 // Default claims: the insertion reads both neighbours and writes the gap.
118 const insert = repo.graph.atom(h3);
119 assert.deepEqual(insert.reads, ["L:a0.10", "L:a0.9"]);
120 assert.deepEqual(insert.writes, ["G:a0.9"]);
121 // The file deletion is one atom deleting every line.
122 const drop = repo.graph.atom(result.commits[2]!.hunks[0]!);
123 assert.equal(drop.deletes.length, 3);
124 assert.equal(drop.inserts.length, 0);
125 });
127 it("ties each commit to its hunks and the state before it", () => {
128 result.commits.forEach((c, i) => {
129 const tie = repo.graph.atom(c.tie);
130 assert.deepEqual(tie.inserts, []);
131 assert.deepEqual(tie.deletes, []);
132 assert.deepEqual(new Set(tie.needs), new Set([...c.hunks, i === 0 ? "a0" : result.commits[i - 1]!.tie]));
133 assert.ok(tie.message.startsWith(git(person.dir, ["show", "-s", "--format=%B", c.commit]).trim()));
134 assert.deepEqual(parseTrailers(tie.message).find((t) => t.key === "Git-Commit")?.value, c.commit);
135 });
136 });
138 it("resumes where the last ingest stopped, and ingests nothing twice", async () => {
139 const again = await ingest(repo.graph, repo.shared, person.dir);
140 assert.equal(again.base.kind, "ingested");
141 assert.equal(again.base.commit, shas[3]);
142 assert.equal(again.atoms.length, 0);
143 assert.deepEqual(again.shared, repo.shared);
145 person.setLine("app.txt", 12, "app line twelve");
146 const sha = person.commit("One more");
147 const size = repo.graph.size;
148 const more = await ingest(repo.graph, repo.shared, person.dir);
149 assert.equal(more.base.kind, "ingested");
150 assert.equal(more.base.atom, result.commits[3]!.tie);
151 assert.equal(more.base.label, result.commits[3]!.label);
152 assert.deepEqual(
153 more.commits.map((c) => c.commit),
154 [sha],
155 );
156 assert.equal(repo.graph.size, size + 2);
157 const label = repo.graph.label(more.commits[0]!.label)!;
158 assert.equal(label.base, result.commits[3]!.label);
159 assert.deepEqual(labelContents(repo.graph, label), [...more.commits[0]!.hunks, more.commits[0]!.tie]);
160 repo.shared = more.shared;
161 assert.deepEqual(sorted(project(repo.graph, repo.shared)), workingFiles(person.dir));
162 });
164 it("takes a fresh view of the ingested work back in, and by-label views rebuild the git history", async () => {
165 // Per-atom view of the new shared set; a second person continues from it.
166 const viewDir = freshPath("view");
167 await writeView(repo.graph, repo.shared, viewDir);
168 const sam = Person.clone("Sam", viewDir);
169 sam.setLine("lib/new.txt", 2, "new TWO");
170 sam.commit("Shout two");
171 const res = await ingest(repo.graph, repo.shared, sam.dir);
172 assert.equal(res.base.kind, "view");
173 assert.equal(res.commits.length, 1);
174 repo.shared = res.shared;
175 assert.deepEqual(sorted(project(repo.graph, repo.shared)), workingFiles(sam.dir));
177 // One commit per label: a0, then Pat's commits, then Sam's, each tree equal to the git commit's.
178 const byLabel = freshPath("view");
179 const written = await writeView(repo.graph, repo.shared, byLabel, { byLabel: true });
180 const patCommits = [...shas, git(person.dir, ["rev-parse", "HEAD"]).trim()];
181 const expected = [...patCommits.map((s) => ({ dir: person.dir, sha: s })), { dir: sam.dir, sha: sam.head() }];
182 assert.deepEqual(written.commits.length, 1 + expected.length);
183 for (const [i, e] of expected.entries()) {
184 assert.deepEqual(await commitFiles(byLabel, written.commits[i + 1]!.oid), await commitFiles(e.dir, e.sha));
185 }
186 const subjects = git(byLabel, ["log", "--reverse", "--format=%s"]).trim().split("\n");
187 assert.deepEqual(subjects.slice(1), [
188 "Spell out line three",
189 "Add a new file",
190 "Drop the notes",
191 "Three changes at once",
192 "One more",
193 "Shout two",
194 ]);
195 });
196});
198describe("ingest against live work", () => {
199 it("reports a conflict with both sides when a live atom changed the same line", async () => {
200 const repo = start();
201 const { person } = await viewAndClone(repo, "Pat");
202 // Meanwhile, in the shared workspace:
203 const live = repo.edit({
204 author: "live-agent",
205 message: "line 5 becomes five",
206 edits: [{ file: "app.txt", old: "app line 5\n", new: "app line five\n" }],
207 });
208 person.setLine("app.txt", 5, "app line FIVE");
209 person.commit("Pat shouts line five");
211 const result = await ingest(repo.graph, repo.shared, person.dir);
212 assert.equal(result.conflicts.length, 1);
213 const [c] = result.conflicts;
214 const hunk = result.commits[0]!.hunks[0]!;
215 assert.deepEqual(c!.atoms, [live.id, hunk]);
216 assert.equal(c!.kind, "edit-edit");
217 assert.deepEqual(c!.sides, [
218 { atom: live.id, author: "live-agent", message: "line 5 becomes five" },
219 { atom: hunk, author: "Pat", message: "Pat shouts line five\n\nHunk: app.txt @@ -5,1 +5,1 @@" },
220 ]);
221 assert.deepEqual(
222 c!.keys.map((k) => k.key),
223 ["L:a0.4"],
224 );
225 assert.deepEqual(c!.resolvedBy, []);
226 // Accepted anyway: both atoms are in the shared set.
227 assert.ok(result.shared.has(live.id) && result.shared.has(hunk));
228 });
230 it("reports no conflict when the commit only touches lines nobody else touched", async () => {
231 const repo = start();
232 const { person } = await viewAndClone(repo, "Pat");
233 repo.edit({
234 author: "live-agent",
235 message: "line 2",
236 edits: [{ file: "app.txt", old: "app line 2\n", new: "app line two\n" }],
237 });
238 repo.edit({
239 author: "live-agent",
240 message: "notes",
241 edits: [{ file: "notes.md", old: "two\n", new: "two\nthree\n" }],
242 });
243 person.setLine("app.txt", 9, "app line nine");
244 person.write("app.txt", `${person.read("app.txt")}app line 13\n`);
245 person.commit("Pat edits the bottom");
247 const result = await ingest(repo.graph, repo.shared, person.dir);
248 assert.deepEqual(result.conflicts, []);
249 assert.equal(result.commits[0]!.hunks.length, 2);
250 // Both sides' changes are in the projection.
251 const expected = person.read("app.txt").replace("app line 2\n", "app line two\n");
252 assert.equal(project(repo.graph, result.shared)["app.txt"], expected);
253 assert.equal(project(repo.graph, result.shared)["notes.md"], lines("# Notes", "one", "two", "three"));
254 });
256 it("puts base atoms the shared set dropped back, and says so", async () => {
257 const repo = start();
258 const extra = repo.edit({
259 author: "ann",
260 message: "append",
261 edits: [{ file: "notes.md", old: "two\n", new: "two\nthree\n" }],
262 });
263 const { person } = await viewAndClone(repo, "Pat");
264 repo.shared.delete(extra.id); // dropped from the workspace after the view was written
265 person.setLine("notes.md", 4, "THREE");
266 person.commit("Pat edits the appended line");
267 const result = await ingest(repo.graph, repo.shared, person.dir);
268 assert.deepEqual(result.revived, [extra.id]);
269 assert.ok(result.shared.has(extra.id));
270 });
271});
273describe("ingest edge cases", () => {
274 it("ties a view with several heads with one anchor atom, reused by the next ingest", async () => {
275 const repo = start();
276 const x = repo.edit({ author: "ann", message: "x file", edits: [{ file: "x.txt", old: "", new: "x\n" }] });
277 const y = repo.edit({ author: "bob", message: "y file", edits: [{ file: "y.txt", old: "", new: "y\n" }] });
278 const viewDir = freshPath("view");
279 await writeView(repo.graph, repo.shared, viewDir);
280 const pat = Person.clone("Pat", viewDir);
281 pat.setLine("app.txt", 1, "first");
282 pat.commit("Pat edits line 1");
283 const one = await ingest(repo.graph, repo.shared, pat.dir);
284 assert.deepEqual(one.base.heads, ["a0", x.id, y.id]);
285 const anchor = one.base.anchor!;
286 assert.equal(one.base.atom, anchor);
287 assert.deepEqual(repo.graph.atom(anchor).needs, ["a0", x.id, y.id]);
288 assert.equal(repo.graph.label(one.commits[0]!.label)!.base, anchor);
289 assert.deepEqual(labelContents(repo.graph, repo.graph.label(one.commits[0]!.label)!), [
290 ...one.commits[0]!.hunks,
291 one.commits[0]!.tie,
292 ]);
293 repo.shared = one.shared;
295 const sam = Person.clone("Sam", viewDir);
296 sam.setLine("app.txt", 12, "last");
297 sam.commit("Sam edits line 12");
298 const two = await ingest(repo.graph, repo.shared, sam.dir);
299 assert.equal(two.base.anchor, anchor);
300 assert.equal(two.atoms.length, 2, "one hunk and one tie, no new anchor");
301 repo.shared = two.shared;
302 assert.deepEqual(two.conflicts, []);
303 assert.equal(
304 project(repo.graph, repo.shared)["app.txt"],
305 APP.replace("app line 1\n", "first\n").replace("app line 12\n", "last\n"),
306 );
307 });
309 it("skips empty and binary files, listing each when it appears or changes", async () => {
310 const repo = start();
311 const { person } = await viewAndClone(repo);
312 person.write("pkg/__init__.py", "");
313 person.write("logo.bin", new Uint8Array([0x89, 0x50, 0x00, 0x01]));
314 const first = person.commit("Add a package marker and a logo");
315 person.setLine("app.txt", 2, "two");
316 person.commit("Edit line 2");
317 person.write("logo.bin", new Uint8Array([0x89, 0x50, 0x00, 0x02]));
318 person.write("notes.md", "");
319 const third = person.commit("New logo, empty notes");
321 const result = await ingest(repo.graph, repo.shared, person.dir);
322 assert.deepEqual(result.skipped, [
323 { commit: first, path: "logo.bin", reason: "binary" },
324 { commit: first, path: "pkg/__init__.py", reason: "empty" },
325 { commit: third, path: "logo.bin", reason: "binary" },
326 { commit: third, path: "notes.md", reason: "empty" },
327 ]);
328 assert.deepEqual(
329 result.commits.map((c) => c.hunks.length),
330 [0, 1, 1],
331 );
332 // Emptying a file deletes its lines; the projection has no such file.
333 const files = project(repo.graph, result.shared);
334 assert.deepEqual(Object.keys(files), ["app.txt"]);
335 assert.deepEqual(sorted(files), workingFiles(person.dir));
336 });
338 it("ingests an amended view commit as the person's own change on its heads, without the view trailers", async () => {
339 const repo = start();
340 const uno = repo.edit({
341 author: "ann",
342 message: "touch notes",
343 edits: [{ file: "notes.md", old: "one\n", new: "uno\n" }],
344 });
345 const { person } = await viewAndClone(repo);
346 person.setLine("app.txt", 4, "four");
347 const sha = person.amend();
348 const result = await ingest(repo.graph, repo.shared, person.dir);
349 assert.equal(result.base.commit, sha);
350 assert.deepEqual(result.base.heads, [uno.id]);
351 assert.deepEqual(
352 result.commits.map((c) => c.commit),
353 [sha],
354 );
355 assert.deepEqual(
356 result.commits.map((c) => c.hunks.length),
357 [1],
358 "only the amended-in line, not ann's",
359 );
360 assert.deepEqual(result.conflicts, []);
361 const label = repo.graph.label(result.commits[0]!.label)!;
362 assert.equal(label.title, "touch notes");
363 assert.equal(label.message, "");
364 assert.equal(label.base, uno.id);
365 assert.ok(!repo.graph.atom(result.commits[0]!.tie).message.includes("View-Heads"));
366 assert.deepEqual(sorted(project(repo.graph, result.shared)), workingFiles(person.dir));
367 });
369 it("does not take an amended root view commit as a base", async () => {
370 const repo = start();
371 const { person } = await viewAndClone(repo);
372 person.setLine("app.txt", 4, "four");
373 person.amend(); // the view's only commit: nothing exact below it vouches for its trailer
374 await assert.rejects(ingest(repo.graph, repo.shared, person.dir), { code: "no-view-base" });
375 });
377 it("reads CRLF files as their lines", async () => {
378 const repo = start();
379 const { person } = await viewAndClone(repo);
380 person.write("notes.md", "# Notes\r\none\r\nTWO\r\n");
381 person.commit("CRLF and a change");
382 const result = await ingest(repo.graph, repo.shared, person.dir);
383 assert.equal(result.commits[0]!.hunks.length, 1);
384 assert.equal(project(repo.graph, result.shared)["notes.md"], lines("# Notes", "one", "TWO"));
385 });
387 it("follows the first parent through a merge", async () => {
388 const repo = start();
389 const { person } = await viewAndClone(repo);
390 git(person.dir, ["checkout", "--quiet", "-b", "side"]);
391 person.setLine("app.txt", 11, "from the side");
392 person.commit("Side change");
393 git(person.dir, ["checkout", "--quiet", "main"]);
394 person.setLine("app.txt", 2, "from main");
395 person.commit("Main change");
396 git(person.dir, ["merge", "--quiet", "--no-ff", "-m", "Merge side", "side"], {
397 GIT_AUTHOR_NAME: "Pat",
398 GIT_AUTHOR_EMAIL: "pat@example.com",
399 GIT_COMMITTER_NAME: "Pat",
400 GIT_COMMITTER_EMAIL: "pat@example.com",
401 });
402 const result = await ingest(repo.graph, repo.shared, person.dir);
403 assert.deepEqual(
404 result.commits.map((c) => c.hunks.length),
405 [1, 1],
406 );
407 assert.equal(repo.graph.label(result.commits[1]!.label)!.title, "Merge side");
408 assert.deepEqual(sorted(project(repo.graph, result.shared)), workingFiles(person.dir));
409 });
411 it("refuses a repo that is not a view of this graph, and leaves the graph alone", async () => {
412 const repo = start();
413 const dir = freshPath("plain");
414 git(process.cwd(), ["init", "--quiet", "-b", "main", dir]);
415 const person = new Person("Pat", dir);
416 person.write("a.txt", "a\n");
417 person.commit("A plain repo");
418 const size = repo.graph.size;
419 await assert.rejects(ingest(repo.graph, repo.shared, dir), (err: unknown) => {
420 assert.ok(err instanceof GitViewError);
421 assert.equal(err.code, "no-view-base");
422 return true;
423 });
424 await assert.rejects(ingest(repo.graph, repo.shared, freshPath("missing")), { code: "bad-repo" });
425 // A view of another graph with different lines doesn't count either.
426 const other = Repo.init({ "app.txt": "other\n" });
427 const viewDir = freshPath("view");
428 await writeView(other.graph, other.shared, viewDir);
429 await assert.rejects(ingest(repo.graph, repo.shared, viewDir), { code: "no-view-base" });
430 assert.equal(repo.graph.size, size);
431 assert.ok(Graph.fromJSON(repo.graph.toJSON()));
432 });
433});