dptlib/tests/DPT015.test.ts
2022-03-11 12:09:49 +01:00

103 lines
2.6 KiB
TypeScript

import { expect } from "chai";
import { DPT15, DPT15Result } from "../src/DPT15"
import { BufferLengthError } from "../src/errors/BufferLengthError";
import { InvalidValueError } from "../src/errors/InvalidValueError";
import { compareBuffers } from "./util"
describe("Test DPT015", (): void => {
let dpt = new DPT15();
let value: DPT15Result = {
U: 0x03,
V: 0x09,
W: 0x04,
X: 0x08,
Y: 0x00,
Z: 0x02,
E: 0x01,
P: 0x00,
D: 0x01,
C: 0x00,
N: 0x05
}
let bufferValid = Buffer.from([0x39, 0x48, 0x02, 0xa5])
let bufferInvalid = Buffer.from([0x39, 0xc8, 0xa2, 0xa5])
let bufferEmpty = Buffer.from([])
let bufferBiggerSize = Buffer.from([0xff, 0xff, 0x63, 0xc0, 0x00])
it("Decode buffer acceptable", async function () {
const decoded = dpt.decoder(bufferValid)
expect(decoded.U).is.equal(value.U);
expect(decoded.V).is.equal(value.V);
expect(decoded.W).is.equal(value.W);
expect(decoded.X).is.equal(value.X);
expect(decoded.Y).is.equal(value.Y);
expect(decoded.Z).is.equal(value.Z);
expect(decoded.E).is.equal(value.E);
expect(decoded.P).is.equal(value.P);
expect(decoded.D).is.equal(value.D);
expect(decoded.C).is.equal(value.C);
expect(decoded.N).is.equal(value.N);
});
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);
});
it("Decode invalid buffer", async function () {
var testFunction = function () {
const value = dpt.decoder(bufferInvalid)
}
expect(testFunction).to.throw(InvalidValueError);
});
/* Encoder */
it("encode valid", async function () {
expect(compareBuffers(dpt.encoder(value), bufferValid)).is.true;
});
it("encode undefined", async function () {
var testFunction = function () {
const value = dpt.encoder(undefined)
}
expect(testFunction).to.throw(InvalidValueError);
});
it("encode invalid", async function () {
var testFunction = function () {
let invalidValue: DPT15Result = {
U: 0x03,
V: 0x0a, // invalid value as per spec
W: 0x04,
X: 0x08,
Y: 0x00,
Z: 0x02,
E: 0x01,
P: 0x00,
D: 0x01,
C: 0x00,
N: 0x05
}
const result = dpt.encoder(invalidValue)
}
expect(testFunction).to.throw(InvalidValueError);
});
});