test(server): Phase 1 — server-core suites (occupancy, pay, exit, shift)

Completes the anti-fraud/safety core coverage on a fresh in-memory DB:

- occupancy.test.ts (12): the ledger-fold count, the capacity/full gate, and the
  reserved-subscriber-spots model — never double-count a parked subscriber, reserve
  tightens only the TRANSIENT gate.
- pay-station.test.ts (12): quote math against the frozen tariff, the signed-payment
  side effect (+ chain verify), no-session / no-tariff errors, the booth lookup view,
  active-session listing.
- exit-flow.test.ts (9): the GATE — refuse unknown / unpaid / grace-expired (no exit
  signed); a paid-within-grace session signs the exit; the booth transient path has NO
  subscription bypass; a prepaid subscriber leaves via the assist (reopenBarrier) path.
- shift-service.test.ts (14): site-wide single-open invariant, the takings SPLIT by
  source (subscription sales vs out-of-window vs transient tickets), drawer carry-
  forward + cash_in/out vouchers, Z-report sign + listShifts read-back.
- entry-flow.test.ts (5): the exported validateTicketCode Luhn typo-guard. (The
  capacity-gate/print-hold/sign-before-open paths need device fakes — covered in the
  device + route phases.)

Adds test-helpers.ts (real EventLog, silent logger, tariff seeder). server 68/68 green.

Note: apps/vision has 2 PRE-EXISTING failures (test_app.py) — environment drift now
that fast_alpr + the ONNX model are installed (the "stub mode" assertions are stale).
Untouched here; to be fixed in the vision phase.

Claude-Session: https://claude.ai/code/session_01Xcm6ikLgGoCxxHrxtjkk5V
This commit is contained in:
2026-06-21 16:15:56 +02:00
parent 0985b86fa7
commit 5e9be16f65
6 changed files with 655 additions and 0 deletions
+63
View File
@@ -0,0 +1,63 @@
import { randomUUID } from "node:crypto";
import { tariffs, tariffVersions, type Db } from "@parking/db";
import type { TariffStructure } from "@parking/shared";
import type { FastifyBaseLogger } from "fastify";
import { EventLog } from "./event-log.js";
import { SoftwareSigner, buildVerifier } from "./signer.js";
// Shared scaffolding for server tests (NOT a *.test file, so it is not collected as a
// suite and stays out of shipped dist via the tsconfig test-exclude). Builds the real
// EventLog over a fresh test DB, a silent logger, and a minimal active tariff so the
// pay/exit flows have something to price against.
const SECRET = "test-event-signing-key-0123456789";
/** Real EventLog (real signer + per-keyId verifier) over a test DB. */
export function makeLog(db: Db): EventLog {
return new EventLog(db, new SoftwareSigner(SECRET), buildVerifier);
}
/** A logger that swallows everything — flows log liberally; tests don't care. */
export function silentLogger(): FastifyBaseLogger {
const noop = () => {};
const l: Record<string, unknown> = {
info: noop, warn: noop, error: noop, debug: noop, fatal: noop, trace: noop,
silent: noop, level: "silent",
};
l.child = () => l;
return l as unknown as FastifyBaseLogger;
}
/** A simple flat-rate V1 tariff: free under the entry grace, then a fixed price per
* increment, with a walk-back exit grace. Returns the tariffVersionId + currency. */
export function seedTariff(
db: Db,
opts: { pricePerIncrementMinor?: number; incrementMin?: number; gracePeriodEntryMin?: number; gracePeriodExitMin?: number; currency?: string; effectiveFrom?: string } = {},
): { tariffVersionId: string; currency: string } {
const tariffId = randomUUID();
const versionId = randomUUID();
const currency = opts.currency ?? "ALL";
const structure: TariffStructure = {
gracePeriodEntryMin: opts.gracePeriodEntryMin ?? 10,
incrementMin: opts.incrementMin ?? 60,
blocks: [{ uptoMin: null, priceMinorPerIncrement: opts.pricePerIncrementMinor ?? 10000 }],
dailyCapMinor: null,
lostTicketMinor: 50000,
gracePeriodExitMin: opts.gracePeriodExitMin ?? 15,
overstay: "reprice",
};
db.insert(tariffs).values({ id: tariffId, scope: "site", name: "Test" }).run();
db.insert(tariffVersions).values({
id: versionId,
tariffId,
effectiveFrom: opts.effectiveFrom ?? "2000-01-01T00:00:00.000Z",
currency,
structure: structure as unknown as Record<string, unknown>,
}).run();
return { tariffVersionId: versionId, currency };
}
/** ISO string `minutes` ago from now (for entries that should already owe a fee). */
export function minutesAgo(minutes: number): string {
return new Date(Date.now() - minutes * 60_000).toISOString();
}