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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';import { IDBFactory } from 'fake-indexeddb';import * as db from './db';import { resetDbForTests } from './db';import { ensureBootstrapped, resetBootstrapForTests } from './bootstrap';
// bootstrap wires the sync engine and identity together; we stub those and the// http transport so the test drives only the branch logic against the real// (faked) IndexedDB meta store.const manifest = vi.fn();const startSync = vi.fn();const syncNow = vi.fn(async () => undefined);const registerSeedDevice = vi.fn(async () => undefined);
vi.mock('../sync/engine', () => ({ startSync: (peer: unknown) => startSync(peer), syncNow: () => syncNow(),}));vi.mock('../sync/transports/http', () => ({ httpPeer: () => ({ manifest: () => manifest() }),}));vi.mock('./identity', () => ({ deviceId: async () => 'device-1', registerSeedDevice: () => registerSeedDevice(),}));
describe('ensureBootstrapped', () => { beforeEach(() => { globalThis.indexedDB = new IDBFactory(); resetDbForTests(); resetBootstrapForTests(); manifest.mockReset(); startSync.mockReset(); syncNow.mockReset(); registerSeedDevice.mockReset(); }); afterEach(() => { resetDbForTests(); resetBootstrapForTests(); });
it('starts sync straight away on an already-bootstrapped device, skipping the pull', async () => { await db.setMeta('bootstrapped', true); await ensureBootstrapped(); expect(startSync).toHaveBeenCalledTimes(1); expect(manifest).not.toHaveBeenCalled(); expect(registerSeedDevice).not.toHaveBeenCalled(); });
it('pulls the server history, seeds the device, and marks bootstrapped on first run', async () => { manifest.mockResolvedValue([{ doc_id: 'workspace', latest_seq: 3 }]); await ensureBootstrapped(); expect(startSync).toHaveBeenCalledTimes(1); expect(syncNow).toHaveBeenCalledTimes(1); expect(registerSeedDevice).toHaveBeenCalledTimes(1); expect(await db.getMeta<boolean>('bootstrapped')).toBe(true); });
it('starts sync but does not sync when the server manifest is empty', async () => { manifest.mockResolvedValue([]); await ensureBootstrapped(); expect(startSync).toHaveBeenCalledTimes(1); expect(syncNow).not.toHaveBeenCalled(); expect(await db.getMeta<boolean>('bootstrapped')).toBe(true); });
it('stays unbootstrapped and retries on the next call when the server is unreachable', async () => { manifest.mockRejectedValueOnce(new Error('unreachable')); await ensureBootstrapped(); expect(registerSeedDevice).not.toHaveBeenCalled(); expect(await db.getMeta<boolean>('bootstrapped')).toBeUndefined();
// The failed run cleared the cached promise, so a retry runs again and can // succeed once the server is reachable. manifest.mockResolvedValue([]); await ensureBootstrapped(); expect(manifest).toHaveBeenCalledTimes(2); expect(await db.getMeta<boolean>('bootstrapped')).toBe(true); });
it('memoises the in-flight run so concurrent callers share one bootstrap', async () => { manifest.mockResolvedValue([]); await Promise.all([ensureBootstrapped(), ensureBootstrapped()]); expect(startSync).toHaveBeenCalledTimes(1); });});