forked from tuya/tuya-homebridge
-
-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
169 additions
and
125 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 () => { | ||
// | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 () => { | ||
// | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |