- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/src/engine/test/graph.test.ts?format=json
259 linesFirst import 100%Last change First import in version 0, 1 version changed it
1// The graph: accepting atoms, indexes, lamport, closure, labels, JSON.
3import assert from "node:assert/strict";
4import { describe, it } from "node:test";
5import { type Atom, GRAPH_FORMAT, Graph, labelContents, View } from "../index.ts";
6import { EditGen, filesFromView, filesToRecord, sameFiles } from "./gen.ts";
7import { lines, Repo, Rng } from "./helpers.ts";
9function reload(graph: Graph): Graph {
10 return Graph.fromJSON(JSON.parse(JSON.stringify(graph)));
11}
13function sample(): { repo: Repo; x: Atom; y: Atom; z: Atom } {
14 const repo = Repo.init({ "f.txt": lines("one", "two", "three") });
15 const x = repo.edit({ author: "ann", message: "x", edits: [{ file: "f.txt", old: "one", new: "ONE" }] });
16 const y = repo.edit({
17 author: "bob",
18 message: "y",
19 edits: [{ file: "f.txt", old: "ONE", new: "uno" }],
20 reasons: [{ to: x.id, kind: "together", text: "same idea" }],
21 });
22 const z = repo.edit({
23 author: "cy",
24 message: "z",
25 edits: [{ file: "f.txt", old: "three", new: "THREE" }],
26 resolves: [x.id],
27 });
28 return { repo, x, y, z };
29}
31describe("graph.add", () => {
32 it("requires the next id and seq", () => {
33 const { repo, x } = sample();
34 assert.throws(() => repo.graph.add({ ...x }), { code: "bad-atom" });
35 assert.throws(() => repo.graph.add({ ...x, id: "a7", seq: 7 }), { code: "bad-atom" });
36 });
38 it("rejects touching lines from outside the closure, and other malformed atoms", () => {
39 const { repo, x } = sample();
40 const next = repo.graph.size;
41 const base: Atom = { ...x, id: `a${next}`, seq: next, deps: ["a0"], needs: [], resolves: [], reasons: [] };
42 // Deletes x's line without depending on x.
43 assert.throws(() => repo.graph.add({ ...base, deletes: [`${x.id}.0`], writes: [`L:${x.id}.0`] }), {
44 code: "bad-atom",
45 });
46 // Deletes a line without writing its key.
47 assert.throws(() => repo.graph.add({ ...base, deletes: ["a0.1"], writes: [] }), { code: "bad-atom" });
48 // Unknown dependency; needs outside deps; bad key. (A run may hang off a
49 // line written in another file: a move can have taken the line there.)
50 assert.throws(() => repo.graph.add({ ...base, deps: ["a99"] }), { code: "bad-atom" });
51 assert.throws(() => repo.graph.add({ ...base, needs: [x.id] }), { code: "bad-atom" });
52 assert.throws(() => repo.graph.add({ ...base, reads: ["X:nope"] }), { code: "bad-atom" });
53 // Moves: without writing P: and G:, of an unknown line, after itself, twice.
54 const move = { ...base, deletes: [], writes: ["G:^g.txt", "P:a0.0"] };
55 assert.throws(() => repo.graph.add({ ...move, writes: ["P:a0.0"], moves: [{ line: "a0.0", parent: "^g.txt" }] }), {
56 code: "bad-atom",
57 });
58 assert.throws(() => repo.graph.add({ ...move, moves: [{ line: "a9.0", parent: "^g.txt" }] }), { code: "bad-atom" });
59 assert.throws(
60 () => repo.graph.add({ ...move, writes: ["G:a0.0", "P:a0.0"], moves: [{ line: "a0.0", parent: "a0.0" }] }),
61 { code: "bad-atom" },
62 );
63 assert.throws(
64 () =>
65 repo.graph.add({
66 ...move,
67 moves: [
68 { line: "a0.0", parent: "^g.txt" },
69 { line: "a0.0", parent: "^g.txt" },
70 ],
71 }),
72 { code: "bad-atom" },
73 );
74 assert.throws(() => repo.graph.add({ ...move, moves: [] }), { code: "bad-atom" });
75 assert.throws(
76 () =>
77 repo.graph.add({
78 ...base,
79 deletes: [],
80 writes: [],
81 inserts: [{ file: "g.txt", parent: "^f.txt", lines: ["x"] }],
82 }),
83 { code: "bad-atom" },
84 );
85 assert.throws(
86 () =>
87 repo.graph.add({
88 ...base,
89 deletes: [],
90 writes: [],
91 inserts: [{ file: "f.txt", parent: "a0.0", lines: ["a\nb"] }],
92 }),
93 { code: "bad-atom" },
94 );
95 assert.equal(repo.graph.size, next);
96 });
98 it("freezes accepted atoms", () => {
99 const { repo, x } = sample();
100 const stored = repo.graph.atom(x.id);
101 assert.ok(Object.isFrozen(stored));
102 assert.ok(Object.isFrozen(stored.inserts));
103 assert.ok(Object.isFrozen(stored.inserts[0]!.lines));
104 assert.throws(() => (stored.deps as string[]).push("a0"));
105 });
107 it("indexes lines, keys, dependents, resolvers and together links", () => {
108 const { repo, x, y, z } = sample();
109 const g = repo.graph;
110 assert.deepEqual(g.line(`${x.id}.0`), {
111 id: `${x.id}.0`,
112 atom: x.id,
113 k: 0,
114 file: "f.txt",
115 parent: "a0.0",
116 text: "ONE",
117 });
118 assert.deepEqual(g.deletersOf("a0.0"), [x.id]);
119 assert.deepEqual(g.writersOf(`L:${x.id}.0`), [y.id]);
120 assert.deepEqual(g.dependentsOf(x.id), [y.id, z.id]);
121 assert.deepEqual(g.resolversOf(x.id), [z.id]);
122 assert.deepEqual(g.togetherLinks(x.id), [{ other: y.id, text: "same idea", from: y.id }]);
123 assert.deepEqual(g.togetherLinks(y.id), [{ other: x.id, text: "same idea", from: y.id }]);
124 assert.throws(() => g.atom("a99"), { code: "unknown-atom" });
125 assert.throws(() => g.atom("nope"), { code: "unknown-atom" });
126 assert.equal(g.has("a0.1"), false);
127 });
129 it("computes lamport from deps, closures, and dependsOn", () => {
130 const { repo, x, y, z } = sample();
131 const g = repo.graph;
132 assert.deepEqual([g.lamport("a0"), g.lamport(x.id), g.lamport(y.id), g.lamport(z.id)], [0, 1, 2, 2]);
133 assert.deepEqual([...g.closure(y.id)].sort(), ["a0", x.id, y.id].sort());
134 assert.deepEqual([...g.closureOf([y.id, z.id])].sort(), ["a0", x.id, y.id, z.id].sort());
135 assert.deepEqual([g.dependsOn(y.id, x.id), g.dependsOn(y.id, "a0"), g.dependsOn(y.id, y.id)], [true, true, true]);
136 assert.deepEqual(
137 [g.dependsOn(x.id, y.id), g.dependsOn(z.id, y.id), g.dependsOn(y.id, z.id)],
138 [false, false, false],
139 );
140 assert.throws(() => g.dependsOn(y.id, "a99"), { code: "unknown-atom" });
141 assert.ok(g.comparePriority(x.id, y.id) < 0);
142 assert.ok(g.comparePriority(z.id, y.id) > 0);
143 });
145 it("dependsOn agrees with closure on branching histories (seeded)", () => {
146 for (let seed = 1; seed <= 40; seed++) {
147 const rng = new Rng(seed);
148 const gen = new EditGen(rng, { duplicates: rng.chance(0.5), wide: false });
149 const repo = Repo.init(filesToRecord(gen.initialFiles()));
150 const snapshots: Set<string>[] = [new Set(repo.shared)];
151 for (let s = 0; s < 20; s++) {
152 const against = rng.chance(0.4) ? rng.pick(snapshots) : repo.shared;
153 const view = repo.view(against);
154 const g = gen.next(filesFromView(view), view);
155 if (!g || sameFiles(g.apply(filesFromView(view)), filesFromView(view))) continue;
156 repo.edit({ edits: g.edits }, against);
157 snapshots.push(new Set(repo.shared));
158 }
159 const ids = repo.graph.atoms().map((a) => a.id);
160 for (const a of ids) {
161 const inside = repo.graph.closure(a);
162 for (const b of ids)
163 assert.equal(repo.graph.dependsOn(a, b), inside.has(b), `seed ${seed}: dependsOn(${a}, ${b})`);
164 }
165 }
166 });
167});
169describe("labels on the graph", () => {
170 it("validates names, heads and bases", () => {
171 const { repo, x, y } = sample();
172 const g = repo.graph;
173 g.setLabel({ name: "first", head: x.id, title: "First", message: "" });
174 g.setLabel({ name: "second", head: y.id, base: "first", title: "Second", message: "", follows: ["first"] });
175 assert.throws(() => g.setLabel({ name: "a5", head: x.id, title: "", message: "" }), { code: "bad-label" });
176 assert.throws(() => g.setLabel({ name: "bad", head: "a99", title: "", message: "" }), { code: "bad-label" });
177 assert.throws(() => g.setLabel({ name: "bad", head: x.id, base: "missing", title: "", message: "" }), {
178 code: "bad-label",
179 });
180 assert.deepEqual(
181 g.labels().map((l) => l.name),
182 ["first", "second"],
183 );
184 assert.equal(g.removeLabel("first"), true);
185 assert.equal(g.label("first"), undefined);
186 assert.equal(g.removeLabel("first"), false);
187 });
189 it("removing a label that another uses as its base gives that label the head atom as its base", () => {
190 const { repo, x, y } = sample();
191 const g = repo.graph;
192 g.setLabel({ name: "release-1", head: x.id, title: "r1", message: "" });
193 g.setLabel({ name: "feature", head: y.id, base: "release-1", title: "f", message: "", follows: ["release-1"] });
194 const before = labelContents(g, g.label("feature")!);
195 g.removeLabel("release-1");
196 assert.deepEqual(g.label("feature"), {
197 name: "feature",
198 head: y.id,
199 base: x.id,
200 title: "f",
201 message: "",
202 follows: ["release-1"],
203 });
204 assert.deepEqual(labelContents(g, g.label("feature")!), before);
205 assert.deepEqual(reload(g).labels(), g.labels());
206 });
208 it("bases may name each other in a cycle; each resolves to its head when used, and the graph reloads", () => {
209 const { repo, x, y } = sample();
210 const g = repo.graph;
211 g.setLabel({ name: "y", head: y.id, base: "a0", title: "y", message: "" });
212 g.setLabel({ name: "x", head: x.id, base: "y", title: "x", message: "" });
213 g.setLabel({ name: "y", head: y.id, base: "x", title: "y", message: "" });
214 assert.deepEqual(labelContents(g, g.label("y")!), [y.id]);
215 assert.deepEqual(labelContents(g, g.label("x")!), []);
216 assert.deepEqual(reload(g).labels(), g.labels());
217 });
218});
220describe("JSON", () => {
221 it("round-trips atoms and labels exactly", () => {
222 const { repo, y } = sample();
223 repo.graph.setLabel({ name: "base-label", head: "a0", title: "Import", message: "the import" });
224 repo.graph.setLabel({
225 name: "y-label",
226 head: y.id,
227 base: "base-label",
228 title: "Y",
229 message: "y",
230 follows: ["base-label"],
231 });
232 const text = JSON.stringify(repo.graph);
233 const parsed = JSON.parse(text);
234 assert.equal(parsed.format, GRAPH_FORMAT);
235 // Labels that name each other load in any order.
236 parsed.labels.reverse();
237 const again = Graph.fromJSON(parsed);
238 assert.equal(JSON.stringify(again), text);
239 assert.deepEqual(new View(again, repo.shared).files(), repo.files());
240 assert.deepEqual(
241 repo.graph.atoms().map((a) => again.lamport(a.id)),
242 repo.graph.atoms().map((a) => repo.graph.lamport(a.id)),
243 );
244 });
246 it("rejects other formats and tampered atoms", () => {
247 const { repo, x } = sample();
248 const data = JSON.parse(JSON.stringify(repo.graph));
249 assert.throws(() => Graph.fromJSON(null), { code: "bad-json" });
250 assert.throws(() => Graph.fromJSON({ ...data, format: "git" }), { code: "bad-json" });
251 assert.throws(() => Graph.fromJSON({ ...data, version: 2 }), { code: "bad-json" });
252 const tampered = structuredClone(data);
253 tampered.atoms[Number(x.id.slice(1))].deps = [];
254 assert.throws(() => Graph.fromJSON(tampered), { code: "bad-atom" });
255 const dangling = structuredClone(data);
256 dangling.labels = [{ name: "l", head: x.id, base: "nowhere", title: "", message: "" }];
257 assert.throws(() => Graph.fromJSON(dangling), { code: "bad-json" });
258 });
259});