dptlib/tests/DPT019.test.ts
2022-03-13 02:20:30 +01:00

226 lines
6.7 KiB
TypeScript

import { expect } from "chai";
import { DPT19, DPT19Result } from "../src/DPT19"
import { BufferLengthError } from "../src/errors/BufferLengthError";
import { InvalidValueError } from "../src/errors/InvalidValueError";
import { compareBuffers } from "./util"
describe("Test DPT019", (): void => {
let dpt = new DPT19();
it("Decode buffer acceptable", async function () {
let value: DPT19Result = {
dateTime: new Date(1921, 8, 22, 10, 11, 19, 0),
dayOfWeek: undefined,
f: false,
wd: false,
nwd: false,
ny: false,
nd: false,
ndow: false,
nt: false,
sst: false,
internalClock: false,
reliability: false
}
let buffer = Buffer.from([21, 9, 22, 10, 11, 19, 0, 0])
const decoded = dpt.decoder(buffer)
expect(decoded.dateTime.valueOf()).is.equal(value.dateTime.valueOf());
expect(decoded.dayOfWeek).is.equal(value.dayOfWeek);
expect(decoded.f).is.equal(value.f);
expect(decoded.wd).is.equal(value.wd);
expect(decoded.nwd).is.equal(value.nwd);
expect(decoded.ny).is.equal(value.ny);
expect(decoded.nd).is.equal(value.nd);
expect(decoded.ndow).is.equal(value.ndow);
expect(decoded.nt).is.equal(value.nt);
expect(decoded.sst).is.equal(value.sst);
expect(decoded.internalClock).is.equal(value.internalClock);
expect(decoded.reliability).is.equal(value.reliability);
});
it("Decode buffer DayOfWeek == 7", async function () {
let value: DPT19Result = {
dateTime: new Date(2021, 8, 27, 10, 11, 19, 0),
dayOfWeek: 0,
f: false,
wd: false,
nwd: false,
ny: false,
nd: false,
ndow: false,
nt: false,
sst: false,
internalClock: false,
reliability: false
}
let buffer = Buffer.from([121, 9, 27, 10 + (7 << 5), 11, 19, 0, 0])
const decoded = dpt.decoder(buffer)
expect(decoded.dateTime.valueOf()).is.equal(value.dateTime.valueOf());
expect(decoded.dayOfWeek).is.equal(value.dayOfWeek);
expect(decoded.f).is.equal(value.f);
expect(decoded.wd).is.equal(value.wd);
expect(decoded.nwd).is.equal(value.nwd);
expect(decoded.ny).is.equal(value.ny);
expect(decoded.nd).is.equal(value.nd);
expect(decoded.ndow).is.equal(value.ndow);
expect(decoded.nt).is.equal(value.nt);
expect(decoded.sst).is.equal(value.sst);
expect(decoded.internalClock).is.equal(value.internalClock);
expect(decoded.reliability).is.equal(value.reliability);
});
it("Decode buffer DayOfWeek == 0", async function () {
let value: DPT19Result = {
dateTime: new Date(2021, 8, 27, 10, 11, 19, 0),
dayOfWeek: 3,
f: false,
wd: false,
nwd: false,
ny: false,
nd: false,
ndow: false,
nt: false,
sst: false,
internalClock: false,
reliability: false
}
let buffer = Buffer.from([121, 9, 27, 10 + (3 << 5), 11, 19, 0, 0])
const decoded = dpt.decoder(buffer)
expect(decoded.dateTime.valueOf()).is.equal(value.dateTime.valueOf());
expect(decoded.dayOfWeek).is.equal(value.dayOfWeek);
expect(decoded.f).is.equal(value.f);
expect(decoded.wd).is.equal(value.wd);
expect(decoded.nwd).is.equal(value.nwd);
expect(decoded.ny).is.equal(value.ny);
expect(decoded.nd).is.equal(value.nd);
expect(decoded.ndow).is.equal(value.ndow);
expect(decoded.nt).is.equal(value.nt);
expect(decoded.sst).is.equal(value.sst);
expect(decoded.internalClock).is.equal(value.internalClock);
expect(decoded.reliability).is.equal(value.reliability);
});
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 invalid buffer", async function () {
let buffer = Buffer.from([21, 9, 22, 24, 0, 1, 0, 0])
var testFunction = function () {
const value = dpt.decoder(buffer)
}
expect(testFunction).to.throw(RangeError);
});
it("Decode oversized buffer", async function () {
let bufferBiggerSize = Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
var testFunction = function () {
const value = dpt.decoder(bufferBiggerSize)
}
expect(testFunction).to.throw(BufferLengthError);
});
/* Encoder tests */
it("encode valid", async function () {
let value: DPT19Result = {
dateTime: new Date(1921, 10, 22, 10, 11, 19, 0),
dayOfWeek: undefined,
f: false,
wd: false,
nwd: false,
ny: false,
nd: false,
ndow: false,
nt: false,
sst: false,
internalClock: false,
reliability: false
}
let buffer = Buffer.from([21, 11, 22, 10, 11, 19, 0, 0])
expect(compareBuffers(dpt.encoder(value), buffer)).is.true;
});
it("encode with dayOfWeek", async function () {
let value: DPT19Result = {
dateTime: new Date(1921, 10, 22, 10, 11, 19, 0),
dayOfWeek: 7,
f: false,
wd: false,
nwd: false,
ny: false,
nd: false,
ndow: false,
nt: false,
sst: false,
internalClock: false,
reliability: false
}
let buffer = Buffer.from([21, 11, 22, 234, 11, 19, 0, 0])
expect(compareBuffers(dpt.encoder(value), buffer)).is.true;
value = {
dateTime: new Date(1921, 10, 22, 10, 11, 19, 0),
dayOfWeek: 0,
f: false,
wd: false,
nwd: false,
ny: false,
nd: false,
ndow: false,
nt: false,
sst: false,
internalClock: false,
reliability: false
}
buffer = Buffer.from([21, 11, 22, 234, 11, 19, 0, 0])
expect(compareBuffers(dpt.encoder(value), buffer)).is.true;
});
it("encode from Date", async function () {
let value = new Date(1921, 10, 22, 10, 11, 19, 0)
let buffer = Buffer.from([21, 11, 22, 74, 11, 19, 0, 128])
// Note the conversion from date says that the clock is unreliable.
// It also gets the day of week and populates it.
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 testFunction = function () {
let value: DPT19Result = {
dateTime: new Date(1921, 9, 22, 24, 11, 19, 0),
dayOfWeek: 10,
f: false,
wd: false,
nwd: false,
ny: false,
nd: false,
ndow: false,
nt: false,
sst: false,
internalClock: false,
reliability: false
}
const result = dpt.encoder(value)
}
expect(testFunction).to.throw(RangeError);
});
});