{"draft":null,"from":7,"to":318,"files":[{"path":"worker/project/test/view-cache.test.ts","added":187,"removed":0,"diff":"--- /dev/null\n+++ b/worker/project/test/view-cache.test.ts\n@@ -0,0 +1,187 @@\n+import assert from \"node:assert/strict\";\n+import { test } from \"node:test\";\n+import type { ReadView } from \"../../../src/engine/index.ts\";\n+import { buildBase, Graph, View } from \"../../../src/engine/index.ts\";\n+import { StableOverlaySet } from \"../../../src/engine/state-source.ts\";\n+import type { AtomId } from \"../../../src/engine/types.ts\";\n+import { VIEW_COUNT, ViewCache } from \"../views.ts\";\n+\n+const VIEW_BUDGET = 24_000_000;\n+\n+test(\"indexed view cache keys never enumerate membership and remain bounded by eviction\", () => {\n+\tclass Counted extends StableOverlaySet<AtomId> {\n+\t\titerations = 0;\n+\t\toverride values(): SetIterator<AtomId> {\n+\t\t\tthis.iterations++;\n+\t\t\treturn super.values();\n+\t\t}\n+\t}\n+\tconst graph = new Graph();\n+\tgraph.add(buildBase({ \"a.txt\": \"one\\n\" }, { author: \"test\", message: \"base\" }));\n+\tconst cache = new ViewCache(graph);\n+\tconst set = new Counted([\"a0\"]);\n+\tconst view = cache.view(set);\n+\tassert.equal(set.iterations, 1, \"only View closure validation enumerates membership\");\n+\tassert.equal(cache.view(set), view);\n+\tassert.equal(set.iterations, 1, \"cache hit must not construct a full-history key\");\n+\tassert.equal(view.text(\"a.txt\"), \"one\\n\");\n+\tfor (let i = 0; i < VIEW_COUNT + 1; i++) cache.view(new Counted([\"a0\"]));\n+\tassert.notEqual(cache.view(set), view, \"indexed identities obey the same cache eviction limit\");\n+\tassert.equal(set.iterations, 2);\n+});\n+\n+type WeightControls = {\n+\tsetIncomingWeights(values: number[]): void;\n+\tsetWeight(view: ReadView, weight: number): void;\n+};\n+\n+function withControlledWeights(run: (controls: WeightControls) => void): void {\n+\tconst original = Object.getOwnPropertyDescriptor(View.prototype, \"weight\")!;\n+\tconst weights = new WeakMap<ReadView, number>();\n+\tlet newWeights: number[] = [];\n+\tObject.defineProperty(View.prototype, \"weight\", {\n+\t\tconfigurable: true,\n+\t\tvalue(this: ReadView) {\n+\t\t\tif (!weights.has(this)) weights.set(this, newWeights.shift() ?? 0);\n+\t\t\treturn weights.get(this)!;\n+\t\t},\n+\t});\n+\tconst controls: WeightControls = {\n+\t\tsetWeight: (view, weight) => {\n+\t\t\tweights.set(view, weight);\n+\t\t},\n+\t\tsetIncomingWeights: (values) => {\n+\t\t\tnewWeights = [...values];\n+\t\t},\n+\t};\n+\ttry {\n+\t\trun(controls);\n+\t} finally {\n+\t\tObject.defineProperty(View.prototype, \"weight\", original);\n+\t}\n+}\n+\n+function get(cache: ViewCache, key: string): ReadView {\n+\treturn cache.keyed(key, () => new Set());\n+}\n+\n+test(\"the incoming view counts toward the retained byte budget\", () => {\n+\twithControlledWeights((control) => {\n+\t\tconst cache = new ViewCache(new Graph());\n+\t\tcontrol.setIncomingWeights([10_000_000, 15_000_001]);\n+\t\tconst old = get(cache, \"old\");\n+\t\tconst incoming = get(cache, \"incoming\");\n+\t\tassert.equal(get(cache, \"incoming\"), incoming);\n+\t\tassert.notEqual(get(cache, \"old\"), old);\n+\t});\n+});\n+\n+test(\"the cache retains exactly VIEW_COUNT views and evicts the least recently used one\", () => {\n+\twithControlledWeights((control) => {\n+\t\tconst cache = new ViewCache(new Graph());\n+\t\tcontrol.setIncomingWeights(Array(VIEW_COUNT + 1).fill(0));\n+\t\tconst first = get(cache, \"key-0\");\n+\t\tconst retained = Array.from({ length: VIEW_COUNT - 1 }, (_, i) => get(cache, `key-${i + 1}`));\n+\t\tassert.equal(get(cache, \"key-0\"), first);\n+\t\tconst last = get(cache, `key-${VIEW_COUNT}`);\n+\t\tassert.equal(get(cache, \"key-0\"), first);\n+\t\tassert.equal(get(cache, `key-${VIEW_COUNT}`), last);\n+\t\tfor (let i = 1; i < retained.length; i++) assert.equal(get(cache, `key-${i + 1}`), retained[i]);\n+\t\tassert.notEqual(get(cache, \"key-1\"), retained[0]);\n+\t});\n+});\n+\n+test(\"a repeated hit reweighs lazy growth and evicts older entries\", () => {\n+\twithControlledWeights((control) => {\n+\t\tconst cache = new ViewCache(new Graph());\n+\t\tcontrol.setIncomingWeights([10_000_000, 10_000_000]);\n+\t\tconst growing = get(cache, \"growing\");\n+\t\tconst older = get(cache, \"older\");\n+\t\tcontrol.setWeight(growing, 16_000_000);\n+\t\tassert.equal(get(cache, \"growing\"), growing);\n+\t\tassert.notEqual(get(cache, \"older\"), older);\n+\t});\n+});\n+\n+test(\"trim reweighs growth with no further hit and evicts until the views fit (abso Capacity)\", () => {\n+\twithControlledWeights((control) => {\n+\t\tconst cache = new ViewCache(new Graph());\n+\t\tcontrol.setIncomingWeights([10_000_000, 10_000_000]);\n+\t\tconst growing = get(cache, \"growing\");\n+\t\tconst newer = get(cache, \"newer\");\n+\t\t// A call lays files out in the older view after the cache last weighed it, and nothing asks for it again.\n+\t\tcontrol.setWeight(growing, 16_000_000);\n+\t\tcache.trim();\n+\t\tassert.equal(get(cache, \"newer\"), newer);\n+\t\tassert.notEqual(get(cache, \"growing\"), growing);\n+\t});\n+});\n+\n+test(\"an oversized view is returned but not retained\", () => {\n+\twithControlledWeights((control) => {\n+\t\tconst cache = new ViewCache(new Graph());\n+\t\tcontrol.setIncomingWeights([VIEW_BUDGET + 1, 0]);\n+\t\tconst oversized = get(cache, \"large\");\n+\t\tassert.ok(oversized instanceof View);\n+\t\tassert.notEqual(get(cache, \"large\"), oversized);\n+\t});\n+});\n+\n+test(\"cache eviction preserves real Graph and View layouts\", () => {\n+\tconst graph = new Graph();\n+\tgraph.add(\n+\t\tbuildBase(\n+\t\t\t{ \"notes.txt\": \"one\\ntwo\\n\", \"folder/readme.md\": \"cached view\\n\" },\n+\t\t\t{ author: \"test\", message: \"view cache\" },\n+\t\t),\n+\t);\n+\tconst set = new Set([\"a0\"]);\n+\tconst cache = new ViewCache(graph);\n+\tconst cached = cache.view(set);\n+\tconst expected = new View(graph, set);\n+\tassert.deepEqual(cached.files(), expected.files());\n+\tassert.equal(cached.text(\"notes.txt\"), expected.text(\"notes.txt\"));\n+\tfor (let i = 0; i < 40; i++) cache.keyed(`other-${i}`, () => set);\n+\tassert.deepEqual(cache.view(set).files(), new View(graph, set).files());\n+});\n+\n+test(\"trim lets go of the least recently used file layouts across views before whole views (abso Capacity)\", () => {\n+\tconst graph = new Graph();\n+\tconst lines = (name: string) => Array.from({ length: 40 }, (_, i) => `${name} line ${i}\\n`).join(\"\");\n+\tgraph.add(\n+\t\tbuildBase({ \"a.txt\": lines(\"a\"), \"b.txt\": lines(\"b\"), \"c.txt\": lines(\"c\") }, { author: \"test\", message: \"base\" }),\n+\t);\n+\tconst set = new Set([\"a0\"]);\n+\t// a.txt in x first, then b.txt in y, then c.txt in x: a.txt is the least recently used.\n+\tconst use = (cache: ViewCache) => {\n+\t\tconst x = cache.keyed(\"x\", () => set);\n+\t\tconst y = cache.keyed(\"y\", () => set);\n+\t\tx.text(\"a.txt\");\n+\t\ty.text(\"b.txt\");\n+\t\tx.text(\"c.txt\");\n+\t\treturn { x: x as View, y: y as View };\n+\t};\n+\tconst probe = use(new ViewCache(graph));\n+\tconst total = probe.x.weight() + probe.y.weight();\n+\tconst cache = new ViewCache(graph, undefined, total - 1);\n+\tconst { x, y } = use(cache);\n+\tcache.trim();\n+\tassert.deepEqual(\n+\t\t[...x.laidOut()].map((f) => f.path),\n+\t\t[\"c.txt\"],\n+\t);\n+\tassert.deepEqual(\n+\t\t[...y.laidOut()].map((f) => f.path),\n+\t\t[\"b.txt\"],\n+\t);\n+\tassert.equal(\n+\t\tcache.keyed(\"x\", () => set),\n+\t\tx,\n+\t\t\"both views are still cached\",\n+\t);\n+\tassert.equal(\n+\t\tcache.keyed(\"y\", () => set),\n+\t\ty,\n+\t);\n+\tassert.equal(x.text(\"a.txt\"), lines(\"a\"), \"a.txt is laid out again, the same\");\n+});"}],"unmatched":[]}