Files
parking_solution/apps/server/src/relay-pulse.test.ts
T
julian 9332802e5d feat(server): log every relay pulse and echo it to device_events (kind relay)
One pulse path, pulseBarrier (relay-pulse.ts), for all six callers: entry
button/operator mint, reader and booth exits, subscription opens, manual
(forgiven) exits and the setup relay test. Each pulse writes an info line
naming relay, controller, direction, identity and operator, and inserts a
device_events row of kind "relay" for success and failure alike; failures
are still re-thrown so each flow keeps its own anomaly handling.

Prompted by the 2026-10-09 park-2 "barrier opened by itself" claim: the
ledger showed the why, but nothing recorded the moment a relay was actually
driven. The signed ledger stays the authority; this is unsigned telemetry.

Also git-ignores tmp/ (site snapshots pulled for incident review).
2026-10-11 18:17:25 +02:00

87 lines
2.9 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { createTestDb } from "@parking/db/testing";
import { devices, deviceEvents as deviceEventsTable, type Db, type DeviceRow } from "@parking/db";
import { pulseBarrier } from "./relay-pulse.js";
import type { FastifyBaseLogger } from "fastify";
const silentLogger = { info() {}, warn() {}, error() {}, debug() {} } as unknown as FastifyBaseLogger;
// Every barrier pulse — success or failure — must leave an UNSIGNED device_events row
// (kind "relay") naming the relay, the direction, the identity and who asked, so a
// "did the server open the barrier?" question is answerable from telemetry alone
// (park-2, 2026-10-09). A failure is recorded AND re-thrown (each flow keeps its own
// anomaly handling).
let db: Db;
let close: () => void;
let controller: DeviceRow;
beforeEach(() => {
const t = createTestDb();
db = t.db;
close = t.close;
db.insert(devices)
.values({
id: "ctl-1",
category: "access",
driverId: "stub-access",
config: { relays: [{ relay: 2, direction: "exit" }] },
enabled: true,
})
.run();
controller = db.select().from(devices).get()!;
});
afterEach(() => close());
function relayRows() {
return db
.select()
.from(deviceEventsTable)
.all()
.filter((r) => r.kind === "relay")
.map((r) => ({ deviceId: r.deviceId, category: r.category, detail: r.detail as Record<string, unknown> }));
}
describe("pulseBarrier", () => {
it("pulses the relay and records a successful relay event with the context", async () => {
const fired: number[] = [];
const access = { pulseOpen: async (door: number) => void fired.push(door) };
await pulseBarrier(db, silentLogger, access, { controller, relay: 2 }, {
direction: "exit",
identity: "forgiven:abc",
source: "booth",
operator: "admin",
});
expect(fired).toEqual([2]);
const rows = relayRows();
expect(rows).toHaveLength(1);
expect(rows[0].deviceId).toBe("ctl-1");
expect(rows[0].category).toBe("access");
expect(rows[0].detail).toMatchObject({
driverId: "stub-access",
relay: 2,
direction: "exit",
identity: "forgiven:abc",
source: "booth",
operator: "admin",
ok: true,
});
expect(typeof rows[0].detail.tookMs).toBe("number");
});
it("records a failed pulse with the error, then re-throws it", async () => {
const access = {
pulseOpen: async () => {
throw new Error("timeout");
},
};
await expect(
pulseBarrier(db, silentLogger, access, { controller, relay: 2 }, { direction: "exit", identity: "T1", source: "ticket" }),
).rejects.toThrow("timeout");
const rows = relayRows();
expect(rows).toHaveLength(1);
expect(rows[0].detail).toMatchObject({ relay: 2, identity: "T1", source: "ticket", ok: false, error: "timeout" });
expect(rows[0].detail.operator).toBeUndefined();
});
});