import Database from 'bun:sqlite'; import { describe, it, expect } from 'bun:test'; import { getDb, setDb, withDb, DbNotConfiguredError } from '../../../src/db/locator'; describe('db/locator', () => { it('getDb lanza si no está configurada', () => { expect(() => getDb()).toThrow(DbNotConfiguredError); }); it('setDb y getDb devuelven la misma instancia', () => { const db = new Database(':memory:'); setDb(db); expect(getDb()).toBe(db); try { db.close(); } catch {} }); it('withDb soporta callback síncrono', () => { const db = new Database(':memory:'); setDb(db); const result = withDb(d => { expect(d).toBe(db); return 42; }); expect(result).toBe(42); try { db.close(); } catch {} }); it('withDb soporta callback asíncrono', async () => { const db = new Database(':memory:'); setDb(db); const result = await withDb(async d => { expect(d).toBe(db); await Promise.resolve(); return 'ok'; }); expect(result).toBe('ok'); try { db.close(); } catch {} }); it('permitir sobrescritura de setDb', () => { const db1 = new Database(':memory:'); const db2 = new Database(':memory:'); setDb(db1); setDb(db2); const got = getDb(); expect(got).toBe(db2); try { db1.close(); } catch {} try { db2.close(); } catch {} }); });