UNPKG

dlms-protocol

Version:

DLMS Protocol parser for JavaScript

86 lines (75 loc) 3.42 kB
import DlmsDateTime from "../src/typesDLMS/DlmsDateTime"; describe("DlmsDateTime class", () => { test("should initialize from a Date object", () => { const date = new Date(2023, 4, 15, 14, 30, 45); // 15 May 2023, 14:30:45 const dlmsDateTime = new DlmsDateTime(date); expect(dlmsDateTime.yearHighbyte).toBe(0x07); // 2023 in hex: 0x07E7 expect(dlmsDateTime.yearLowbyte).toBe(0xE7); expect(dlmsDateTime.month).toBe(5); // May expect(dlmsDateTime.dayOfMonth).toBe(15); expect(dlmsDateTime.dayOfWeek).toBe(1); // Monday expect(dlmsDateTime.hour).toBe(14); expect(dlmsDateTime.minute).toBe(30); expect(dlmsDateTime.second).toBe(45); expect(dlmsDateTime.hundredthsOfSecond).toBe(0xff); expect(dlmsDateTime.deviationHighbyte).toBe(0x80); expect(dlmsDateTime.deviationLowbyte).toBe(0x00); expect(dlmsDateTime.clockStatus).toBe(0x00); expect(dlmsDateTime.value).toBe("2023-05-15 17:30:45"); }); test("should initialize from a byte array", () => { const bytes = [ 0x07, 0xE7, // Year 2023 0x05, // May 0x15, // 21th 0x01, // Monday 0x14, // 20 hours 0x1E, // 30 minutes 0x2D, // 45 seconds 0xFF, // Hundredths of second 0x00, 0x80, // Deviation 0x00, // Clock status ]; const dlmsDateTime = new DlmsDateTime(bytes); expect(dlmsDateTime.yearHighbyte).toBe(0x07); expect(dlmsDateTime.yearLowbyte).toBe(0xE7); expect(dlmsDateTime.month).toBe(0x05); expect(dlmsDateTime.dayOfMonth).toBe(0x15); // Matches the byte array expect(dlmsDateTime.dayOfWeek).toBe(0x01); // Monday expect(dlmsDateTime.hour).toBe(0x14); expect(dlmsDateTime.minute).toBe(0x1E); expect(dlmsDateTime.second).toBe(0x2D); expect(dlmsDateTime.valueTime).toEqual(new Date(2023, 4, 21, 20, 30, 45)); expect(dlmsDateTime.value).toBe("21/05/2023 20:30"); }); test("should throw an error if byte array length is less than 12", () => { const bytes = [0x07, 0xE7, 0x05, 0x15]; // Incomplete byte array expect(() => new DlmsDateTime(bytes)).toThrow("Invalid byte array length"); }); test("should throw an error for invalid day of the week", () => { const dlmsDateTime = new DlmsDateTime(); expect(() => dlmsDateTime.getDayOfWeekValue(8)).toThrow("Invalid day of the week"); }); test("should return the correct date with getValue()", () => { const date = new Date(2023, 4, 15, 14, 30, 45); const dlmsDateTime = new DlmsDateTime(date); expect(dlmsDateTime.getValue()).toEqual(date); }); test("should return the correct byte representation with getBytes()", () => { const date = new Date(2023, 4, 15, 14, 30, 45); const dlmsDateTime = new DlmsDateTime(date); const expectedBytes = [ 0x07, 0xE7, // Year 2023 0x05, // May 0x0F, // 15th 0x01, // Monday 0x0E, // 14 hours 0x1E, // 30 minutes 0x2D, // 45 seconds 0xFF, // Hundredths of second 0x80, 0x00, // Deviation 0x00, // Clock status ]; expect(dlmsDateTime.getBytes()).toEqual(expectedBytes); }); });