{"draft":null,"from":45,"to":318,"files":[{"path":"src/engine/test/storage-helper.ts","added":46,"removed":0,"diff":"--- /dev/null\n+++ b/src/engine/test/storage-helper.ts\n@@ -0,0 +1,46 @@\n+import { DatabaseSync } from \"node:sqlite\";\n+import type { GraphStateStorage } from \"../state-source.ts\";\n+\n+export class SqlState implements GraphStateStorage {\n+\treadonly db: DatabaseSync;\n+\treads = 0;\n+\twrites = 0;\n+\tscans = 0;\n+\tfailWrite = Infinity;\n+\tconstructor(filename = \":memory:\") {\n+\t\tthis.db = new DatabaseSync(filename);\n+\t\tthis.db.exec(\n+\t\t\t\"CREATE TABLE IF NOT EXISTS state (collection TEXT, key TEXT, value TEXT, PRIMARY KEY(collection,key))\",\n+\t\t);\n+\t}\n+\tread(collection: string, key: string): unknown {\n+\t\tthis.reads++;\n+\t\tconst row = this.db.prepare(\"SELECT value FROM state WHERE collection = ? AND key = ?\").get(collection, key);\n+\t\treturn row ? JSON.parse(row.value as string).value : undefined;\n+\t}\n+\twrite(collection: string, key: string, value: unknown): void {\n+\t\tif (++this.writes === this.failWrite) throw new Error(\"injected storage failure\");\n+\t\tthis.db.prepare(\"INSERT OR REPLACE INTO state VALUES (?, ?, ?)\").run(collection, key, JSON.stringify({ value }));\n+\t}\n+\tremove(collection: string, key: string): void {\n+\t\tthis.db.prepare(\"DELETE FROM state WHERE collection = ? AND key = ?\").run(collection, key);\n+\t}\n+\t*entries(collection: string): Iterable<readonly [string, unknown]> {\n+\t\tthis.scans++;\n+\t\tfor (const row of this.db\n+\t\t\t.prepare(\"SELECT key, value FROM state WHERE collection = ? ORDER BY key\")\n+\t\t\t.iterate(collection))\n+\t\t\tyield [row.key as string, JSON.parse(row.value as string).value];\n+\t}\n+\ttransaction<T>(body: () => T): T {\n+\t\tthis.db.exec(\"SAVEPOINT graph_change\");\n+\t\ttry {\n+\t\t\tconst value = body();\n+\t\t\tthis.db.exec(\"RELEASE graph_change\");\n+\t\t\treturn value;\n+\t\t} catch (error) {\n+\t\t\tthis.db.exec(\"ROLLBACK TO graph_change; RELEASE graph_change\");\n+\t\t\tthrow error;\n+\t\t}\n+\t}\n+}"}],"unmatched":[]}