{"draft":null,"from":32,"to":318,"files":[{"path":"src/engine/test/projection-marker.test.ts","added":189,"removed":0,"diff":"--- /dev/null\n+++ b/src/engine/test/projection-marker.test.ts\n@@ -0,0 +1,189 @@\n+import assert from \"node:assert/strict\";\n+import { test } from \"node:test\";\n+import { ExperimentalMarkerProjection as MarkerProjection } from \"../experimental/projection-marker.ts\";\n+import { Graph } from \"../graph.ts\";\n+import { CachedStateStorage } from \"../state-source.ts\";\n+import { Repo } from \"./helpers.ts\";\n+import { SqlState } from \"./storage-helper.ts\";\n+\n+test(\"projection apply and removal compare atom contents without whole-atom JSON\", (t) => {\n+\tconst storage = new SqlState();\n+\tt.after(() => storage.db.close());\n+\tconst repo = Repo.init({ \"f.txt\": `${\"x\".repeat(40000)}\\n` });\n+\tconst atom = repo.graph.atom(\"a0\");\n+\tconst projection = new MarkerProjection(storage, repo.graph, \"f.txt\", \"streamed-comparison\");\n+\tconst stringify = JSON.stringify;\n+\tt.mock.method(JSON, \"stringify\", (...args: Parameters<typeof JSON.stringify>) => {\n+\t\tassert.notEqual(args[0], atom, \"projection validation must not serialize the full atom\");\n+\t\treturn stringify(...args);\n+\t});\n+\tconst state = projection.apply(projection.empty(), atom);\n+\tconst before = storage.writes;\n+\tconst altered = { ...atom, message: \"forged\" };\n+\tassert.throws(() => projection.apply(state, altered), /canonical atom mismatch/);\n+\tassert.throws(() => projection.removeAtom(state, altered), /canonical atom mismatch/);\n+\tassert.equal(storage.writes, before);\n+\tconst removed = projection.removeAtom(state, atom);\n+\tassert.deepEqual(projection.lines(removed, \"f.txt\"), []);\n+\tassert.equal(projection.lines(state, \"f.txt\").length, 1);\n+});\n+\n+test(\"typed marker projection matches mixed multi-file edits and reverse removals\", () => {\n+\tconst db = new SqlState();\n+\ttry {\n+\t\tconst repo = Repo.init({ \"f.txt\": \"a\\nb\\nc\\n\", \"g.txt\": \"x\\ny\\n\" }),\n+\t\t\tgraph = new Graph({ storage: new CachedStateStorage(db) });\n+\t\tgraph.add(repo.graph.atom(\"a0\"));\n+\t\tconst p = new MarkerProjection(db, graph, \"f.txt\", \"typed-mixed\");\n+\t\tlet state = p.apply(p.empty(), repo.graph.atom(\"a0\")),\n+\t\t\tchecks = 0;\n+\t\tconst atoms = [],\n+\t\t\troots = [];\n+\t\tconst check = () => {\n+\t\t\tconst view = repo.view();\n+\t\t\tfor (const path of [\"f.txt\", \"g.txt\"]) {\n+\t\t\t\tassert.deepEqual(\n+\t\t\t\t\tp.lines(state, path),\n+\t\t\t\t\tview.lines(path).map((x) => x.id),\n+\t\t\t\t);\n+\t\t\t\tassert.deepEqual(p.attrs(state, path), view.attrs(path));\n+\t\t\t}\n+\t\t\tchecks++;\n+\t\t};\n+\t\tfor (let i = 0; i < 36; i++) {\n+\t\t\tconst file = i % 2 ? \"f.txt\" : \"g.txt\",\n+\t\t\t\tother = i % 2 ? \"g.txt\" : \"f.txt\",\n+\t\t\t\tview = repo.view();\n+\t\t\tlet input: Parameters<Repo[\"edit\"]>[0];\n+\t\t\tif (i % 3 === 0 && view.lines(file).length) input = { move: { file, start: 0, end: 1, to: other, at: 0 } };\n+\t\t\telse if (i % 3 === 1)\n+\t\t\t\tinput = {\n+\t\t\t\t\tedits: [{ file, old: view.text(file), new: `new ${i}\\n${view.text(file)}` }],\n+\t\t\t\t\tattrs: [{ file, name: \"exec\", value: i % 2 ? \"1\" : null }],\n+\t\t\t\t};\n+\t\t\telse input = { edits: [{ file, old: view.text(file), new: `rewrite ${i}\\n` }] };\n+\t\t\tconst atom = repo.edit(input);\n+\t\t\tgraph.add(atom);\n+\t\t\tatoms.push(atom);\n+\t\t\tstate = p.apply(state, atom);\n+\t\t\tcheck();\n+\t\t\tif (i % 9 === 0) roots.push({ state, f: p.lines(state, \"f.txt\"), g: p.lines(state, \"g.txt\") });\n+\t\t}\n+\t\tfor (const atom of atoms.toReversed()) {\n+\t\t\trepo.shared.delete(atom.id);\n+\t\t\tstate = p.removeAtom(state, atom);\n+\t\t\tcheck();\n+\t\t}\n+\t\tfor (const root of roots) {\n+\t\t\tassert.deepEqual(p.lines(root.state, \"f.txt\"), root.f);\n+\t\t\tassert.deepEqual(p.lines(root.state, \"g.txt\"), root.g);\n+\t\t}\n+\t\tassert.equal(checks, 72);\n+\t} finally {\n+\t\tdb.db.close();\n+\t}\n+});\n+\n+test(\"typed marker namespaces isolate file identities and temporary metadata\", () => {\n+\tconst storage = new SqlState();\n+\ttry {\n+\t\tconst repo = Repo.init({ \"f.txt\": \"f\\n\", \"g.txt\": \"g\\n\" });\n+\t\tconst graph = new Graph({ storage: new CachedStateStorage(storage) });\n+\t\tgraph.add(repo.graph.atom(\"a0\"));\n+\t\tconst alpha = new MarkerProjection(storage, graph, \"f.txt\", \"alpha\");\n+\t\tconst beta = new MarkerProjection(storage, graph, \"g.txt\", \"beta\");\n+\t\tconst a = alpha.apply(alpha.empty(), repo.graph.atom(\"a0\"));\n+\t\tconst b = beta.apply(beta.empty(), repo.graph.atom(\"a0\"));\n+\t\tconst wrongFile = new MarkerProjection(storage, graph, \"g.txt\", \"alpha\");\n+\t\tfor (const other of [beta, wrongFile]) {\n+\t\t\tconst reads = storage.reads,\n+\t\t\t\twrites = storage.writes;\n+\t\t\tassert.throws(() => other.lines(a, \"f.txt\"), /constructor context mismatch/);\n+\t\t\tassert.throws(() => other.apply(a, repo.graph.atom(\"a0\")), /constructor context mismatch/);\n+\t\t\tassert.equal(storage.reads, reads);\n+\t\t\tassert.equal(storage.writes, writes);\n+\t\t}\n+\n+\t\tfor (const path of [\"f.txt\", \"g.txt\"]) {\n+\t\t\tassert.deepEqual(\n+\t\t\t\talpha.lines(a, path),\n+\t\t\t\trepo\n+\t\t\t\t\t.view()\n+\t\t\t\t\t.lines(path)\n+\t\t\t\t\t.map((x) => x.id),\n+\t\t\t);\n+\t\t\tassert.deepEqual(\n+\t\t\t\tbeta.lines(b, path),\n+\t\t\t\trepo\n+\t\t\t\t\t.view()\n+\t\t\t\t\t.lines(path)\n+\t\t\t\t\t.map((x) => x.id),\n+\t\t\t);\n+\t\t}\n+\t\talpha.storage.write(\"sequence-meta\", \"work-scope\", 91);\n+\t\tassert.equal(beta.storage.read(\"sequence-meta\", \"work-scope\"), undefined);\n+\t\tconst rows = () => JSON.stringify(storage.db.prepare(\"SELECT * FROM state ORDER BY collection,key\").all());\n+\t\tconst before = rows();\n+\t\tassert.throws(\n+\t\t\t() => alpha.relocateBatch(a, [{ n: -1, parentKey: 0, key: \"0\".repeat(57) }]),\n+\t\t\t/invalid relocation plan/,\n+\t\t);\n+\t\tassert.equal(rows(), before);\n+\t\tassert.deepEqual(\n+\t\t\tbeta.lines(b, \"f.txt\"),\n+\t\t\trepo\n+\t\t\t\t.view()\n+\t\t\t\t.lines(\"f.txt\")\n+\t\t\t\t.map((x) => x.id),\n+\t\t);\n+\t} finally {\n+\t\tstorage.db.close();\n+\t}\n+});\n+\n+test(\"visibility batches roll back earlier batches and retry with exact line parity\", (t) => {\n+\tconst db = new SqlState();\n+\tt.after(() => db.db.close());\n+\tconst text = Array.from({ length: 35 }, (_, n) => `line ${n}\\n`).join(\"\");\n+\tconst repo = Repo.init({ \"f.txt\": text });\n+\tconst graph = new Graph({ storage: new CachedStateStorage(db) });\n+\tgraph.add(repo.graph.atom(\"a0\"));\n+\tconst p = new MarkerProjection(db, graph, \"f.txt\", \"batch-failure\");\n+\tconst initial = p.apply(p.empty(), repo.graph.atom(\"a0\"));\n+\tconst originalLines = p.lines(initial, \"f.txt\");\n+\tconst atom = repo.edit({ edits: [{ file: \"f.txt\", old: text, new: \"\" }] });\n+\tgraph.add(atom);\n+\tconst rows = () => db.db.prepare(\"SELECT * FROM state ORDER BY collection,key\").all();\n+\tconst before = rows();\n+\tconst original = p.sequence.updateMany.bind(p.sequence);\n+\tconst sizes: number[] = [];\n+\tlet fail = true;\n+\tt.mock.method(\n+\t\tp.sequence,\n+\t\t\"updateMany\",\n+\t\t(state: Parameters<typeof original>[0], operations: Parameters<typeof original>[1]) => {\n+\t\t\tconst next = original(state, operations);\n+\t\t\tif (operations.every((op) => op.kind === \"visibility\")) {\n+\t\t\t\tsizes.push(operations.length);\n+\t\t\t\tif (fail && sizes.length === 2) throw Error(\"second visibility batch failure\");\n+\t\t\t}\n+\t\t\treturn next;\n+\t\t},\n+\t);\n+\tassert.throws(() => p.apply(initial, atom), /second visibility batch/);\n+\tassert.deepEqual(sizes, [16, 16]);\n+\tassert.deepEqual(rows(), before);\n+\tassert.deepEqual(p.lines(initial, \"f.txt\"), originalLines);\n+\tfail = false;\n+\tsizes.length = 0;\n+\tconst next = p.apply(initial, atom);\n+\tassert.deepEqual(sizes, [16, 16, 3]);\n+\tassert.deepEqual(\n+\t\tp.lines(next, \"f.txt\"),\n+\t\trepo\n+\t\t\t.view()\n+\t\t\t.lines(\"f.txt\")\n+\t\t\t.map((line) => line.id),\n+\t);\n+\tassert.deepEqual(p.lines(initial, \"f.txt\"), originalLines);\n+});"}],"unmatched":[]}