import { describe, expect, it } from "vitest"; import { boxNumberFromTopic, idleCommand, offCommand, pickCommand, topics, } from "../src/lib/mqtt-topics.js"; describe("mqtt topic builders", () => { const t = topics("findr"); it("builds the command and state topics for a box", () => { expect(t.boxConfig(2)).toBe("findr/box/2/config"); expect(t.cmdLight(2)).toBe("findr/box/2/cmd/light"); expect(t.stateLight(2)).toBe("findr/box/2/state/light"); expect(t.ctrlOnline("abc")).toBe("findr/controller/abc/state/online"); expect(t.allBoxState()).toBe("findr/box/+/state/+"); expect(t.allControllerState()).toBe("findr/controller/+/state/+"); }); it("extracts the box number from a topic", () => { expect(boxNumberFromTopic("findr/box/7/state/light")).toBe(7); expect(boxNumberFromTopic("findr/box/x/state/light")).toBeNull(); expect(boxNumberFromTopic("other/box/7/state/light")).toBeNull(); }); it("respects a custom prefix", () => { const p = topics("lab"); expect(p.cmdLight(3)).toBe("lab/box/3/cmd/light"); expect(boxNumberFromTopic("lab/box/3/evt/button", "lab")).toBe(3); }); }); describe("light command builders", () => { it("pick lights exactly the given bins", () => { const cmd = pickCommand([{ cell: "D4", ledIndex: 27, mode: "take" }], { seconds: 30, requestId: "r1", }); expect(cmd).toEqual({ effect: "pick", bins: [{ cell: "D4", ledIndex: 27, mode: "take" }], seconds: 30, requestId: "r1", }); }); it("idle and off carry just the effect", () => { expect(idleCommand().effect).toBe("idle"); expect(offCommand().effect).toBe("off"); }); });