Versions 129 to 318
new file, 76 lines
1+import assert from "node:assert/strict"; 2+import { test } from "node:test"; 3+import { buildBase } from "../build.ts"; 4+import { Graph } from "../graph.ts"; 5+import { JsonStringChunks, jsonValueChunks, registerJsonChunkValue } from "../json-value-chunks.ts"; 6+import { StoredText } from "../stored-text.ts"; 7+import { SqlState } from "./storage-helper.ts"; 9+test("many stored insertion runs serialize with linear reads and support backwards access", (t) => { 10+ const storage = new SqlState(); 11+ t.after(() => storage.db.close()); 12+ const files = Object.fromEntries(Array.from({ length: 128 }, (_, i) => [`f${i}`, `${i}:${"x".repeat(17000)}\n`])); 13+ const graph = new Graph({ storage }); 14+ graph.add(buildBase(files, { author: "writer", message: "many runs" })); 15+ const stored = graph.atom("a0"); 16+ const expected = JSON.stringify(stored); 17+ const before = storage.reads; 18+ assert.equal([...jsonValueChunks(stored)].join(""), expected); 19+ assert.ok(storage.reads - before < 128 * 25, `serialization used ${storage.reads - before} reads`); 20+ for (const index of [127, 0, 64, 2, 126, 1]) { 21+ const run = stored.inserts[index]!; 22+ assert.equal(run.lines[0], files[run.file]!.trimEnd()); 24+ assert.equal([...jsonValueChunks(stored)].join(""), expected); 27+test("chunk-backed JSON strings preserve escaping and split UTF-16 exactly", () => { 38+ for (const text of values) 39+ for (const size of [1, 2, 17, 16384]) { 40+ const value = new JsonStringChunks(function* () { 42+ for (let offset = 0; offset < text.length; offset += size) { 43+ yield text.slice(offset, offset + size); 47+ assert.equal([...jsonValueChunks({ value })].join(""), JSON.stringify({ value: text })); 48+ assert.equal(JSON.stringify(value), JSON.stringify(text)); 52+test("registered lazy values stream without invoking their whole-value toJSON", () => { 55+ throw Error("whole value forbidden"); 58+ registerJsonChunkValue(value, () => ({ lines: [new JsonStringChunks(() => ["abc", "def"])] })); 59+ assert.equal([...jsonValueChunks(value)].join(""), '{"lines":["abcdef"]}'); 60+ assert.throws(() => [...jsonValueChunks(new JsonStringChunks(() => ["x".repeat(16385)]))], /exceeds bound/); 63+for (const length of [60000, 100000]) 64+ test(`stored atom JSON streams a ${length}-character line without whole reads`, (t) => { 65+ const storage = new SqlState(); 66+ t.after(() => storage.db.close()); 67+ const graph = new Graph({ storage }); 68+ const atom = buildBase({ f: `${"x".repeat(length)}\n` }, { author: "writer", message: "init" }); 70+ const stored = graph.atom("a0"); 71+ const expected = JSON.stringify(stored); 72+ t.mock.method(StoredText.prototype, "read", () => { 73+ throw Error("whole stored text forbidden"); 75+ assert.equal([...jsonValueChunks(stored)].join(""), expected);