// author: simon-pierre boucher import { afterAll, beforeAll, describe, expect, it } from "vitest"; import type { FastifyInstance } from "fastify"; import { buildApp } from "./app.js"; let app: FastifyInstance; beforeAll(async () => { app = buildApp(); await app.ready(); }); afterAll(async () => { await app.close(); }); describe("api", () => { it("serves a shallow healthz", async () => { const res = await app.inject({ method: "GET", url: "/healthz" }); expect(res.statusCode).toBe(200); expect(res.json()).toMatchObject({ success: true, data: { status: "ok" } }); }); it("rejects an invalid scrape body with ERR_INVALID_URL and a requestId", async () => { const res = await app.inject({ method: "POST", url: "/v1/scrape", payload: { url: "not-a-url" } }); expect(res.statusCode).toBe(400); const body = res.json(); expect(body.success).toBe(false); expect(body.error.code).toBe("ERR_INVALID_URL"); expect(body.requestId).toMatch(/^tdr_req_/); expect(res.headers["x-request-id"]).toMatch(/^tdr_req_/); }); it("rejects unknown fields (strict schema)", async () => { const res = await app.inject({ method: "POST", url: "/v1/scrape", payload: { url: "https://example.com", bogus: true }, }); expect(res.statusCode).toBe(400); }); });