import { expect } from "chai"; import { DPT2 } from "../src/DPT2" import { BufferLengthError } from "../src/errors/BufferLengthError"; import { InvalidValueError } from "../src/errors/InvalidValueError"; import { compareBuffers } from "./util" let bufferZeroZero = Buffer.from([0]) let bufferZeroOne = Buffer.from([1]) let bufferOneZero = Buffer.from([2]) let bufferOneOne = Buffer.from([3]) let bufferEmpty = Buffer.from([]) let bufferBiggerSize = Buffer.from([1, 0]) describe("Test DPT002", (): void => { let dpt = new DPT2(); it("Decode buffer00", async function () { const value = dpt.decoder(bufferZeroZero) expect(value.data).is.false; expect(value.priority).is.false; }); it("Decode buffer01", async function () { const value = dpt.decoder(bufferZeroOne) expect(value.data).is.true; expect(value.priority).is.false; }); it("Decode buffer10", async function () { const value = dpt.decoder(bufferOneZero) expect(value.data).is.false; expect(value.priority).is.true; }); it("Decode buffer11", async function () { const value = dpt.decoder(bufferOneOne) expect(value.data).is.true; expect(value.priority).is.true; }); it("Decode empty buffer", async function () { var testFunction = function () { const value = dpt.decoder(bufferEmpty) } expect(testFunction).to.throw(BufferLengthError); }); it("Decode oversized buffer", async function () { var testFunction = function () { const value = dpt.decoder(bufferBiggerSize) } expect(testFunction).to.throw(BufferLengthError); }); it("encode true", async function () { expect(compareBuffers(dpt.encoder({ data: false, priority: false }), bufferZeroZero)).is.true; expect(compareBuffers(dpt.encoder({ data: false, priority: true }), bufferOneZero)).is.true; expect(compareBuffers(dpt.encoder({ data: true, priority: false }), bufferZeroOne)).is.true; expect(compareBuffers(dpt.encoder({ data: true, priority: true }), bufferOneOne)).is.true; }); it("encode invalid", async function () { var testFunction = function () { const value = dpt.encoder(undefined) } expect(testFunction).to.throw(InvalidValueError); }); });