Versions 129 to 318
new file, 72 lines
1+import assert from "node:assert/strict"; 2+import { test } from "node:test"; 3+import { withDecodedFrames } from "../src/decoded-frames.ts"; 4+import { logParts, SqliteLog } from "../src/engine-project.ts"; 5+import { SqliteWriterState, writerFrameDigest } from "../src/writer-host.ts"; 6+import { memoryStorage } from "./helpers.ts"; 8+test("decoded frame pages retain views through consumption and remove scratch on all exits", (t) => { 9+ const storage = memoryStorage(); 10+ t.after(() => storage.db.close()); 11+ const log = new SqliteLog(storage); 12+ const json = JSON.stringify({ type: "join", agent: "alice", version: 0 }); 13+ for (const [part, text] of logParts(json).entries()) 14+ storage.sql.exec("INSERT INTO log(seq,part,text) VALUES(?,?,?)", 0, part, text); 15+ SqliteWriterState.initialize(storage); 16+ const before = storage.db.prepare("SELECT * FROM writer_state").all(); 17+ const input = { from: 0, to: 1, maxEntries: 1, maxBytes: 1000, digest: "" }; 18+ const result = withDecodedFrames(storage, "decoded-page", log, input, (frames) => { 19+ const frame = [...frames][0]!; 20+ assert.ok("entry" in frame); 21+ assert.equal(frame.entry.type, "join"); 23+ storage.db.prepare("SELECT COUNT(*) AS n FROM writer_state WHERE namespace = ?").get("decoded-page")!.n, 26+ return frame.entry.type; 28+ assert.equal(result.result, "join"); 29+ assert.equal(result.digest, writerFrameDigest("", 0, json)); 30+ assert.deepEqual(storage.db.prepare("SELECT * FROM writer_state").all(), before); 33+ withDecodedFrames(storage, "decoded-page", log, input, (frames) => { 35+ throw Error("consumer failure"); 39+ assert.deepEqual(storage.db.prepare("SELECT * FROM writer_state").all(), before); 42+test("frames above the small bound still use temporary disk storage", (t) => { 43+ const storage = memoryStorage(); 44+ t.after(() => storage.db.close()); 45+ const log = new SqliteLog(storage); 46+ const agent = "a".repeat(5000); 47+ const json = JSON.stringify({ type: "join", agent, version: 0 }); 48+ for (const [part, text] of logParts(json).entries()) 49+ storage.sql.exec("INSERT INTO log(seq,part,text) VALUES(?,?,?)", 0, part, text); 50+ const result = withDecodedFrames( 52+ "large-decoded-page", 54+ { from: 0, to: 1, maxEntries: 1, maxBytes: 10000, digest: "" }, 56+ const frame = [...frames][0]!; 57+ assert.ok("entry" in frame); 60+ storage.db.prepare("SELECT COUNT(*) AS n FROM writer_state WHERE namespace = ?").get("large-decoded-page")!.n, 63+ assert.equal((frame.entry as { agent: string }).agent, agent); 64+ return frame.entry.type; 67+ assert.equal(result.result, "join"); 69+ storage.db.prepare("SELECT COUNT(*) AS n FROM writer_state WHERE namespace = ?").get("large-decoded-page")!.n,