import { describe, it, expect, beforeAll, afterAll } from "vitest"; import { eq } from "drizzle-orm"; import { startTestServer, createBoard, join, type TestServer } from "./helpers.js"; import { runCleanup } from "../src/app.js"; import { db, schema } from "../src/db/index.js"; let server: TestServer; beforeAll(async () => { server = await startTestServer(); }); afterAll(async () => { await server.close(); }); describe("Autodestrucción", () => { it("borra boards caducados y avisa a la room", async () => { const board = await createBoard(server.url, { ttlHours: 1 }); // forzamos expires_at al pasado await db .update(schema.boards) .set({ expiresAt: new Date(Date.now() - 1000) }) .where(eq(schema.boards.id, board.boardId)); const alice = await join(server.url, board.boardId, "alice", "Alice"); const destroyed = new Promise((resolve) => alice.socket.once("board:destroyed", () => resolve()), ); const deleted = await runCleanup(server.io); expect(deleted).toContain(board.boardId); await destroyed; // la room recibió el aviso // ya no existe en DB const rows = await db.select().from(schema.boards).where(eq(schema.boards.id, board.boardId)); expect(rows).toHaveLength(0); alice.socket.close(); }); it("no borra boards sin caducidad ni los aún vigentes", async () => { const persistent = await createBoard(server.url, { ttlHours: null }); const future = await createBoard(server.url, { ttlHours: 24 }); const deleted = await runCleanup(server.io); expect(deleted).not.toContain(persistent.boardId); expect(deleted).not.toContain(future.boardId); }); });