{"draft":null,"from":2,"to":318,"files":[{"path":"src/engine/test/projection-priority.test.ts","added":177,"removed":0,"diff":"--- /dev/null\n+++ b/src/engine/test/projection-priority.test.ts\n@@ -0,0 +1,177 @@\n+import assert from \"node:assert/strict\";\n+import { test } from \"node:test\";\n+import { ProjectionPriority } from \"../experimental/projection-priority.ts\";\n+import { orderedChildKey } from \"../ordered-index.ts\";\n+import { SqlState } from \"./storage-helper.ts\";\n+\n+const decode = (value: unknown): number => {\n+\tif (!Number.isSafeInteger(value)) throw Error(\"invalid payload\");\n+\treturn value as number;\n+};\n+test(\"experimental priority retains old roots, removes winners and rejects corrupt pages\", () => {\n+\tconst storage = new SqlState();\n+\ttry {\n+\t\tconst index = new ProjectionPriority(storage, \"test\", decode),\n+\t\t\toracle: { key: string; value: number }[] = [];\n+\t\tlet root: number | null = null;\n+\t\tfor (let n = 0; n < 96; n++) {\n+\t\t\tconst key = orderedChildKey({ n, replaces: n % 2, lamport: (n * 71) % 97, seq: n, k: 0 });\n+\t\t\tconst expected = oracle.filter((entry) => entry.key < key).at(-1)?.value;\n+\t\t\tassert.equal(index.before(root, key), expected);\n+\t\t\tconst old: number | null = root;\n+\t\t\troot = index.insert(root, key, n);\n+\t\t\tassert.equal(index.before(old, key), expected);\n+\t\t\toracle.push({ key, value: n });\n+\t\t\toracle.sort((a, b) => a.key.localeCompare(b.key));\n+\t\t}\n+\t\tconst full = root;\n+\t\tconst greatest = oracle.pop()!;\n+\t\troot = index.remove(root, greatest.key);\n+\t\tassert.equal(index.before(root, \"f\".repeat(57)), oracle.at(-1)!.value);\n+\t\tassert.equal(index.before(full, \"f\".repeat(57)), greatest.value);\n+\t\tconst rows = () => JSON.stringify(storage.db.prepare(\"SELECT * FROM state ORDER BY collection,key\").all());\n+\t\tconst before = rows();\n+\t\tstorage.failWrite = storage.writes + 3;\n+\t\tassert.throws(() => index.insert(root, \"e\".repeat(57), 100), /injected/);\n+\t\tassert.equal(rows(), before);\n+\t\tstorage.failWrite = Infinity;\n+\t\tconst collection = 'experimental-priority:\"test\"',\n+\t\t\tsaved = storage.read(collection, String(root));\n+\t\tfor (const corrupt of [\n+\t\t\t{ format: 1, kind: \"branch\", depth: 1, children: { \"0\": 0 } },\n+\t\t\t{ format: 1, kind: \"branch\", depth: 0, children: { f: root } },\n+\t\t\t{ format: 1, kind: \"branch\", depth: 0, children: {} },\n+\t\t]) {\n+\t\t\tstorage.write(collection, String(root), corrupt);\n+\t\t\tassert.throws(() => index.before(root, \"f\".repeat(57)), /invalid priority/);\n+\t\t}\n+\t\tstorage.write(collection, String(root), saved);\n+\t\tassert.equal(index.before(root, \"f\".repeat(57)), oracle.at(-1)!.value);\n+\t} finally {\n+\t\tstorage.db.close();\n+\t}\n+});\n+\n+test(\"priority allocation rolls back a failed final counter and retries without collisions\", (t) => {\n+\tconst storage = new SqlState();\n+\tt.after(() => storage.db.close());\n+\tconst index = new ProjectionPriority(storage, \"counter-retry\", decode);\n+\tconst key = \"1\".repeat(57);\n+\tconst root = index.insert(null, key, 1);\n+\tconst rows = () => storage.db.prepare(\"SELECT * FROM state ORDER BY collection,key\").all();\n+\tconst before = rows();\n+\tconst original = storage.write.bind(storage);\n+\tlet fail = true;\n+\tt.mock.method(storage, \"write\", (collection: string, key: string, value: unknown) => {\n+\t\toriginal(collection, key, value);\n+\t\tif (key === \"next\" && fail) throw Error(\"final allocation counter failure\");\n+\t});\n+\tassert.throws(() => index.insert(root, \"2\".repeat(57), 2), /final allocation counter/);\n+\tassert.deepEqual(rows(), before);\n+\tassert.equal(index.before(root, \"f\".repeat(57)), 1);\n+\tfail = false;\n+\tconst next = index.insert(root, \"2\".repeat(57), 2);\n+\tassert.equal(index.before(next, \"f\".repeat(57)), 2);\n+\tassert.equal(index.before(root, \"f\".repeat(57)), 1);\n+\tconst beforeRemove = rows();\n+\tfail = true;\n+\tassert.throws(() => index.remove(next, \"2\".repeat(57)), /final allocation counter/);\n+\tassert.deepEqual(rows(), beforeRemove);\n+\tfail = false;\n+\tconst removed = index.remove(next, \"2\".repeat(57));\n+\tassert.equal(index.before(removed, \"f\".repeat(57)), 1);\n+});\n+\n+test(\"compressed priority updates legacy depth paths without changing old roots\", (t) => {\n+\tconst storage = new SqlState();\n+\tt.after(() => storage.db.close());\n+\tconst collection = 'experimental-priority:\"legacy\"';\n+\tconst key = \"1\".repeat(57);\n+\tstorage.write(collection, \"57\", { format: 1, kind: \"leaf\", depth: 57, value: 1 });\n+\tfor (let depth = 56; depth >= 0; depth--)\n+\t\tstorage.write(collection, String(depth), {\n+\t\t\tformat: 1,\n+\t\t\tkind: \"branch\",\n+\t\t\tdepth,\n+\t\t\tchildren: { [key[depth]!]: depth + 1 },\n+\t\t});\n+\tstorage.write(collection, \"next\", 58);\n+\tconst index = new ProjectionPriority(storage, \"legacy\", decode);\n+\tassert.equal(index.before(0, \"f\".repeat(57)), 1);\n+\tconst mixed = index.insert(0, \"1\".repeat(56) + \"2\", 2);\n+\tassert.equal(index.before(mixed, \"f\".repeat(57)), 2);\n+\tassert.equal(index.before(mixed, \"1\".repeat(56) + \"2\"), 1);\n+\tassert.equal(index.before(0, \"f\".repeat(57)), 1);\n+\tconst removed = index.remove(mixed, key);\n+\tassert.equal(index.before(removed, \"f\".repeat(57)), 2);\n+\tassert.equal(index.remove(removed, \"1\".repeat(56) + \"2\"), null);\n+\tassert.equal(index.before(0, \"f\".repeat(57)), 1);\n+});\n+\n+test(\"compressed priority matches a sorted oracle across varied prefixes and removals\", (t) => {\n+\tconst storage = new SqlState();\n+\tt.after(() => storage.db.close());\n+\tconst index = new ProjectionPriority(storage, \"varied\", decode);\n+\tconst oracle = new Map<string, number>();\n+\tlet root: number | null = null;\n+\tlet seed = 731;\n+\tconst randomKey = () =>\n+\t\tArray.from({ length: 57 }, () => {\n+\t\t\tseed = (Math.imul(seed, 1664525) + 1013904223) >>> 0;\n+\t\t\treturn (seed >>> 28).toString(16);\n+\t\t}).join(\"\");\n+\tconst keys = [\n+\t\t\"0\".repeat(57),\n+\t\t\"f\".repeat(57),\n+\t\t...Array.from({ length: 128 }, randomKey),\n+\t\t...Array.from({ length: 32 }, (_, n) => \"a\".repeat(55) + n.toString(16).padStart(2, \"0\")),\n+\t];\n+\tconst verify = () => {\n+\t\tconst sorted = [...oracle.keys()].sort();\n+\t\tfor (const key of [...keys, randomKey()]) {\n+\t\t\tconst before = sorted.filter((candidate) => candidate < key).at(-1);\n+\t\t\tassert.equal(index.before(root, key), before === undefined ? undefined : oracle.get(before));\n+\t\t}\n+\t};\n+\tfor (const [n, key] of keys.entries()) {\n+\t\troot = index.insert(root, key, n);\n+\t\toracle.set(key, n);\n+\t\tif (n % 23 === 0) verify();\n+\t}\n+\tverify();\n+\tfor (const [n, key] of keys.entries()) {\n+\t\troot = index.remove(root, key);\n+\t\toracle.delete(key);\n+\t\tif (n % 19 === 0) verify();\n+\t}\n+\tassert.equal(root, null);\n+\tverify();\n+});\n+\n+test(\"compressed priority rejects malformed prefixes and cyclic child edges\", (t) => {\n+\tconst storage = new SqlState();\n+\tt.after(() => storage.db.close());\n+\tconst collection = 'experimental-priority:\"corrupt\"';\n+\tconst index = new ProjectionPriority(storage, \"corrupt\", decode);\n+\tconst first = index.insert(null, \"1\".repeat(57), 1);\n+\tconst root = index.insert(first, \"2\".repeat(57), 2);\n+\tconst saved = storage.read(collection, String(root));\n+\tfor (const node of [\n+\t\t{ format: 2, kind: \"branch\", prefix: \"\", children: { \"1\": root, \"2\": first } },\n+\t\t{ format: 2, kind: \"branch\", prefix: \"x\", children: { \"1\": first, \"2\": first } },\n+\t\t{ format: 2, kind: \"branch\", prefix: \"\", children: { \"1\": first } },\n+\t\t{ format: 2, kind: \"leaf\", prefix: \"1\", value: 1 },\n+\t\t{ format: 3, kind: \"leaf\", prefix: \"1\".repeat(57), value: 1 },\n+\t]) {\n+\t\tstorage.write(collection, String(root), node);\n+\t\tassert.throws(() => index.before(root, \"f\".repeat(57)), /invalid priority/);\n+\t}\n+\tstorage.write(collection, String(root), saved);\n+\tconst branch = saved as { children: Record<string, number> };\n+\tconst child = branch.children[\"2\"]!;\n+\tconst leaf = storage.read(collection, String(child));\n+\tstorage.write(collection, String(child), { format: 2, kind: \"leaf\", prefix: \"3\".repeat(57), value: 2 });\n+\tassert.throws(() => index.before(root, \"f\".repeat(57)), /invalid priority/);\n+\tstorage.write(collection, String(child), leaf);\n+\tassert.equal(index.before(root, \"f\".repeat(57)), 2);\n+});"}],"unmatched":[]}