import { 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('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('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('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('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); }); });