{"draft":null,"from":39,"to":318,"files":[{"path":"src/engine/test/projection-move-resolution.test.ts","added":136,"removed":0,"diff":"--- /dev/null\n+++ b/src/engine/test/projection-move-resolution.test.ts\n@@ -0,0 +1,136 @@\n+import assert from \"node:assert/strict\";\n+import { createHash } from \"node:crypto\";\n+import { test } from \"node:test\";\n+import { resolveMovesPage } from \"../experimental/projection-move-resolution.ts\";\n+import type { RelocationInfo } from \"../graph.ts\";\n+import { resolveScratchMoves } from \"../relocation-scratch.ts\";\n+import { Repo, Rng } from \"./helpers.ts\";\n+import { SqlState } from \"./storage-helper.ts\";\n+\n+test(\"one-step move pages preserve cycle resolution across competing and excluded moves\", () => {\n+\tconst rng = new Rng(45131);\n+\tlet resolvedCycles = 0;\n+\tfor (let trial = 0; trial < 8; trial++) {\n+\t\tconst repo = Repo.init({ \"f.txt\": Array.from({ length: 12 }, (_, i) => `${i}\\n`).join(\"\"), \"g.txt\": \"\" });\n+\t\tfor (let j = 0; j < 12; j++) {\n+\t\t\tconst offset = rng.int(12);\n+\t\t\tconst moves = Array.from({ length: 3 }, (_, k) => {\n+\t\t\t\tconst line = `a0.${(offset + k) % 12}`,\n+\t\t\t\t\tparent = `a0.${rng.int(12)}`;\n+\t\t\t\treturn { line, parent: parent === line ? \"^g.txt\" : parent };\n+\t\t\t});\n+\t\t\tconst id = repo.graph.nextId();\n+\t\t\trepo.graph.add({\n+\t\t\t\tid,\n+\t\t\t\tseq: repo.graph.size,\n+\t\t\t\tauthor: \"fixture\",\n+\t\t\t\tmessage: \"move\",\n+\t\t\t\ttime: j,\n+\t\t\t\tinserts: [],\n+\t\t\t\tdeletes: [],\n+\t\t\t\twrites: moves.flatMap((m) => [`P:${m.line}`, `G:${m.parent}`]),\n+\t\t\t\treads: [],\n+\t\t\t\tholds: [],\n+\t\t\t\tneeds: [],\n+\t\t\t\tresolves: [],\n+\t\t\t\treasons: [],\n+\t\t\t\tdeps: [\"a0\"],\n+\t\t\t\tmoves,\n+\t\t\t});\n+\t\t\tif ((j + trial) % 3) repo.shared.add(id);\n+\t\t}\n+\t\tconst expected = new SqlState(),\n+\t\t\tactual = new SqlState();\n+\t\ttry {\n+\t\t\tconst oracle = resolveScratchMoves(repo.graph, (id) => repo.shared.has(id), expected);\n+\t\t\tlet done = false,\n+\t\t\t\tpages = 0;\n+\t\t\twhile (!done) {\n+\t\t\t\tconst before = actual.db.prepare(\"SELECT * FROM state ORDER BY collection,key\").all();\n+\t\t\t\tactual.failWrite = actual.writes + 1;\n+\t\t\t\tassert.throws(\n+\t\t\t\t\t() => resolveMovesPage(repo.graph, (id) => repo.shared.has(id), actual, `trial-${trial}`, 1),\n+\t\t\t\t\t/injected storage failure/,\n+\t\t\t\t);\n+\t\t\t\tassert.deepEqual(actual.db.prepare(\"SELECT * FROM state ORDER BY collection,key\").all(), before);\n+\t\t\t\tactual.failWrite = Infinity;\n+\t\t\t\tconst result = resolveMovesPage(repo.graph, (id) => repo.shared.has(id), actual, `trial-${trial}`, 1);\n+\t\t\t\tassert.ok(result.steps <= 1);\n+\t\t\t\tdone = result.done;\n+\t\t\t\tassert.ok(++pages < 10000);\n+\t\t\t}\n+\t\t\tfor (let n = 0; n < 12; n++) {\n+\t\t\t\tconst line = `a0.${n}`,\n+\t\t\t\t\tselected = actual.read(\"selected\", line) as RelocationInfo | undefined;\n+\t\t\t\tassert.deepEqual(selected, oracle.selected(line));\n+\t\t\t\tassert.equal(selected?.parent ?? repo.graph.lineParent(line), oracle.parent(line));\n+\t\t\t}\n+\t\t\tresolvedCycles += [...actual.entries(\"ignored\")].length;\n+\t\t\tconst rows = actual.db.prepare(\"SELECT * FROM state ORDER BY collection,key\").all();\n+\t\t\tassert.throws(() => resolveMovesPage(repo.graph, () => true, actual, \"changed\", 1), /identity changed/);\n+\t\t\tassert.deepEqual(actual.db.prepare(\"SELECT * FROM state ORDER BY collection,key\").all(), rows);\n+\t\t} finally {\n+\t\t\texpected.db.close();\n+\t\t\tactual.db.close();\n+\t\t}\n+\t}\n+\tassert.ok(resolvedCycles > 0, \"the fixture must exercise cycle breaking\");\n+});\n+\n+test(\"damaged and structurally invalid move cursors refuse without changing stored state\", () => {\n+\tconst repo = Repo.init({ \"f.txt\": \"a\\nb\\n\" });\n+\tconst storage = new SqlState();\n+\ttry {\n+\t\tresolveMovesPage(repo.graph, () => true, storage, \"source\", 1);\n+\t\tconst valid = storage.read(\"progress\", \"cursor\") as {\n+\t\t\tformat: number;\n+\t\t\tchecksum: string;\n+\t\t\tstate: Record<string, unknown>;\n+\t\t};\n+\t\tconst changes: Array<(record: typeof valid) => void> = [\n+\t\t\t(record) => {\n+\t\t\t\trecord.checksum = \"damaged\";\n+\t\t\t},\n+\t\t\t(record) => {\n+\t\t\t\trecord.format = 2;\n+\t\t\t},\n+\t\t\t(record) => {\n+\t\t\t\trecord.state.phase = \"unknown\";\n+\t\t\t},\n+\t\t\t(record) => {\n+\t\t\t\trecord.state.mover = 1;\n+\t\t\t},\n+\t\t\t(record) => {\n+\t\t\t\trecord.state.move = 1;\n+\t\t\t},\n+\t\t\t(record) => {\n+\t\t\t\trecord.state.item = 1;\n+\t\t\t},\n+\t\t\t(record) => {\n+\t\t\t\trecord.state.work = repo.graph.lineCount + 1;\n+\t\t\t},\n+\t\t\t(record) => {\n+\t\t\t\trecord.state.slow = 4;\n+\t\t\t},\n+\t\t\t(record) => {\n+\t\t\t\trecord.state.phase = \"repick\";\n+\t\t\t},\n+\t\t\t(record) => {\n+\t\t\t\trecord.state.extra = true;\n+\t\t\t},\n+\t\t];\n+\t\tfor (const [index, change] of changes.entries()) {\n+\t\t\tconst damaged = structuredClone(valid);\n+\t\t\tchange(damaged);\n+\t\t\tif (index > 0) damaged.checksum = createHash(\"sha256\").update(JSON.stringify(damaged.state)).digest(\"hex\");\n+\t\t\tstorage.write(\"progress\", \"cursor\", damaged);\n+\t\t\tconst before = storage.db.prepare(\"SELECT * FROM state ORDER BY collection,key\").all();\n+\t\t\tassert.throws(() => resolveMovesPage(repo.graph, () => true, storage, \"source\", 1), /invalid|damaged|incomplete/);\n+\t\t\tassert.deepEqual(storage.db.prepare(\"SELECT * FROM state ORDER BY collection,key\").all(), before);\n+\t\t}\n+\t\tstorage.write(\"progress\", \"cursor\", valid);\n+\t\tassert.equal(resolveMovesPage(repo.graph, () => true, storage, \"source\", 1).done, true);\n+\t} finally {\n+\t\tstorage.db.close();\n+\t}\n+});"}],"unmatched":[]}