- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/src/engine/test/projection-priority.test.ts?format=json
177 linescodex-capacity-chair 100%Last change codex-capacity-chair in version 162, 2 versions changed it
1import assert from "node:assert/strict";
2import { test } from "node:test";
3import { ProjectionPriority } from "../experimental/projection-priority.ts";
4import { orderedChildKey } from "../ordered-index.ts";
5import { SqlState } from "./storage-helper.ts";
7const decode = (value: unknown): number => {
8 if (!Number.isSafeInteger(value)) throw Error("invalid payload");
9 return value as number;
10};
11test("experimental priority retains old roots, removes winners and rejects corrupt pages", () => {
12 const storage = new SqlState();
13 try {
14 const index = new ProjectionPriority(storage, "test", decode),
15 oracle: { key: string; value: number }[] = [];
16 let root: number | null = null;
17 for (let n = 0; n < 96; n++) {
18 const key = orderedChildKey({ n, replaces: n % 2, lamport: (n * 71) % 97, seq: n, k: 0 });
19 const expected = oracle.filter((entry) => entry.key < key).at(-1)?.value;
20 assert.equal(index.before(root, key), expected);
21 const old: number | null = root;
22 root = index.insert(root, key, n);
23 assert.equal(index.before(old, key), expected);
24 oracle.push({ key, value: n });
25 oracle.sort((a, b) => a.key.localeCompare(b.key));
26 }
27 const full = root;
28 const greatest = oracle.pop()!;
29 root = index.remove(root, greatest.key);
30 assert.equal(index.before(root, "f".repeat(57)), oracle.at(-1)!.value);
31 assert.equal(index.before(full, "f".repeat(57)), greatest.value);
32 const rows = () => JSON.stringify(storage.db.prepare("SELECT * FROM state ORDER BY collection,key").all());
33 const before = rows();
34 storage.failWrite = storage.writes + 3;
35 assert.throws(() => index.insert(root, "e".repeat(57), 100), /injected/);
36 assert.equal(rows(), before);
37 storage.failWrite = Infinity;
38 const collection = 'experimental-priority:"test"',
39 saved = storage.read(collection, String(root));
40 for (const corrupt of [
41 { format: 1, kind: "branch", depth: 1, children: { "0": 0 } },
42 { format: 1, kind: "branch", depth: 0, children: { f: root } },
43 { format: 1, kind: "branch", depth: 0, children: {} },
44 ]) {
45 storage.write(collection, String(root), corrupt);
46 assert.throws(() => index.before(root, "f".repeat(57)), /invalid priority/);
47 }
48 storage.write(collection, String(root), saved);
49 assert.equal(index.before(root, "f".repeat(57)), oracle.at(-1)!.value);
50 } finally {
51 storage.db.close();
52 }
53});
55test("priority allocation rolls back a failed final counter and retries without collisions", (t) => {
56 const storage = new SqlState();
57 t.after(() => storage.db.close());
58 const index = new ProjectionPriority(storage, "counter-retry", decode);
59 const key = "1".repeat(57);
60 const root = index.insert(null, key, 1);
61 const rows = () => storage.db.prepare("SELECT * FROM state ORDER BY collection,key").all();
62 const before = rows();
63 const original = storage.write.bind(storage);
64 let fail = true;
65 t.mock.method(storage, "write", (collection: string, key: string, value: unknown) => {
66 original(collection, key, value);
67 if (key === "next" && fail) throw Error("final allocation counter failure");
68 });
69 assert.throws(() => index.insert(root, "2".repeat(57), 2), /final allocation counter/);
70 assert.deepEqual(rows(), before);
71 assert.equal(index.before(root, "f".repeat(57)), 1);
72 fail = false;
73 const next = index.insert(root, "2".repeat(57), 2);
74 assert.equal(index.before(next, "f".repeat(57)), 2);
75 assert.equal(index.before(root, "f".repeat(57)), 1);
76 const beforeRemove = rows();
77 fail = true;
78 assert.throws(() => index.remove(next, "2".repeat(57)), /final allocation counter/);
79 assert.deepEqual(rows(), beforeRemove);
80 fail = false;
81 const removed = index.remove(next, "2".repeat(57));
82 assert.equal(index.before(removed, "f".repeat(57)), 1);
83});
85test("compressed priority updates legacy depth paths without changing old roots", (t) => {
86 const storage = new SqlState();
87 t.after(() => storage.db.close());
88 const collection = 'experimental-priority:"legacy"';
89 const key = "1".repeat(57);
90 storage.write(collection, "57", { format: 1, kind: "leaf", depth: 57, value: 1 });
91 for (let depth = 56; depth >= 0; depth--)
92 storage.write(collection, String(depth), {
93 format: 1,
94 kind: "branch",
95 depth,
96 children: { [key[depth]!]: depth + 1 },
97 });
98 storage.write(collection, "next", 58);
99 const index = new ProjectionPriority(storage, "legacy", decode);
100 assert.equal(index.before(0, "f".repeat(57)), 1);
101 const mixed = index.insert(0, "1".repeat(56) + "2", 2);
102 assert.equal(index.before(mixed, "f".repeat(57)), 2);
103 assert.equal(index.before(mixed, "1".repeat(56) + "2"), 1);
104 assert.equal(index.before(0, "f".repeat(57)), 1);
105 const removed = index.remove(mixed, key);
106 assert.equal(index.before(removed, "f".repeat(57)), 2);
107 assert.equal(index.remove(removed, "1".repeat(56) + "2"), null);
108 assert.equal(index.before(0, "f".repeat(57)), 1);
109});
111test("compressed priority matches a sorted oracle across varied prefixes and removals", (t) => {
112 const storage = new SqlState();
113 t.after(() => storage.db.close());
114 const index = new ProjectionPriority(storage, "varied", decode);
115 const oracle = new Map<string, number>();
116 let root: number | null = null;
117 let seed = 731;
118 const randomKey = () =>
119 Array.from({ length: 57 }, () => {
120 seed = (Math.imul(seed, 1664525) + 1013904223) >>> 0;
121 return (seed >>> 28).toString(16);
122 }).join("");
123 const keys = [
124 "0".repeat(57),
125 "f".repeat(57),
126 ...Array.from({ length: 128 }, randomKey),
127 ...Array.from({ length: 32 }, (_, n) => "a".repeat(55) + n.toString(16).padStart(2, "0")),
128 ];
129 const verify = () => {
130 const sorted = [...oracle.keys()].sort();
131 for (const key of [...keys, randomKey()]) {
132 const before = sorted.filter((candidate) => candidate < key).at(-1);
133 assert.equal(index.before(root, key), before === undefined ? undefined : oracle.get(before));
134 }
135 };
136 for (const [n, key] of keys.entries()) {
137 root = index.insert(root, key, n);
138 oracle.set(key, n);
139 if (n % 23 === 0) verify();
140 }
141 verify();
142 for (const [n, key] of keys.entries()) {
143 root = index.remove(root, key);
144 oracle.delete(key);
145 if (n % 19 === 0) verify();
146 }
147 assert.equal(root, null);
148 verify();
149});
151test("compressed priority rejects malformed prefixes and cyclic child edges", (t) => {
152 const storage = new SqlState();
153 t.after(() => storage.db.close());
154 const collection = 'experimental-priority:"corrupt"';
155 const index = new ProjectionPriority(storage, "corrupt", decode);
156 const first = index.insert(null, "1".repeat(57), 1);
157 const root = index.insert(first, "2".repeat(57), 2);
158 const saved = storage.read(collection, String(root));
159 for (const node of [
160 { format: 2, kind: "branch", prefix: "", children: { "1": root, "2": first } },
161 { format: 2, kind: "branch", prefix: "x", children: { "1": first, "2": first } },
162 { format: 2, kind: "branch", prefix: "", children: { "1": first } },
163 { format: 2, kind: "leaf", prefix: "1", value: 1 },
164 { format: 3, kind: "leaf", prefix: "1".repeat(57), value: 1 },
165 ]) {
166 storage.write(collection, String(root), node);
167 assert.throws(() => index.before(root, "f".repeat(57)), /invalid priority/);
168 }
169 storage.write(collection, String(root), saved);
170 const branch = saved as { children: Record<string, number> };
171 const child = branch.children["2"]!;
172 const leaf = storage.read(collection, String(child));
173 storage.write(collection, String(child), { format: 2, kind: "leaf", prefix: "3".repeat(57), value: 2 });
174 assert.throws(() => index.before(root, "f".repeat(57)), /invalid priority/);
175 storage.write(collection, String(child), leaf);
176 assert.equal(index.before(root, "f".repeat(57)), 2);
177});