- MCP
- blame on this site's /mcp
- CLI
- abso blame src/engine/test/build.test.ts --json
- JSON
- /christopher/abso-core/blame/src/engine/test/build.test.ts?format=json
549 linesFirst import 98% · codex-capacity-chair 2% · pinch 1%Last change codex-capacity-chair in version 124,
549 lines from 3 changes in 3 versions.
3import assert from "node:assert/strict";
4import { describe, it } from "node:test";
5import {
6 type Atom,
7 buildAtom,
8 buildBase,
9 canonicalText,
10 checkDraft,
11 conflicts,
12 drop,
13 EngineError,
14 Graph,
15 gapKey,
16 lineKey,
17 type Mode,
18 project,
19 View,
20} from "../index.ts";
21import { lines, Repo, Rng } from "./helpers.ts";
23const FIVE = lines("one", "two", "three", "four", "five");
25describe("replacement", () => {
26 it("deletes the old lines, hangs the new ones off the last deleted line, and writes each deleted line", () => {
27 const repo = Repo.init({ "f.txt": FIVE });
28 const atom = repo.edit({ edits: [{ file: "f.txt", old: "two\nthree\n", new: "TWO\nTHREE\nextra\n" }] });
29 assert.deepEqual(atom.deletes, ["a0.1", "a0.2"]);
30 assert.deepEqual(atom.inserts, [{ file: "f.txt", parent: "a0.2", lines: ["TWO", "THREE", "extra"] }]);
31 assert.deepEqual(atom.writes, [lineKey("a0.1"), lineKey("a0.2")]);
32 assert.deepEqual(atom.reads, []);
33 assert.deepEqual(atom.holds, []);
34 assert.equal(repo.text("f.txt"), lines("one", "TWO", "THREE", "extra", "four", "five"));
35 });
37 it("does not depend on the writer of the line below the last deleted one", () => {
38 const repo = Repo.init({ "f.txt": FIVE });
39 const four = repo.edit({ edits: [{ file: "f.txt", old: "four", new: "FOUR" }] });
40 // Replacing two..three: the next visible line after "three" is four's replacement.
41 const atom = repo.edit({ edits: [{ file: "f.txt", old: "two\nthree\n", new: "x\n" }] });
42 assert.deepEqual(atom.deps, ["a0"]);
43 assert.equal(repo.text("f.txt"), lines("one", "x", "FOUR", "five"));
44 // Its closure has the old "four" below it, and dropping four's edit keeps it.
45 assert.equal(repo.text("f.txt", repo.graph.closure(atom.id)), lines("one", "x", "four", "five"));
46 assert.deepEqual(drop(repo.graph, repo.shared, four.id).removed, [four.id]);
47 });
49 it("a deletion-only hunk inserts nothing and has no placement deps", () => {
50 const repo = Repo.init({ "f.txt": FIVE });
51 repo.edit({ edits: [{ file: "f.txt", old: "four", new: "FOUR" }] });
52 const atom = repo.edit({ edits: [{ file: "f.txt", old: "three\n", new: "" }] });
53 assert.deepEqual(atom.inserts, []);
54 assert.deepEqual(atom.deps, ["a0"]);
55 });
57 it("claims only the lines the line diff changes, not unchanged context inside old", () => {
58 const repo = Repo.init({ "f.txt": FIVE });
59 const atom = repo.edit({ edits: [{ file: "f.txt", old: "two\nthree\nfour\n", new: "TWO\nthree\nFOUR\n" }] });
60 assert.deepEqual(atom.deletes, ["a0.1", "a0.3"]);
61 assert.deepEqual(atom.writes, [lineKey("a0.1"), lineKey("a0.3")]);
62 assert.deepEqual(
63 atom.inserts.map((r) => r.parent),
64 ["a0.1", "a0.3"],
65 );
66 });
67});
69describe("pure insertion modes", () => {
70 const insert = (mode: Mode): Atom => {
71 const repo = Repo.init({ "f.txt": FIVE });
72 return repo.edit({ edits: [{ file: "f.txt", old: "two\nthree\n", new: "two\nnew\nthree\n", mode }] });
73 };
75 it("both: writes the gap, reads the line above and the line below", () => {
76 const atom = insert("both");
77 assert.deepEqual(atom.inserts, [{ file: "f.txt", parent: "a0.1", lines: ["new"] }]);
78 assert.deepEqual(atom.writes, [gapKey("a0.1")]);
79 assert.deepEqual(atom.reads, [lineKey("a0.1"), lineKey("a0.2")]);
80 assert.deepEqual(atom.holds, []);
81 });
83 it("after: reads only the line above", () => {
84 const atom = insert("after");
85 assert.deepEqual(atom.writes, [gapKey("a0.1")]);
86 assert.deepEqual(atom.reads, [lineKey("a0.1")]);
87 });
89 it("before: reads only the line below, still hangs off the line above and depends on its writer", () => {
90 const atom = insert("before");
91 assert.deepEqual(atom.inserts, [{ file: "f.txt", parent: "a0.1", lines: ["new"] }]);
92 assert.deepEqual(atom.writes, [gapKey("a0.1")]);
93 assert.deepEqual(atom.reads, [lineKey("a0.2")]);
94 assert.deepEqual(atom.deps, ["a0"]);
95 });
97 it("at the edge of a file, after and before read the one neighbour there is", () => {
98 const repo = Repo.init({ "f.txt": FIVE });
99 const top = repo.draft({ edits: [{ file: "f.txt", start: 0, end: 0, lines: ["zero"], mode: "after" }] });
100 assert.deepEqual(top.writes, [gapKey("^f.txt")]);
101 assert.deepEqual(top.reads, [lineKey("a0.0")]);
102 const bottom = repo.draft({ edits: [{ file: "f.txt", start: 5, end: 5, lines: ["six"], mode: "before" }] });
103 assert.deepEqual(bottom.writes, [gapKey("a0.4")]);
104 assert.deepEqual(bottom.reads, [lineKey("a0.4")]);
105 // So an insertion at the top conflicts with deleting the whole file concurrently.
106 repo.edit({ edits: [{ file: "f.txt", old: FIVE, new: "" }] }, ["a0"]);
107 repo.edit({ edits: [{ file: "f.txt", start: 0, end: 0, lines: ["// header"], mode: "after" }] }, ["a0"]);
108 assert.equal(conflicts(repo.graph, repo.shared).length, 1);
109 });
111 it("wide: also holds every line of old and the gaps between them", () => {
112 const atom = insert("wide");
113 assert.deepEqual(atom.reads, [lineKey("a0.1"), lineKey("a0.2")]);
114 assert.deepEqual(atom.holds, [gapKey("a0.1"), lineKey("a0.1"), lineKey("a0.2")]);
115 });
117 it("at the top of a file it writes the start gap and reads only the line below", () => {
118 const repo = Repo.init({ "f.txt": FIVE });
119 const atom = repo.edit({ edits: [{ file: "f.txt", old: "one\n", new: "zero\none\n" }] });
120 assert.deepEqual(atom.inserts, [{ file: "f.txt", parent: "^f.txt", lines: ["zero"] }]);
121 assert.deepEqual(atom.writes, [gapKey("^f.txt")]);
122 assert.deepEqual(atom.reads, [lineKey("a0.0")]);
123 });
125 it("at the end of a file it reads only the line above", () => {
126 const repo = Repo.init({ "f.txt": FIVE });
127 const atom = repo.edit({ edits: [{ file: "f.txt", old: "five\n", new: "five\nsix\n" }] });
128 assert.deepEqual(atom.writes, [gapKey("a0.4")]);
129 assert.deepEqual(atom.reads, [lineKey("a0.4")]);
pinch78Biome is silent: about 90 hints cleared. String joins as templates, dotted keys, unused imports, variables and a private method removed, optional chains (one kept boolean with === true), the preset field instead of the deprecated recommended. Two rules off where they don't fit: noExplicitAny in the hook modules, whose engine types come from the host, and noTemplateCurlyInString in tests, which hold source text as data.Compare130 assert.equal(repo.text("f.txt"), `${FIVE}six\n`);
132});
134describe("matching old text", () => {
135 it("widens a match inside a line to the whole line", () => {
136 const repo = Repo.init({ "f.txt": FIVE });
137 const atom = repo.edit({ edits: [{ file: "f.txt", old: "hre", new: "HRE" }] });
138 assert.deepEqual(atom.deletes, ["a0.2"]);
139 assert.equal(repo.text("f.txt"), lines("one", "two", "tHREe", "four", "five"));
140 });
142 it("widens a match across two partial lines", () => {
143 const repo = Repo.init({ "f.txt": FIVE });
144 repo.edit({ edits: [{ file: "f.txt", old: "ree\nfo", new: "REE\nFO" }] });
145 assert.equal(repo.text("f.txt"), lines("one", "two", "thREE", "FOur", "five"));
146 });
148 it("joins the next line when the replacement removes a line break, like plain text does", () => {
149 const repo = Repo.init({ "f.txt": FIVE });
150 const atom = repo.edit({ edits: [{ file: "f.txt", old: "two\n", new: "two " }] });
151 assert.deepEqual(atom.deletes, ["a0.1", "a0.2"]);
152 assert.equal(repo.text("f.txt"), lines("one", "two three", "four", "five"));
153 // At the end of the file there is no next line; a file always ends with a newline, so it is a no-op.
154 assert.throws(() => repo.draft({ edits: [{ file: "f.txt", old: "five\n", new: "five" }] }), { code: "empty-atom" });
155 repo.edit({ edits: [{ file: "f.txt", old: "four\nfive\n", new: "four\nfive and more" }] });
156 assert.equal(repo.text("f.txt"), lines("one", "two three", "four", "five and more"));
157 });
159 it("normalizes CRLF in old and new", () => {
160 const repo = Repo.init({ "f.txt": "one\r\ntwo\r\n" });
161 repo.edit({ edits: [{ file: "f.txt", old: "one\r\ntwo", new: "uno\r\ndos" }] });
162 assert.equal(repo.text("f.txt"), "uno\ndos\n");
163 });
165 it("rejects text that is missing, ambiguous, or empty in a non-empty file", () => {
166 const repo = Repo.init({ "f.txt": lines("x", "x", "y") });
167 const view = repo.view();
168 const build = (old: string) => () =>
169 buildAtom(view, { author: "a", message: "m", edits: [{ file: "f.txt", old, new: "z" }] });
170 assert.throws(build("nope"), { code: "no-match" });
171 assert.throws(build("x"), { code: "ambiguous-match" });
172 assert.throws(build(""), { code: "ambiguous-match" });
173 assert.doesNotThrow(build("x\ny"));
174 });
176 it("rejects overlapping edits in one atom", () => {
177 const repo = Repo.init({ "f.txt": FIVE });
178 assert.throws(
179 () =>
180 repo.draft({
181 edits: [
182 { file: "f.txt", old: "two\nthree", new: "a" },
183 { file: "f.txt", old: "three\nfour", new: "b" },
184 ],
185 }),
186 { code: "overlapping-edits" },
187 );
188 });
190 // Step 1 widens a partial match to whole lines, and line endings are
191 // normalized, so an accepted text edit must project exactly like plain string
192 // replacement followed by that normalization, for any unique substring:
193 // mid-line starts and ends, spans across line breaks, the final newline,
194 // CRLF in the new text, and empty new text.
195 it("projects exactly like plain string replacement, for arbitrary unique substrings (seeded)", () => {
196 const alphabet = ["a", "b", "c", " ", "\n", "\n"];
197 let checked = 0;
198 let noops = 0;
199 for (let seed = 1; seed <= 3000; seed++) {
200 const rng = new Rng(seed);
201 let text = "";
202 for (let i = rng.range(1, 14); i > 0; i--) text += rng.pick(alphabet);
203 text = canonicalText(text);
204 if (text === "") continue;
205 const s = rng.int(text.length);
206 const e = rng.range(s + 1, text.length);
207 const old = text.slice(s, e);
208 if (text.indexOf(old, text.indexOf(old) + 1) >= 0) continue;
209 let replacement = "";
210 for (let i = rng.range(0, 5); i > 0; i--) replacement += rng.pick(alphabet);
211 if (rng.chance(0.2)) replacement = replacement.replace(/\n/g, "\r\n");
212 const expected = canonicalText(text.slice(0, s) + replacement + text.slice(e));
213 const graph = new Graph();
214 graph.add(buildBase({ f: text }, { author: "import", message: "snapshot" }));
215 const set = new Set(["a0"]);
216 const context = `seed ${seed}: ${JSON.stringify({ text, old, replacement })}`;
217 let atom;
218 try {
219 atom = buildAtom(new View(graph, set), {
220 author: "x",
221 message: "x",
222 edits: [{ file: "f", old, new: replacement }],
223 });
224 } catch (err) {
225 // A no-op (after normalization) is the only acceptable refusal.
226 assert.ok(
227 err instanceof EngineError && err.code === "empty-atom" && expected === text,
228 `${context}: ${String(err)}`,
229 );
230 noops++;
231 continue;
232 }
233 graph.add(atom);
234 set.add(atom.id);
235 assert.equal(new View(graph, set).text("f"), expected, context);
236 checked++;
237 }
238 assert.ok(checked > 1500, `only ${checked} edits checked`);
239 assert.ok(noops > 0);
240 });
242 it("rejects an edit that changes and claims nothing", () => {
243 const repo = Repo.init({ "f.txt": FIVE });
244 assert.throws(() => repo.draft({ edits: [{ file: "f.txt", old: "two", new: "two" }] }), { code: "empty-atom" });
245 // The same no-op in wide mode is a claim on the span.
246 const claim = repo.draft({ edits: [{ file: "f.txt", old: "two", new: "two", mode: "wide" }] });
247 assert.deepEqual(claim.holds, [lineKey("a0.1")]);
248 });
249});
251describe("splice edits", () => {
252 it("replace visible lines by index, diffing old against new", () => {
253 const repo = Repo.init({ "f.txt": FIVE });
254 const atom = repo.edit({ edits: [{ file: "f.txt", start: 1, end: 3, lines: ["two", "TRES"] }] });
255 assert.deepEqual(atom.deletes, ["a0.2"]);
256 assert.equal(repo.text("f.txt"), lines("one", "two", "TRES", "four", "five"));
257 });
259 it("insert at start === end, including the very top and bottom", () => {
260 const repo = Repo.init({ "f.txt": FIVE });
261 repo.edit({ edits: [{ file: "f.txt", start: 0, end: 0, lines: ["zero"] }] });
262 repo.edit({ edits: [{ file: "f.txt", start: 6, end: 6, lines: ["six"] }] });
263 assert.equal(repo.text("f.txt"), lines("zero", "one", "two", "three", "four", "five", "six"));
264 });
266 it("a large edit is still a minimal line diff: changing the ends of 2100 lines deletes two", () => {
267 const n = 2100;
268 const base = Array.from({ length: n }, (_, i) => `line ${i}`);
269 const repo = Repo.init({ "big.txt": lines(...base) });
270 const next = [...base];
271 next[0] = "FIRST";
272 next[n - 1] = "LAST";
273 const draft = repo.draft({ edits: [{ file: "big.txt", start: 0, end: n, lines: next }] });
274 assert.deepEqual(draft.deletes, ["a0.0", `a0.${n - 1}`]);
275 // So a concurrent edit to the middle of the file does not collide with it.
276 const middle = repo.draft({ edits: [{ file: "big.txt", old: "line 1000\n", new: "line one thousand\n" }] }, ["a0"]);
277 assert.deepEqual(
278 draft.writes.filter((k) => middle.writes.includes(k)),
279 [],
280 );
281 });
283 it("reject bad ranges and lines with breaks", () => {
284 const repo = Repo.init({ "f.txt": FIVE });
285 assert.throws(() => repo.draft({ edits: [{ file: "f.txt", start: 3, end: 2, lines: [] }] }), { code: "bad-range" });
286 assert.throws(() => repo.draft({ edits: [{ file: "f.txt", start: 0, end: 9, lines: [] }] }), { code: "bad-range" });
287 assert.throws(() => repo.draft({ edits: [{ file: "f.txt", start: 0, end: 1, lines: ["a\nb"] }] }), {
288 code: "bad-edit",
289 });
290 });
291});
293describe("several edits in one atom", () => {
294 it("numbers inserted lines across all runs, in file then position order", () => {
295 const repo = Repo.init({ "a.txt": lines("a1", "a2"), "b.txt": lines("b1") });
296 const atom = repo.edit({
297 edits: [
298 { file: "b.txt", old: "b1\n", new: "b1\nB\n" },
299 { file: "a.txt", old: "a2\n", new: "a2\nA2\n" },
300 { file: "a.txt", old: "a1\n", new: "A1\n" },
301 ],
302 });
303 assert.deepEqual(
304 atom.inserts.map((r) => [r.file, r.parent, r.lines]),
305 [
306 ["b.txt", "a0.2", ["B"]],
307 ["a.txt", "a0.0", ["A1"]],
308 ["a.txt", "a0.1", ["A2"]],
309 ],
310 );
311 assert.deepEqual(repo.graph.linesOf(atom.id), [`${atom.id}.0`, `${atom.id}.1`, `${atom.id}.2`]);
312 assert.equal(repo.graph.line(`${atom.id}.1`)!.text, "A1");
313 assert.deepEqual(repo.files(), { "a.txt": lines("A1", "a2", "A2"), "b.txt": lines("b1", "B") });
314 });
316 it("two insertions into the same gap keep their text order", () => {
317 const repo = Repo.init({ "f.txt": FIVE });
318 const atom = repo.edit({
319 edits: [
320 { file: "f.txt", start: 2, end: 2, lines: ["first"] },
321 { file: "f.txt", start: 2, end: 2, lines: ["second"] },
322 { file: "f.txt", start: 2, end: 3, lines: ["third", "THREE"] },
323 ],
324 });
325 assert.equal(atom.inserts.length, 3);
326 assert.equal(repo.text("f.txt"), lines("one", "two", "first", "second", "third", "THREE", "four", "five"));
327 });
328});
330describe("files", () => {
331 it("creates a file with an empty old text, deletes it by deleting every line, and recreates it", () => {
332 const repo = Repo.init({ "f.txt": FIVE });
333 const created = repo.edit({ edits: [{ file: "g.txt", old: "", new: "g1\ng2" }] });
334 assert.deepEqual(created.deps, []);
335 assert.equal(repo.text("g.txt"), "g1\ng2\n");
336 const edited = repo.edit({ edits: [{ file: "g.txt", old: "g2", new: "G2" }] });
337 const removed = repo.edit({ edits: [{ file: "g.txt", old: "g1\nG2\n", new: "" }] });
codex-capacity-chair124Reduce implicit dependencies to their maximal frontierCompare338 assert.deepEqual(removed.deps, [edited.id]);
339 assert.deepEqual(repo.graph.closureOf(removed.deps), repo.graph.closureOf([created.id, edited.id]));
341 assert.deepEqual(Object.keys(repo.files()), ["f.txt"]);
342 assert.deepEqual(repo.view().knownPaths(), ["f.txt", "g.txt"]);
343 // Recreating lands in front of the tombstones. It depends on the atom whose
344 // lines hang off the start (gap rule) and on the deletions: it relies on
345 // the file being empty, so its closure shows only the new text.
346 const again = repo.edit({ edits: [{ file: "g.txt", old: "", new: "fresh\n" }] });
codex-capacity-chair124Reduce implicit dependencies to their maximal frontierCompare347 assert.deepEqual(again.deps, [removed.id]);
348 assert.deepEqual(repo.graph.closureOf(again.deps), repo.graph.closureOf([created.id, edited.id, removed.id]));
350 assert.equal(repo.text("g.txt", repo.graph.closure(again.id)), "fresh\n");
351 });
353 it("a path may start with '^' (its start node is '^^...')", () => {
354 const graph = new Graph();
355 graph.add(
356 buildBase({ "^notes.txt": lines("one"), "a.txt": lines("a") }, { author: "import", message: "snapshot" }),
357 );
358 const set = new Set(["a0"]);
359 assert.equal(new View(graph, set).text("^notes.txt"), lines("one"));
360 assert.deepEqual(graph.atom("a0").writes, [gapKey("^^notes.txt"), gapKey("^a.txt")]);
361 const edit = buildAtom(new View(graph, set), {
362 author: "x",
363 message: "x",
364 edits: [{ file: "^notes.txt", old: "one", new: "ONE" }],
365 });
366 graph.add(edit);
367 set.add(edit.id);
368 const top = buildAtom(new View(graph, set), {
369 author: "x",
370 message: "x",
371 edits: [{ file: "^notes.txt", start: 0, end: 0, lines: ["zero"] }],
372 });
373 graph.add(top);
374 set.add(top.id);
375 assert.equal(project(graph, set)["^notes.txt"], lines("zero", "ONE"));
376 assert.deepEqual(new View(Graph.fromJSON(JSON.parse(JSON.stringify(graph))), set).files(), project(graph, set));
377 });
379 it("the base snapshot skips empty files and writes each file start gap", () => {
380 const repo = Repo.init({ "b.txt": "b\n", "a.txt": "a\n", "empty.txt": "" });
381 const a0 = repo.graph.atom("a0");
382 assert.deepEqual(
383 a0.inserts.map((r) => r.file),
384 ["a.txt", "b.txt"],
385 );
386 assert.deepEqual(a0.writes, [gapKey("^a.txt"), gapKey("^b.txt")]);
387 assert.equal(repo.graph.lamport("a0"), 0);
388 });
389});
391describe("explicit claims", () => {
392 it("reliesOn holds lines anywhere and depends on their writers", () => {
393 const repo = Repo.init({ "f.txt": FIVE, "g.txt": lines("g") });
394 const g2 = repo.edit({ edits: [{ file: "g.txt", old: "g", new: "G" }] });
395 const atom = repo.edit({
396 edits: [{ file: "f.txt", old: "one", new: "ONE" }],
397 reliesOn: [{ file: "g.txt", start: 0, end: 1 }],
398 });
399 assert.deepEqual(atom.holds, [lineKey(`${g2.id}.0`)]);
codex-capacity-chair124Reduce implicit dependencies to their maximal frontierCompare400 assert.deepEqual(atom.deps, [g2.id]);
401 assert.deepEqual(repo.graph.closureOf(atom.deps), repo.graph.closureOf(["a0", g2.id]));
404 it("reliesOn a range of lines also holds the gaps between them", () => {
405 const repo = Repo.init({ "f.txt": FIVE });
406 const atom = repo.draft({ reliesOn: [{ file: "f.txt", start: 1, end: 4 }] });
407 assert.deepEqual(atom.holds, [gapKey("a0.1"), gapKey("a0.2"), lineKey("a0.1"), lineKey("a0.2"), lineKey("a0.3")]);
408 });
410 it("rejects empty or out-of-range ranges", () => {
411 const repo = Repo.init({ "f.txt": FIVE });
412 assert.throws(() => repo.draft({ spans: [{ file: "f.txt", start: 2, end: 2 }] }), { code: "bad-range" });
413 assert.throws(() => repo.draft({ reliesOn: [{ file: "f.txt", start: 4, end: 6 }] }), { code: "bad-range" });
414 assert.throws(() => repo.draft({ reliesOn: [{ file: "nope.txt", start: 0, end: 1 }] }), { code: "bad-range" });
415 });
417 it("needs and resolves must be atoms in the view; reasons must point at real atoms", () => {
418 const repo = Repo.init({ "f.txt": FIVE });
419 const x = repo.edit({ edits: [{ file: "f.txt", old: "one", new: "ONE" }] });
420 assert.throws(() => repo.draft({ needs: ["a99"] }), { code: "unknown-atom" });
421 assert.throws(() => repo.draft({ needs: [x.id] }, ["a0"]), { code: "not-in-view" });
422 assert.throws(() => repo.draft({ resolves: [x.id] }, ["a0"]), { code: "not-in-view" });
423 assert.throws(() => repo.draft({ reasons: [{ to: "a42", kind: "together", text: "" }] }), { code: "unknown-atom" });
424 assert.throws(() => repo.draft({ reasons: [{ to: x.id, kind: "nope" as "together", text: "" }] }), {
425 code: "bad-edit",
426 });
427 // An atom with only needs is allowed: it ties others together (a label head, say).
428 const tie = repo.edit({ message: "feature", needs: [x.id] });
429 assert.deepEqual(tie.deps, [x.id]);
430 assert.deepEqual(repo.graph.atom(tie.id).inserts, []);
431 });
432});
434describe("derived dependencies", () => {
435 it("gap rule: an insertion depends on every atom whose lines hang off the same line, and on nothing below", () => {
436 const repo = Repo.init({ "f.txt": FIVE });
437 const added = repo.edit({ edits: [{ file: "f.txt", old: "two\n", new: "two\nmid\n" }] });
438 const removed = repo.edit({ edits: [{ file: "f.txt", old: "mid\n", new: "" }] });
439 const three = repo.edit({ edits: [{ file: "f.txt", old: "three", new: "THREE" }] });
440 // Inserting after "two": the gap holds mid (a tombstone hanging off "two") and then THREE.
441 const atom = repo.edit({ edits: [{ file: "f.txt", old: "two\n", new: "two\nnew\n", mode: "after" }] });
codex-capacity-chair124Reduce implicit dependencies to their maximal frontierCompare442 assert.deepEqual(atom.deps, [added.id]);
443 assert.deepEqual(repo.graph.closureOf(atom.deps), repo.graph.closureOf(["a0", added.id]));
init0Start the project444 assert.equal(repo.text("f.txt"), lines("one", "two", "new", "THREE", "four", "five"));
445 // It outranks mid, so it sits directly after "two" in any set; in its own
446 // closure mid is back (the deletion isn't in it) and stays below it.
447 assert.equal(
448 repo.text("f.txt", repo.graph.closure(atom.id)),
449 lines("one", "two", "new", "mid", "three", "four", "five"),
450 );
451 assert.deepEqual(drop(repo.graph, repo.shared, removed.id).removed, [removed.id]);
452 assert.deepEqual(drop(repo.graph, repo.shared, three.id).removed, [three.id]);
453 });
455 it("editing unrelated lines bottom-up gives the same independent atoms as top-down", () => {
456 const SIX = lines("l1", "l2", "l3", "l4", "l5", "l6");
457 const topDown = Repo.init({ "f.txt": SIX });
458 const down = [1, 2, 3, 4, 5, 6].map((i) =>
459 topDown.edit({ edits: [{ file: "f.txt", old: `l${i}`, new: `L${i}` }] }),
460 );
461 const bottomUp = Repo.init({ "f.txt": SIX });
462 const up = [6, 5, 4, 3, 2, 1].map((i) => bottomUp.edit({ edits: [{ file: "f.txt", old: `l${i}`, new: `L${i}` }] }));
463 assert.equal(bottomUp.text("f.txt"), topDown.text("f.txt"));
464 assert.deepEqual(
465 down.map((a) => a.deps),
466 down.map(() => ["a0"]),
467 );
468 assert.deepEqual(
469 up.map((a) => a.deps),
470 up.map(() => ["a0"]),
471 );
472 // Dropping the edit to l6 leaves the edit to l5 (and the rest) alone.
473 const { set, removed } = drop(bottomUp.graph, bottomUp.shared, up[0]!.id);
474 assert.deepEqual(removed, [up[0]!.id]);
475 assert.equal(bottomUp.text("f.txt", set), lines("L1", "L2", "L3", "L4", "L5", "l6"));
476 });
478 it("a live insert into a gap with an unresolved conflict depends on both sides of it", () => {
479 // Kept on purpose (docs/prototype.md, "Dependencies"): without the gap
480 // rule N would conflict with A on G:X, and live edits must not collide.
481 const repo = Repo.init({ "f.txt": lines("X", "Y") });
482 const A = repo.edit({ message: "A after X", edits: [{ file: "f.txt", old: "X\n", new: "X\nA\n" }] }, ["a0"]);
483 const B = repo.edit({ message: "B after X", edits: [{ file: "f.txt", old: "X\n", new: "X\nB\n" }] }, ["a0"]);
484 assert.equal(repo.text("f.txt"), lines("X", "B", "A", "Y"));
485 const draft = repo.draft({ message: "N after X", edits: [{ file: "f.txt", start: 1, end: 1, lines: ["N"] }] });
486 assert.deepEqual(repo.check(draft).conflicts, []);
487 const N = repo.edit({ message: "N after X", edits: [{ file: "f.txt", start: 1, end: 1, lines: ["N"] }] });
codex-capacity-chair124Reduce implicit dependencies to their maximal frontierCompare488 assert.deepEqual(N.deps, [A.id, B.id]);
489 assert.deepEqual(repo.graph.closureOf(N.deps), repo.graph.closureOf(["a0", A.id, B.id]));
491 assert.deepEqual(
492 conflicts(repo.graph, repo.shared).map((c) => c.atoms),
493 [[A.id, B.id]],
494 );
495 // Dropping a side takes N along; resolving the conflict keeps everything.
496 assert.deepEqual(drop(repo.graph, repo.shared, A.id).removed, [A.id, N.id]);
497 repo.edit({
498 message: "keep both, A first",
499 resolves: [A.id, B.id],
500 edits: [{ file: "f.txt", old: "B\nA\n", new: "A\nB\n" }],
501 });
502 assert.equal(repo.text("f.txt"), lines("X", "N", "A", "B", "Y"));
503 });
505 it("implicit-read rule: changing a line someone inserted next to depends on that insert", () => {
506 const repo = Repo.init({ "f.txt": FIVE });
507 const between = repo.edit({ edits: [{ file: "f.txt", old: "two\n", new: "two\nbetween\n" }] });
508 assert.ok(between.reads.includes(lineKey("a0.2")));
509 // Deleting "three" (the line below the insert) touches nothing the insert wrote,
510 // but it changes a line the insert read: a sequential edit, so a dependency.
511 const atom = repo.edit({ edits: [{ file: "f.txt", old: "three\n", new: "" }] });
codex-capacity-chair124Reduce implicit dependencies to their maximal frontierCompare512 assert.deepEqual(atom.deps, [between.id]);
513 assert.deepEqual(repo.graph.closureOf(atom.deps), repo.graph.closureOf(["a0", between.id]));
515 checkDraft(repo.graph, repo.shared, repo.draft({ edits: [{ file: "f.txt", old: "four\n", new: "" }] })).conflicts,
516 [],
517 );
518 });
520 it("gap rule: claiming a gap someone inserted into (even if their lines are gone) depends on them", () => {
521 const repo = Repo.init({ "f.txt": FIVE });
522 const added = repo.edit({ edits: [{ file: "f.txt", old: "two\n", new: "two\nmid\n" }] });
523 const removed = repo.edit({ edits: [{ file: "f.txt", old: "mid\n", new: "" }] });
524 // A wide edit over two..three holds G:two, which `added` wrote.
525 const draft = repo.draft({ edits: [{ file: "f.txt", old: "two\nthree\n", new: "two\nTHREE\n", mode: "wide" }] });
526 assert.ok(draft.holds.includes(gapKey("a0.1")));
527 assert.ok(draft.deps.includes(added.id));
528 assert.ok(!draft.deps.includes(removed.id));
529 assert.deepEqual(repo.check(draft).conflicts, []);
530 });
532 it("does not depend on the rest of its view", () => {
533 const repo = Repo.init({ "f.txt": FIVE, "g.txt": lines("g") });
534 repo.edit({ edits: [{ file: "g.txt", old: "g", new: "G" }] });
535 repo.edit({ edits: [{ file: "f.txt", old: "five", new: "FIVE" }] });
536 const atom = repo.edit({ edits: [{ file: "f.txt", old: "one", new: "ONE" }] });
537 assert.deepEqual(atom.deps, ["a0"]);
538 });
540 it("builds against any closed set, and refuses one that is not closed", () => {
541 const repo = Repo.init({ "f.txt": FIVE });
542 const x = repo.edit({ edits: [{ file: "f.txt", old: "one", new: "ONE" }] });
543 const y = repo.edit({ edits: [{ file: "f.txt", old: "ONE", new: "uno" }] });
544 assert.throws(() => new View(repo.graph, ["a0", y.id]), { code: "not-closed" });
545 const old = repo.draft({ edits: [{ file: "f.txt", old: "one", new: "eins" }] }, ["a0"]);
546 assert.deepEqual(old.deps, ["a0"]);
547 assert.ok(!old.deps.includes(x.id));
548 });
549});