import { afterEach, describe, expect, it, vi } from "vitest"; import { createTestDb } from "@parking/db/testing"; import { buildServer } from "./server.js"; // Request logging shape (2026-10-08 noise cut, wiki/concepts/app-logs.md §Noise cut): // - /health logs NOTHING (Docker probes it every 30 s — it was half a quiet booth's log), // - every other request logs ONE line "METHOD url → status Nms" (not pino's two), // - a handler failure still logs the error with its stack, because // `disableRequestLogging` also silences Fastify's default error-handler logging. // The logger's stream is process.stdout (pinoDbStream tees warn+ into app_logs), so // capture stdout writes and look at the JSON lines. const lines: Record[] = []; let restore: (() => void) | null = null; function captureStdout() { const spy = vi.spyOn(process.stdout, "write").mockImplementation(((chunk: unknown) => { const text = typeof chunk === "string" ? chunk : Buffer.from(chunk as Uint8Array).toString("utf8"); for (const l of text.split("\n")) { if (!l.trim()) continue; try { lines.push(JSON.parse(l)); } catch { /* non-JSON stdout noise — ignore */ } } return true; }) as typeof process.stdout.write); restore = () => spy.mockRestore(); } afterEach(() => { restore?.(); restore = null; lines.length = 0; }); const msgs = () => lines.map((l) => String(l.msg ?? "")); describe("request logging", () => { it("/health is silent; other requests log exactly one line; 5xx keeps the error", async () => { const t = createTestDb(); captureStdout(); // vitest.config forces LOG_LEVEL=silent for every test; this one needs the real level. const prevLevel = process.env.LOG_LEVEL; process.env.LOG_LEVEL = "info"; const app = await buildServer({ db: t.db }); process.env.LOG_LEVEL = prevLevel; app.get("/boom", async () => { throw new Error("kaboom"); }); await app.ready(); try { lines.length = 0; // drop startup lines expect((await app.inject({ method: "GET", url: "/health" })).statusCode).toBe(200); expect(msgs().filter((m) => m.includes("/health"))).toEqual([]); expect((await app.inject({ method: "GET", url: "/api/nope" })).statusCode).toBe(404); const notFound = msgs().filter((m) => m.includes("/api/nope")); expect(notFound).toHaveLength(1); expect(notFound[0]).toMatch(/^GET \/api\/nope → 404 \d+ms$/); expect(msgs().some((m) => /incoming request|request completed/.test(m))).toBe(false); expect((await app.inject({ method: "GET", url: "/boom" })).statusCode).toBe(500); const boom = lines.filter((l) => String(l.msg).includes("/boom")); // one error line (with the err object) + the one-line request summary expect(boom.map((l) => [l.level, String(l.msg).replace(/\d+ms/, "Nms")])).toEqual([ ["error", "GET /boom failed: kaboom"], ["info", "GET /boom → 500 Nms"], ]); expect((boom[0]!.err as { message?: string })?.message).toBe("kaboom"); } finally { await app.close(); t.close(); } }); });