Files
integrations/test/hlk_sw16/test.hlk_sw16.node.ts
T

73 lines
3.7 KiB
TypeScript

import { expect, tap } from '@git.zone/tstest/tapbundle';
import { HlkSw16Client, HlkSw16ConfigFlow, HlkSw16Integration, HlkSw16Mapper, HomeAssistantHlkSw16Integration, createHlkSw16DiscoveryDescriptor, hlkSw16Profile, type IHlkSw16Snapshot, type THlkSw16RawData } from '../../ts/integrations/hlk_sw16/index.js';
const rawData: THlkSw16RawData = {
relays: {
'0': true,
'1': false,
a: true,
f: false,
},
};
tap.test('matches manual HLK-SW16 candidates and creates config flow output', async () => {
const descriptor = createHlkSw16DiscoveryDescriptor();
const matcher = descriptor.getMatchers().find((matcherArg) => matcherArg.id === 'hlk_sw16-manual-match');
const result = await matcher!.matches({ source: 'manual', host: '192.0.2.16', port: 8080, name: 'HLK-SW16 Board', metadata: { rawData } }, {});
expect(result.matched).toBeTrue();
expect(result.candidate?.integrationDomain).toEqual('hlk_sw16');
const validation = await descriptor.getValidators()[0].validate(result.candidate!, {});
expect(validation.matched).toBeTrue();
const done = await (await new HlkSw16ConfigFlow().start(result.candidate!, {})).submit!({});
expect(done.kind).toEqual('done');
expect(done.config?.host).toEqual('192.0.2.16');
expect(done.config?.port).toEqual(8080);
expect(done.config?.rawData).toEqual(rawData);
});
tap.test('maps HLK-SW16 raw snapshots to runtime devices and entities', async () => {
const client = new HlkSw16Client({ name: 'HLK-SW16 Board', host: '192.0.2.16', rawData, switches: { '0': 'Pump', a: { name: 'Light Circuit' } } });
const snapshot = await client.getSnapshot();
const mappedSnapshot = HlkSw16Mapper.toSnapshotFromRaw({ name: 'HLK-SW16 Board', host: '192.0.2.16', switches: { '0': 'Pump' } }, rawData);
const devices = HlkSw16Mapper.toDevices(mappedSnapshot);
const entities = HlkSw16Mapper.toEntities(mappedSnapshot);
expect(snapshot.online).toBeTrue();
expect(mappedSnapshot.entities.length).toEqual(16);
expect(devices[0].integrationDomain).toEqual('hlk_sw16');
expect(devices[0].manufacturer).toEqual('Hi-Link');
expect(entities.some((entityArg) => entityArg.id === 'switch.hlk_sw16_board_relay_0')).toBeTrue();
expect(entities.some((entityArg) => entityArg.id === 'switch.hlk_sw16_board_relay_a')).toBeTrue();
});
tap.test('exposes HLK-SW16 read-only runtime, HA alias, and unsupported control', async () => {
const integration = new HlkSw16Integration();
const alias = new HomeAssistantHlkSw16Integration();
expect(alias instanceof HlkSw16Integration).toBeTrue();
expect(alias.domain).toEqual('hlk_sw16');
expect(integration.status).toEqual('read-only-runtime');
expect(hlkSw16Profile.metadata.configFlow).toEqual(true);
expect(hlkSw16Profile.metadata.requirements).toEqual(['hlk-sw16==0.0.9']);
expect(hlkSw16Profile.metadata.qualityScale).toBeUndefined();
const runtime = await integration.setup({ name: 'HLK-SW16 Board', host: '192.0.2.16', rawData }, {});
const status = await runtime.callService!({ domain: 'hlk_sw16', service: 'status', target: {} });
const refresh = await runtime.callService!({ domain: 'hlk_sw16', service: 'refresh', target: {} });
const snapshot = status.data as IHlkSw16Snapshot;
expect(status.success).toBeTrue();
expect(refresh.success).toBeTrue();
expect(snapshot.online).toBeTrue();
expect((await runtime.devices())[0].name).toEqual('HLK-SW16 Board');
const command = await runtime.callService!({ domain: 'switch', service: 'turn_on', target: { entityId: 'switch.hlk_sw16_board_relay_0' } });
expect(command.success).toBeFalse();
expect(command.error!).toContain('requires an injected client.execute() or commandExecutor');
await runtime.destroy();
});
export default tap.start();