Skip to content

abso-core

Public

abso is version control for agents: the client, the engine and a server you can run on your own Cloudflare account.

Check passed
MCP
code on this site's /mcp
CLI
none: the JSON below is the answer
JSON
/christopher/abso-core/files/src/engine/test/scenarios.test.ts?format=json
879 linesFirst import 100% · pinch 1%Last change pinch in version 78, 2 versions changed it
1// The nine scenarios from the June 2026 conversation (docs/prototype.md,
2// "Tests the prototype must have").
3
4import assert from "node:assert/strict";
5import { describe, it } from "node:test";
6import {
7 type Atom,
8 type AtomInput,
9 buildAtom,
10 conflicts,
11 drop,
12 Graph,
13 gapKey,
14 history,
15 labelContents,
16 lineKey,
17 project,
18 reslice,
19 unresolvedConflicts,
20 View,
21} from "../index.ts";
22import { lines, Repo } from "./helpers.ts";
24const SIDEBAR = lines(
25 "import { useState } from 'react';",
26 "import { Panel } from './Panel';",
27 "",
28 "export function Sidebar() {",
29 " return (",
30 ' <aside className="sidebar">',
31 " <Panel />",
32 " </aside>",
33 " );",
34 "}",
35);
37describe("1. sidebar rail", () => {
38 const repo = Repo.init({ "Panel.tsx": lines("export function Panel() {}"), "Sidebar.tsx": SIDEBAR });
39 const rail = repo.edit({
40 message: "add the draggable rail component",
41 edits: [
42 { file: "Rail.tsx", old: "", new: lines("export function Rail() {", ' return <div className="rail" />;', "}") },
43 ],
44 });
45 const importInput = [
46 {
47 file: "Sidebar.tsx",
48 old: "import { Panel } from './Panel';\n",
49 new: "import { Panel } from './Panel';\nimport { Rail } from './Rail';\n",
50 },
51 ];
52 const importRail = repo.edit({ message: "import Rail into the sidebar", edits: importInput });
53 const placeInput: AtomInput = {
54 author: "agent",
55 message: "place the rail after the panel",
56 edits: [{ file: "Sidebar.tsx", old: " <Panel />\n", new: " <Panel />\n <Rail />\n" }],
57 };
58 const placeTextOnly = buildAtom(repo.view(), placeInput);
59 const placeWithReason = buildAtom(repo.view(), {
60 ...placeInput,
61 reasons: [{ to: importRail.id, kind: "because-of", text: "the import exists because the rail is placed here" }],
62 });
63 const place = repo.edit({
64 ...placeInput,
65 reasons: [{ to: importRail.id, kind: "because-of", text: "the import exists because the rail is placed here" }],
66 needs: [importRail.id],
67 });
68 const width = repo.edit({
69 message: "track the sidebar width",
70 edits: [
71 {
72 file: "Sidebar.tsx",
73 old: "export function Sidebar() {\n",
74 new: "export function Sidebar() {\n const [width, setWidth] = useState(240);\n",
75 },
76 ],
77 });
79 it("creating a file depends on nothing", () => {
80 assert.deepEqual(rail.deps, []);
81 assert.deepEqual(rail.writes, [gapKey("^Rail.tsx")]);
82 assert.deepEqual(rail.reads, []);
83 assert.deepEqual([...repo.graph.closure(rail.id)], [rail.id]);
84 });
86 it("the import depends only on the lines it sits between, not on the new file", () => {
87 const panelImport = repo.lineId("Sidebar.tsx", "import { Panel } from './Panel';");
88 const blank = repo.lineId("Sidebar.tsx", "");
89 assert.deepEqual(importRail.deps, ["a0"]);
90 assert.deepEqual(importRail.writes, [gapKey(panelImport)]);
91 assert.deepEqual(importRail.reads, [lineKey(panelImport), lineKey(blank)].sort());
92 assert.ok(!repo.graph.closure(importRail.id).has(rail.id));
93 });
95 it("placing the rail does not textually need the import", () => {
96 assert.deepEqual(placeTextOnly.deps, ["a0"]);
97 // Accepted on its own, its closure renders <Rail /> without importing it.
98 const bare = Repo.init({ "Panel.tsx": lines("export function Panel() {}"), "Sidebar.tsx": SIDEBAR });
99 bare.edit({ message: "import Rail", edits: importInput });
100 const placed = bare.edit(placeInput);
101 const alone = new View(bare.graph, bare.graph.closure(placed.id)).text("Sidebar.tsx");
102 assert.ok(alone.includes("<Rail />"));
103 assert.ok(!alone.includes("import { Rail }"));
104 assert.ok(bare.text("Sidebar.tsx").includes("import { Rail }"));
105 });
107 it("a because-of reason alone is not a dependency; reason plus needs puts the import in the closure", () => {
108 assert.deepEqual(placeWithReason.deps, ["a0"]);
109 assert.deepEqual(place.deps, ["a0", importRail.id]);
110 assert.ok(repo.graph.closure(place.id).has(importRail.id));
111 assert.deepEqual(place.reasons, [
112 { to: importRail.id, kind: "because-of", text: "the import exists because the rail is placed here" },
113 ]);
114 });
116 it("the width state depends only on the base", () => {
117 assert.deepEqual(width.deps, ["a0"]);
118 assert.deepEqual(conflicts(repo.graph, repo.shared), []);
119 });
121 it("the shared projection has every piece in place", () => {
122 assert.equal(
123 repo.text("Sidebar.tsx"),
124 lines(
125 "import { useState } from 'react';",
126 "import { Panel } from './Panel';",
127 "import { Rail } from './Rail';",
128 "",
129 "export function Sidebar() {",
130 " const [width, setWidth] = useState(240);",
131 " return (",
132 ' <aside className="sidebar">',
133 " <Panel />",
134 " <Rail />",
135 " </aside>",
136 " );",
137 "}",
138 ),
139 );
140 assert.equal(repo.text("Rail.tsx"), lines("export function Rail() {", ' return <div className="rail" />;', "}"));
141 });
142});
144describe("2. two imports", () => {
145 const BASE = lines(
146 "import { Panel } from './Panel';",
147 "import { Header } from './Header';",
148 "import { OldFooter } from './OldFooter';",
149 "",
150 "export function Sidebar() {}",
151 );
152 const RAIL = "import { Rail } from './Rail';";
153 const FOOTER = "import { Footer } from './Footer';";
154 const PANEL = "import { Panel } from './Panel';";
155 const HEADER = "import { Header } from './Header';";
156 const OLD_FOOTER = "import { OldFooter } from './OldFooter';";
158 type Spec = Omit<AtomInput, "author" | "message" | "time"> & { author: string; message: string };
160 /** Build both atoms against the base, accepted in the given order. */
161 function concurrently(first: Spec, second: Spec): { repo: Repo; a: Atom; b: Atom } {
162 const repo = Repo.init({ "Sidebar.tsx": BASE });
163 const a = repo.edit(first, ["a0"]);
164 const b = repo.edit(second, ["a0"]);
165 return { repo, a, b };
166 }
167 const insertAfter = (anchor: string, line: string, mode: "both" | "after" = "both") => ({
168 file: "Sidebar.tsx",
169 old: `${anchor}\n`,
170 new: `${anchor}\n${line}\n`,
171 mode,
172 });
173 const railAfter = (anchor: string, mode: "both" | "after" = "both"): Spec => ({
174 author: "rail-agent",
175 message: "import Rail for the draggable rail",
176 edits: [insertAfter(anchor, RAIL, mode)],
177 });
178 const footerAfter = (anchor: string, mode: "both" | "after" = "both"): Spec => ({
179 author: "footer-agent",
180 message: "import Footer for the new footer",
181 edits: [insertAfter(anchor, FOOTER, mode)],
182 });
183 const footerReplacesOld: Spec = {
184 author: "footer-agent",
185 message: "replace the old footer with the new one",
186 edits: [{ file: "Sidebar.tsx", old: OLD_FOOTER, new: FOOTER }],
187 };
189 it("different neighbours: no conflict, same bytes in either acceptance order", () => {
190 const one = concurrently(railAfter(PANEL), footerAfter(HEADER));
191 const two = concurrently(footerAfter(HEADER), railAfter(PANEL));
192 assert.deepEqual(conflicts(one.repo.graph, one.repo.shared), []);
193 assert.deepEqual(conflicts(two.repo.graph, two.repo.shared), []);
194 const expected = lines(PANEL, RAIL, HEADER, FOOTER, OLD_FOOTER, "", "export function Sidebar() {}");
195 assert.equal(one.repo.text("Sidebar.tsx"), expected);
196 assert.equal(two.repo.text("Sidebar.tsx"), expected);
197 });
199 it("after mode: the rail doesn't claim its south neighbour, so replacing that line doesn't conflict", () => {
200 const one = concurrently(railAfter(HEADER, "after"), footerReplacesOld);
201 const two = concurrently(footerReplacesOld, railAfter(HEADER, "after"));
202 assert.deepEqual(conflicts(one.repo.graph, one.repo.shared), []);
203 assert.deepEqual(conflicts(two.repo.graph, two.repo.shared), []);
204 const expected = lines(PANEL, HEADER, RAIL, FOOTER, "", "export function Sidebar() {}");
205 assert.equal(one.repo.text("Sidebar.tsx"), expected);
206 assert.equal(two.repo.text("Sidebar.tsx"), expected);
207 });
209 it("the same pair in both mode conflicts: the rail relied on the line below it", () => {
210 const { repo, a, b } = concurrently(railAfter(HEADER, "both"), footerReplacesOld);
211 const found = conflicts(repo.graph, repo.shared);
212 assert.equal(found.length, 1);
213 const c = found[0]!;
214 assert.deepEqual(c.atoms, [a.id, b.id]);
215 assert.equal(c.kind, "edit-edit");
216 const oldFooterLine = repo.lineId("Sidebar.tsx", OLD_FOOTER, ["a0"]);
217 assert.deepEqual(c.keys, [{ key: lineKey(oldFooterLine), writer: b.id, other: a.id, relation: "read" }]);
218 });
220 it("both inserting into the same gap in both mode conflict; a third atom resolves it with both reasons visible", () => {
221 const { repo, a, b } = concurrently(railAfter(HEADER), footerAfter(HEADER));
222 const found = conflicts(repo.graph, repo.shared);
223 assert.equal(found.length, 1);
224 const c = found[0]!;
225 assert.equal(c.kind, "edit-edit");
226 const headerLine = repo.lineId("Sidebar.tsx", HEADER);
227 assert.deepEqual(c.keys, [{ key: gapKey(headerLine), writer: a.id, other: b.id, relation: "write" }]);
228 assert.deepEqual(c.sides, [
229 { atom: a.id, author: "rail-agent", message: "import Rail for the draggable rail" },
230 { atom: b.id, author: "footer-agent", message: "import Footer for the new footer" },
231 ]);
232 assert.deepEqual(c.resolvedBy, []);
234 // A conflicted set still projects deterministically: the higher priority run comes first.
235 const merged = repo.text("Sidebar.tsx");
236 assert.equal(merged, lines(PANEL, HEADER, FOOTER, RAIL, OLD_FOOTER, "", "export function Sidebar() {}"));
238 // The resolver sees both sides and orders them on purpose.
239 const resolver = repo.edit({
240 author: "reviewer",
241 message: "keep both imports, Rail before Footer",
242 resolves: [a.id, b.id],
243 edits: [{ file: "Sidebar.tsx", old: `${FOOTER}\n${RAIL}\n`, new: `${RAIL}\n${FOOTER}\n` }],
244 });
245 assert.deepEqual(
246 resolver.deps.filter((d) => d === a.id || d === b.id),
247 [a.id, b.id],
248 );
249 const after = conflicts(repo.graph, repo.shared);
250 assert.equal(after.length, 1);
251 assert.deepEqual(after[0]!.resolvedBy, [resolver.id]);
252 assert.deepEqual(unresolvedConflicts(repo.graph, repo.shared), []);
253 assert.equal(
254 repo.text("Sidebar.tsx"),
255 lines(PANEL, HEADER, RAIL, FOOTER, OLD_FOOTER, "", "export function Sidebar() {}"),
256 );
257 });
259 it("two after-mode inserts into the same gap also conflict (both claim the slot right after X)", () => {
260 const { repo } = concurrently(railAfter(HEADER, "after"), footerAfter(HEADER, "after"));
261 const found = conflicts(repo.graph, repo.shared);
262 assert.equal(found.length, 1);
263 assert.equal(found[0]!.keys[0]!.key, gapKey(repo.lineId("Sidebar.tsx", HEADER)));
264 });
266 // June: "you maybe even claim your neighbor to the south or your neighbor to the north".
267 it("before mode claims only the south neighbour: an append above a closing bracket commutes with editing the last item", () => {
268 const LIST = lines("const items = [", " a,", " b,", " c,", "];");
269 const append: Spec = {
270 author: "append-agent",
271 message: "add d at the end",
272 edits: [{ file: "list.ts", start: 4, end: 4, lines: [" d,"], mode: "before" }],
273 };
274 const upper: Spec = {
275 author: "case-agent",
276 message: "c is upper case",
277 edits: [{ file: "list.ts", old: " c,", new: " C," }],
278 };
279 for (const order of [
280 [append, upper],
281 [upper, append],
282 ]) {
283 const repo = Repo.init({ "list.ts": LIST });
284 for (const input of order) repo.edit(input, ["a0"]);
285 assert.deepEqual(conflicts(repo.graph, repo.shared), []);
286 assert.equal(repo.text("list.ts"), lines("const items = [", " a,", " b,", " C,", " d,", "];"));
287 }
288 // In both or after mode the append reads " c," and the pair conflicts.
289 for (const mode of ["both", "after"] as const) {
290 const repo = Repo.init({ "list.ts": LIST });
291 repo.edit({ ...append, edits: [{ ...append.edits![0]!, mode }] }, ["a0"]);
292 repo.edit(upper, ["a0"]);
293 assert.equal(conflicts(repo.graph, repo.shared).length, 1, mode);
294 }
295 // Two appends before the same bracket still conflict: both claim the gap.
296 const repo = Repo.init({ "list.ts": LIST });
297 repo.edit(append, ["a0"]);
298 repo.edit(
299 {
300 ...append,
301 author: "other-agent",
302 edits: [{ file: "list.ts", start: 4, end: 4, lines: [" e,"], mode: "before" }],
303 },
304 ["a0"],
305 );
306 assert.equal(conflicts(repo.graph, repo.shared).length, 1);
307 });
309 it("live, a later edit to the last item lands above an earlier before-mode append without depending on it", () => {
310 const repo = Repo.init({ "list.ts": lines("const items = [", " a,", "];") });
311 const append = repo.edit({ edits: [{ file: "list.ts", start: 2, end: 2, lines: [" b,"], mode: "before" }] });
312 const draft = repo.draft({ edits: [{ file: "list.ts", old: " a,", new: " A," }] });
313 assert.deepEqual(draft.deps, ["a0"]);
314 assert.deepEqual(repo.check(draft).conflicts, []);
315 repo.edit({ edits: [{ file: "list.ts", old: " a,", new: " A," }] });
316 assert.equal(repo.text("list.ts"), lines("const items = [", " A,", " b,", "];"));
317 assert.equal(repo.text("list.ts", repo.graph.closure(append.id)), lines("const items = [", " a,", " b,", "];"));
318 });
319});
321describe("3. rename against body edit", () => {
322 const DRAG = lines("export function handleDrag(e: PointerEvent) {", " const x = e.clientX;", " return x;", "}");
323 const body: Omit<AtomInput, "author" | "time"> & { author: string } = {
324 author: "body-agent",
325 message: "subtract the rail offset",
326 edits: [{ file: "drag.ts", old: "e.clientX;", new: "e.clientX - offset;" }],
327 };
328 const rename = {
329 author: "rename-agent",
330 message: "rename handleDrag to onDrag",
331 edits: [{ file: "drag.ts", old: "function handleDrag(", new: "function onDrag(" }],
332 };
334 it("don't conflict textually", () => {
335 const repo = Repo.init({ "drag.ts": DRAG });
336 repo.edit(body, ["a0"]);
337 repo.edit(rename, ["a0"]);
338 assert.deepEqual(conflicts(repo.graph, repo.shared), []);
339 assert.equal(
340 repo.text("drag.ts"),
341 lines("export function onDrag(e: PointerEvent) {", " const x = e.clientX - offset;", " return x;", "}"),
342 );
343 });
345 it('conflict as "semantic" when the body edit relies on the signature line', () => {
346 const repo = Repo.init({ "drag.ts": DRAG });
347 const a = repo.edit({ ...body, reliesOn: [{ file: "drag.ts", start: 0, end: 1 }] }, ["a0"]);
348 const b = repo.edit(rename, ["a0"]);
349 const sig = repo.lineId("drag.ts", "export function handleDrag(e: PointerEvent) {", ["a0"]);
350 assert.deepEqual(a.holds, [lineKey(sig)]);
351 const found = conflicts(repo.graph, repo.shared);
352 assert.equal(found.length, 1);
353 assert.equal(found[0]!.kind, "semantic");
354 assert.deepEqual(found[0]!.keys, [{ key: lineKey(sig), writer: b.id, other: a.id, relation: "hold" }]);
355 assert.equal(found[0]!.sides[0].message, "subtract the rail offset");
356 assert.equal(found[0]!.sides[1].author, "rename-agent");
357 });
359 it("live: a rename after the body edit is caught too, and needing the body edit releases it", () => {
360 const repo = Repo.init({ "drag.ts": DRAG });
361 const a = repo.edit({ ...body, reliesOn: [{ file: "drag.ts", start: 0, end: 1 }] });
362 const draft = repo.draft(rename);
363 // The rename doesn't depend on the body edit (nothing it touched is the body edit's).
364 assert.deepEqual(draft.deps, ["a0"]);
365 const report = repo.check(draft);
366 assert.equal(report.conflicts.length, 1);
367 assert.equal(report.conflicts[0]!.kind, "semantic");
368 assert.equal(report.conflicts[0]!.sides[0].atom, a.id);
369 assert.deepEqual(repo.check(repo.draft({ ...rename, needs: [a.id] })).conflicts, []);
370 });
372 it("live: a body line inserted under the signature, relying on it, catches a rename that has to depend on it", () => {
373 const repo = Repo.init({ "drag.ts": DRAG });
374 const guard = repo.edit({
375 author: "body-agent",
376 message: "ignore drags without a target",
377 edits: [{ file: "drag.ts", start: 1, end: 1, lines: [" if (!e.target) return 0;"] }],
378 reliesOn: [{ file: "drag.ts", start: 0, end: 1 }],
379 });
380 // The new signature must land above the guard, which hangs off the old
381 // signature, so the rename depends on the guard; that alone doesn't
382 // release the guard's hold on the signature.
383 const draft = repo.draft(rename);
384 assert.ok(draft.deps.includes(guard.id));
385 const report = repo.check(draft);
386 assert.equal(report.conflicts.length, 1);
387 assert.equal(report.conflicts[0]!.kind, "semantic");
388 assert.deepEqual(report.conflicts[0]!.atoms, [guard.id, draft.id]);
389 // Accepted anyway (--allow-conflict), the set reports the same conflict.
390 repo.edit(rename);
391 assert.deepEqual(
392 conflicts(repo.graph, repo.shared).map((c) => [c.atoms, c.kind]),
393 [[[guard.id, draft.id], "semantic"]],
394 );
395 assert.equal(
396 repo.text("drag.ts"),
397 lines(
398 "export function onDrag(e: PointerEvent) {",
399 " if (!e.target) return 0;",
400 " const x = e.clientX;",
401 " return x;",
402 "}",
403 ),
404 );
405 });
406});
408describe("4. remote reliance", () => {
409 const GEOMETRY = lines("export function getOffset(rect: DOMRect) {", " return rect.left;", "}");
410 const SIDE = lines("export function place(rect: DOMRect) {", " return 0;", "}");
411 const usesOffset = {
412 author: "sidebar-agent",
413 message: "position the rail with getOffset (pixels)",
414 edits: [{ file: "Sidebar.tsx", old: " return 0;", new: " return getOffset(rect) + 4;" }],
415 reliesOn: [{ file: "geometry.ts", start: 1, end: 2 }],
416 };
417 const changesReturn = {
418 author: "geometry-agent",
419 message: "getOffset returns a ratio now",
420 edits: [{ file: "geometry.ts", old: "return rect.left;", new: "return rect.left / rect.width;" }],
421 };
423 it("a change to the relied-on line in another file is a conflict", () => {
424 const repo = Repo.init({ "geometry.ts": GEOMETRY, "Sidebar.tsx": SIDE });
425 const a = repo.edit(usesOffset, ["a0"]);
426 const b = repo.edit(changesReturn, ["a0"]);
427 const found = conflicts(repo.graph, repo.shared);
428 assert.equal(found.length, 1);
429 assert.equal(found[0]!.kind, "semantic");
430 assert.deepEqual(found[0]!.atoms, [a.id, b.id]);
431 assert.equal(found[0]!.keys[0]!.relation, "hold");
432 });
434 it("live: the later edit is reported before acceptance; needing the reliance clears it", () => {
435 const repo = Repo.init({ "geometry.ts": GEOMETRY, "Sidebar.tsx": SIDE });
436 const a = repo.edit(usesOffset);
437 const draft = repo.draft(changesReturn);
438 const report = repo.check(draft);
439 assert.equal(report.conflicts.length, 1);
440 assert.equal(report.conflicts[0]!.kind, "semantic");
441 assert.deepEqual(report.conflicts[0]!.sides[0], {
442 atom: a.id,
443 author: "sidebar-agent",
444 message: usesOffset.message,
445 });
446 assert.deepEqual(
447 report.touches.map((t) => t.atom),
448 ["a0"],
449 );
451 const onTop = repo.draft({ ...changesReturn, needs: [a.id] });
452 assert.deepEqual(repo.check(onTop).conflicts, []);
453 });
455 it("live: an unrelated edit next to the holder's own change does not release its remote hold", () => {
456 const repo = Repo.init({
457 "geometry.ts": GEOMETRY,
458 "Sidebar.tsx": lines("const gap = 4;", "const left = 0;"),
459 });
460 const holder = repo.edit({
461 author: "sidebar-agent",
462 message: "position the rail with getOffset (pixels)",
463 edits: [{ file: "Sidebar.tsx", old: "const left = 0;", new: "const left = getOffset(rect) + gap;" }],
464 reliesOn: [{ file: "geometry.ts", start: 1, end: 2 }],
465 });
466 const ratio = changesReturn.edits[0]!;
467 assert.equal(repo.check(repo.draft({ edits: [ratio] })).conflicts.length, 1);
468 // The same change plus an edit to the line above the holder's edit.
469 const both = repo.draft({
470 author: "geometry-agent",
471 message: "ratios, and a wider gap",
472 edits: [ratio, { file: "Sidebar.tsx", old: "gap = 4", new: "gap = 8" }],
473 });
474 assert.deepEqual(both.deps, ["a0"]);
475 const report = repo.check(both);
476 assert.equal(report.conflicts.length, 1);
477 assert.equal(report.conflicts[0]!.sides[0].atom, holder.id);
478 });
480 // June: "you anchor the semantic claim to the definition's lines, and any edit landing in that span trips it."
481 it("relying on a whole function catches a line inserted inside it", () => {
482 const repo = Repo.init({
483 "geometry.ts": lines(
484 "export function getOffset(rect: DOMRect) {",
485 " const left = rect.left;",
486 " return left;",
487 "}",
488 ),
489 "Sidebar.tsx": lines("const left = 0;"),
490 });
491 const a = repo.edit(
492 {
493 author: "sidebar-agent",
494 message: "position the rail with getOffset (pixels)",
495 edits: [{ file: "Sidebar.tsx", old: "= 0", new: "= getOffset(rect)" }],
496 reliesOn: [{ file: "geometry.ts", start: 0, end: 4 }],
497 },
498 ["a0"],
499 );
500 const b = repo.edit(
501 {
502 author: "geometry-agent",
503 message: "return a ratio when the rect has a width",
504 edits: [
505 {
506 file: "geometry.ts",
507 start: 2,
508 end: 2,
509 lines: [" if (rect.width) return left / rect.width;"],
510 mode: "after",
511 },
512 ],
513 },
514 ["a0"],
515 );
516 const found = conflicts(repo.graph, repo.shared);
517 assert.equal(found.length, 1);
518 assert.equal(found[0]!.kind, "semantic");
519 assert.deepEqual(found[0]!.keys, [
520 {
521 key: gapKey(repo.lineId("geometry.ts", " const left = rect.left;")),
522 writer: b.id,
523 other: a.id,
524 relation: "hold",
525 },
526 ]);
527 });
528});
530describe("5. file1 -> file2 -> file3 -> file1 again", () => {
531 const repo = Repo.init({
532 "file1.ts": lines('export const unit = "px";', "export const scale = 1;"),
533 "file2.ts": lines("export const width = 10;"),
534 "file3.ts": lines("export const height = 10;"),
535 "file4.ts": lines("export const unrelated = true;"),
536 });
537 const one = repo.edit({ message: "file1: switch units", edits: [{ file: "file1.ts", old: '"px"', new: '"rem"' }] });
538 const two = repo.edit({
539 message: "file2: width in rem",
540 edits: [{ file: "file2.ts", old: "10;", new: "0.625;" }],
541 needs: [one.id],
542 reasons: [{ to: one.id, kind: "because-of", text: "units changed" }],
543 });
544 const three = repo.edit({
545 message: "file3: height in rem",
546 edits: [{ file: "file3.ts", old: "10;", new: "0.625;" }],
547 needs: [two.id],
548 });
549 const again = repo.edit({
550 message: "file1: scale follows the new sizes",
551 edits: [{ file: "file1.ts", old: '"rem";\nexport const scale = 1;', new: '"rem";\nexport const scale = 16;' }],
552 needs: [three.id],
553 });
554 const unrelated = repo.edit({
555 message: "file4: unrelated flag",
556 edits: [{ file: "file4.ts", old: "true", new: "false" }],
557 });
559 it("the second file1 change is its own atom, ordered after file3 by dependencies alone", () => {
560 assert.notEqual(again.id, one.id);
561 assert.deepEqual(
562 [one, two, three, again, unrelated].map((a) => repo.graph.lamport(a.id)),
563 [1, 2, 3, 4, 1],
564 );
565 assert.ok(repo.graph.closure(again.id).has(three.id));
566 assert.ok(!repo.graph.closure(one.id).has(again.id));
567 // The unrelated atom was accepted last but sorts before file2's: history is read off deps.
568 assert.deepEqual(
569 history(repo.graph, repo.shared).map((a) => a.id),
570 ["a0", one.id, unrelated.id, two.id, three.id, again.id],
571 );
572 });
574 it("file3's closure shows file1 as the first pass left it", () => {
575 const files = project(repo.graph, repo.graph.closure(three.id));
576 assert.equal(files["file1.ts"], lines('export const unit = "rem";', "export const scale = 1;"));
577 assert.equal(repo.text("file1.ts"), lines('export const unit = "rem";', "export const scale = 16;"));
578 });
579});
581describe("6. claim-only atom", () => {
582 const FILE = lines(
583 "function render() {",
584 " const a = 1;",
585 " const b = 2;",
586 " const c = 3;",
587 " return a + b + c;",
588 "}",
589 "export { render };",
590 );
591 const setup = (): { repo: Repo; claim: Atom } => {
592 const repo = Repo.init({ "render.ts": FILE });
593 const claim = repo.edit({
594 author: "refactor-agent",
595 message: "about to restructure the body of render",
596 spans: [{ file: "render.ts", start: 1, end: 5 }],
597 });
598 return { repo, claim };
599 };
601 it("holds the lines and the gaps between them, and depends on their writer", () => {
602 const { repo, claim } = setup();
603 const ids = repo
604 .view()
605 .lines("render.ts")
606 .slice(1, 5)
607 .map((l) => l.id);
608 assert.deepEqual(claim.inserts, []);
609 assert.deepEqual(claim.deletes, []);
610 assert.deepEqual(claim.writes, []);
611 assert.deepEqual(claim.holds, [...ids.map(lineKey), ...ids.slice(0, 3).map(gapKey)].sort());
612 assert.deepEqual(claim.deps, ["a0"]);
613 });
615 it("an independent edit inside the span conflicts", () => {
616 const { repo, claim } = setup();
617 const report = repo.check(
618 repo.draft({ author: "other", message: "tweak b", edits: [{ file: "render.ts", old: "b = 2", new: "b = 20" }] }),
619 );
620 assert.equal(report.conflicts.length, 1);
621 assert.equal(report.conflicts[0]!.kind, "semantic");
622 assert.deepEqual(report.conflicts[0]!.sides[0], {
623 atom: claim.id,
624 author: "refactor-agent",
625 message: "about to restructure the body of render",
626 });
627 });
629 it("an independent insertion into a held gap conflicts", () => {
630 const { repo } = setup();
631 const report = repo.check(
632 repo.draft({
633 edits: [{ file: "render.ts", old: " const b = 2;\n", new: " const b = 2;\n const d = 4;\n", mode: "after" }],
634 }),
635 );
636 assert.equal(report.conflicts.length, 1);
637 assert.ok(report.conflicts[0]!.keys.some((k) => k.key.startsWith("G:") && k.relation === "hold"));
638 });
640 it("an edit that needs the claim does not conflict", () => {
641 const { repo, claim } = setup();
642 const report = repo.check(
643 repo.draft({ edits: [{ file: "render.ts", old: "b = 2", new: "b = 20" }], needs: [claim.id] }),
644 );
645 assert.deepEqual(report.conflicts, []);
646 });
648 it("an edit outside the span does not conflict", () => {
649 const { repo } = setup();
650 const report = repo.check(
651 repo.draft({ edits: [{ file: "render.ts", old: "export { render };", new: "export { render as draw };" }] }),
652 );
653 assert.deepEqual(report.conflicts, []);
654 });
655});
657describe("7. reason warning", () => {
658 const setup = () => {
659 const repo = Repo.init({
660 "filter.ts": lines("export function keep(x: number) {", " return x > 0;", "}"),
661 "sort.ts": lines("export function before(a: number, b: number) {", " return a < b;", "}"),
662 });
663 const p = repo.edit({ message: "invert keep", edits: [{ file: "filter.ts", old: "x > 0", new: "x <= 0" }] });
664 const q = repo.edit({
665 message: "invert before",
666 edits: [{ file: "sort.ts", old: "a < b", new: "a >= b" }],
667 reasons: [{ to: p.id, kind: "together", text: "this logic was inverted, so that logic was too" }],
668 });
669 return { repo, p, q };
670 };
672 it("changing one side only warns, in either direction", () => {
673 const { repo, p, q } = setup();
674 const onlyP = repo.check(repo.draft({ edits: [{ file: "filter.ts", old: "x <= 0", new: "x < 0" }] }));
675 assert.deepEqual(onlyP.warnings, [
676 {
677 atom: p.id,
678 partner: q.id,
679 text: "this logic was inverted, so that logic was too",
680 message: `${p.id} was changed together with ${q.id} because: this logic was inverted, so that logic was too; you changed ${p.id}'s lines but not ${q.id}'s.`,
681 },
682 ]);
683 const onlyQ = repo.check(repo.draft({ edits: [{ file: "sort.ts", old: "a >= b", new: "a > b" }] }));
684 assert.deepEqual(
685 onlyQ.warnings.map((w) => [w.atom, w.partner]),
686 [[q.id, p.id]],
687 );
688 });
690 it("changing both sides, or reading the other side, does not warn", () => {
691 const { repo } = setup();
692 const both = repo.check(
693 repo.draft({
694 edits: [
695 { file: "filter.ts", old: "x <= 0", new: "x < 0" },
696 { file: "sort.ts", old: "a >= b", new: "a > b" },
697 ],
698 }),
699 );
700 assert.deepEqual(both.warnings, []);
701 const relying = repo.check(
702 repo.draft({
703 edits: [{ file: "filter.ts", old: "x <= 0", new: "x < 0" }],
704 reliesOn: [{ file: "sort.ts", start: 1, end: 2 }],
705 }),
706 );
707 assert.deepEqual(relying.warnings, []);
708 });
710 it("saying why the other side is fine (a reason to it) silences the warning", () => {
711 const { repo, q } = setup();
712 const report = repo.check(
713 repo.draft({
714 edits: [{ file: "filter.ts", old: "x <= 0", new: "x < 0" }],
715 reasons: [{ to: q.id, kind: "because-of", text: "zero is excluded on purpose; before() is unaffected" }],
716 }),
717 );
718 assert.deepEqual(report.warnings, []);
719 });
721 it("inserting a line between two lines of one side's change warns", () => {
722 const repo = Repo.init({
723 "filter.ts": lines("export function keep(x: number) {", " return x > 0;", "}"),
724 "sort.ts": lines("export function before(a: number, b: number) {", " return a < b;", "}"),
725 });
726 const p = repo.edit({
727 message: "invert keep",
728 edits: [{ file: "filter.ts", old: " return x > 0;\n", new: " if (x === 0) return true;\n return x < 0;\n" }],
729 });
730 const q = repo.edit({
731 message: "invert before",
732 edits: [{ file: "sort.ts", old: "a < b", new: "a >= b" }],
733 reasons: [{ to: p.id, kind: "together", text: "this logic was inverted, so that logic was too" }],
734 });
735 const inside = repo.draft({
736 edits: [{ file: "filter.ts", start: 2, end: 2, lines: [" if (Number.isNaN(x)) return false;"] }],
737 });
738 assert.deepEqual(
739 repo.check(inside).warnings.map((w) => [w.atom, w.partner]),
740 [[p.id, q.id]],
741 );
742 // Right after the last line of p's change is outside it: no warning.
743 const after = repo.draft({ edits: [{ file: "filter.ts", start: 3, end: 3, lines: [" // done"], mode: "after" }] });
744 assert.deepEqual(repo.check(after).warnings, []);
745 });
747 it("when the other side only deleted lines, touching a line next to the deletion counts as touching it", () => {
748 const repo = Repo.init({
749 "check.ts": lines("export function allowed(x: number) {", " return x > 0;", "}"),
750 "call.ts": lines("if (!ready) return;", "run();", "done();"),
751 });
752 const p = repo.edit({ message: "invert the check", edits: [{ file: "check.ts", old: "x > 0", new: "x <= 0" }] });
753 const q = repo.edit({
754 message: "drop the ready guard",
755 edits: [{ file: "call.ts", old: "if (!ready) return;\n", new: "" }],
756 reasons: [{ to: p.id, kind: "together", text: "the inverted check replaces the guard" }],
757 });
758 const onlyP = repo.check(repo.draft({ edits: [{ file: "check.ts", old: "x <= 0", new: "x < 0" }] }));
759 assert.deepEqual(
760 onlyP.warnings.map((w) => [w.atom, w.partner]),
761 [[p.id, q.id]],
762 );
763 // Putting the guard back, right where it was, touches q's side.
764 const restore = repo.check(
765 repo.draft({
766 edits: [
767 { file: "check.ts", old: "x <= 0", new: "x > 0" },
768 { file: "call.ts", start: 0, end: 0, lines: ["if (!ready) return;"] },
769 ],
770 }),
771 );
772 assert.deepEqual(restore.warnings, []);
773 // An edit elsewhere in that file doesn't.
774 const elsewhere = repo.check(
775 repo.draft({
776 edits: [
777 { file: "check.ts", old: "x <= 0", new: "x > 0" },
778 { file: "call.ts", old: "done();", new: "finish();" },
779 ],
780 }),
781 );
782 assert.deepEqual(
783 elsewhere.warnings.map((w) => w.partner),
784 [q.id],
785 );
786 });
787});
789describe("8. drop", () => {
790 it("removes the atom and its dependents, and the projection reflows", () => {
791 const repo = Repo.init({
792 "list.ts": lines("const items = [", ' "a",', ' "b",', "];"),
793 "other.ts": lines("let n = 1;"),
794 });
795 const base = repo.files();
796 const addC = repo.edit({
797 message: "add c",
798 edits: [{ file: "list.ts", old: ' "b",\n', new: ' "b",\n "c",\n' }],
799 });
800 const addD = repo.edit({
801 message: "add d after c",
802 edits: [{ file: "list.ts", old: ' "c",\n', new: ' "c",\n "d",\n' }],
803 });
804 const fixC = repo.edit({ message: "C is upper case", edits: [{ file: "list.ts", old: '"c"', new: '"C"' }] });
805 const removeA = repo.edit({ message: "drop a", edits: [{ file: "list.ts", old: ' "a",\n', new: "" }] });
806 const other = repo.edit({ message: "n is 2", edits: [{ file: "other.ts", old: "1", new: "2" }] });
807 assert.equal(repo.text("list.ts"), lines("const items = [", ' "b",', ' "C",', ' "d",', "];"));
809 const dropped = drop(repo.graph, repo.shared, addC.id);
810 assert.deepEqual(dropped.removed, [addC.id, addD.id, fixC.id]);
811 assert.deepEqual([...dropped.set].sort(), ["a0", removeA.id, other.id].sort());
812 const files = project(repo.graph, dropped.set);
813 assert.equal(files["list.ts"], lines("const items = [", ' "b",', "];"));
814 assert.equal(files["other.ts"], lines("let n = 2;"));
816 // Dropping a deletion brings the line back.
817 const undone = drop(repo.graph, dropped.set, removeA.id);
818 assert.deepEqual(undone.removed, [removeA.id]);
819 assert.deepEqual(project(repo.graph, undone.set), { ...base, "other.ts": lines("let n = 2;") });
820 });
821});
823describe("9. labels", () => {
824 const repo = Repo.init({ "app.ts": lines("export const a = 1;", "export const b = 2;", "export const c = 3;") });
825 const x1 = repo.edit({ message: "a is 10", edits: [{ file: "app.ts", old: "a = 1", new: "a = 10" }] });
826 const x2 = repo.edit({
827 message: "b is a doubled",
828 edits: [{ file: "app.ts", old: "b = 2", new: "b = a * 2" }],
829 needs: [x1.id],
830 });
831 const x3 = repo.edit({ message: "c is 30", edits: [{ file: "app.ts", old: "c = 3", new: "c = 30" }] });
832 const head = repo.edit({ message: "constants feature", needs: [x2.id, x3.id] });
833 const y1 = repo.edit({ message: "a is 100", edits: [{ file: "app.ts", old: "a = 10", new: "a = 100" }] });
834 const graph: Graph = repo.graph;
835 graph.setLabel({ name: "constants", head: head.id, title: "Constants", message: "set the constants" });
836 graph.setLabel({
837 name: "bigger-a",
838 head: y1.id,
839 base: "constants",
840 title: "Bigger a",
841 message: "a grows",
842 follows: ["constants"],
843 });
845 it("contents are closure(head) minus closure(base), in history order", () => {
846 assert.deepEqual(labelContents(graph, graph.label("constants")!), [x1.id, x3.id, x2.id, head.id]);
847 assert.deepEqual(labelContents(graph, graph.label("bigger-a")!), [y1.id]);
848 assert.deepEqual(labelContents(graph, { head: x2.id, base: x1.id }), [x2.id]);
849 });
851 it("any atom inside a label re-slices into a smaller label", () => {
852 assert.deepEqual(reslice(graph, graph.label("constants")!, x2.id), [x1.id, x2.id]);
853 assert.deepEqual(reslice(graph, graph.label("constants")!, x3.id), [x3.id]);
854 assert.throws(() => reslice(graph, graph.label("constants")!, y1.id), { code: "not-in-set" });
855 const files = project(graph, graph.closure(x2.id));
856 assert.equal(files["app.ts"], lines("export const a = 10;", "export const b = a * 2;", "export const c = 3;"));
857 });
859 it("history orders by lamport, then seq, and shows deps", () => {
860 const order = history(graph, repo.shared);
861 assert.deepEqual(
862 order.map((a) => a.id),
863 ["a0", x1.id, x3.id, x2.id, y1.id, head.id],
864 );
865 const lamports = order.map((a) => graph.lamport(a.id));
866 assert.deepEqual(lamports, [0, 1, 1, 2, 2, 3]);
867 // y1 rewrites x1's line and depends on nothing else, not on x2 below it.
868 assert.deepEqual(graph.atom(y1.id).deps, [x1.id]);
869 // x2 and y1 have no path between them: same lamport, no real order.
870 assert.ok(!graph.closure(x2.id).has(y1.id) && !graph.closure(y1.id).has(x2.id));
871 assert.ok(!graph.closure(head.id).has(y1.id) && !graph.closure(y1.id).has(head.id));
872 });
874 it("labels survive a JSON round trip", () => {
875 const again = Graph.fromJSON(JSON.parse(JSON.stringify(graph)));
876 assert.deepEqual(again.labels(), graph.labels());
877 assert.deepEqual(labelContents(again, again.label("bigger-a")!), [y1.id]);
878 });
879});