describe('GET /service/philips-hue/bridge', () => { it('should get bridges', async () => { const philipsHueController = PhilipsHueControllers(philipsHueLightService); await philipsHueController['get /api/v1/service/philips-hue/bridge'].controller({}, res); assert.calledOnce(philipsHueLightService.getBridges); }); });
describe('GET /api/v1/service/tasmota/discover', () => { let controller; beforeEach(() => { controller = TasmotaController(tasmotaHandler); }); it('Discover', () => { const res = { json: fake.returns(null), }; controller['get /api/v1/service/tasmota/discover'].controller(undefined, res); assert.calledOnce(tasmotaHandler.getDiscoveredDevices); assert.calledWith(res.json, discoveredDevices); }); });
describe('corsMiddleware', () => { it('should send response directly', async () => { const req = new MockExpressRequest({ method: 'OPTIONS', }); corsMiddleware(req, res, () => { throw new Error('next should not be calld'); }); assert.called(res.header); assert.calledOnce(res.send); }); });
it('should execute switch turn on', async () => { const device = { setValue: fake.resolves(null), }; const stateManager = new StateManager(event); await executeActions( { stateManager, event, device }, [ [ { type: ACTIONS.SWITCH.TURN_ON, devices: ['switch-1'], }, ], ], {}, ); assert.calledOnce(device.setValue); });
describe('MqttHandler', () => { let mqttHandler; beforeEach(() => { mqttHandler = new MqttHandler(gladys, MockedMqttClient, 'faea9c35-759a-44d5-bcc9-2af1de37b8b4'); sinon.reset(); }); it('should have falsy status', () => { expect(mqttHandler.configured).to.eq(false); expect(mqttHandler.connected).to.eq(false); }); it('should have binded topics', async () => { await mqttHandler.init(); expect(Object.keys(mqttHandler.topicBinds)).deep.eq(['gladys/master/#']); assert.callCount(gladys.variable.getValue, 3); assert.calledOnce(MockedMqttClient.internalConnect); expect(mqttHandler.configured).to.eq(true); expect(mqttHandler.connected).to.eq(false); }); });
describe('TasmotaService', () => { const tasmotaService = TasmotaService({}, 'faea9c35-759a-44d5-bcc9-2af1de37b8b4'); beforeEach(() => { sinon.reset(); }); it('should start service', async () => { await tasmotaService.start(); assert.calledOnce(tasmotaService.device.connect); assert.notCalled(tasmotaService.device.disconnect); }); it('should stop service', async () => { tasmotaService.stop(); assert.notCalled(tasmotaService.device.connect); assert.calledOnce(tasmotaService.device.disconnect); }); });
describe('system.init', () => { let system; beforeEach(async () => { system = new System(sequelize, event, config); }); afterEach(() => { sinon.reset(); }); it('should init system', async () => { await system.init(); assert.calledOnce(system.dockerode.listContainers); assert.notCalled(sequelize.close); assert.calledOnce(event.on); }); });
describe('gateway.login', () => { const variable = { getValue: fake.resolves(null), setValue: fake.resolves(null), }; const gateway = new Gateway(variable, event, system, sequelize, config); it('should login to gladys gateway', async () => { const loginResults = await gateway.login('tony.stark@gladysassistant.com', 'warmachine123'); expect(loginResults).to.have.property('two_factor_token'); assert.calledWith(gateway.gladysGatewayClient.login, 'tony.stark@gladysassistant.com', 'warmachine123'); }); it('should login two factor to gladys gateway', async () => { await gateway.loginTwoFactor('token', '123456'); assert.calledWith(gateway.gladysGatewayClient.loginInstance, 'token', '123456'); assert.called(variable.getValue); assert.called(variable.setValue); assert.calledOnce(gateway.gladysGatewayClient.createInstance); }); });
it('should execute stop service', async () => { const example = { stop: fake.resolves(null), }; const stateManager = new StateManager(event); stateManager.setState('service', 'example', example); await executeActions( { stateManager, event }, [ [ { type: ACTIONS.SERVICE.STOP, service: 'example', }, ], ], {}, ); assert.calledOnce(example.stop); });
describe('room.destroy', () => { const brain = { addRoom: fake.returns(null), removeRoom: fake.returns(null), }; const room = new Room(brain); it('should destroy a room', async () => { await room.destroy('test-room'); assert.calledOnce(brain.removeRoom); }); it('should return room not found', async () => { const promise = room.destroy('room-does-not-exist'); return assertChai.isRejected(promise, 'Room not found'); }); });
describe('POST /api/v1/service/mqtt/config/docker', () => { let controller; beforeEach(() => { controller = MqttController(mqttHandler); sinon.reset(); }); it('Install container', async () => { const req = {}; const res = { json: fake.returns(null), }; await controller['post /api/v1/service/mqtt/config/docker'].controller(req, res); assert.calledOnce(mqttHandler.installContainer); assert.calledOnce(res.json); }); });
describe('GET /api/v1/service/xiaomi/sensor', () => { it('should get sensors', async () => { const xiaomiController = XiaomiController({ getSensors }); const req = {}; await xiaomiController['get /api/v1/service/xiaomi/sensor'].controller(req, res); assert.calledOnce(getSensors); }); });
it('should execute light turn on', async () => { const device = { setValue: fake.resolves(null), }; const stateManager = new StateManager(event); await executeActions( { stateManager, event, device }, [ [ { type: ACTIONS.LIGHT.TURN_ON, devices: ['light-1'], }, ], ], {}, ); assert.calledOnce(device.setValue); });
describe('GET /api/v1/service/usb/port', () => { it('should configure bridge', async () => { const usbController = UsbController({ list }); const req = {}; await usbController['get /api/v1/service/usb/port'].controller(req, res); assert.calledOnce(list); }); });
describe('GET /api/v1/service/mqtt/status', () => { let controller; beforeEach(() => { controller = MqttController(mqttHandler); sinon.reset(); }); it('Status test', async () => { const req = {}; const res = { json: fake.returns(null), }; await controller['get /api/v1/service/mqtt/status'].controller(req, res); assert.calledOnce(mqttHandler.status); assert.calledOnce(res.json); }); });