- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/src/engine/test/conflicts.test.ts?format=json
189 linesFirst import 100%Last change First import in version 0, 1 version changed it
1// Conflicts, resolution, draft reports.
3import assert from "node:assert/strict";
4import { describe, it } from "node:test";
5import {
6 checkDraft,
7 claimsOf,
8 conflicts,
9 gapKey,
10 independent,
11 keyOverlap,
12 lineKey,
13 touchesOf,
14 unresolvedConflicts,
15} from "../index.ts";
16import { lines, Repo } from "./helpers.ts";
18const FILE = lines("one", "two", "three", "four");
20describe("keyOverlap", () => {
21 it("counts writes against claims only; read-read and hold-hold never count", () => {
22 const repo = Repo.init({ "f.txt": FILE });
23 const readA = repo.edit({ edits: [{ file: "f.txt", start: 1, end: 1, lines: ["x"], mode: "after" }] }, ["a0"]);
24 const readB = repo.edit(
25 { edits: [{ file: "f.txt", start: 4, end: 4, lines: ["y"] }], reliesOn: [{ file: "f.txt", start: 0, end: 1 }] },
26 ["a0"],
27 );
28 const holdC = repo.edit({ spans: [{ file: "f.txt", start: 0, end: 2 }] }, ["a0"]);
29 assert.deepEqual(keyOverlap(repo.graph.atom(readA.id), repo.graph.atom(readB.id)), []); // both read/hold "one"
30 assert.deepEqual(keyOverlap(readB, holdC), []); // both hold "one"
31 // readA writes G:one, which holdC holds (the gap between one and two).
32 assert.deepEqual(keyOverlap(readA, holdC), [
33 { key: gapKey("a0.0"), writer: readA.id, other: holdC.id, relation: "hold" },
34 ]);
35 assert.deepEqual(claimsOf(readA), [gapKey("a0.0"), lineKey("a0.0")]);
36 });
37});
39describe("conflicts in a set", () => {
40 it("edits only: an atom that depends on the other never conflicts with it", () => {
41 const repo = Repo.init({ "f.txt": FILE });
42 const a = repo.edit({ edits: [{ file: "f.txt", old: "two", new: "TWO" }] });
43 const b = repo.edit({ edits: [{ file: "f.txt", old: "TWO", new: "2" }] });
44 assert.equal(independent(repo.graph, a.id, b.id), false);
45 assert.deepEqual(conflicts(repo.graph, repo.shared), []);
46 });
48 it("a hold is released only by naming the holder, not by a derived dependency", () => {
49 const repo = Repo.init({ "f.txt": FILE });
50 // A wide insert between two and three holds both lines and reads three.
51 const wide = repo.edit({
52 author: "ann",
53 message: "this stack is mine",
54 edits: [{ file: "f.txt", old: "two\nthree\n", new: "two\nmid\nthree\n", mode: "wide" }],
55 });
56 assert.ok(wide.holds.includes(lineKey("a0.2")));
57 // Deleting three depends on the wide atom (it read three), but writes into its hold.
58 const del = { edits: [{ file: "f.txt", old: "three\n", new: "" }] };
59 const draft = repo.draft(del);
60 assert.ok(draft.deps.includes(wide.id));
61 assert.deepEqual(
62 repo.check(draft).conflicts.map((c) => [c.atoms, c.kind, c.keys]),
63 [
64 [
65 [wide.id, draft.id],
66 "semantic",
67 [{ key: lineKey("a0.2"), writer: draft.id, other: wide.id, relation: "hold" }],
68 ],
69 ],
70 );
71 // Naming the holder releases it.
72 assert.deepEqual(repo.check(repo.draft({ ...del, needs: [wide.id] })).conflicts, []);
73 // Accepted without naming it (--allow-conflict), the set reports the same conflict.
74 const accepted = repo.edit(del);
75 assert.deepEqual(
76 conflicts(repo.graph, repo.shared).map((c) => [c.atoms, c.kind]),
77 [[[wide.id, accepted.id], "semantic"]],
78 );
79 // A resolution settles it.
80 repo.edit({ message: "fine by ann", resolves: [wide.id, accepted.id] });
81 assert.deepEqual(unresolvedConflicts(repo.graph, repo.shared), []);
82 });
84 it("checkDraft and conflicts name the same writer for a key both sides write", () => {
85 const repo = Repo.init({ "f.txt": FILE });
86 const a = repo.edit({ edits: [{ file: "f.txt", old: "two", new: "TWO" }] }, ["a0"]);
87 const draft = repo.draft({ edits: [{ file: "f.txt", old: "two", new: "2" }] }, ["a0"]);
88 const reported = repo.check(draft).conflicts;
89 repo.edit({ edits: [{ file: "f.txt", old: "two", new: "2" }] }, ["a0"]);
90 const stored = conflicts(repo.graph, repo.shared);
91 assert.deepEqual(
92 reported.map((c) => c.keys),
93 stored.map((c) => c.keys),
94 );
95 assert.deepEqual(stored[0]!.keys, [{ key: lineKey("a0.1"), writer: a.id, other: draft.id, relation: "write" }]);
96 });
98 it("is resolved only by an atom that names both sides", () => {
99 const repo = Repo.init({ "f.txt": FILE });
100 const a = repo.edit({ message: "two is TWO", edits: [{ file: "f.txt", old: "two", new: "TWO" }] }, ["a0"]);
101 const b = repo.edit({ message: "two is 2", edits: [{ file: "f.txt", old: "two", new: "2" }] }, ["a0"]);
102 const c = repo.edit({ message: "three is 3", edits: [{ file: "f.txt", old: "three\n", new: "3\n" }] }, ["a0"]);
103 const d = repo.edit({ message: "three is III", edits: [{ file: "f.txt", old: "three", new: "III" }] }, ["a0"]);
104 assert.deepEqual(
105 conflicts(repo.graph, repo.shared).map((x) => x.atoms),
106 [
107 [a.id, b.id],
108 [c.id, d.id],
109 ],
110 );
112 const half = repo.edit({ message: "keep TWO", resolves: [a.id] });
113 assert.equal(unresolvedConflicts(repo.graph, repo.shared).length, 2);
114 const both = repo.edit({ message: "TWO wins over 2", resolves: [a.id, b.id] });
115 const after = conflicts(repo.graph, repo.shared);
116 assert.deepEqual(
117 after.map((x) => [x.atoms, x.resolvedBy]),
118 [
119 [[a.id, b.id], [both.id]],
120 [[c.id, d.id], []],
121 ],
122 );
123 assert.deepEqual(
124 unresolvedConflicts(repo.graph, repo.shared).map((x) => x.atoms),
125 [[c.id, d.id]],
126 );
127 // A resolution outside the set doesn't count.
128 const without = new Set([...repo.shared].filter((id) => id !== both.id));
129 assert.equal(unresolvedConflicts(repo.graph, without).length, 2);
130 void half;
131 });
133 it("a conflicted set still projects, priority breaking the tie", () => {
134 const repo = Repo.init({ "f.txt": FILE });
135 repo.edit({ edits: [{ file: "f.txt", old: "two", new: "TWO" }] }, ["a0"]);
136 repo.edit({ edits: [{ file: "f.txt", old: "two", new: "2" }] }, ["a0"]);
137 // Both replaced "two": both runs hang off it, the higher seq first.
138 assert.equal(repo.text("f.txt"), lines("one", "2", "TWO", "three", "four"));
139 });
141 it("kind: edit-edit when any overlap is textual, semantic when every overlap is against a hold", () => {
142 const repo = Repo.init({ "f.txt": FILE });
143 const holder = repo.edit(
144 { edits: [{ file: "f.txt", old: "one", new: "ONE" }], reliesOn: [{ file: "f.txt", start: 2, end: 3 }] },
145 ["a0"],
146 );
147 const writer = repo.edit({ edits: [{ file: "f.txt", old: "three", new: "THREE" }] }, ["a0"]);
148 const both = repo.edit({ edits: [{ file: "f.txt", old: "one\ntwo\nthree", new: "1\n2\n3" }] }, ["a0"]);
149 const found = conflicts(repo.graph, repo.shared);
150 const kindOf = (x: string, y: string) => found.find((c) => c.atoms[0] === x && c.atoms[1] === y)?.kind;
151 assert.equal(kindOf(holder.id, writer.id), "semantic");
152 assert.equal(kindOf(holder.id, both.id), "edit-edit");
153 assert.equal(kindOf(writer.id, both.id), "edit-edit");
154 });
155});
157describe("checkDraft", () => {
158 it("reports whose lines a draft writes, reads and holds, with author and message", () => {
159 const repo = Repo.init({ "f.txt": FILE });
160 const ann = repo.edit({ author: "ann", message: "two is TWO", edits: [{ file: "f.txt", old: "two", new: "TWO" }] });
161 const draft = repo.draft({
162 edits: [{ file: "f.txt", old: "TWO\nthree\n", new: "TWO\nnew\nthree\n" }],
163 reliesOn: [{ file: "f.txt", start: 3, end: 4 }],
164 });
165 assert.deepEqual(touchesOf(repo.graph, draft), [
166 { atom: "a0", author: "import", message: "starting snapshot", writes: [], reads: ["a0.2"], holds: ["a0.3"] },
167 { atom: ann.id, author: "ann", message: "two is TWO", writes: [], reads: [`${ann.id}.0`], holds: [] },
168 ]);
169 const report = checkDraft(repo.graph, repo.shared, draft);
170 assert.deepEqual(report.conflicts, []);
171 assert.deepEqual(report.warnings, []);
172 assert.equal(report.touches.length, 2);
173 });
175 it("reports conflicts with independent atoms only, never resolved", () => {
176 const repo = Repo.init({ "f.txt": FILE });
177 const claim = repo.edit({ author: "ann", message: "mine", spans: [{ file: "f.txt", start: 1, end: 3 }] });
178 const draft = repo.draft({ edits: [{ file: "f.txt", old: "three", new: "THREE" }] });
179 const report = checkDraft(repo.graph, repo.shared, draft);
180 assert.equal(report.conflicts.length, 1);
181 assert.deepEqual(report.conflicts[0]!.atoms, [claim.id, draft.id]);
182 assert.deepEqual(report.conflicts[0]!.resolvedBy, []);
183 assert.deepEqual(report.conflicts[0]!.keys, [
184 { key: lineKey("a0.2"), writer: draft.id, other: claim.id, relation: "hold" },
185 ]);
186 // Checked against a set without the claim, nothing collides.
187 assert.deepEqual(checkDraft(repo.graph, new Set(["a0"]), draft).conflicts, []);
188 });
189});