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