import { expect } from "chai"; import { DPT18, DPT18Result } from "../src/DPT18" import { BufferLengthError } from "../src/errors/BufferLengthError"; import { InvalidValueError } from "../src/errors/InvalidValueError"; import { compareBuffers } from "./util" describe("Test DPT018", (): void => { let dpt = new DPT18(); it("Decode correct buffer", async function () { let buffer = Buffer.from([0x80 + 0x21]) const decoded = dpt.decoder(buffer) expect(decoded.activateLearn).is.equal(1); expect(decoded.sceneNumber).is.equal(0x21); }); 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 () { const value: DPT18Result = { activateLearn: 1, sceneNumber: 0x11 } const buffer = Buffer.from([0x80 + 0x11]) 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_invalidActivateLearn = function () { let value: DPT18Result = { activateLearn: 2, sceneNumber: 0x11 } const result = dpt.encoder(value) } var testFunction_invalidSceneNumber = function () { let value: DPT18Result = { activateLearn: 0, sceneNumber: 64 } const result = dpt.encoder(value) } expect(testFunction_invalidActivateLearn).to.throw(InvalidValueError); expect(testFunction_invalidSceneNumber).to.throw(InvalidValueError); }); });