- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/src/engine/test/graph-long-text.test.ts?format=json
100 linescodex-capacity-chair 100%Last change open in version 174, 3 versions changed it
1import assert from "node:assert/strict";
2import { test } from "node:test";
3import { MemoryStore, Project } from "../../../worker/project/project.ts";
4import { Graph } from "../graph.ts";
5import { SqlState } from "./storage-helper.ts";
7test("large insertion text stays lazy even when the run has few lines", (t) => {
8 const storage = new SqlState();
9 t.after(() => storage.db.close());
10 const memory = new MemoryStore();
11 const line = "x".repeat(100000);
12 new Project(memory).init({ "a.txt": `${line}\n${line}\n${line}\n` }, 0);
13 const entry = memory.entries[0]!;
14 if (entry.type !== "init") throw Error("expected init");
15 new Graph({ storage }).add(entry.atom);
16 const read = storage.read.bind(storage);
17 let textReads = 0;
18 t.mock.method(storage, "read", (collection: string, key: string) => {
19 if (collection === "atom-text:long:chunks") textReads++;
20 return read(collection, key);
21 });
22 const atom = new Graph({ storage }).atom("a0");
23 assert.equal(atom.inserts[0]!.lines.length, 3);
24 assert.equal(textReads, 0, "reading line count must not decode the complete run");
25 assert.equal(atom.inserts[0]!.lines[1], line);
26 assert.equal(textReads, Math.ceil(line.length / 16384), "an indexed read decodes only the selected line");
27});
29test("persistent graph stores long lines in chunks and reopens exact text", () => {
30 const storage = new SqlState();
31 try {
32 const memory = new MemoryStore();
33 const value = `${"x".repeat(100000)}\uD83D\uDE00`;
34 new Project(memory).init({ "a.txt": value }, 0);
35 const entry = memory.entries[0]!;
36 assert.equal(entry.type, "init");
37 if (entry.type !== "init") throw Error("expected init");
38 new Graph({ storage }).add(entry.atom);
39 assert.deepEqual(storage.read("line-text", "0"), { format: 1, text: JSON.stringify(["0", 0]) });
40 assert.equal(new Graph({ storage }).lineText("a0.0"), value);
41 assert.ok(
42 (storage.db.prepare("SELECT COUNT(*) AS n FROM state WHERE collection = 'atom-text:long:chunks'").get()!
43 .n as number) > 1,
44 );
45 } finally {
46 storage.db.close();
47 }
48});
50test("long atom acceptance rolls back partial chunks and detects missing text", (t) => {
51 const storage = new SqlState();
52 t.after(() => storage.db.close());
53 const memory = new MemoryStore();
54 const value = "z".repeat(100000);
55 new Project(memory).init({ "a.txt": value }, 0);
56 const entry = memory.entries[0]!;
57 if (entry.type !== "init") throw Error("expected init");
58 const graph = new Graph({ storage });
59 const rows = () => storage.db.prepare("SELECT * FROM state ORDER BY collection,key").all();
60 const before = rows();
61 const write = storage.write.bind(storage);
62 let chunks = 0;
63 const failure = t.mock.method(storage, "write", (collection: string, key: string, value: unknown) => {
64 write(collection, key, value);
65 if (collection === "atom-text:long:chunks" && ++chunks === 2) throw Error("injected partial atom text");
66 });
67 assert.throws(() => graph.add(entry.atom), /partial atom text/);
68 assert.deepEqual(rows(), before);
69 assert.equal(graph.size, 0);
70 failure.mock.restore();
71 graph.add(entry.atom);
72 assert.equal(graph.lineText("a0.0"), value);
73 const textKey = JSON.stringify(["0", 0]);
74 storage.remove("atom-text:long:chunks", JSON.stringify([textKey, 1]));
75 const reopened = new Graph({ storage });
76 assert.throws(() => reopened.lineText("a0.0"), /incomplete stored text/);
77 assert.throws(() => reopened.atom("a0").inserts[0]!.lines[0], /incomplete stored text/);
78});
80test("persistent graph detaches accepted lists without freezing a second input copy", () => {
81 const storage = new SqlState();
82 try {
83 const memory = new MemoryStore();
84 new Project(memory).init({ "a.txt": "before\nsecond" }, 0);
85 const entry = memory.entries[0]!;
86 if (entry.type !== "init") throw Error("expected init");
87 const atom = structuredClone(entry.atom);
88 const expected = structuredClone(atom);
89 const graph = new Graph({ storage });
90 graph.add(atom);
91 (atom.inserts[0]!.lines as string[])[0] = "changed";
92 (atom.writes as string[]).length = 0;
93 for (const loaded of [graph, new Graph({ storage })]) {
94 assert.deepEqual(JSON.parse(JSON.stringify(loaded.atom("a0"))), expected);
95 assert.equal(loaded.lineText("a0.0"), expected.inserts[0]!.lines[0]);
96 }
97 } finally {
98 storage.db.close();
99 }
100});