const ewelink = require('../main'); const { email, password, deviceId } = require('./_setup/credentials.json'); const { loginExpectations, allDevicesExpectations, specificDeviceExpectations, } = require('./_setup/expectations'); describe('env: serverless', () => { let accessToken; let apiKey; test('login into ewelink', async () => { const conn = new ewelink({ email, password }); const login = await conn.login(); accessToken = login.at; apiKey = login.user.apikey; expect(typeof login).toBe('object'); expect(login).toMatchObject(loginExpectations); }); test('get all devices', async () => { const conn = new ewelink({ at: accessToken }); const devices = await conn.getDevices(); expect(Array.isArray(devices)).toBe(true); expect(devices[0]).toMatchObject(allDevicesExpectations); }); test('get specific device', async () => { const conn = new ewelink({ at: accessToken }); const device = await conn.getDevice(deviceId); expect(typeof device).toBe('object'); expect(device.deviceid).toBe(deviceId); expect(device).toMatchObject(specificDeviceExpectations); }); test('get device power state', async () => { const conn = new ewelink({ at: accessToken }); const device = await conn.getDevice(deviceId); const currentState = device.params.switch; const powerState = await conn.getDevicePowerState(deviceId); expect(typeof powerState).toBe('object'); expect(powerState.status).toBe('ok'); expect(powerState.state).toBe(currentState); }); test('set device power state', async () => { jest.setTimeout(30000); const conn = new ewelink({ at: accessToken, apiKey }); const device = await conn.getDevice(deviceId); const currentState = device.params.switch; const newState = currentState === 'on' ? 'off' : 'on'; const powerState = await conn.setDevicePowerState(deviceId, newState); expect(typeof powerState).toBe('object'); expect(powerState.status).toBe('ok'); expect(powerState.state).toBe(newState); const deviceVerify = await conn.getDevice(deviceId); const currentStateVerify = deviceVerify.params.switch; expect(newState).toBe(currentStateVerify); }); test('toggle device power state', async () => { jest.setTimeout(30000); const conn = new ewelink({ at: accessToken, apiKey }); const device = await conn.getDevice(deviceId); const currentState = device.params.switch; const newState = currentState === 'on' ? 'off' : 'on'; await conn.toggleDevice(deviceId); const deviceVerify = await conn.getDevice(deviceId); const currentStateVerify = deviceVerify.params.switch; expect(newState).toBe(currentStateVerify); await conn.toggleDevice(deviceId); const deviceVerifyAgain = await conn.getDevice(deviceId); const currentStateVerifyAgain = deviceVerifyAgain.params.switch; expect(currentState).toBe(currentStateVerifyAgain); }); });