- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/src/engine/test/projection-locator.test.ts?format=json
140 linescodex-capacity-chair 100%Last change codex-capacity-chair in version 165, 4 versions changed it
1import assert from "node:assert/strict";
2import { test } from "node:test";
3import { ProjectionLocator } from "../experimental/projection-locator.ts";
4import { SqlState } from "./storage-helper.ts";
6const decode = (value: unknown): number => {
7 if (!Number.isSafeInteger(value)) throw Error("invalid payload");
8 return value as number;
9};
10test("experimental locator preserves sparse old roots and rejects corrupt storage", () => {
11 const storage = new SqlState();
12 try {
13 const index = new ProjectionLocator(storage, "test", decode),
14 keys = [0, 0xffffffff, 16, 1, 0x80000000, 255, 256];
15 let root: number | null = null;
16 const versions: number[] = [];
17 for (const id of keys) {
18 root = index.set(root, id, id);
19 versions.push(root);
20 }
21 for (let i = 0; i < versions.length; i++)
22 for (let j = 0; j < keys.length; j++)
23 assert.equal(index.get(versions[i]!, keys[j]!), j <= i ? keys[j] : undefined);
24 const before = JSON.stringify(storage.db.prepare("SELECT * FROM state ORDER BY collection,key").all());
25 storage.failWrite = storage.writes + 3;
26 assert.throws(() => index.set(root, 42, 42), /injected/);
27 assert.equal(JSON.stringify(storage.db.prepare("SELECT * FROM state ORDER BY collection,key").all()), before);
28 storage.failWrite = Infinity;
29 for (const id of [-1, 2 ** 32, 0.5, NaN]) assert.throws(() => index.get(root, id), /identity limit/);
30 const collection = 'experimental-locator:"test"',
31 saved = storage.read(collection, String(root));
32 for (const corrupt of [
33 { format: 2, kind: "leaf", prefix: "00000000", value: 0 },
34 { format: 1, kind: "leaf", prefix: "0", value: 0 },
35 { format: 1, kind: "branch", prefix: "", children: { "0": root, f: 0 } },
36 { format: 1, kind: "branch", prefix: "", children: { "00": 0, f: 1 } },
37 ]) {
38 storage.write(collection, String(root), corrupt);
39 assert.throws(() => index.get(root, 0), /invalid locator/);
40 }
41 storage.write(collection, String(root), saved);
42 assert.equal(index.get(root, 16), 16);
43 const leaf = index.set(null, 7, 7);
44 storage.write(collection, String(leaf), { format: 1, kind: "leaf", prefix: "00000007", value: "bad" });
45 assert.throws(() => index.get(leaf, 7), /invalid payload/);
46 } finally {
47 storage.db.close();
48 }
49});
51test("bounded locator batches preserve old roots, discard intermediate pages, and roll back writes", () => {
52 const db = new SqlState();
53 try {
54 const index = new ProjectionLocator(db, "batch", decode);
55 const old = index.set(null, 0, 1);
56 const values = Array.from({ length: 64 }, (_, i) => [i, i + 10] as const);
57 const before = db.writes;
58 const root = index.setMany(old, values);
59 const batchWrites = db.writes - before;
60 assert.equal(index.get(old, 0), 1);
61 for (const [id, value] of values) assert.equal(index.get(root, id), value);
62 const plain = new ProjectionLocator(db, "plain", decode);
63 let sequential: number | null = plain.set(null, 0, 1);
64 const baseline = db.writes;
65 for (const [id, value] of values) sequential = plain.set(sequential, id, value);
66 assert.ok(batchWrites < (db.writes - baseline) / 2);
67 const rows = db.db.prepare("SELECT * FROM state ORDER BY collection,key").all();
68 // Fail every persisted page and the final allocation-counter write.
69 for (let offset = 1; offset <= batchWrites; offset++) {
70 db.failWrite = db.writes + offset;
71 assert.throws(() => index.setMany(old, values), /injected/);
72 assert.deepEqual(db.db.prepare("SELECT * FROM state ORDER BY collection,key").all(), rows);
73 }
74 db.failWrite = Infinity;
75 assert.throws(
76 () =>
77 index.setMany(
78 root,
79 Array.from({ length: 257 }, () => [0, 1] as const),
80 ),
81 /batch limit/,
82 );
83 assert.equal(index.setMany(root, []), root);
84 const duplicate = index.setMany(root, [
85 [0, 100],
86 [0, 200],
87 [0xffffffff, 300],
88 ]);
89 assert.equal(index.get(duplicate, 0), 200);
90 assert.equal(index.get(duplicate, 0xffffffff), 300);
91 assert.equal(index.get(root, 0), 10);
92 const stable = db.db.prepare("SELECT * FROM state ORDER BY collection,key").all();
93 assert.throws(
94 () =>
95 index.setMany(duplicate, [
96 [1, 3],
97 [2 ** 32, 4],
98 ]),
99 /identity limit/,
100 );
101 assert.deepEqual(db.db.prepare("SELECT * FROM state ORDER BY collection,key").all(), stable);
102 } finally {
103 db.db.close();
104 }
105});
107test("multi-key locator updates match sequential sparse edits and retain previous roots", () => {
108 const db = new SqlState();
109 try {
110 const batched = new ProjectionLocator(db, "multi", decode);
111 const sequential = new ProjectionLocator(db, "oracle", decode);
112 let left: number | null = null,
113 right: number | null = null;
114 let seed = 12345;
115 const random = () => (seed = (Math.imul(seed, 1664525) + 1013904223) >>> 0);
116 const keys = [0, 0xffffffff, 1, 16, 255, 256, ...Array.from({ length: 60 }, random)];
117 const expected = new Map<number, number>();
118 for (let round = 0; round < 8; round++) {
119 const edits: [number, number][] = Array.from({ length: 64 }, (_, i) => [
120 keys[random() % keys.length]!,
121 round * 100 + i,
122 ]);
123 const prior: number | null = left;
124 const oldEntries: (readonly [number, number])[] = [...batched.entries(prior)];
125 left = batched.setMany(left, edits);
126 for (const [key, value] of edits) {
127 right = sequential.set(right, key, value);
128 expected.set(key, value);
129 }
130 assert.deepEqual([...batched.entries(left)], [...sequential.entries(right)]);
131 assert.deepEqual(
132 [...batched.entries(left)],
133 [...expected].sort(([a], [b]) => a - b),
134 );
135 assert.deepEqual([...batched.entries(prior)], oldEntries);
136 }
137 } finally {
138 db.db.close();
139 }
140});