Versions 29 to 318
new file, 135 lines
1+import assert from "node:assert/strict"; 2+import { test } from "node:test"; 3+import { Graph } from "../graph.ts"; 4+import { withProjectionScratch } from "../projection-scratch.ts"; 5+import { resolveScratchMoves } from "../relocation-scratch.ts"; 6+import { CachedStateStorage, StableOverlaySet } from "../state-source.ts"; 7+import { View } from "../view.ts"; 8+import { Repo, Rng } from "./helpers.ts"; 9+import { SqlState } from "./storage-helper.ts"; 11+test("scratch relocation cycle selection matches View across differing memberships and tied moves", () => { 12+ const rng = new Rng(45131); 13+ const source = new SqlState(); 14+ const storage = new CachedStateStorage(source, 4096, 16); 16+ for (let trial = 0; trial < 24; trial++) { 17+ const repo = Repo.init({ "f.txt": Array.from({ length: 24 }, (_, n) => `line ${n}\n`).join(""), "g.txt": "" }); 18+ const earlier = new Set(repo.shared); 19+ for (let j = 0; j < 32; j++) { 20+ const offset = rng.int(24); 21+ const moves = Array.from({ length: 3 }, (_, index) => { 22+ const line = `a0.${(offset + index) % 24}`; 23+ const candidate = `a0.${rng.int(24)}`; 24+ return { line, parent: candidate === line ? (j % 2 ? "^f.txt" : "^g.txt") : candidate }; 26+ const id = repo.graph.nextId(); 29+ seq: repo.graph.size, 35+ writes: moves.flatMap((move) => [`P:${move.line}`, `G:${move.parent}`]), 45+ if (j < 16) earlier.add(id); 47+ for (const membership of [repo.shared, earlier, repo.shared]) { 48+ const oracle = new View(repo.graph, membership); 49+ const parents = withProjectionScratch( 53+ const resolved = resolveScratchMoves(repo.graph, (id) => membership.has(id), scratch); 54+ return Array.from({ length: 24 }, (_, n) => resolved.parent(`a0.${n}`)); 59+ Array.from({ length: 24 }, (_, n) => oracle.parentOf(`a0.${n}`)), 61+ assert.equal(source.db.prepare("SELECT count(*) AS n FROM state").get()!.n, 0); 62+ assert.equal(storage.retainedEntries, 0); 65+ const persisted = new SqlState(); 67+ const indexed = new Graph({ storage: new CachedStateStorage(persisted, 8192, 32) }); 68+ for (const id of repo.shared) indexed.add(repo.graph.atom(id)); 69+ const before = persisted.db.prepare("SELECT * FROM state ORDER BY collection, key").all(); 70+ // Restart before projecting, then alternate membership on the same graph. 71+ const reopened = new Graph({ storage: new CachedStateStorage(persisted, 8192, 32) }); 72+ for (const membership of [repo.shared, earlier, repo.shared]) { 73+ const expected = new View(repo.graph, membership); 74+ const actual = new View(reopened, new StableOverlaySet(membership)); 75+ for (const path of ["f.txt", "g.txt"]) { 76+ assert.equal(actual.text(path), expected.text(path)); 77+ assert.deepEqual(actual.sequence(path), expected.sequence(path)); 79+ for (const n of [0, 11, 23]) { 80+ assert.equal(actual.parentOf(`a0.${n}`), expected.parentOf(`a0.${n}`)); 81+ assert.equal(actual.fileOf(`a0.${n}`), expected.fileOf(`a0.${n}`)); 83+ assert.deepEqual(persisted.db.prepare("SELECT * FROM state ORDER BY collection, key").all(), before); 86+ persisted.db.close(); 95+test("scratch relocation write failure rolls back without changing canonical graph state", () => { 96+ const repo = Repo.init({ f: "one\ntwo\n" }); 97+ const source = new SqlState(); 107+ writes: ["P:a0.0", "G:a0.1"], 114+ moves: [{ line: "a0.0", parent: "a0.1" }], 116+ repo.shared.add("a1"); 117+ const before = { size: repo.graph.size, lines: repo.graph.lineCount }; 118+ source.failWrite = 3; 121+ withProjectionScratch( 125+ resolveScratchMoves(repo.graph, (id) => repo.shared.has(id), scratch); 128+ /injected storage failure/, 130+ assert.deepEqual({ size: repo.graph.size, lines: repo.graph.lineCount }, before); 131+ assert.equal(source.db.prepare("SELECT count(*) AS n FROM state").get()!.n, 0);