- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/src/engine/test/projection-membership-iterator.test.ts?format=json
71 linescodex-capacity-chair 100%Last change codex-capacity-chair in version 158, 1 version changed it
1import assert from "node:assert/strict";
2import { test } from "node:test";
3import { ProjectionLocator } from "../experimental/projection-locator.ts";
4import { ExperimentalMarkerProjection } from "../experimental/projection-marker.ts";
5import { ProjectionReader } from "../experimental/projection-reader.ts";
6import { Graph } from "../graph.ts";
7import { Repo } from "./helpers.ts";
8import { SqlState } from "./storage-helper.ts";
10test("locator streams ordered immutable leaves with bounded first-result reads", () => {
11 const db = new SqlState();
12 try {
13 const index = new ProjectionLocator(db, "iterate", (v) => {
14 if (typeof v !== "number") throw Error("invalid value");
15 return v;
16 });
17 let root: number | null = null;
18 for (let id = 511; id >= 0; id--) root = index.set(root, id, id);
19 const previous = root;
20 root = index.set(root, 256, -1);
21 const before = db.reads;
22 const iterator = index.entries(root);
23 assert.deepEqual(iterator.next().value, [0, 0]);
24 assert.ok(db.reads - before <= 9);
25 iterator.return?.();
26 assert.deepEqual(
27 [...index.entries(previous)],
28 Array.from({ length: 512 }, (_, id) => [id, id]),
29 );
30 assert.equal([...index.entries(root)][256]?.[1], -1);
31 db.write('experimental-locator:"iterate"', String(root), {
32 format: 1,
33 kind: "branch",
34 prefix: "",
35 children: { 0: root, 1: root },
36 });
37 assert.throws(() => index.entries(root).next(), /invalid locator child/);
38 } finally {
39 db.db.close();
40 }
41});
43test("projected membership streams without atom payload reads and expires with its scope", () => {
44 const db = new SqlState();
45 try {
46 const repo = Repo.init({ "a.txt": "one\n" });
47 const atoms = [repo.graph.atom("a0"), repo.edit({ edits: [{ file: "a.txt", old: "one\n", new: "two\n" }] })];
48 const graph = new Graph({ storage: db });
49 for (const atom of atoms) graph.add(atom);
50 const adapter = new ExperimentalMarkerProjection(db, graph, "a.txt", "membership");
51 let state = adapter.empty();
52 for (const atom of atoms) state = adapter.apply(state, atom);
53 let live = true;
54 const reader = new ProjectionReader(adapter, state, () => {
55 if (!live) throw Error("expired");
56 });
57 const read = db.read.bind(db);
58 db.read = (collection, key) => {
59 if (collection === "atoms") throw Error("atom payload read forbidden");
60 return read(collection, key);
61 };
62 assert.deepEqual([...reader.set], ["a0", "a1"]);
63 assert.equal(reader.set.has("a1"), true);
64 const iterator = reader.set.values();
65 assert.equal(iterator.next().value, "a0");
66 live = false;
67 assert.throws(() => iterator.next(), /expired/);
68 } finally {
69 db.db.close();
70 }
71});