import { expect } from "chai"; import { DPT232, DPT232Result } from "../src/DPT232" import { BufferLengthError } from "../src/errors/BufferLengthError"; import { InvalidValueError } from "../src/errors/InvalidValueError"; import { compareBuffers } from "./util" let value: DPT232Result = { red: 127, green: 200, blue: 20 } let buffer = Buffer.from([127, 200, 20]) let bufferEmpty = Buffer.from([]) let bufferBiggerSize = Buffer.from([0xff, 0xff, 0x63, 0xc0]) describe("Test DPT232", (): void => { let dpt = new DPT232(); it("Decode buffer acceptable", async function () { const v = dpt.decoder(buffer) expect(v.red).is.equal(value.red); expect(v.green).is.equal(value.green); expect(v.blue).is.equal(value.blue); }); 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); }); /* Encoder tests */ it("encode valid", async function () { expect(compareBuffers(dpt.encoder(value), buffer)).is.true; }); it("encode invalid", async function () { var testFunctionRedPlus = function () { const value = dpt.encoder({ red: 300, green: 0, blue: 0 }) } var testFunctionRedMinus = function () { const value = dpt.encoder({ red: -2, green: 0, blue: 0 }) } var testFunctionGreenPlus = function () { const value = dpt.encoder({ green: 300, red: 0, blue: 0 }) } var testFunctionGreenMinus = function () { const value = dpt.encoder({ green: -2, red: 0, blue: 0 }) } var testFunctionBluePlus = function () { const value = dpt.encoder({ blue: 300, green: 0, red: 0 }) } var testFunctionBlueMinus = function () { const value = dpt.encoder({ blue: -2, green: 0, red: 0 }) } expect(testFunctionRedPlus).to.throw(InvalidValueError); expect(testFunctionRedMinus).to.throw(InvalidValueError); expect(testFunctionGreenPlus).to.throw(InvalidValueError); expect(testFunctionGreenMinus).to.throw(InvalidValueError); expect(testFunctionBluePlus).to.throw(InvalidValueError); expect(testFunctionBlueMinus).to.throw(InvalidValueError); }); it("encode undefined", async function () { var testFunction = function () { const value = dpt.encoder(undefined) } expect(testFunction).to.throw(InvalidValueError); }); });