{"draft":null,"from":108,"to":109,"files":[{"path":"src/engine/test/state-source.test.ts","added":96,"removed":0,"diff":"--- /dev/null\n+++ b/src/engine/test/state-source.test.ts\n@@ -0,0 +1,96 @@\n+import assert from \"node:assert/strict\";\n+import { test } from \"node:test\";\n+import { checkDraft, Graph, View } from \"../index.ts\";\n+import { lines, Repo } from \"./helpers.ts\";\n+import { SqlState } from \"./storage-helper.ts\";\n+\n+test(\"stored graph reopens without replay and preserves moves, conflicts and new edits\", () => {\n+\tconst oracle = Repo.init({ \"a.txt\": lines(\"one\", \"two\", \"three\"), \"b.txt\": \"\" });\n+\toracle.edit({ edits: [{ file: \"a.txt\", old: \"two\", new: \"TWO\" }] });\n+\toracle.edit({ move: { file: \"a.txt\", start: 1, end: 2, to: \"b.txt\", at: 0 } });\n+\tconst storage = new SqlState();\n+\tconst original = new Graph({ storage });\n+\tfor (const atom of oracle.graph.atoms()) original.add(atom);\n+\toriginal.setLabel({ name: \"saved\", head: \"a2\", title: \"Saved\", message: \"\" });\n+\tconst reads = storage.reads;\n+\tconst reopened = new Graph({ storage });\n+\tassert.equal(storage.reads, reads, \"opening does not hydrate historical collections\");\n+\tassert.equal(storage.scans, 0);\n+\tassert.equal(reopened.size, oracle.graph.size);\n+\tassert.equal(reopened.lineCount, oracle.graph.lineCount);\n+\tassert.deepEqual([...reopened.movers()], [...oracle.graph.movers()]);\n+\tfor (const atom of oracle.graph.atoms()) {\n+\t\tassert.deepEqual(reopened.filesOf(atom.id), oracle.graph.filesOf(atom.id));\n+\t\tfor (const move of atom.moves ?? []) {\n+\t\t\tassert.deepEqual(reopened.relocationsOf(move.line), oracle.graph.relocationsOf(move.line));\n+\t\t\tassert.deepEqual(reopened.relocatedTo(move.parent), oracle.graph.relocatedTo(move.parent));\n+\t\t\tassert.equal(reopened.codeOf(move.parent), oracle.graph.codeOf(move.parent));\n+\t\t}\n+\t}\n+\tassert.deepEqual(new View(reopened, oracle.shared).files(), oracle.files());\n+\tassert.equal(reopened.label(\"saved\")?.head, \"a2\");\n+\tconst draft = oracle.draft({ edits: [{ file: \"a.txt\", old: \"one\", new: \"ONE\" }] });\n+\tassert.deepEqual(checkDraft(reopened, oracle.shared, draft), oracle.check(draft));\n+\toracle.graph.add(draft);\n+\treopened.add(draft);\n+\toracle.shared.add(draft.id);\n+\tassert.deepEqual(new View(new Graph({ storage }), oracle.shared).files(), oracle.files());\n+\tfor (let n = 0; n < oracle.graph.lineCount; n++) {\n+\t\tconst id = oracle.graph.lineIdOf(n);\n+\t\tassert.deepEqual(reopened.line(id), oracle.graph.line(id));\n+\t\tassert.deepEqual(reopened.deletersOf(id), oracle.graph.deletersOf(id));\n+\t}\n+\tassert.ok(Object.isFrozen(reopened.atom(\"a0\")));\n+\tstorage.db.close();\n+});\n+\n+test(\"stored indexes retain multiple claimants and preserve a real divergent conflict\", () => {\n+\tconst oracle = Repo.init({ \"a.txt\": lines(\"one\", \"two\", \"three\") });\n+\toracle.edit({ edits: [{ file: \"a.txt\", old: \"one\", new: \"ONE\" }], author: \"left\" });\n+\tconst right = oracle.draft({ edits: [{ file: \"a.txt\", old: \"one\", new: \"uno\" }], author: \"right\" }, [\"a0\"]);\n+\tconst storage = new SqlState();\n+\tconst graph = new Graph({ storage });\n+\tfor (const atom of oracle.graph.atoms()) graph.add(atom);\n+\tassert.equal(oracle.check(right).conflicts.length, 1);\n+\tassert.deepEqual(checkDraft(new Graph({ storage }), oracle.shared, right), oracle.check(right));\n+\tgraph.add(right);\n+\toracle.graph.add(right);\n+\tassert.deepEqual(new Graph({ storage }).writersOf(\"L:a0.0\"), oracle.graph.writersOf(\"L:a0.0\"));\n+\tassert.deepEqual(new Graph({ storage }).deletedLines(right.id), oracle.graph.deletedLines(right.id));\n+\tstorage.db.close();\n+});\n+\n+test(\"failed stored append rolls back all graph indexes and can be retried\", () => {\n+\tconst oracle = Repo.init({ \"a.txt\": lines(\"one\", \"two\") });\n+\tconst storage = new SqlState();\n+\tconst graph = new Graph({ storage });\n+\tgraph.add(oracle.graph.atom(\"a0\"));\n+\tconst before = JSON.stringify(graph.toJSON());\n+\tconst draft = oracle.draft({ edits: [{ file: \"a.txt\", old: \"one\", new: \"ONE\" }] });\n+\tstorage.failWrite = storage.writes + 10;\n+\tassert.throws(() => graph.add(draft), /injected storage failure/);\n+\tassert.equal(JSON.stringify(graph.toJSON()), before);\n+\tassert.equal(JSON.stringify(new Graph({ storage }).toJSON()), before);\n+\tstorage.failWrite = Infinity;\n+\tgraph.add(draft);\n+\tassert.equal(new Graph({ storage }).size, 2);\n+\tstorage.db.close();\n+});\n+\n+test(\"removing a stored label rolls back dependent rewrites on failure\", () => {\n+\tconst oracle = Repo.init({ \"a.txt\": lines(\"one\") });\n+\tconst storage = new SqlState();\n+\tconst graph = new Graph({ storage });\n+\tgraph.add(oracle.graph.atom(\"a0\"));\n+\tgraph.setLabel({ name: \"base\", head: \"a0\", title: \"Base\", message: \"\" });\n+\tgraph.setLabel({ name: \"child\", head: \"a0\", base: \"base\", title: \"Child\", message: \"\" });\n+\tconst before = graph.labels();\n+\tstorage.failWrite = storage.writes + 1;\n+\tassert.throws(() => graph.removeLabel(\"base\"), /injected storage failure/);\n+\tassert.deepEqual(new Graph({ storage }).labels(), before);\n+\tstorage.failWrite = Infinity;\n+\tassert.equal(graph.removeLabel(\"base\"), true);\n+\tassert.equal(new Graph({ storage }).label(\"base\"), undefined);\n+\tassert.equal(new Graph({ storage }).label(\"child\")?.base, \"a0\");\n+\tstorage.db.close();\n+});"}],"unmatched":[]}