UNPKG

@shadman-a/homebridge-my-ac

Version:

A Homebridge plugin for controlling/monitoring LG ThinQ devices via LG ThinQ platform.

53 lines 2.6 kB
import { describe, test, expect, beforeEach, jest } from '@jest/globals'; import { ThinQ } from '../lib/ThinQ.js'; import { ValueType } from '../lib/DeviceModel.js'; describe('ThinQ.deviceControl coercion', () => { let thinq; beforeEach(() => { // Minimal fake platform/config/logger to instantiate ThinQ const platform = { api: { user: { storagePath: () => process.cwd() } } }; const config = { country: 'US', language: 'en' }; const logger = { debug: () => { }, info: () => { }, warn: () => { }, error: () => { } }; thinq = new ThinQ(platform, config, logger); // stub out network calls thinq.api = { sendCommandToDevice: jest.fn(async () => ({ resultCode: '0000' })), }; }); test('coerces Bit value (boolean -> 1)', async () => { // fake model that reports key 'power' as Bit const fakeModel = { value: (k) => (k === 'power' ? { type: ValueType.Bit } : null), }; thinq.deviceModel.dev1 = fakeModel; const values = { dataKey: 'power', dataValue: true }; await thinq.deviceControl('dev1', values); // assert that api.sendCommandToDevice received coerced numeric value expect(thinq.api.sendCommandToDevice.mock.calls.length).toBe(1); const sentValues = thinq.api.sendCommandToDevice.mock.calls[0][1]; expect(sentValues.dataValue).toBe(1); }); test('coerces Range value (string -> number)', async () => { const fakeModel = { value: (k) => (k === 'temp' ? { type: ValueType.Range } : null), }; thinq.deviceModel.dev2 = fakeModel; const values = { dataSetList: { temp: '23' } }; await thinq.deviceControl('dev2', values); const sentValues2 = thinq.api.sendCommandToDevice.mock.calls[0][1]; expect(typeof sentValues2.dataSetList.temp).toBe('number'); expect(sentValues2.dataSetList.temp).toBe(23); }); test('maps Enum label to enum key using model.enumValue', async () => { const fakeModel = { value: (k) => (k === 'mode' ? { type: ValueType.Enum } : null), enumValue: (k, name) => (k === 'mode' && name === 'Cool' ? '1' : null), }; thinq.deviceModel.dev3 = fakeModel; const values = { dataSetList: { mode: 'Cool' } }; await thinq.deviceControl('dev3', values); const sentValues3 = thinq.api.sendCommandToDevice.mock.calls[0][1]; expect(sentValues3.dataSetList.mode).toBe('1'); }); }); //# sourceMappingURL=deviceControl.coercion.spec.js.map