{"draft":null,"from":68,"to":318,"files":[{"path":"src/engine/test/view.test.ts","added":173,"removed":2,"diff":"--- a/src/engine/test/view.test.ts\n+++ b/src/engine/test/view.test.ts\n@@ -3,9 +3,101 @@\n import assert from \"node:assert/strict\";\n import { describe, it } from \"node:test\";\n import { buildBase, conflicts, Graph, project, View } from \"../index.ts\";\n+import { StableOverlaySet, StableSet } from \"../state-source.ts\";\n+import type { AtomId } from \"../types.ts\";\n import { lines, Repo } from \"./helpers.ts\";\n \n describe(\"View\", () => {\n+\tit(\"bounds file-location memo growth across long ancestry walks with moves\", () => {\n+\t\tconst retained = (count: number) => {\n+\t\t\tconst repo = Repo.init({ \"long.txt\": \"line\\n\".repeat(count), \"a.txt\": \"move\\n\", \"b.txt\": \"base\\n\" });\n+\t\t\trepo.edit({ move: { file: \"a.txt\", start: 0, end: 1, to: \"b.txt\", at: 1 } });\n+\t\t\tconst view = new View(repo.graph, new StableOverlaySet<AtomId>([\"a0\", \"a1\"]));\n+\t\t\tconst oracle = repo.view();\n+\t\t\t// Resolve the deepest ancestry first, then force repeated cache eviction.\n+\t\t\tconst sequence = oracle.sequence(\"long.txt\");\n+\t\t\tassert.equal(sequence.length, count);\n+\t\t\tassert.equal(view.fileOf(sequence[count - 1]!.id), \"long.txt\");\n+\t\t\tfor (const line of sequence) assert.equal(view.fileOf(line.id), \"long.txt\");\n+\t\t\tfor (const line of oracle.lines(\"b.txt\")) assert.equal(view.fileOf(line.id), \"b.txt\");\n+\t\t\tassert.equal(view.fileOf(sequence[count - 1]!.id), \"long.txt\");\n+\t\t\treturn view.weight();\n+\t\t};\n+\t\tassert.equal(retained(2048), retained(512), \"file-location retained state must not grow with ancestry depth\");\n+\t});\n+\tit(\"current indexed paths do not retain deleted historical file layouts\", () => {\n+\t\tconst retained = (count: number) => {\n+\t\t\tconst files: Record<string, string> = { \"live.txt\": \"live\\n\" };\n+\t\t\tfor (let i = 0; i < count; i++) files[`old-${i}.txt`] = \"gone\\n\";\n+\t\t\tconst repo = Repo.init(files);\n+\t\t\trepo.edit({ edits: Array.from({ length: count }, (_, i) => ({ file: `old-${i}.txt`, old: \"gone\\n\", new: \"\" })) });\n+\t\t\tconst view = new View(repo.graph, new StableOverlaySet<AtomId>([\"a0\", \"a1\"]));\n+\t\t\tassert.deepEqual(view.files(), { \"live.txt\": \"live\\n\" });\n+\t\t\tconst weight = view.weight();\n+\t\t\tassert.equal(view.knownPaths().length, count + 1, \"explicit historical enumeration is preserved\");\n+\t\t\treturn weight;\n+\t\t};\n+\t\tassert.equal(retained(200), retained(20), \"deleted path count must not grow current-view retained state\");\n+\t});\n+\tit(\"reuses indexed membership across a long run of lines without a history-sized memo\", () => {\n+\t\tconst repo = Repo.init({ \"long.txt\": \"line\\n\".repeat(1000) });\n+\t\tclass CountingSet extends StableOverlaySet<AtomId> {\n+\t\t\tcalls = 0;\n+\t\t\toverride has(id: AtomId): boolean {\n+\t\t\t\tthis.calls++;\n+\t\t\t\treturn super.has(id);\n+\t\t\t}\n+\t\t}\n+\t\tconst set = new CountingSet([\"a0\"]);\n+\t\tconst view = new View(repo.graph, set);\n+\t\tset.calls = 0;\n+\t\tassert.equal(view.text(\"long.txt\"), \"line\\n\".repeat(1000));\n+\t\tassert.ok(set.calls <= 2, `membership was queried ${set.calls} times for one atom`);\n+\t});\n+\tit(\"immutable indexed membership avoids a history-sized bitmap and remains a snapshot\", () => {\n+\t\tclass RangeMembership extends StableSet<AtomId> {\n+\t\t\treadonly retainedBytes = 64;\n+\t\t\treadonly count: number;\n+\t\t\tconstructor(count: number) {\n+\t\t\t\tsuper();\n+\t\t\t\tthis.count = count;\n+\t\t\t}\n+\t\t\thas(id: AtomId): boolean {\n+\t\t\t\treturn /^a[0-9]+$/.test(id) && Number(id.slice(1)) < this.count;\n+\t\t\t}\n+\t\t\t*values(): SetIterator<AtomId> {\n+\t\t\t\tfor (let i = 0; i < this.count; i++) yield `a${i}`;\n+\t\t\t}\n+\t\t}\n+\t\tconst repo = Repo.init({ \"a.txt\": \"0\\n\" });\n+\t\tfor (let i = 1; i <= 40; i++) repo.edit({ edits: [{ file: \"a.txt\", old: `${i - 1}\\n`, new: `${i}\\n` }] });\n+\t\tconst first = new View(repo.graph, new RangeMembership(repo.graph.size));\n+\t\tconst weight = first.weight();\n+\t\tfor (let i = 41; i <= 80; i++) repo.edit({ edits: [{ file: \"a.txt\", old: `${i - 1}\\n`, new: `${i}\\n` }] });\n+\t\tconst second = new View(repo.graph, new RangeMembership(repo.graph.size));\n+\t\tassert.equal(second.weight(), weight, \"membership resident size does not increase with historical atom count\");\n+\t\tassert.equal(first.text(\"a.txt\"), \"40\\n\");\n+\t\tassert.equal(second.text(\"a.txt\"), \"80\\n\");\n+\t\tassert.equal(second.weight(), first.weight(), \"current text retains visible lines, not historical tombstones\");\n+\t\tconst currentWeight = second.weight();\n+\t\tassert.equal(second.sequence(\"a.txt\").length, 81);\n+\t\tassert.equal(second.sequence(\"a.txt\").filter((line) => line.visible).length, 1);\n+\t\tassert.ok(second.weight() > currentWeight, \"explicit history reads may retain their requested sequence\");\n+\t\tassert.equal(second.text(\"a.txt\"), \"80\\n\");\n+\t\tassert.equal(first.set.has(\"a80\"), false);\n+\t\tassert.throws(() => new View(repo.graph, new StableOverlaySet<AtomId>([\"a1\"])), /not closed/);\n+\t});\n+\tit(\"membership memo collisions preserve both included and excluded historical atoms\", () => {\n+\t\tconst repo = Repo.init({ \"f.txt\": \"0\\n\" });\n+\t\tfor (let i = 1; i <= 257; i++) repo.edit({ edits: [{ file: \"f.txt\", old: `${i - 1}\\n`, new: `${i}\\n` }] });\n+\t\tconst old = new View(repo.graph, new StableOverlaySet<AtomId>([\"a0\"]));\n+\t\tassert.equal(old.text(\"f.txt\"), \"0\\n\");\n+\t\tassert.deepEqual(\n+\t\t\told.sequence(\"f.txt\").map((line) => line.text),\n+\t\t\t[\"0\"],\n+\t\t);\n+\t\tassert.equal(repo.view().text(\"f.txt\"), \"257\\n\");\n+\t});\n \tit(\"keeps deleted lines as tombstones in the full sequence\", () => {\n \t\tconst repo = Repo.init({ \"f.txt\": lines(\"one\", \"two\", \"three\") });\n \t\tconst del = repo.edit({ edits: [{ file: \"f.txt\", old: \"two\\n\", new: \"\" }] });\n@@ -42,8 +134,9 @@\n \t\tassert.equal(repo.text(\"f.txt\"), lines(\"top\", \"r\", \"q\", \"p\", \"bottom\"));\n \t\tassert.deepEqual(\n \t\t\tr.deps,\n-\t\t\t[\"a0\", p.id, q.id].sort((a, b) => Number(a.slice(1)) - Number(b.slice(1))),\n+\t\t\t[p.id, q.id].sort((a, b) => Number(a.slice(1)) - Number(b.slice(1))),\n \t\t);\n+\t\tassert.deepEqual(repo.graph.closureOf(r.deps), repo.graph.closureOf([\"a0\", p.id, q.id]));\n \t});\n \n \tit(\"puts a line's replacements ahead of the lines inserted after it, whatever their priority\", () => {\n@@ -78,7 +171,7 @@\n \tit(\"projects a long file (a deep chain) without recursion limits\", () => {\n \t\tconst big = Array.from({ length: 30_000 }, (_, i) => `line ${i}`);\n \t\tconst graph = new Graph();\n-\t\tgraph.add(buildBase({ \"big.txt\": big.join(\"\\n\") + \"\\n\" }, { author: \"import\", message: \"big\" }));\n+\t\tgraph.add(buildBase({ \"big.txt\": `${big.join(\"\\n\")}\\n` }, { author: \"import\", message: \"big\" }));\n \t\tconst files = project(graph, [\"a0\"]);\n \t\tassert.equal(files[\"big.txt\"]!.split(\"\\n\").length, 30_001);\n \t});\n@@ -91,4 +184,82 @@\n \t\tassert.deepEqual(view.knownPaths(), [\"a.txt\", \"b.txt\"]);\n \t\tassert.deepEqual(view.files(), { \"a.txt\": \"a\\n\" });\n \t});\n+\n+\tit(\"accounts for the membership bitmap before layout and stays observational\", () => {\n+\t\tconst small = Repo.init({ \"f.txt\": lines(\"same\") });\n+\t\tconst larger = Repo.init({ \"f.txt\": lines(\"same\") });\n+\t\tfor (let i = 0; i < 8; i++)\n+\t\t\tlarger.edit({ attrs: [{ file: \"f.txt\", name: \"exec\", value: i % 2 === 0 ? \"1\" : null }] });\n+\n+\t\tconst smallView = new View(small.graph, small.shared);\n+\t\tconst largerView = new View(larger.graph, larger.shared);\n+\t\tconst smallBeforeLayout = smallView.weight();\n+\t\tconst largerBeforeLayout = largerView.weight();\n+\t\tassert.ok(smallBeforeLayout >= small.graph.size);\n+\t\tassert.ok(largerBeforeLayout >= larger.graph.size);\n+\t\tassert.equal(largerBeforeLayout - smallBeforeLayout, larger.graph.size - small.graph.size);\n+\t\tassert.equal(largerView.weight(), largerBeforeLayout);\n+\n+\t\tassert.deepEqual(smallView.files(), largerView.files());\n+\t\tconst afterLayout = largerView.weight();\n+\t\tassert.equal(largerView.weight(), afterLayout);\n+\t\tassert.deepEqual(largerView.files(), { \"f.txt\": \"same\\n\" });\n+\t});\n+\n+\tit(\"accounts for lazy view-owned containers and relocation/index growth\", () => {\n+\t\tconst repo = Repo.init({ \"a.txt\": lines(\"one\", \"two\"), \"b.txt\": lines(\"base\") });\n+\t\trepo.edit({ move: { file: \"a.txt\", start: 0, end: 1, to: \"b.txt\", at: 1 } });\n+\t\tconst deletion = repo.edit({ edits: [{ file: \"b.txt\", old: \"base\\n\", new: \"\" }] });\n+\t\tconst view = repo.view();\n+\t\tlet weight = view.weight();\n+\t\tassert.equal(view.weight(), weight);\n+\n+\t\tassert.equal(view.set.size, 3);\n+\t\tassert.ok(view.weight() > weight);\n+\t\tweight = view.weight();\n+\t\tassert.deepEqual(view.knownPaths(), [\"a.txt\", \"b.txt\"]);\n+\t\tassert.ok(view.weight() > weight);\n+\t\tweight = view.weight();\n+\t\tassert.equal(view.fileOf(\"a0.0\"), \"b.txt\");\n+\t\tassert.ok(view.weight() > weight);\n+\n+\t\tconst expectedFiles = { \"a.txt\": \"two\\n\", \"b.txt\": \"one\\n\" };\n+\t\tassert.deepEqual(view.files(), expectedFiles);\n+\t\tweight = view.weight();\n+\t\tassert.deepEqual(\n+\t\t\tview.sequence(\"b.txt\").map((line) => [line.text, line.visible, line.deletedBy]),\n+\t\t\t[\n+\t\t\t\t[\"base\", false, [deletion.id]],\n+\t\t\t\t[\"one\", true, []],\n+\t\t\t],\n+\t\t);\n+\t\tassert.ok(view.weight() > weight);\n+\t\tweight = view.weight();\n+\t\tassert.equal(view.sequenceIndex(\"b.txt\", \"a0.0\"), 1);\n+\t\tassert.ok(view.weight() > weight);\n+\t\tweight = view.weight();\n+\t\tassert.equal(view.lineNumber(\"b.txt\", \"a0.0\"), 0);\n+\t\tassert.ok(view.weight() > weight);\n+\t\tweight = view.weight();\n+\t\tassert.deepEqual(\n+\t\t\tview.lines(\"b.txt\").map((line) => line.text),\n+\t\t\t[\"one\"],\n+\t\t);\n+\t\tassert.ok(view.weight() > weight);\n+\t\tconst finalWeight = view.weight();\n+\t\tassert.equal(view.weight(), finalWeight);\n+\t\tassert.deepEqual(view.files(), expectedFiles);\n+\t});\n+\n+\tit(\"counts line-cache references without charging Graph-owned text per view\", () => {\n+\t\tconst short = Repo.init({ \"f.txt\": lines(\"x\") });\n+\t\tconst long = Repo.init({ \"f.txt\": lines(\"x\".repeat(10_000)) });\n+\t\tconst shortView = short.view();\n+\t\tconst longView = long.view();\n+\t\tshortView.lines(\"f.txt\");\n+\t\tlongView.lines(\"f.txt\");\n+\t\tassert.equal(shortView.weight(), longView.weight());\n+\t\tconst longWeight = longView.weight();\n+\t\tassert.equal(longView.weight(), longWeight);\n+\t});\n });"}],"unmatched":[]}