import { expect } from "chai"; import { DPT17 } from "../src/DPT17" import { BufferLengthError } from "../src/errors/BufferLengthError"; import { InvalidValueError } from "../src/errors/InvalidValueError"; import { compareBuffers } from "./util" describe("Test DPT017", (): void => { let dpt = new DPT17(); it("Decode buffer acceptable", async function () { let value = 0x24 let buffer = Buffer.from([0x24]) const decoded = dpt.decoder(buffer) expect(decoded).is.equal(value); }); it("Decode empty buffer", async function () { let bufferEmpty = Buffer.from([]) var testFunction = function () { const value = dpt.decoder(bufferEmpty) } expect(testFunction).to.throw(BufferLengthError); }); it("Decode oversized buffer", async function () { let bufferBiggerSize = Buffer.from([0x20, 0x15]) var testFunction = function () { const value = dpt.decoder(bufferBiggerSize) } expect(testFunction).to.throw(BufferLengthError); }); /* Encoder tests */ it("encode valid", async function () { let value = 0x15 let buffer = Buffer.from([0x15]) expect(compareBuffers(dpt.encoder(value), buffer)).is.true; }); it("encode undefined", async function () { var testFunction = function () { const value = dpt.encoder(undefined) } expect(testFunction).to.throw(InvalidValueError); }); it("encode invalid value", async function () { const value = 0x80 var testFunction = function () { const result = dpt.encoder(value) } expect(testFunction).to.throw(InvalidValueError); }); });