import { expect, tap } from '@git.zone/tstest/tapbundle'; import { UnifiMapper, type IUnifiSnapshot } from '../../ts/integrations/unifi/index.js'; const now = Math.floor(Date.now() / 1000); const snapshot: IUnifiSnapshot = { connected: true, host: '192.168.1.1', port: 443, site: 'default', controller: { id: 'controller-1', name: 'UniFi Network', version: '9.0.0', connected: true, }, sites: [{ id: 'site-1', name: 'default', description: 'Default' }], clients: [ { mac: 'aa:bb:cc:dd:ee:ff', name: 'Kitchen Phone', ip: '192.168.1.55', essid: 'Guest WiFi', is_wired: false, blocked: false, last_seen: now, ap_mac: 'b4:fb:e4:12:34:56', 'rx_bytes-r': 2000000, 'tx_bytes-r': 1000000, rssi: -55, }, ], devices: [ { mac: 'b4:fb:e4:12:34:56', name: 'Switch 24', model: 'USW-24-PoE', version: '6.6.1', ip: '192.168.1.10', state: 1, uptime: 3600, general_temperature: 42, 'system-stats': { cpu: '12.5', mem: '31.1' }, port_table: [ { deviceMac: 'b4:fb:e4:12:34:56', port_idx: 1, name: 'Office AP', enable: true, up: true, port_poe: true, poe_mode: 'auto', poe_power: '7.2', speed: 1000, 'rx_bytes-r': 1024, 'tx_bytes-r': 2048, }, ], }, ], wlans: [ { _id: 'wlan-1', name: 'Guest WiFi', enabled: true, security: 'wpapsk', is_guest: true, }, ], ports: [], events: [], }; tap.test('maps UniFi clients, devices, WLANs, and ports', async () => { const devices = UnifiMapper.toDevices(snapshot); const entities = UnifiMapper.toEntities(snapshot); expect(devices.some((deviceArg) => deviceArg.id === 'unifi.client.aa_bb_cc_dd_ee_ff')).toBeTrue(); expect(devices.some((deviceArg) => deviceArg.id === 'unifi.device.b4_fb_e4_12_34_56')).toBeTrue(); expect(devices.some((deviceArg) => deviceArg.id === 'unifi.wlan.wlan_1')).toBeTrue(); expect(entities.find((entityArg) => entityArg.id === 'binary_sensor.kitchen_phone_connected')?.state).toEqual('on'); expect(entities.find((entityArg) => entityArg.id === 'switch.guest_wifi')?.state).toEqual('on'); expect(entities.find((entityArg) => entityArg.id === 'switch.switch_24_office_ap_poe')?.state).toEqual('on'); expect(entities.find((entityArg) => entityArg.id === 'sensor.switch_24_office_ap_poe_power')?.state).toEqual(7.2); }); tap.test('maps services to UniFi commands', async () => { const blockCommand = UnifiMapper.commandForService(snapshot, { domain: 'unifi', service: 'block_client', target: {}, data: { mac: 'aa:bb:cc:dd:ee:ff' }, }); const wlanCommand = UnifiMapper.commandForService(snapshot, { domain: 'switch', service: 'turn_off', target: { entityId: 'switch.guest_wifi' }, }); const poeCommand = UnifiMapper.commandForService(snapshot, { domain: 'switch', service: 'turn_off', target: { entityId: 'switch.switch_24_office_ap_poe' }, }); expect(blockCommand?.type).toEqual('blockClient'); expect(blockCommand?.block).toBeTrue(); expect(wlanCommand?.type).toEqual('setWlanEnabled'); expect(wlanCommand?.enabled).toBeFalse(); expect(poeCommand?.type).toEqual('setPoePortEnabled'); expect(poeCommand?.portIdx).toEqual('1'); }); export default tap.start();