feat(cameras): crossing line editor in Setup (A→B / B→A / A↔B)
Our own line-crossing rule for cameras that have none: draw a segment over the camera's live picture, sides A and B, and pick which crossing counts. Stored as camera config.crossing (CrossingLine in @parking/shared, points normalised to the frame, side A on the screen-left of p1→p2), validated on save; null clears it. Nothing consumes it yet (passage confirmation). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01RZgP5oMncSaVmKRKk1UvN4
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
||||
import { devices, eq, type Db } from "@parking/db";
|
||||
import { createTestDb } from "@parking/db/testing";
|
||||
import type { FastifyInstance } from "fastify";
|
||||
import { buildServer } from "../server.js";
|
||||
import { seedUser, login } from "../test-helpers.js";
|
||||
|
||||
// A camera's crossing line (passage confirmation) rides in its config as `crossing`. The
|
||||
// save validates it (the driver never reads it), and since an edit MERGES onto the stored
|
||||
// config, clearing it must be an explicit `null`.
|
||||
|
||||
let db: Db;
|
||||
let close: () => void;
|
||||
let app: FastifyInstance;
|
||||
const CAM = "cam-1";
|
||||
const line = { p1: { x: 0.1, y: 0.6 }, p2: { x: 0.9, y: 0.55 }, direction: "a-to-b" };
|
||||
|
||||
beforeEach(async () => {
|
||||
({ db, close } = createTestDb());
|
||||
db.insert(devices).values({
|
||||
id: CAM,
|
||||
category: "camera",
|
||||
driverId: "dahua",
|
||||
config: { host: "127.0.0.1", port: 80, username: "u", password: "p" },
|
||||
enabled: true,
|
||||
}).run();
|
||||
app = await buildServer({ db });
|
||||
await app.ready();
|
||||
auth = undefined;
|
||||
});
|
||||
afterEach(async () => {
|
||||
await app.close();
|
||||
close();
|
||||
});
|
||||
|
||||
let auth: { cookie: string; csrf: string } | undefined;
|
||||
async function edit(config: Record<string, unknown>) {
|
||||
if (!auth) {
|
||||
await seedUser(db, { username: "admin", password: "pw" });
|
||||
auth = await login(app, "admin", "pw");
|
||||
}
|
||||
return app.inject({
|
||||
method: "PATCH",
|
||||
url: `/api/setup/assign/${CAM}`,
|
||||
headers: { cookie: auth.cookie, "x-csrf-token": auth.csrf },
|
||||
payload: { config: { host: "127.0.0.1", port: 80, username: "u", password: "p", ...config } },
|
||||
});
|
||||
}
|
||||
const stored = () => db.select().from(devices).where(eq(devices.id, CAM)).get()!.config as Record<string, unknown>;
|
||||
|
||||
describe("camera crossing line on save", () => {
|
||||
it("stores a valid line", async () => {
|
||||
const res = await edit({ crossing: line });
|
||||
expect(res.statusCode).toBe(200);
|
||||
expect(stored().crossing).toEqual(line);
|
||||
});
|
||||
|
||||
it("keeps the stored line when the edit doesn't mention it, and clears it on null", async () => {
|
||||
await edit({ crossing: line });
|
||||
await edit({});
|
||||
expect(stored().crossing).toEqual(line);
|
||||
const res = await edit({ crossing: null });
|
||||
expect(res.statusCode).toBe(200);
|
||||
expect(stored()).not.toHaveProperty("crossing");
|
||||
});
|
||||
|
||||
it("rejects an invalid line and leaves the stored config alone", async () => {
|
||||
const res = await edit({ crossing: { ...line, p2: { x: 1.4, y: 0.5 } } });
|
||||
expect(res.statusCode).toBe(400);
|
||||
expect(stored()).not.toHaveProperty("crossing");
|
||||
});
|
||||
});
|
||||
@@ -16,7 +16,7 @@ import {
|
||||
type DeviceCategory,
|
||||
type DeviceConfig,
|
||||
} from "@parking/devices";
|
||||
import { reasonPayload } from "@parking/shared";
|
||||
import { parseCrossingLine, reasonPayload } from "@parking/shared";
|
||||
import { requirePermission } from "../auth.js";
|
||||
import type { EventLog } from "../event-log.js";
|
||||
import { backendIpCandidates, backendIpForDevice, backendPort } from "../net.js";
|
||||
@@ -147,6 +147,17 @@ async function configureDevice(
|
||||
// webPasswordCurrent is an input-only credential (the OLD password used to
|
||||
// authorize the change) — never persist it as typed.
|
||||
delete fullConfig.webPasswordCurrent;
|
||||
// Camera crossing line (passage confirmation): validated here, since the driver doesn't
|
||||
// read it. `null` clears it — the merge above would otherwise keep the stored line.
|
||||
if ("crossing" in config) {
|
||||
if (config.crossing === null) {
|
||||
delete fullConfig.crossing;
|
||||
} else {
|
||||
const line = parseCrossingLine(config.crossing);
|
||||
if (!line) return { error: { code: 400, message: "invalid crossing line" } };
|
||||
fullConfig.crossing = { ...line };
|
||||
}
|
||||
}
|
||||
// Residual-risk warnings from device hardening (shown to the admin; the
|
||||
// save still succeeds — these are "configured, but note X" advisories).
|
||||
const hardenWarnings: string[] = [];
|
||||
|
||||
Reference in New Issue
Block a user