import { afterEach, beforeEach, describe, expect, it } from "vitest"; import type { FastifyInstance } from "fastify"; import { createTestDb } from "@parking/db/testing"; import { ledgerEvents, eq, type Db } from "@parking/db"; import { buildServer } from "../server.js"; import { login, makeLog, minutesAgo, seedTariff, seedUser } from "../test-helpers.js"; // HTTP integration for the FORGIVEN exit (wiki/decisions/forgiven-exit.md): the route is // gated on `barrier:create` (admin by default, grantable per role), needs an open shift // and a reason, and signs a $0 payment carrying the forgone fee + the exit. Drives the // REAL app over a fresh in-memory DB via app.inject. let db: Db; let close: () => void; let app: FastifyInstance; beforeEach(async () => { const t = createTestDb(); db = t.db; close = t.close; app = await buildServer({ db }); await app.ready(); seedTariff(db, { pricePerIncrementMinor: 10000, incrementMin: 60 }); }); afterEach(async () => { await app.close(); close(); }); async function as(opts: { username: string; roleId?: string; permissions?: Parameters[1]["permissions"] }) { const { username, password } = await seedUser(db, opts); const { cookie, csrf } = await login(app, username, password); return { cookie, "x-csrf-token": csrf }; } async function enter(identity: string) { await makeLog(db).append({ type: "vehicle_entry", direction: "entry", identity, occurredAt: minutesAgo(90), payload: null }); } function events(identity: string) { return db.select().from(ledgerEvents).where(eq(ledgerEvents.identity, identity)).all(); } describe("POST /api/barrier/forgive", () => { it("is refused without barrier:create (an operator who can take payment but was not granted it)", async () => { const h = await as({ username: "op", roleId: "operator", permissions: ["payment:create", "session:read", "shift:create"] }); await app.inject({ method: "POST", url: "/api/shift/open", headers: h, payload: {} }); await enter("T1"); const r = await app.inject({ method: "POST", url: "/api/barrier/forgive", headers: h, payload: { identity: "T1", note: "friend" } }); expect(r.statusCode).toBe(403); expect(events("T1").map((e) => e.type)).toEqual(["vehicle_entry"]); }); it("a role the owner granted barrier:create can forgive; the reason is optional; a shift must be open", async () => { const h = await as({ username: "trusted", roleId: "senior", permissions: ["payment:create", "session:read", "shift:create", "shift:read", "barrier:create"] }); await enter("T1"); // No shift yet → the money-path gate refuses. let r = await app.inject({ method: "POST", url: "/api/barrier/forgive", headers: h, payload: { identity: "T1", note: "x" } }); expect(r.statusCode).toBe(409); expect(r.json()).toMatchObject({ code: "no_shift" }); await app.inject({ method: "POST", url: "/api/shift/open", headers: h, payload: {} }); // Forgiven: $0 payment with the fee as forgone, then the exit. No relay configured → // ok:true, opened:false with the open-by-hand reason (the ledger side is complete). r = await app.inject({ method: "POST", url: "/api/barrier/forgive", headers: h, payload: { identity: "T1", note: "known customer, lost ticket" } }); expect(r.statusCode).toBe(200); expect(r.json()).toMatchObject({ ok: true, opened: false, forgoneMinor: 20000 }); const types = events("T1").map((e) => e.type); expect(types).toEqual(["vehicle_entry", "payment", "anomaly", "vehicle_exit"]); const payment = events("T1").find((e) => e.type === "payment")!; expect(payment.payload).toMatchObject({ amountMinor: 0, forgoneMinor: 20000, operator: "trusted", reasonCode: "exit.forgiven" }); // The Z-report carries it as its own line, and the takings stay at zero. const rep = (await app.inject({ method: "GET", url: "/api/shift/report", headers: h })).json() as Record; expect(rep).toMatchObject({ cashTotalMinor: 0, cardTotalMinor: 0, forgoneTotalMinor: 20000, forgoneCount: 1 }); }); it("no ticket, no reason: admin opens the exit for a car with no session → anomaly only, readable sentence", async () => { const h = await as({ username: "boss", roleId: "admin" }); await app.inject({ method: "POST", url: "/api/shift/open", headers: h, payload: {} }); const r = await app.inject({ method: "POST", url: "/api/barrier/forgive", headers: h, payload: {} }); expect(r.statusCode).toBe(200); const all = db.select().from(ledgerEvents).all(); const anomaly = all.find((e) => e.type === "anomaly")!; expect(anomaly.identity).toMatch(/^forgiven:/); expect(anomaly.payload).toMatchObject({ reasonCode: "exit.forgiven.noSession", operator: "boss" }); expect((anomaly.payload as { reason: string }).reason).toContain("(no reason given)"); expect(all.filter((e) => e.type === "payment" || e.type === "vehicle_exit")).toHaveLength(0); }); });