{"draft":null,"from":175,"to":318,"files":[{"path":"src/engine/test/packed-files.test.ts","added":84,"removed":0,"diff":"--- /dev/null\n+++ b/src/engine/test/packed-files.test.ts\n@@ -0,0 +1,84 @@\n+// Each file's line history packed (abso Capacity, Graph.packedLines): a stored graph lays files out from a few pages,\n+// and must give exactly what the in-memory graph gives, through edits, deletes, moves between files and back.\n+\n+import assert from \"node:assert/strict\";\n+import { test } from \"node:test\";\n+import { Graph, View } from \"../index.ts\";\n+import { StableOverlaySet } from \"../state-source.ts\";\n+import { lines, Repo } from \"./helpers.ts\";\n+import { SqlState } from \"./storage-helper.ts\";\n+\n+test(\"a packed file lays out as the in-memory graph does, at every step, moves included\", () => {\n+\tconst oracle = Repo.init({ \"a.txt\": lines(\"one\", \"two\", \"three\", \"four\"), \"b.txt\": lines(\"bee\"), \"c.txt\": \"\" });\n+\tconst sets: Set<string>[] = [new Set(oracle.shared)];\n+\tconst step = (change: Parameters<Repo[\"edit\"]>[0]) => {\n+\t\toracle.edit(change);\n+\t\tsets.push(new Set(oracle.shared));\n+\t};\n+\tstep({ edits: [{ file: \"a.txt\", old: \"two\", new: \"TWO\" }] });\n+\tstep({ move: { file: \"a.txt\", start: 1, end: 3, to: \"b.txt\", at: 1 } });\n+\tstep({ edits: [{ file: \"b.txt\", old: \"TWO\", new: \"two again\" }] });\n+\tstep({ edits: [{ file: \"a.txt\", old: \"four\", new: \"\" }] });\n+\tstep({ move: { file: \"b.txt\", start: 0, end: 2, to: \"c.txt\", at: 0 } });\n+\tstep({ edits: [{ file: \"c.txt\", old: \"bee\", new: \"bee\\nafter bee\" }] });\n+\tconst storage = new SqlState();\n+\ttry {\n+\t\tconst graph = new Graph({ storage });\n+\t\tfor (const atom of oracle.graph.atoms()) graph.add(atom);\n+\t\tconst reopened = new Graph({ storage });\n+\t\tassert.equal(reopened.packedFiles, true);\n+\t\tfor (const [i, set] of sets.entries()) {\n+\t\t\tconst packed = new View(reopened, new StableOverlaySet(set));\n+\t\t\tconst memory = new View(oracle.graph, set);\n+\t\t\tassert.deepEqual(packed.paths(), memory.paths(), `step ${i}: paths`);\n+\t\t\tfor (const path of [\"a.txt\", \"b.txt\", \"c.txt\"]) {\n+\t\t\t\tassert.equal(packed.text(path), memory.text(path), `step ${i}: ${path}`);\n+\t\t\t\tassert.deepEqual(packed.sequence(path), memory.sequence(path), `step ${i}: ${path}'s sequence`);\n+\t\t\t\tassert.deepEqual(packed.attrs(path), memory.attrs(path), `step ${i}: ${path}'s attributes`);\n+\t\t\t}\n+\t\t}\n+\t} finally {\n+\t\tstorage.db.close();\n+\t}\n+});\n+\n+test(\"a graph made with packFiles off keeps no packed history\", () => {\n+\tconst oracle = Repo.init({ \"a.txt\": lines(\"one\") });\n+\tconst storage = new SqlState();\n+\ttry {\n+\t\tconst graph = new Graph({ storage, packFiles: false });\n+\t\tfor (const atom of oracle.graph.atoms()) graph.add(atom);\n+\t\tassert.equal(new Graph({ storage }).packedFiles, false);\n+\t\tassert.equal(new View(new Graph({ storage }), new StableOverlaySet(oracle.shared)).text(\"a.txt\"), \"one\\n\");\n+\t} finally {\n+\t\tstorage.db.close();\n+\t}\n+});\n+\n+test(\"a file's decoded history is kept between views and let go when an atom is added\", () => {\n+\tconst oracle = Repo.init({ \"a.txt\": lines(\"one\", \"two\", \"three\"), \"b.txt\": lines(\"bee\") });\n+\tconst storage = new SqlState();\n+\ttry {\n+\t\tconst graph = new Graph({ storage });\n+\t\tfor (const atom of oracle.graph.atoms()) graph.add(atom);\n+\t\tconst first = new View(graph, new StableOverlaySet(oracle.shared));\n+\t\tassert.equal(first.text(\"a.txt\"), \"one\\ntwo\\nthree\\n\");\n+\t\tassert.ok(graph.historyBytes > 0, \"the first layout keeps a.txt's history\");\n+\t\t// Another view of the same file reads only what its membership needs, not the history's pages again.\n+\t\tconst before = storage.reads;\n+\t\tassert.equal(new View(graph, new StableOverlaySet(oracle.shared)).sequence(\"a.txt\").length, 3);\n+\t\tconst pages = storage.reads - before;\n+\t\tconst cold = new Graph({ storage });\n+\t\tconst coldBefore = storage.reads;\n+\t\tnew View(cold, new StableOverlaySet(oracle.shared)).sequence(\"a.txt\");\n+\t\tassert.ok(pages < storage.reads - coldBefore, \"the kept history saved reads\");\n+\t\t// A new atom lets every kept history go; the next layout sees its lines.\n+\t\toracle.edit({ edits: [{ file: \"a.txt\", old: \"two\", new: \"TWO\" }] });\n+\t\tgraph.add(oracle.graph.atom([...oracle.shared].at(-1)!));\n+\t\tassert.equal(graph.historyBytes, 0);\n+\t\tconst after = new View(graph, new StableOverlaySet(oracle.shared));\n+\t\tassert.equal(after.text(\"a.txt\"), new View(oracle.graph, oracle.shared).text(\"a.txt\"));\n+\t} finally {\n+\t\tstorage.db.close();\n+\t}\n+});"}],"unmatched":[]}