Versions 116 to 318
new file, 63 lines
1+import assert from "node:assert/strict"; 2+import { test } from "node:test"; 3+import { ExperimentalMarkerProjection } from "../experimental/projection-marker.ts"; 4+import { ProjectionReadScope } from "../experimental/projection-read-scope.ts"; 5+import { ProjectionReader } from "../experimental/projection-reader.ts"; 6+import { Graph } from "../graph.ts"; 7+import { Repo } from "./helpers.ts"; 8+import { SqlState } from "./storage-helper.ts"; 10+for (const end of ["commit", "rollback", "source", "nested", "graph"] as const) { 11+ test(`projected reads reject stale methods, membership and iterator resume after ${end}`, (t) => { 12+ const storage = new SqlState(); 14+ const repo = Repo.init({ "f.txt": "one\ntwo\n" }); 15+ const graph = new Graph(); 16+ graph.add(repo.graph.atom("a0")); 17+ const adapter = new ExperimentalMarkerProjection(storage, graph, "f.txt", "scope-test"); 18+ const state = adapter.apply(adapter.empty(), graph.atom("a0")); 20+ const scope = new ProjectionReadScope(graph, () => source); 21+ assert.throws(() => scope.guard(new Graph()), /mismatch/); 22+ const reader = new ProjectionReader(adapter, state, scope.guard(graph)); 23+ const membership = reader.set; 24+ const heldLine = reader.buildLines("f.txt")[0]!; 25+ const chunkSource = reader.lineChunks("f.txt").next().value!; 26+ const chunks = chunkSource()[Symbol.iterator](); 27+ assert.equal(chunks.next().value, "one"); 28+ const iterator = reader.visibleLines("f.txt"); 29+ assert.equal(iterator.next().value?.text, "one"); 30+ if (end === "source") source = {}; 31+ else if (end === "nested") { 32+ const child = scope.nested(); 33+ assert.throws(() => reader.text("f.txt"), /stale/); 34+ const childGuard = child.guard(graph); 37+ assert.throws(childGuard, /expired/); 38+ scope.guard(graph)(); 39+ } else if (end === "graph") { 40+ repo.edit({ edits: [{ file: "f.txt", old: "one\ntwo\n", new: "changed\n" }] }); 41+ graph.add(repo.graph.atom("a1")); 42+ } else scope.close(); 44+ const refuseAccess = () => { 46+ throw Error("expired reader touched data"); 48+ t.mock.method(storage, "read", refuseAccess); 49+ t.mock.method(storage, "entries", refuseAccess); 50+ t.mock.method(graph, "lineN", refuseAccess); 51+ t.mock.method(graph, "textOf", refuseAccess); 52+ assert.throws(() => reader.text("f.txt"), /expired|stale/); 53+ assert.throws(() => heldLine.text, /expired|stale/); 54+ assert.throws(() => chunks.next(), /expired|stale/); 55+ assert.throws(() => chunkSource()[Symbol.iterator]().next(), /expired|stale/); 56+ assert.throws(() => membership.has("a0"), /expired|stale/); 57+ assert.throws(() => iterator.next(), /expired|stale/); 58+ assert.equal(touched, false);