dlms-protocol
Version:
DLMS Protocol parser for JavaScript
123 lines (103 loc) • 4.45 kB
JavaScript
import InformationReport from "../src/messages/InformationReport";
import DlmsDateTime from "../src/typesDLMS/DlmsDateTime";
import DlmsDatas from "../src/typesDLMS/DlmsDatas";
jest.mock("../src/typesDLMS/DlmsDateTime");
jest.mock("../src/typesDLMS/DlmsDatas");
describe("InformationReport class", () => {
beforeEach(() => {
DlmsDateTime.mockClear();
DlmsDatas.factory.mockClear();
});
const mockDlmsDateTime = {
insertData: jest.fn(),
};
const mockDlmsData = {
insertData: jest.fn(),
totalBytes: 3,
};
DlmsDateTime.mockImplementation(() => mockDlmsDateTime);
DlmsDatas.factory.mockReturnValue(mockDlmsData);
test("should initialize with default values when no bytes are provided", () => {
const report = new InformationReport();
expect(report.currentTime).toBeNull();
expect(report.variableAccessSpecificationQty).toBe(0);
expect(report.variableAccessSpecification).toBe(0);
expect(report.variableName).toEqual([]);
expect(report.dataQty).toBe(0);
expect(report.data).toEqual([]);
expect(report.value).toEqual([]);
});
test("should correctly parse bytes when currentTime is present", () => {
const bytes = [
0x01, // Message type
0x0C, // currentTime length
0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, 0x11, 0x22, 0x33, 0x44, // currentTime bytes
0x02, // variableAccessSpecificationQty
0x00, // variableAccessSpecification (irrelevant here)
0x01, 0x00, 0x02, 0x00, // variableNames
0x02, // dataQty
0x01, 0xAA, 0xBB, 0xCC, // first DlmsData
0x02, 0xDD, 0xEE, 0xFF, // second DlmsData
];
const report = new InformationReport(bytes);
expect(report.currentTime).toEqual(mockDlmsDateTime);
expect(mockDlmsDateTime.insertData).toHaveBeenCalledWith([
0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, 0x11, 0x22, 0x33, 0x44,
]);
expect(report.variableAccessSpecificationQty).toBe(2);
expect(report.variableName).toEqual([256, 512]); // Parsed from bytes
expect(report.dataQty).toBe(2);
expect(report.data.length).toBe(2);
expect(report.data[0]).toEqual(mockDlmsData);
expect(report.data[1]).toEqual(mockDlmsData);
});
test("should correctly parse bytes when currentTime is not present", () => {
const bytes = [
0x01, // Message type
0x00, // No currentTime
0x01, // variableAccessSpecificationQty
0x00, // variableAccessSpecification (irrelevant here)
0x01, 0x00, // variableNames
0x01, // dataQty
0x01, 0xAA, 0xBB, 0xCC, // DlmsData
];
const report = new InformationReport(bytes);
expect(report.currentTime).toBeNull();
expect(report.variableAccessSpecificationQty).toBe(1);
expect(report.variableName).toEqual([256]); // Parsed from bytes
expect(report.dataQty).toBe(1);
expect(report.data.length).toBe(1);
expect(report.data[0]).toEqual(mockDlmsData);
});
test("should return the correct byte array with getBytes()", () => {
const bytes = [
0x01, // Message type
0x00, // No currentTime
0x01, // variableAccessSpecificationQty
0x00, // variableAccessSpecification
0x01, 0x00, // variableNames
0x01, // dataQty
0x01, 0xAA, 0xBB, 0xCC, // DlmsData
];
const report = new InformationReport(bytes);
const result = report.getBytes();
expect(result).toEqual(bytes);
});
test("should return the correct hexadecimal string with getHexString()", () => {
const bytes = [
0x01, // Message type
0x00, // No currentTime
0x01, // variableAccessSpecificationQty
0x00, // variableAccessSpecification
0x01, 0x00, // variableNames
0x01, // dataQty
0x01, 0xAA, 0xBB, 0xCC, // DlmsData
];
const report = new InformationReport(bytes);
const hexString = report.getHexString();
const expectedHex = bytes
.map(byte => byte.toString(16).toUpperCase().padStart(2, '0'))
.join(' ');
expect(hexString).toBe(expectedHex);
});
});