import { afterEach, beforeEach, describe, expect, it } from "vitest"; import { createTestDb } from "@parking/db/testing"; import { ledgerEvents, siteConfig, subscriptions, type Db } from "@parking/db"; import { getOccupancy, occupancyCount, reservedSubscriberSpots } from "./occupancy.js"; // Occupancy is a FOLD over the signed ledger, never a stored counter. These tests // pin: the entries-minus-exits count, the capacity/full gate, and the reserved- // subscriber-spots model (its trickiest invariant — never double-count a parked // subscriber, and never gate the subscriber's own entry). let db: Db; let close: () => void; beforeEach(() => { const t = createTestDb(); db = t.db; close = t.close; }); afterEach(() => close()); // Insert a ledger row directly (these fns read raw rows; signing is event-log's job). let idx = 0; function entry(identity: string, payload?: Record) { idx += 1; db.insert(ledgerEvents).values({ id: `e${idx}`, index: idx, type: "vehicle_entry", direction: "entry", identity, payload: payload ?? null, occurredAt: new Date().toISOString(), signature: "x", keyId: "test", }).run(); } function exit(identity: string) { idx += 1; db.insert(ledgerEvents).values({ id: `e${idx}`, index: idx, type: "vehicle_exit", direction: "exit", identity, payload: null, occurredAt: new Date().toISOString(), signature: "x", keyId: "test", }).run(); } function voidEvt(identity: string) { idx += 1; db.insert(ledgerEvents).values({ id: `e${idx}`, index: idx, type: "void", identity, payload: { sessionRef: identity, voidReason: "misprint" }, occurredAt: new Date().toISOString(), signature: "x", keyId: "test", }).run(); } function setSite(v: Partial) { db.insert(siteConfig).values({ id: 1, ...v }).onConflictDoUpdate({ target: siteConfig.id, set: v }).run(); } describe("occupancyCount", () => { beforeEach(() => { idx = 0; }); it("is 0 with no events", () => { expect(occupancyCount(db)).toBe(0); }); it("counts open sessions (entries minus matching exits)", () => { entry("A"); entry("B"); entry("C"); exit("B"); expect(occupancyCount(db)).toBe(2); }); it("a re-entry after exit counts again", () => { entry("A"); exit("A"); entry("A"); expect(occupancyCount(db)).toBe(1); }); it("a voided (cancelled) entry does NOT count inside", () => { entry("A"); entry("B"); voidEvt("B"); // B's ticket was a misprint — cancelled expect(occupancyCount(db)).toBe(1); }); }); describe("getOccupancy — capacity + full gate", () => { beforeEach(() => { idx = 0; }); it("uncapped: never full, free/effectiveFree null", () => { setSite({ capacity: null }); entry("A"); const o = getOccupancy(db); expect(o.full).toBe(false); expect(o.free).toBeNull(); expect(o.effectiveFree).toBeNull(); }); it("capped: full when count reaches capacity", () => { setSite({ capacity: 2 }); entry("A"); expect(getOccupancy(db).full).toBe(false); entry("B"); const o = getOccupancy(db); expect(o.full).toBe(true); expect(o.free).toBe(0); }); }); describe("reservedSubscriberSpots", () => { beforeEach(() => { idx = 0; }); function addSub(id: string, opts: Partial = {}) { db.insert(subscriptions).values({ id, status: "active", quantity: 1, period: "month", ...opts }).run(); } it("is 0 when the toggle is off (default)", () => { setSite({ capacity: 10, reserveSubscriberSpots: false }); addSub("s1", { quantity: 2 }); expect(reservedSubscriberSpots(db)).toBe(0); }); it("holds quantity spots for an active, not-parked subscription", () => { setSite({ capacity: 10, reserveSubscriberSpots: true }); addSub("s1", { quantity: 2 }); expect(reservedSubscriberSpots(db)).toBe(2); }); it("does NOT double-count a subscriber already parked (holds only the rest)", () => { setSite({ capacity: 10, reserveSubscriberSpots: true }); addSub("s1", { quantity: 2 }); // One of the family's two cars is inside (occurrence entry carries permitId = sub id). entry("SUBSESS-1", { permitId: "s1" }); expect(reservedSubscriberSpots(db)).toBe(1); // 2 quantity − 1 inside }); it("ignores suspended/revoked and out-of-window subscriptions", () => { setSite({ capacity: 10, reserveSubscriberSpots: true }); addSub("active", { quantity: 1 }); addSub("suspended", { quantity: 5, status: "suspended" }); addSub("expired", { quantity: 5, validTo: "2000-01-01T00:00:00.000Z" }); expect(reservedSubscriberSpots(db)).toBe(1); }); }); describe("getOccupancy — reserved tightens the transient gate", () => { beforeEach(() => { idx = 0; }); it("transient sees full once count + reserved ≥ capacity", () => { setSite({ capacity: 3, reserveSubscriberSpots: true }); db.insert(subscriptions).values({ id: "s1", status: "active", quantity: 2, period: "month" }).run(); entry("A"); // 1 inside + 2 reserved = 3 ≥ capacity 3 const o = getOccupancy(db); expect(o.reserved).toBe(2); expect(o.effectiveFree).toBe(0); expect(o.full).toBe(true); }); });