import { afterEach, describe, expect, it, vi } from "vitest"; // Reader health: push-only QR readers expose no TCP port, so liveness is an ICMP // ping of the (optional) configured IP. With no IP we must NOT claim "ready" (the old // stub did, hiding offline readers behind a green dot) — we report degraded instead. // icmpPing is mocked so the test is deterministic + offline. const icmpPing = vi.fn<(host: string, timeoutMs?: number) => Promise>(); vi.mock("./icmp.js", () => ({ icmpPing: (...a: [string, number?]) => icmpPing(...a) })); const { dingtianQrReaderDriver } = await import("./reader.js"); afterEach(() => { icmpPing.mockReset(); }); describe("QR reader healthCheck (ICMP liveness)", () => { it("with an IP that replies → ready", async () => { icmpPing.mockResolvedValue(true); const r = dingtianQrReaderDriver.create({ serial: "H05M2AFA", host: "10.0.10.7" }); expect(await r.healthCheck()).toEqual({ status: "ready", detail: "ping 10.0.10.7" }); expect(icmpPing).toHaveBeenCalledWith("10.0.10.7"); }); it("with an IP that does NOT reply → offline (this is the bug fix)", async () => { icmpPing.mockResolvedValue(false); const r = dingtianQrReaderDriver.create({ serial: "H05M2AFA", host: "10.0.10.7" }); expect(await r.healthCheck()).toEqual({ status: "offline", detail: "no ping reply from 10.0.10.7" }); }); it("with NO IP → degraded (never a false 'ready')", async () => { const r = dingtianQrReaderDriver.create({ serial: "H05M2AFA" }); const h = await r.healthCheck(); expect(h.status).toBe("degraded"); expect(icmpPing).not.toHaveBeenCalled(); // nothing to ping }); it("exposes an optional host field for monitoring", () => { const hostField = dingtianQrReaderDriver.configFields.find((f) => f.key === "host"); expect(hostField).toBeDefined(); expect(hostField!.required).toBe(false); // operation is push-by-serial; IP is monitor-only }); }); // --- DT-008 configuration over the vendor UDP protocol ----------------------------- // The protocol layer is mocked (it has its own fake-reader tests in // dingtian-qr-config.test.ts); here we check what the ADAPTER decides: which // preconditions it reports, what it writes to fix them, and how it points the reader at // the booth. Scenario values are park-2's real readers (WG26, server 10.0.10.101:3000). const qr = vi.hoisted(() => ({ readFirmware: vi.fn(), readParams: vi.fn(), writeSetting: vi.fn(), setNetwork: vi.fn(), subnetBroadcastFor: vi.fn(), discoverReaders: vi.fn(), })); vi.mock("./dingtian-qr-config.js", async (importOriginal) => { const real = await importOriginal(); return { ...real, ...qr }; }); const fw = { model: "doorqrcode", firmware: "ver_client1_1.1", chip: "v3s", hardware: "h42", serial: "HC009E84" }; const park2Params = { dhcp: 0, ip: "10.0.10.7", netmask: "255.255.255.0", gateway: "0.0.0.0", dns1: "0.0.0.0", dns2: "0.0.0.0", server: "10.0.10.101", serverPort: 3000, httpLanguage: 1, outputType: 6, // WG26 — the park-2 fault cardInputFormat: 1, lastAddEnter: 1, lastAddNewline: 0, qrPrefix: "", qrSuffix: "", restartInterval: 1, workMode: 0, buzzer: 0, highPulseUs: 2000, lowPulseUs: 400, baudrate: 9600, dataBits: 8, stopBits: 1, parity: 2, qrOutputFormat: 4, fillLight: 1, cardOutputFormat: 1, cardPrefix: "", cardSuffix: "", }; type Configurable = { checkPreconditions(): Promise<{ ok: boolean; issues: { key: string; fixable: boolean; message: string }[] }>; fixPreconditions(): Promise<{ ok: boolean; issues: { key: string }[] }>; configureInputPush(o: { host: string; port: number; pathBase: string; auth: { user: string; password: string } }): Promise; usesPushAuth?: boolean; }; const reader = (cfg: Record) => dingtianQrReaderDriver.create(cfg) as unknown as Configurable; const push = { pathBase: "/x", auth: { user: "u", password: "p" } }; afterEach(() => { for (const m of Object.values(qr)) m.mockReset(); }); describe("DT-008 preconditions (reader must be in HTTP mode, serial must match)", () => { it("WG26 → one FIXABLE issue naming the mode and the current target", async () => { qr.readFirmware.mockResolvedValue(fw); qr.readParams.mockResolvedValue(park2Params); const r = await reader({ serial: "HC009E84", host: "10.0.10.7" }).checkPreconditions(); expect(r.ok).toBe(false); expect(r.issues).toHaveLength(1); expect(r.issues[0]).toMatchObject({ key: "output-type", fixable: true }); expect(r.issues[0]!.message).toMatch(/WG26.*HTTP.*10\.0\.10\.101:3000/); }); it("typed serial ≠ reported serial → UNFIXABLE (scans would land on the wrong device)", async () => { qr.readFirmware.mockResolvedValue(fw); qr.readParams.mockResolvedValue({ ...park2Params, outputType: 3 }); const r = await reader({ serial: "HC00D3B0", host: "10.0.10.7" }).checkPreconditions(); expect(r.ok).toBe(false); expect(r.issues).toEqual([expect.objectContaining({ key: "serial-mismatch", fixable: false })]); expect(r.issues[0]!.message).toContain("HC009E84"); }); it("serial comparison ignores case/whitespace; HTTP mode → ok", async () => { qr.readFirmware.mockResolvedValue(fw); qr.readParams.mockResolvedValue({ ...park2Params, outputType: 3 }); expect(await reader({ serial: " hc009e84 ", host: "10.0.10.7" }).checkPreconditions()).toEqual({ ok: true, issues: [] }); }); it("no IP → ok with no issues (nothing to talk to; legacy vendor-tool setup)", async () => { expect(await reader({ serial: "HC009E84" }).checkPreconditions()).toEqual({ ok: true, issues: [] }); expect(qr.readParams).not.toHaveBeenCalled(); }); it("IP set but no UDP answer → UNFIXABLE 'unreachable' (save fails with a clear message)", async () => { qr.readFirmware.mockResolvedValue(null); qr.readParams.mockResolvedValue(null); const r = await reader({ serial: "HC009E84", host: "10.0.10.9" }).checkPreconditions(); expect(r.ok).toBe(false); expect(r.issues[0]).toMatchObject({ key: "unreachable", fixable: false }); expect(r.issues[0]!.message).toMatch(/10\.0\.10\.9.*UDP 24680/); }); it("fixPreconditions writes cmd 6 = 3 (HTTP) once and re-checks", async () => { qr.readFirmware.mockResolvedValue(fw); qr.readParams.mockResolvedValueOnce(park2Params).mockResolvedValue({ ...park2Params, outputType: 3 }); qr.writeSetting.mockResolvedValue(0); const r = await reader({ serial: "HC009E84", host: "10.0.10.7" }).fixPreconditions(); expect(qr.writeSetting).toHaveBeenCalledTimes(1); expect(qr.writeSetting).toHaveBeenCalledWith("10.0.10.7", 6, 3); expect(r.ok).toBe(true); }); it("fixPreconditions: a rejected / unacknowledged write throws (no silent 'fixed')", async () => { qr.readFirmware.mockResolvedValue(fw); qr.readParams.mockResolvedValue(park2Params); qr.writeSetting.mockResolvedValue(1); await expect(reader({ serial: "HC009E84", host: "10.0.10.7" }).fixPreconditions()).rejects.toThrow(/status 1/); qr.writeSetting.mockResolvedValue(null); await expect(reader({ serial: "HC009E84", host: "10.0.10.7" }).fixPreconditions()).rejects.toThrow(/no acknowledgement/); }); }); describe("DT-008 configureInputPush (point the reader at this booth)", () => { it("already pointing at us → no write at all", async () => { qr.readFirmware.mockResolvedValue(fw); qr.readParams.mockResolvedValue({ ...park2Params, outputType: 3 }); await reader({ serial: "HC009E84", host: "10.0.10.7" }).configureInputPush({ host: "10.0.10.101", port: 3000, ...push }); expect(qr.setNetwork).not.toHaveBeenCalled(); }); it("different target → set-network with the READER's serial + its own network block, then verified by polling (reader is silent right after)", async () => { qr.readFirmware.mockResolvedValue(fw); qr.readParams .mockResolvedValueOnce({ ...park2Params, outputType: 3, server: "10.0.10.203", serverPort: 8080 }) .mockResolvedValueOnce(null) // re-initialising its network: no answer to the first poll .mockResolvedValue({ ...park2Params, outputType: 3, server: "10.0.10.101", serverPort: 3000 }); qr.subnetBroadcastFor.mockReturnValue("10.0.10.255"); qr.setNetwork.mockResolvedValue(undefined); vi.useFakeTimers(); try { const p = reader({ serial: "hc009e84", host: "10.0.10.7" }).configureInputPush({ host: "10.0.10.101", port: 3000, ...push }); await vi.advanceTimersByTimeAsync(3000); // poll 1: silent, poll 2: new target await p; } finally { vi.useRealTimers(); } expect(qr.setNetwork).toHaveBeenCalledWith( { serial: "HC009E84", // as REPORTED, not as typed ip: "10.0.10.7", netmask: "255.255.255.0", gateway: "0.0.0.0", dns1: "0.0.0.0", dns2: "0.0.0.0", server: "10.0.10.101", serverPort: 3000, httpLanguage: 1, }, "10.0.10.255", ); }); it("reader did not apply the new target → throws (set-network has no ack; the re-read is the proof)", async () => { qr.readFirmware.mockResolvedValue(fw); qr.readParams.mockResolvedValue({ ...park2Params, outputType: 3, server: "10.0.10.203", serverPort: 8080 }); qr.subnetBroadcastFor.mockReturnValue("10.0.10.255"); qr.setNetwork.mockResolvedValue(undefined); vi.useFakeTimers(); try { const p = reader({ serial: "HC009E84", host: "10.0.10.7" }).configureInputPush({ host: "10.0.10.101", port: 3000, ...push }); const expectation = expect(p).rejects.toThrow(/did not apply server 10\.0\.10\.101:3000 \(still 10\.0\.10\.203:8080\)/); await vi.advanceTimersByTimeAsync(15_000); // polls once a second until the 12 s deadline await expectation; } finally { vi.useRealTimers(); } }); it("reader not on a directly attached subnet → throws before broadcasting", async () => { qr.readFirmware.mockResolvedValue(fw); qr.readParams.mockResolvedValue({ ...park2Params, outputType: 3 }); qr.subnetBroadcastFor.mockReturnValue(null); await expect( reader({ serial: "HC009E84", host: "10.0.10.7" }).configureInputPush({ host: "10.0.10.101", port: 80, ...push }), ).rejects.toThrow(/directly attached subnet/); expect(qr.setNetwork).not.toHaveBeenCalled(); }); it("declares usesPushAuth=false and pushesToBackend (the scan GET is identified by serial)", () => { expect(reader({ serial: "x" }).usesPushAuth).toBe(false); expect(dingtianQrReaderDriver.pushesToBackend).toBe(true); }); }); describe("DT-008 discover (LAN search → wizard rows)", () => { it("maps each reader to serial/host config + mode/target info", async () => { qr.discoverReaders.mockResolvedValue([ { serial: "HC009E84", ip: "10.0.10.7", netmask: "255.255.255.0", gateway: "0.0.0.0", dns1: "0.0.0.0", dns2: "0.0.0.0", server: "10.0.10.101", serverPort: 3000, httpLanguage: 1, replyFrom: "10.0.10.7" }, ]); qr.readFirmware.mockResolvedValue(fw); qr.readParams.mockResolvedValue(park2Params); const found = await (dingtianQrReaderDriver as unknown as { discover(): Promise }).discover(); expect(found).toEqual([ { id: "HC009E84", label: "HC009E84 @ 10.0.10.7", config: { serial: "HC009E84", host: "10.0.10.7" }, info: { output: "WG26", server: "10.0.10.101:3000", url: "/qa/mcardsea.jsp", cardFormat: "6H", firmware: "ver_client1_1.1" }, }, ]); }); });