You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
2.0 KiB
TypeScript

import Database, { type Database as SqliteDatabase } from 'bun:sqlite';
import { initializeDatabase } from '../../src/db';
// Servicios opcionales para inyección de DB en tests.
// Importamos con nombres existentes en la base de código para respetar convenciones.
import { TaskService } from '../../src/tasks/service';
import { CommandService } from '../../src/services/command';
import { ResponseQueue } from '../../src/services/response-queue';
import { IdentityService } from '../../src/services/identity';
import { GroupSyncService } from '../../src/services/group-sync';
import { RemindersService } from '../../src/services/reminders';
/**
* Crea una DB en memoria y aplica initializeDatabase() con todas las migraciones.
*/
export function makeMemDb(): SqliteDatabase {
const memdb = new Database(':memory:');
initializeDatabase(memdb);
return memdb;
}
/**
* Inyecta la instancia de DB en los servicios que la exponen como propiedad estática.
* Pensado para usarse en beforeAll/beforeEach de tests que usan estos servicios.
*/
export function injectAllServices(db: SqliteDatabase): void {
try { (TaskService as any).dbInstance = db; } catch {}
try { (CommandService as any).dbInstance = db; } catch {}
try { (ResponseQueue as any).dbInstance = db; } catch {}
try { (IdentityService as any).dbInstance = db; } catch {}
try { (GroupSyncService as any).dbInstance = db; } catch {}
try { (RemindersService as any).dbInstance = db; } catch {}
}
/**
* Restablece estado global/cachés en servicios entre tests.
* Best-effort: solo llama si existen los métodos.
*/
export function resetServices(): void {
try { (IdentityService as any).clearCache?.(); } catch {}
try { (GroupSyncService as any).clearCaches?.(); } catch {}
try { (CommandService as any).resetForTests?.(); } catch {}
try { (ResponseQueue as any).resetForTests?.(); } catch {}
try { (RemindersService as any).resetForTests?.(); } catch {}
}
/**
* Nota: en Etapa 1 añadiremos seedAllowed() aquí cuando exista AllowedGroups.
*/