- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/src/engine/test/state-source.test.ts?version=119&format=json
176 linescodex-capacity-chair 100%Last change codex-capacity-chair in version 119, 3 versions changed it
You're reading version 119. The newest is 318, and 12 versions since then changed this file.
codex-capacity-policy has unpushed changes at lines 1–89no start notelast edit
1import assert from "node:assert/strict";
2import { test } from "node:test";
3import { checkDraft, Graph, View } from "../index.ts";
4import { CachedStateStorage, storedLists, storedNumbers } from "../state-source.ts";
5import { lines, Repo } from "./helpers.ts";
6import { SqlState } from "./storage-helper.ts";
8test("per-file list pages append and reopen without reading the full historical list", () => {
9 const source = new SqlState();
10 try {
11 const storage = new CachedStateStorage(source);
12 const lists = storedLists<string>(storage, "files");
13 for (let i = 0; i < 1025; i++) storage.transaction(() => lists.append("a.txt", `a${i}`));
14 assert.equal(lists.length("a.txt"), 1025);
15 assert.deepEqual([...lists.keys()], ["a.txt"]);
16 storage.clear();
17 const before = source.reads;
18 storage.transaction(() => lists.append("a.txt", "a1025"));
19 assert.ok(source.reads - before <= 2, "append reads only one page and its length");
20 assert.throws(
21 () =>
22 storage.transaction(() => {
23 lists.append("a.txt", "rolled-back");
24 throw new Error("rollback");
25 }),
26 /rollback/,
27 );
28 const reopened = storedLists<string>(new CachedStateStorage(source), "files");
29 assert.deepEqual(
30 [...reopened.values("a.txt")],
31 Array.from({ length: 1026 }, (_, i) => `a${i}`),
32 );
33 for (const [, page] of source.entries("files:pages")) assert.ok((page as string[]).length <= 256);
34 source.write("legacy", "a.txt", ["a0"]);
35 assert.throws(() => [...storedLists(source, "legacy").keys()], /canonical-log adoption/);
36 } finally {
37 source.db.close();
38 }
39});
41test("numeric pages cross boundaries, reopen, mutate and roll back without retaining history", () => {
42 const source = new SqlState();
43 const storage = new CachedStateStorage(source);
44 const numbers = storedNumbers(storage, "numbers");
45 storage.transaction(() => {
46 for (let i = 0; i < 1025; i++) numbers.push(i - 1);
47 });
48 storage.clear();
49 const reads = source.reads;
50 for (let i = 0; i < 1025; i++) assert.equal(numbers.at(i), i - 1);
51 assert.ok(source.reads - reads <= 7, "one backing read per page plus metadata");
52 assert.equal([...source.entries("numbers:pages")].length, 5);
53 assert.throws(
54 () =>
55 storage.transaction(() => {
56 numbers.set(255, -99);
57 numbers.set(256, -98);
58 numbers.push(1025);
59 throw new Error("rollback");
60 }),
61 /rollback/,
62 );
63 const reopened = storedNumbers(new CachedStateStorage(source), "numbers");
64 assert.equal(reopened.length, 1025);
65 assert.equal(reopened.at(255), 254);
66 assert.equal(reopened.at(256), 255);
67 assert.equal(numbers.at(255), 254);
68 reopened.set(256, -42);
69 assert.equal(reopened.at(256), -42);
70 assert.throws(() => reopened.at(1025), /out of bounds/);
71 assert.throws(() => reopened.set(-1, 2), /out of bounds/);
72 assert.throws(() => reopened.push(NaN), /safe integers/);
73});
75test("numeric pages reject legacy or missing pages instead of fabricating values", () => {
76 const storage = new SqlState();
77 storage.write("legacy:meta", "length", 256);
78 const legacy = storedNumbers(storage, "legacy");
79 assert.throws(() => legacy.at(0), /canonical-log adoption/);
80 assert.throws(() => legacy.push(1), /canonical-log adoption/);
81 const numbers = storedNumbers(storage, "new");
82 numbers.push(5);
83 storage.remove("new:pages", "0");
84 assert.throws(() => numbers.at(0), /missing writer numeric page/);
85});
87test("stored graph reopens without replay and preserves moves, conflicts and new edits", () => {
88 const oracle = Repo.init({ "a.txt": lines("one", "two", "three"), "b.txt": "" });
89 oracle.edit({ edits: [{ file: "a.txt", old: "two", new: "TWO" }] });
90 oracle.edit({ move: { file: "a.txt", start: 1, end: 2, to: "b.txt", at: 0 } });
91 const storage = new SqlState();
92 const original = new Graph({ storage });
93 for (const atom of oracle.graph.atoms()) original.add(atom);
94 original.setLabel({ name: "saved", head: "a2", title: "Saved", message: "" });
95 const reads = storage.reads;
96 const reopened = new Graph({ storage });
97 assert.equal(storage.reads, reads, "opening does not hydrate historical collections");
98 assert.equal(storage.scans, 0);
99 assert.equal(reopened.size, oracle.graph.size);
100 assert.equal(reopened.lineCount, oracle.graph.lineCount);
101 assert.deepEqual([...reopened.movers()], [...oracle.graph.movers()]);
102 for (const atom of oracle.graph.atoms()) {
103 assert.deepEqual(reopened.filesOf(atom.id), oracle.graph.filesOf(atom.id));
104 for (const move of atom.moves ?? []) {
105 assert.deepEqual(reopened.relocationsOf(move.line), oracle.graph.relocationsOf(move.line));
106 assert.deepEqual(reopened.relocatedTo(move.parent), oracle.graph.relocatedTo(move.parent));
107 assert.equal(reopened.codeOf(move.parent), oracle.graph.codeOf(move.parent));
108 }
109 }
110 assert.deepEqual(new View(reopened, oracle.shared).files(), oracle.files());
111 assert.equal(reopened.label("saved")?.head, "a2");
112 const draft = oracle.draft({ edits: [{ file: "a.txt", old: "one", new: "ONE" }] });
113 assert.deepEqual(checkDraft(reopened, oracle.shared, draft), oracle.check(draft));
114 oracle.graph.add(draft);
115 reopened.add(draft);
116 oracle.shared.add(draft.id);
117 assert.deepEqual(new View(new Graph({ storage }), oracle.shared).files(), oracle.files());
118 for (let n = 0; n < oracle.graph.lineCount; n++) {
119 const id = oracle.graph.lineIdOf(n);
120 assert.deepEqual(reopened.line(id), oracle.graph.line(id));
121 assert.deepEqual(reopened.deletersOf(id), oracle.graph.deletersOf(id));
122 }
123 assert.ok(Object.isFrozen(reopened.atom("a0")));
124 storage.db.close();
125});
127test("stored indexes retain multiple claimants and preserve a real divergent conflict", () => {
128 const oracle = Repo.init({ "a.txt": lines("one", "two", "three") });
129 oracle.edit({ edits: [{ file: "a.txt", old: "one", new: "ONE" }], author: "left" });
130 const right = oracle.draft({ edits: [{ file: "a.txt", old: "one", new: "uno" }], author: "right" }, ["a0"]);
131 const storage = new SqlState();
132 const graph = new Graph({ storage });
133 for (const atom of oracle.graph.atoms()) graph.add(atom);
134 assert.equal(oracle.check(right).conflicts.length, 1);
135 assert.deepEqual(checkDraft(new Graph({ storage }), oracle.shared, right), oracle.check(right));
136 graph.add(right);
137 oracle.graph.add(right);
138 assert.deepEqual(new Graph({ storage }).writersOf("L:a0.0"), oracle.graph.writersOf("L:a0.0"));
139 assert.deepEqual(new Graph({ storage }).deletedLines(right.id), oracle.graph.deletedLines(right.id));
140 storage.db.close();
141});
143test("failed stored append rolls back all graph indexes and can be retried", () => {
144 const oracle = Repo.init({ "a.txt": lines("one", "two") });
145 const storage = new SqlState();
146 const graph = new Graph({ storage });
147 graph.add(oracle.graph.atom("a0"));
148 const before = JSON.stringify(graph.toJSON());
149 const draft = oracle.draft({ edits: [{ file: "a.txt", old: "one", new: "ONE" }] });
150 storage.failWrite = storage.writes + 10;
151 assert.throws(() => graph.add(draft), /injected storage failure/);
152 assert.equal(JSON.stringify(graph.toJSON()), before);
153 assert.equal(JSON.stringify(new Graph({ storage }).toJSON()), before);
154 storage.failWrite = Infinity;
155 graph.add(draft);
156 assert.equal(new Graph({ storage }).size, 2);
157 storage.db.close();
158});
160test("removing a stored label rolls back dependent rewrites on failure", () => {
161 const oracle = Repo.init({ "a.txt": lines("one") });
162 const storage = new SqlState();
163 const graph = new Graph({ storage });
164 graph.add(oracle.graph.atom("a0"));
165 graph.setLabel({ name: "base", head: "a0", title: "Base", message: "" });
166 graph.setLabel({ name: "child", head: "a0", base: "base", title: "Child", message: "" });
167 const before = graph.labels();
168 storage.failWrite = storage.writes + 1;
169 assert.throws(() => graph.removeLabel("base"), /injected storage failure/);
170 assert.deepEqual(new Graph({ storage }).labels(), before);
171 storage.failWrite = Infinity;
172 assert.equal(graph.removeLabel("base"), true);
173 assert.equal(new Graph({ storage }).label("base"), undefined);
174 assert.equal(new Graph({ storage }).label("child")?.base, "a0");
175 storage.db.close();
176});