energy-manager-iot
Version:
Library for energy management in IoT devices via MQTT protocol. Documentation: https://jonhvmp.github.io/energy-manager-iot-docs/
65 lines (64 loc) • 3.54 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const device_registry_1 = require("../lib/device-registry");
const device_1 = require("../types/device");
const error_handler_1 = require("../utils/error-handler");
/**
* Tests for the DeviceRegistry class
*
* This test suite covers validation, device registration, management,
* and group operations of the DeviceRegistry.
*/
// Mock MQTT module to avoid real connections during tests
jest.mock('mqtt', () => {
const mockClient = {
on: jest.fn().mockReturnThis(),
end: jest.fn((_, __, cb) => cb && cb()),
publish: jest.fn((_, __, ___, cb) => cb && cb()),
subscribe: jest.fn((_, __, cb) => cb && cb()),
unsubscribe: jest.fn((_, cb) => cb && cb()),
removeAllListeners: jest.fn()
};
return {
connect: jest.fn().mockReturnValue(mockClient)
};
});
describe('DeviceRegistry', () => {
let registry;
beforeEach(() => {
registry = new device_registry_1.DeviceRegistry();
});
describe('Device Registration', () => {
test('should throw error when registering device with invalid ID', () => {
// Invalid IDs: empty, too short, or with disallowed characters
expect(() => registry.registerDevice('', 'Device', device_1.DeviceType.SENSOR)).toThrow(error_handler_1.EnergyManagerError);
expect(() => registry.registerDevice('ab', 'Device', device_1.DeviceType.SENSOR)).toThrow(error_handler_1.EnergyManagerError);
expect(() => registry.registerDevice('device@123', 'Device', device_1.DeviceType.SENSOR)).toThrow(error_handler_1.EnergyManagerError);
});
test('should throw error when registering duplicate device', () => {
registry.registerDevice('sensor1', 'Sensor 1', device_1.DeviceType.SENSOR);
expect(() => registry.registerDevice('sensor1', 'Duplicate', device_1.DeviceType.SENSOR)).toThrow(error_handler_1.EnergyManagerError);
});
test('should throw error with invalid configuration', () => {
// Test invalid reporting intervals
expect(() => registry.registerDevice('sensor1', 'Sensor 1', device_1.DeviceType.SENSOR, { reportingInterval: 0 } // Invalid value
)).toThrow(error_handler_1.EnergyManagerError);
expect(() => registry.registerDevice('sensor1', 'Sensor 1', device_1.DeviceType.SENSOR, { sleepThreshold: 101 } // Invalid value
)).toThrow(error_handler_1.EnergyManagerError);
});
});
describe('Group Management', () => {
test('should throw error when using invalid group name', () => {
expect(() => registry.createGroup('')).toThrow(error_handler_1.EnergyManagerError);
expect(() => registry.createGroup('group@special')).toThrow(error_handler_1.EnergyManagerError);
});
test('should throw error when accessing non-existent group', () => {
expect(() => registry.getDevicesInGroup('non-existent-group')).toThrow(error_handler_1.EnergyManagerError);
expect(() => registry.getDeviceIdsInGroup('non-existent-group')).toThrow(error_handler_1.EnergyManagerError);
});
test('should throw error when removing device from non-existent group', () => {
registry.registerDevice('sensor1', 'Sensor 1', device_1.DeviceType.SENSOR);
expect(() => registry.removeDeviceFromGroup('sensor1', 'non-existent-group')).toThrow(error_handler_1.EnergyManagerError);
});
});
});