Something went wrong. Try again.
Synapse is a study workspace. Organize courses read and annotate the material, train with generated exercises and watch your progress forked from denishovart.com/synapse
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';import { IDBFactory } from 'fake-indexeddb';import { LoroDoc } from 'loro-crdt';import * as db from './db';import { openDoc, persistNow, resetLoroForTests, WORKSPACE_DOC } from './loro';
// Persistence roundtrips: commits survive a simulated reload (registry reset,// same IndexedDB), and compaction folds update rows into the snapshot.describe('data/loro persistence', () => { beforeEach(() => { globalThis.indexedDB = new IDBFactory(); resetLoroForTests(); }); afterEach(() => resetLoroForTests());
// New registry, same database: what a page reload sees. async function reload(): Promise<void> { await persistNow(); resetLoroForTests(); }
it('persists a commit and reads it back after a reload', async () => { const doc = await openDoc(WORKSPACE_DOC); doc.getMap('courses').set('c1', { name: 'Maths' }); doc.commit(); await reload();
const again = await openDoc(WORKSPACE_DOC); expect(again.getMap('courses').get('c1')).toEqual({ name: 'Maths' }); });
it('stores commits as incremental update rows before compaction', async () => { const doc = await openDoc(WORKSPACE_DOC); doc.getMap('m').set('a', 1); doc.commit(); doc.getMap('m').set('b', 2); doc.commit(); await persistNow();
const rows = await db.updatesFor(WORKSPACE_DOC); expect(rows.length).toBe(2); expect(await db.getDoc(WORKSPACE_DOC)).toBeUndefined(); });
it('compacts into a snapshot once enough updates accumulate', async () => { const doc = await openDoc(WORKSPACE_DOC); for (let i = 0; i < 64; i++) { doc.getMap('m').set(`k${i}`, i); doc.commit(); } await persistNow();
expect((await db.updatesFor(WORKSPACE_DOC)).length).toBe(0); const stored = await db.getDoc(WORKSPACE_DOC); expect(stored).toBeDefined();
await reload(); const again = await openDoc(WORKSPACE_DOC); expect(again.getMap('m').get('k63')).toBe(63); });
// Another tab commits an op this tab's in-memory doc never imported. // When this tab compacts, it must fold that persisted op into the snapshot // rather than delete it against a snapshot that predates it. Simulated by // writing a foreign update row straight to IndexedDB (as a sibling tab // would) that the live registry doc has not seen, then forcing a compaction. it('compaction keeps a sibling tab op it never imported in memory', async () => { const doc = await openDoc(WORKSPACE_DOC); doc.getMap('m').set('mine', 1); doc.commit(); await persistNow();
// A different device/tab: its own peer id, its own edit, persisted as a // raw update row under the same doc, but never imported into `doc`. const sibling = new LoroDoc(); sibling.getMap('m').set('sibling', 2); sibling.commit(); await db.appendUpdate({ docId: WORKSPACE_DOC, bytes: sibling.export({ mode: 'update' }), savedAt: Date.now(), });
// Push this tab over the compaction threshold. for (let i = 0; i < 64; i++) { doc.getMap('m').set(`k${i}`, i); doc.commit(); } await persistNow();
// It folded into a snapshot, and the sibling's edit survived the fold. expect((await db.updatesFor(WORKSPACE_DOC)).length).toBe(0); await reload(); const again = await openDoc(WORKSPACE_DOC); expect(again.getMap('m').get('sibling')).toBe(2); expect(again.getMap('m').get('mine')).toBe(1); expect(again.getMap('m').get('k63')).toBe(63); });
it('retries updates that failed to persist instead of dropping them', async () => { const doc = await openDoc(WORKSPACE_DOC); doc.getMap('m').set('a', 1); doc.commit(); doc.getMap('m').set('b', 2); doc.commit();
const failing = vi.spyOn(db, 'appendUpdate').mockRejectedValueOnce(new Error('quota')); await expect(persistNow()).rejects.toThrow('quota'); failing.mockRestore();
await persistNow(); const rows = await db.updatesFor(WORKSPACE_DOC); expect(rows.length).toBe(2);
await reload(); const again = await openDoc(WORKSPACE_DOC); expect(again.getMap('m').get('a')).toBe(1); expect(again.getMap('m').get('b')).toBe(2); });
it('keeps separate docs separate', async () => { const ws = await openDoc(WORKSPACE_DOC); ws.getMap('m').set('k', 'ws'); ws.commit(); const ann = await openDoc('ann:doc-1'); ann.getMap('annotations').set('uid-1', { page: 1 }); ann.commit(); await reload();
expect((await openDoc(WORKSPACE_DOC)).getMap('m').get('k')).toBe('ws'); expect((await openDoc('ann:doc-1')).getMap('annotations').get('uid-1')).toEqual({ page: 1 }); expect((await openDoc(WORKSPACE_DOC)).getMap('annotations').size).toBe(0); });
it('meta and sync state roundtrip', async () => { await db.setMeta('bootstrapped', true); expect(await db.getMeta('bootstrapped')).toBe(true); await db.putSyncState({ key: 'http:workspace', cursor: 42 }); expect((await db.getSyncState('http:workspace'))?.cursor).toBe(42); });});