Skip to content

Commit

Permalink
split unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
0x5e committed Oct 20, 2022
1 parent 88bdfa0 commit 0ef55f5
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 125 deletions.
125 changes: 0 additions & 125 deletions test/core.test.ts

This file was deleted.

71 changes: 71 additions & 0 deletions test/custom.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/* eslint-disable no-console */
import { describe, expect, test } from '@jest/globals';

import TuyaOpenAPI, { Endpoints } from '../src/core/TuyaOpenAPI';
import TuyaOpenMQ from '../src/core/TuyaOpenMQ';
import TuyaDevice from '../src/device/TuyaDevice';

import TuyaCustomDeviceManager from '../src/device/TuyaCustomDeviceManager';

import { config, expectDevice } from './util';

const { options } = config;
if (options.projectType === '1') {
const api = new TuyaOpenAPI(options.endpoint as Endpoints, options.accessId, options.accessKey);
const mq = new TuyaOpenMQ(api, '2.0');
const customDeviceManager = new TuyaCustomDeviceManager(api, mq);

describe('TuyaOpenAPI', () => {
test('customLogin()', async () => {
await api.customLogin(options.username, options.password);
});

test('_refreshAccessTokenIfNeed()', async () => {
api.tokenInfo.expire = 0;
await api._refreshAccessTokenIfNeed('');
});
});

describe('TuyaCustomDeviceManager', () => {
test('updateDevices()', async () => {
const devices = await customDeviceManager.updateDevices();
expect(devices).not.toBeNull();
for (const device of devices) {
expectDevice(device);
}
}, 10 * 1000);

test('updateDevice()', async () => {
let device: TuyaDevice | null = Array.from(customDeviceManager.devices)[0];
expectDevice(device);
device = await customDeviceManager.updateDevice(device.id);
expectDevice(device!);
});
});

describe('TuyaOpenMQ', () => {

test('start()', async () => {
await new Promise((resolve, reject) => {
mq._onConnect = () => {
console.log('TuyaOpenMQ connected');
resolve(null);
};
mq._onError = (err) => {
console.log('TuyaOpenMQ error:', err);
reject(err);
};
mq.start();
});
});

test('stop()', async () => {
mq.stop();
});

});
} else {
test('', async () => {
//
});
}
73 changes: 73 additions & 0 deletions test/home.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/* eslint-disable no-console */
import { describe, expect, test } from '@jest/globals';

import TuyaOpenAPI, { Endpoints } from '../src/core/TuyaOpenAPI';
import TuyaOpenMQ from '../src/core/TuyaOpenMQ';
import TuyaDevice from '../src/device/TuyaDevice';

import TuyaHomeDeviceManager from '../src/device/TuyaHomeDeviceManager';

import { config, expectDevice } from './util';

const { options } = config;
if (options.projectType === '2') {
const api = new TuyaOpenAPI(TuyaOpenAPI.Endpoints.CHINA, options.accessId, options.accessKey);
const mq = new TuyaOpenMQ(api, '1.0');
const homeDeviceManager = new TuyaHomeDeviceManager(api, mq);

describe('TuyaOpenAPI', () => {
test('homeLogin()', async () => {
await api.homeLogin(options.countryCode, options.username, options.password, options.appSchema);
});

test('_refreshAccessTokenIfNeed()', async () => {
api.tokenInfo.expire = 0;
await api._refreshAccessTokenIfNeed('');
});
});

describe('TuyaHomeDeviceManager', () => {

test('updateDevices()', async () => {
const devices = await homeDeviceManager.updateDevices();
expect(devices).not.toBeNull();
for (const device of devices) {
expectDevice(device);
}
}, 10 * 1000);

test('updateDevice()', async () => {
let device: TuyaDevice | null = Array.from(homeDeviceManager.devices)[0];
expectDevice(device);
device = await homeDeviceManager.updateDevice(device.id);
expectDevice(device!);
});

});

describe('TuyaOpenMQ', () => {

test('start()', async () => {
await new Promise((resolve, reject) => {
mq._onConnect = () => {
console.log('TuyaOpenMQ connected');
resolve(null);
};
mq._onError = (err) => {
console.log('TuyaOpenMQ error:', err);
reject(err);
};
mq.start();
});
});

test('stop()', async () => {
mq.stop();
});

});
} else {
test('', async () => {
//
});
}
25 changes: 25 additions & 0 deletions test/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import fs from 'fs';
import { PLATFORM_NAME } from '../src/settings';
import { TuyaPlatformConfig } from '../src/config';
import TuyaDevice from '../src/device/TuyaDevice';

const file = fs.readFileSync(`${process.env.HOME}/.homebridge-dev/config.json`);
const { platforms } = JSON.parse(file.toString());

export const config: TuyaPlatformConfig = platforms.find(platform => platform.platform === PLATFORM_NAME);

export function expectDevice(device: TuyaDevice) {
// console.debug(JSON.stringify(device));

expect(device).not.toBeUndefined();

expect(device.id.length).toBeGreaterThan(0);
expect(device.uuid.length).toBeGreaterThan(0);
expect(device.online).toBeDefined();

expect(device.product_id.length).toBeGreaterThan(0);
expect(device.category.length).toBeGreaterThan(0);
expect(device.functions).toBeDefined();

expect(device.status).toBeDefined();
}

0 comments on commit 0ef55f5

Please sign in to comment.