dptlib/tests/DPT237.test.ts

85 lines
2.5 KiB
TypeScript
Raw Normal View History

2022-03-09 22:38:02 +01:00
import { expect } from "chai";
2022-03-12 15:56:39 +01:00
import { DPT237, DPT237Result } from "../src/DPT237"
2022-03-09 22:38:02 +01:00
import { BufferLengthError } from "../src/errors/BufferLengthError";
import { InvalidValueError } from "../src/errors/InvalidValueError";
import { compareBuffers } from "./util"
describe("Test DPT237", (): void => {
let dpt = new DPT237();
2022-03-12 15:56:39 +01:00
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 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 undefined", async function () {
var testFunction = function () {
const value = dpt.encoder(undefined)
}
expect(testFunction).to.throw(InvalidValueError);
});
it("encode invalid value", async function () {
let value: DPT237Result = {
readResponse: true,
addressIndicator: false,
daliAddress: 64,
lampFailure: false,
ballastFailure: false,
convertorError: false
}
var testFunction = function () {
const result = dpt.encoder(value)
}
expect(testFunction).to.throw(InvalidValueError);
});
2022-03-09 22:38:02 +01:00
});