UNPKG

dlms-protocol

Version:

DLMS Protocol parser for JavaScript

119 lines (100 loc) 4.52 kB
import DlmsUint64 from "../src/typesDLMS/DlmsUint64"; import DlmsDataType from "../src/enum/DlmsDataType"; describe("DlmsUint64 class", () => { test("should initialize with default values when no data is provided", () => { const dlmsUint64 = new DlmsUint64(); expect(dlmsUint64.tag).toBe(DlmsDataType.UINT64); expect(dlmsUint64.totalBytes).toBe(0); expect(dlmsUint64.rawValue).toEqual([]); expect(dlmsUint64.bodyBytes).toEqual([]); expect(dlmsUint64.value).toBe(BigInt(0)); }); test("should correctly initialize from a byte array", () => { const data = [ DlmsDataType.UINT64, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, ]; // UINT64 tag and value (0x123456789ABCDEF0) const dlmsUint64 = new DlmsUint64(data); expect(dlmsUint64.tag).toBe(DlmsDataType.UINT64); expect(dlmsUint64.totalBytes).toBe(9); expect(dlmsUint64.rawValue).toEqual(data); expect(dlmsUint64.bodyBytes).toEqual([ 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, ]); expect(dlmsUint64.value).toBe(BigInt("0x123456789ABCDEF0")); }); test("should throw an error if tag does not match when inserting data", () => { const data = [0xFF, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0]; // Invalid tag const dlmsUint64 = new DlmsUint64(); expect(() => dlmsUint64.insertData(data)).toThrow("Tag mismatch."); }); test("should correctly initialize from a numeric value", () => { const value = BigInt("0x123456789ABCDEF0"); const dlmsUint64 = new DlmsUint64(value); expect(dlmsUint64.tag).toBe(DlmsDataType.UINT64); expect(dlmsUint64.totalBytes).toBe(9); expect(dlmsUint64.rawValue).toEqual([ DlmsDataType.UINT64, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, ]); expect(dlmsUint64.bodyBytes).toEqual([ 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, ]); expect(dlmsUint64.value).toBe(BigInt("0x123456789ABCDEF0")); }); test("should return the correct standard length", () => { const dlmsUint64 = new DlmsUint64(); expect(dlmsUint64.getStandardLength()).toBe(9); }); test("should return the correct value with getValue()", () => { const data = [ DlmsDataType.UINT64, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, ]; const dlmsUint64 = new DlmsUint64(data); expect(dlmsUint64.getValue()).toBe(BigInt("0x123456789ABCDEF0")); }); test("should correctly insert data from a byte array", () => { const dlmsUint64 = new DlmsUint64(); dlmsUint64.insertData([ DlmsDataType.UINT64, 0xFF, 0xEE, 0xDD, 0xCC, 0xBB, 0xAA, 0x99, 0x88, ]); expect(dlmsUint64.tag).toBe(DlmsDataType.UINT64); expect(dlmsUint64.totalBytes).toBe(9); expect(dlmsUint64.rawValue).toEqual([ DlmsDataType.UINT64, 0xFF, 0xEE, 0xDD, 0xCC, 0xBB, 0xAA, 0x99, 0x88, ]); expect(dlmsUint64.bodyBytes).toEqual([ 0xFF, 0xEE, 0xDD, 0xCC, 0xBB, 0xAA, 0x99, 0x88, ]); expect(dlmsUint64.value).toBe(BigInt("0xFFEEDDCCBBAA9988")); }); test("should correctly insert data from a numeric value", () => { const dlmsUint64 = new DlmsUint64(); dlmsUint64.insertDataFromValue(BigInt("0xFFFFFFFFFFFFFFFF")); expect(dlmsUint64.tag).toBe(DlmsDataType.UINT64); expect(dlmsUint64.totalBytes).toBe(9); expect(dlmsUint64.rawValue).toEqual([ DlmsDataType.UINT64, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, ]); expect(dlmsUint64.bodyBytes).toEqual([ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, ]); expect(dlmsUint64.value).toBe(BigInt("0xFFFFFFFFFFFFFFFF")); }); test("should correctly convert bytes to Uint64", () => { const dlmsUint64 = new DlmsUint64(); const bytes = [0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0]; expect(dlmsUint64._toUint64(bytes)).toBe(BigInt("0x123456789ABCDEF0")); }); test("should correctly convert Uint64 to bytes", () => { const dlmsUint64 = new DlmsUint64(); const value = BigInt("0x123456789ABCDEF0"); expect(dlmsUint64._fromUint64(value)).toEqual([ 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, ]); }); });