import { expect } from "chai"; import { DPT237, DPT237Result } from "../src/DPT237" import { BufferLengthError } from "../src/errors/BufferLengthError"; import { InvalidValueError } from "../src/errors/InvalidValueError"; import { compareBuffers } from "./util" describe("Test DPT237", (): void => { let dpt = new DPT237(); it("Decode buffer acceptable", async function () { let value: DPT237Result = { readResponse: false, addressIndicator: false, daliAddress: 0, lampFailure: false, ballastFailure: false, convertorError: false } let buffer = Buffer.from([0x00, 0x00]) const decoded = dpt.decoder(buffer) expect(decoded.readResponse).is.equal(value.readResponse); expect(decoded.addressIndicator).is.equal(value.addressIndicator); expect(decoded.daliAddress).is.equal(value.daliAddress); expect(decoded.lampFailure).is.equal(value.lampFailure); expect(decoded.ballastFailure).is.equal(value.ballastFailure); expect(decoded.convertorError).is.equal(value.convertorError); }); it("Decode buffer acceptable true", async function () { let value: DPT237Result = { readResponse: true, addressIndicator: true, daliAddress: 63, lampFailure: true, ballastFailure: true, convertorError: true } let buffer = Buffer.from([0xff, 0xff]) const decoded = dpt.decoder(buffer) expect(decoded.readResponse).is.equal(value.readResponse); expect(decoded.addressIndicator).is.equal(value.addressIndicator); expect(decoded.daliAddress).is.equal(value.daliAddress); expect(decoded.lampFailure).is.equal(value.lampFailure); expect(decoded.ballastFailure).is.equal(value.ballastFailure); expect(decoded.convertorError).is.equal(value.convertorError); }); 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, 0x00]) var testFunction = function () { const value = dpt.decoder(bufferBiggerSize) } expect(testFunction).to.throw(BufferLengthError); }); /* Encoder tests */ it("encode valid", async function () { let value: DPT237Result = { readResponse: true, addressIndicator: false, daliAddress: 31, lampFailure: false, ballastFailure: false, convertorError: true } let buffer = Buffer.from([0x04, 0x9f]) expect(compareBuffers(dpt.encoder(value), buffer)).is.true; }); it("encode valid complement flags", async function () { let value: DPT237Result = { readResponse: false, addressIndicator: true, daliAddress: 31, lampFailure: true, ballastFailure: true, convertorError: false } let buffer = Buffer.from([0x03, 0x5f]) 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 () { var testFunctionUpperLimit = function () { const value: DPT237Result = { readResponse: true, addressIndicator: false, daliAddress: 64, lampFailure: false, ballastFailure: false, convertorError: false } const result = dpt.encoder(value) } expect(testFunctionUpperLimit).to.throw(InvalidValueError); var testFunctionLowerLimit = function () { const value: DPT237Result = { readResponse: true, addressIndicator: false, daliAddress: -1, lampFailure: false, ballastFailure: false, convertorError: false } const result = dpt.encoder(value) } expect(testFunctionLowerLimit).to.throw(InvalidValueError); }); });