Files
ewelink-api/test/env-node.spec.js
Martin M 098011b285 Release v2.0.0 (#44)
* Added arpTableSolver (#18)

* Added arpTableSolver

* fix package import

* linting class

* changed arp library

* refactor arp class

* using arpping fork

* refactor arpTableSolver class

* Added Zero Conf functionality (LAN mode) (#46)

* added crypto-js

* zeroconf helper functions

* zeroconf update payload

* new method to save devices cache file

* class renamed

* refactor Zeroconf class

* return cached device if exists

* moved method to get local ip address

* fix mac addresses without leading zeroes

* refactor Zeroconf class

* using new zeroconf functionality

* zeroconf working with single and multichannel devices

* save device mixin enhancement

* working on zeroconf test cases

* catch errors on filesystem methods

* zeroconf: added extra test cases

* better error handling

* zeroconf: 100% code coverage

* removed deprecated login method

* updates on credentials file

* version bump

* Docs for v2.0 (#52)

* added v1 docs

* added zeroconf docs

* updated readme

* docs updated

* removed zeroconf article warning

* updated vscode config

Co-authored-by: Luis Llamas <luisllamas@hotmail.com>
2020-01-11 01:39:29 -03:00

152 lines
5.4 KiB
JavaScript

const delay = require('delay');
const ewelink = require('../main');
const {
email,
password,
singleChannelDeviceId,
fourChannelsDevice,
} = require('./_setup/credentials.js');
const {
credentialsExpectations,
allDevicesExpectations,
specificDeviceExpectations,
} = require('./_setup/expectations');
describe('env: node script', () => {
let conn;
beforeAll(() => {
conn = new ewelink({ email, password });
});
beforeEach(async () => {
await delay(1000);
});
test('get ewelink credentials', async () => {
const credentials = await conn.getCredentials();
expect(typeof credentials).toBe('object');
expect(credentials).toMatchObject(credentialsExpectations);
});
test('get all devices', async () => {
const devices = await conn.getDevices();
expect(Array.isArray(devices)).toBe(true);
expect(devices[0]).toMatchObject(allDevicesExpectations);
});
test('get specific device', async () => {
const device = await conn.getDevice(singleChannelDeviceId);
expect(typeof device).toBe('object');
expect(device.deviceid).toBe(singleChannelDeviceId);
expect(device).toMatchObject(specificDeviceExpectations);
});
test('get single channel device power state', async () => {
const device = await conn.getDevice(singleChannelDeviceId);
const currentState = device.params.switch;
const powerState = await conn.getDevicePowerState(singleChannelDeviceId);
expect(typeof powerState).toBe('object');
expect(powerState.status).toBe('ok');
expect(powerState.state).toBe(currentState);
});
test('get multi channel device power state', async () => {
const channel = 3;
const device = await conn.getDevice(fourChannelsDevice);
const currentState = device.params.switches[channel - 1].switch;
const powerState = await conn.getDevicePowerState(
fourChannelsDevice,
channel
);
expect(typeof powerState).toBe('object');
expect(powerState.status).toBe('ok');
expect(powerState.state).toBe(currentState);
});
test('set single channel device power state', async () => {
jest.setTimeout(30000);
const device = await conn.getDevice(singleChannelDeviceId);
const currentState = device.params.switch;
const newState = currentState === 'on' ? 'off' : 'on';
const powerState = await conn.setDevicePowerState(
singleChannelDeviceId,
newState
);
expect(typeof powerState).toBe('object');
expect(powerState.status).toBe('ok');
expect(powerState.state).toBe(newState);
const deviceVerify = await conn.getDevice(singleChannelDeviceId);
const currentStateVerify = deviceVerify.params.switch;
expect(newState).toBe(currentStateVerify);
});
test('set multi channel device power state', async () => {
jest.setTimeout(30000);
const channel = 3;
const device = await conn.getDevice(fourChannelsDevice);
const currentState = device.params.switches[channel - 1].switch;
const newState = currentState === 'on' ? 'off' : 'on';
const powerState = await conn.setDevicePowerState(
fourChannelsDevice,
newState,
channel
);
expect(typeof powerState).toBe('object');
expect(powerState.status).toBe('ok');
expect(powerState.state).toBe(newState);
const deviceVerify = await conn.getDevice(fourChannelsDevice);
const currentStateVerify = deviceVerify.params.switches[channel - 1].switch;
expect(newState).toBe(currentStateVerify);
});
test('toggle single channel device power state', async () => {
jest.setTimeout(30000);
const device = await conn.getDevice(singleChannelDeviceId);
const currentState = device.params.switch;
const newState = currentState === 'on' ? 'off' : 'on';
await conn.toggleDevice(singleChannelDeviceId);
const deviceVerify = await conn.getDevice(singleChannelDeviceId);
const currentStateVerify = deviceVerify.params.switch;
expect(newState).toBe(currentStateVerify);
await conn.toggleDevice(singleChannelDeviceId);
const deviceVerifyAgain = await conn.getDevice(singleChannelDeviceId);
const currentStateVerifyAgain = deviceVerifyAgain.params.switch;
expect(currentState).toBe(currentStateVerifyAgain);
});
test('toggle multi channel device power state', async () => {
jest.setTimeout(30000);
const channel = 3;
const device = await conn.getDevice(fourChannelsDevice);
const currentState = device.params.switches[channel - 1].switch;
const newState = currentState === 'on' ? 'off' : 'on';
await conn.toggleDevice(fourChannelsDevice, channel);
const deviceVerify = await conn.getDevice(fourChannelsDevice);
const currentStateVerify = deviceVerify.params.switches[channel - 1].switch;
expect(newState).toBe(currentStateVerify);
await conn.toggleDevice(fourChannelsDevice, channel);
const deviceVerifyAgain = await conn.getDevice(fourChannelsDevice);
const currentStateVerifyAgain =
deviceVerifyAgain.params.switches[channel - 1].switch;
expect(currentState).toBe(currentStateVerifyAgain);
});
test('get channel count 1', async () => {
const result = await conn.getDeviceChannelCount(singleChannelDeviceId);
expect(typeof result).toBe('object');
expect(result.status).toBe('ok');
expect(result.switchesAmount).toBe(1);
});
test('get channel count 4', async () => {
const result = await conn.getDeviceChannelCount(fourChannelsDevice);
expect(typeof result).toBe('object');
expect(result.status).toBe('ok');
expect(result.switchesAmount).toBe(4);
});
});