Repository Public
1import { afterAll, describe, expect, it } from "vitest";2import { Client, sleep, sql } from "./helpers";34/** Runs after every process was stopped and started again (scripts/test.sh). */5const anon = new Client();6afterAll(async () => { await sql.end(); });78describe("after a restart", () => {9 it("published sheets are still served", async () => {10 const [s] = await sql<{ slug: string }[]>`SELECT slug FROM sheets WHERE status = 'published' LIMIT 1`;11 expect((await anon.req(`/s/${s.slug}`)).status).toBe(200);12 });13 it("sessions are still valid (they live in the database, not in memory)", async () => {14 // The raw token is unknown here by design (only its hash is stored): check the rows survived and are usable.15 const [r] = await sql<{ n: number }[]>`SELECT count(*)::int AS n FROM sessions WHERE revoked_at IS NULL AND expires_at > now()`;16 expect(r.n).toBeGreaterThan(0);17 });18 it("a job left running by the dead worker is picked up again and finishes", async () => {19 for (let i = 0; i < 150; i++) {20 const [r] = await sql<{ n: number }[]>`SELECT count(*)::int AS n FROM generation_jobs WHERE status IN ('queued', 'running')`;21 if (r.n === 0) break;22 await sleep(200);23 }24 const [j] = await sql<{ status: string; attempts: number; locked_by: string | null }[]>`SELECT status, attempts, locked_by FROM generation_jobs WHERE kind = 'outline' ORDER BY created_at LIMIT 1`;25 expect(j.status).toBe("succeeded"); expect(j.attempts).toBe(2); expect(j.locked_by).toBeNull();26 });27 it("the worker reports itself alive", async () => {28 const [w] = await sql<{ age: number }[]>`SELECT extract(epoch FROM now() - beat_at)::float AS age FROM worker_heartbeat WHERE name = 'worker'`;29 expect(w.age).toBeLessThan(40);30 });31});32