{"draft":null,"from":116,"to":318,"files":[{"path":"src/engine/test/relocation-scratch.test.ts","added":135,"removed":0,"diff":"--- /dev/null\n+++ b/src/engine/test/relocation-scratch.test.ts\n@@ -0,0 +1,135 @@\n+import assert from \"node:assert/strict\";\n+import { test } from \"node:test\";\n+import { Graph } from \"../graph.ts\";\n+import { withProjectionScratch } from \"../projection-scratch.ts\";\n+import { resolveScratchMoves } from \"../relocation-scratch.ts\";\n+import { CachedStateStorage, StableOverlaySet } from \"../state-source.ts\";\n+import { View } from \"../view.ts\";\n+import { Repo, Rng } from \"./helpers.ts\";\n+import { SqlState } from \"./storage-helper.ts\";\n+\n+test(\"scratch relocation cycle selection matches View across differing memberships and tied moves\", () => {\n+\tconst rng = new Rng(45131);\n+\tconst source = new SqlState();\n+\tconst storage = new CachedStateStorage(source, 4096, 16);\n+\ttry {\n+\t\tfor (let trial = 0; trial < 24; trial++) {\n+\t\t\tconst repo = Repo.init({ \"f.txt\": Array.from({ length: 24 }, (_, n) => `line ${n}\\n`).join(\"\"), \"g.txt\": \"\" });\n+\t\t\tconst earlier = new Set(repo.shared);\n+\t\t\tfor (let j = 0; j < 32; j++) {\n+\t\t\t\tconst offset = rng.int(24);\n+\t\t\t\tconst moves = Array.from({ length: 3 }, (_, index) => {\n+\t\t\t\t\tconst line = `a0.${(offset + index) % 24}`;\n+\t\t\t\t\tconst candidate = `a0.${rng.int(24)}`;\n+\t\t\t\t\treturn { line, parent: candidate === line ? (j % 2 ? \"^f.txt\" : \"^g.txt\") : candidate };\n+\t\t\t\t});\n+\t\t\t\tconst id = repo.graph.nextId();\n+\t\t\t\trepo.graph.add({\n+\t\t\t\t\tid,\n+\t\t\t\t\tseq: repo.graph.size,\n+\t\t\t\t\tauthor: \"fixture\",\n+\t\t\t\t\tmessage: \"move\",\n+\t\t\t\t\ttime: j,\n+\t\t\t\t\tinserts: [],\n+\t\t\t\t\tdeletes: [],\n+\t\t\t\t\twrites: moves.flatMap((move) => [`P:${move.line}`, `G:${move.parent}`]),\n+\t\t\t\t\treads: [],\n+\t\t\t\t\tholds: [],\n+\t\t\t\t\tneeds: [],\n+\t\t\t\t\tresolves: [],\n+\t\t\t\t\treasons: [],\n+\t\t\t\t\tdeps: [\"a0\"],\n+\t\t\t\t\tmoves,\n+\t\t\t\t});\n+\t\t\t\trepo.shared.add(id);\n+\t\t\t\tif (j < 16) earlier.add(id);\n+\t\t\t}\n+\t\t\tfor (const membership of [repo.shared, earlier, repo.shared]) {\n+\t\t\t\tconst oracle = new View(repo.graph, membership);\n+\t\t\t\tconst parents = withProjectionScratch(\n+\t\t\t\t\tstorage,\n+\t\t\t\t\t() => {},\n+\t\t\t\t\t(scratch) => {\n+\t\t\t\t\t\tconst resolved = resolveScratchMoves(repo.graph, (id) => membership.has(id), scratch);\n+\t\t\t\t\t\treturn Array.from({ length: 24 }, (_, n) => resolved.parent(`a0.${n}`));\n+\t\t\t\t\t},\n+\t\t\t\t);\n+\t\t\t\tassert.deepEqual(\n+\t\t\t\t\tparents,\n+\t\t\t\t\tArray.from({ length: 24 }, (_, n) => oracle.parentOf(`a0.${n}`)),\n+\t\t\t\t);\n+\t\t\t\tassert.equal(source.db.prepare(\"SELECT count(*) AS n FROM state\").get()!.n, 0);\n+\t\t\t\tassert.equal(storage.retainedEntries, 0);\n+\t\t\t}\n+\t\t\tif (trial < 4) {\n+\t\t\t\tconst persisted = new SqlState();\n+\t\t\t\ttry {\n+\t\t\t\t\tconst indexed = new Graph({ storage: new CachedStateStorage(persisted, 8192, 32) });\n+\t\t\t\t\tfor (const id of repo.shared) indexed.add(repo.graph.atom(id));\n+\t\t\t\t\tconst before = persisted.db.prepare(\"SELECT * FROM state ORDER BY collection, key\").all();\n+\t\t\t\t\t// Restart before projecting, then alternate membership on the same graph.\n+\t\t\t\t\tconst reopened = new Graph({ storage: new CachedStateStorage(persisted, 8192, 32) });\n+\t\t\t\t\tfor (const membership of [repo.shared, earlier, repo.shared]) {\n+\t\t\t\t\t\tconst expected = new View(repo.graph, membership);\n+\t\t\t\t\t\tconst actual = new View(reopened, new StableOverlaySet(membership));\n+\t\t\t\t\t\tfor (const path of [\"f.txt\", \"g.txt\"]) {\n+\t\t\t\t\t\t\tassert.equal(actual.text(path), expected.text(path));\n+\t\t\t\t\t\t\tassert.deepEqual(actual.sequence(path), expected.sequence(path));\n+\t\t\t\t\t\t}\n+\t\t\t\t\t\tfor (const n of [0, 11, 23]) {\n+\t\t\t\t\t\t\tassert.equal(actual.parentOf(`a0.${n}`), expected.parentOf(`a0.${n}`));\n+\t\t\t\t\t\t\tassert.equal(actual.fileOf(`a0.${n}`), expected.fileOf(`a0.${n}`));\n+\t\t\t\t\t\t}\n+\t\t\t\t\t\tassert.deepEqual(persisted.db.prepare(\"SELECT * FROM state ORDER BY collection, key\").all(), before);\n+\t\t\t\t\t}\n+\t\t\t\t} finally {\n+\t\t\t\t\tpersisted.db.close();\n+\t\t\t\t}\n+\t\t\t}\n+\t\t}\n+\t} finally {\n+\t\tsource.db.close();\n+\t}\n+});\n+\n+test(\"scratch relocation write failure rolls back without changing canonical graph state\", () => {\n+\tconst repo = Repo.init({ f: \"one\\ntwo\\n\" });\n+\tconst source = new SqlState();\n+\ttry {\n+\t\trepo.graph.add({\n+\t\t\tid: \"a1\",\n+\t\t\tseq: 1,\n+\t\t\tauthor: \"fixture\",\n+\t\t\tmessage: \"move\",\n+\t\t\ttime: 1,\n+\t\t\tinserts: [],\n+\t\t\tdeletes: [],\n+\t\t\twrites: [\"P:a0.0\", \"G:a0.1\"],\n+\t\t\treads: [],\n+\t\t\tholds: [],\n+\t\t\tneeds: [],\n+\t\t\tresolves: [],\n+\t\t\treasons: [],\n+\t\t\tdeps: [\"a0\"],\n+\t\t\tmoves: [{ line: \"a0.0\", parent: \"a0.1\" }],\n+\t\t});\n+\t\trepo.shared.add(\"a1\");\n+\t\tconst before = { size: repo.graph.size, lines: repo.graph.lineCount };\n+\t\tsource.failWrite = 3;\n+\t\tassert.throws(\n+\t\t\t() =>\n+\t\t\t\twithProjectionScratch(\n+\t\t\t\t\tsource,\n+\t\t\t\t\t() => {},\n+\t\t\t\t\t(scratch) => {\n+\t\t\t\t\t\tresolveScratchMoves(repo.graph, (id) => repo.shared.has(id), scratch);\n+\t\t\t\t\t},\n+\t\t\t\t),\n+\t\t\t/injected storage failure/,\n+\t\t);\n+\t\tassert.deepEqual({ size: repo.graph.size, lines: repo.graph.lineCount }, before);\n+\t\tassert.equal(source.db.prepare(\"SELECT count(*) AS n FROM state\").get()!.n, 0);\n+\t} finally {\n+\t\tsource.db.close();\n+\t}\n+});"}],"unmatched":[]}