import * as Y from 'yjs'; import type { PagePersistence, StoreResult } from '../persistence.js'; /** * In-memory {@link PagePersistence} for tests: it keeps the last merged state * per page so a "server restart" (destroy + recreate around the same instance) * still reloads content, without needing a database. The Postgres implementation * is covered separately by `persistence.db.test.ts`. */ export class InMemoryPagePersistence implements PagePersistence { private readonly states = new Map(); /** Page ids that should report as missing (to exercise the not-found path). */ readonly missing = new Set(); /** Overridable size ceiling so a test can trip it without a 5 MiB document. */ sizeLimit = Number.POSITIVE_INFINITY; /** store() call count, for assertions. */ storeCalls = 0; async loadInto(pageId: string, doc: Y.Doc): Promise { if (this.missing.has(pageId)) return false; const state = this.states.get(pageId); if (state) Y.applyUpdate(doc, state); return true; } async store(pageId: string, doc: Y.Doc): Promise { this.storeCalls += 1; const full = Y.encodeStateAsUpdate(doc); if (full.byteLength > this.sizeLimit) { return { outcome: 'too_large', bytes: full.byteLength, durationMs: 0, merged: false }; } if (this.missing.has(pageId)) { return { outcome: 'not_found', bytes: full.byteLength, durationMs: 0, merged: false }; } this.states.set(pageId, full); return { outcome: 'stored', bytes: full.byteLength, durationMs: 0, merged: false }; } forget(): void { // Nothing to release for the in-memory fake. } }