import { expect, tap } from '@git.zone/tstest/tapbundle';
import { ZwaveJsMapper } from '../../ts/integrations/zwave_js/index.js';

const snapshot = ZwaveJsMapper.toSnapshot({
  version: { serverVersion: '1.39.0', driverVersion: '15.0.0', homeId: 123456 },
  controller: { homeId: 123456, ownNodeId: 1 },
  nodes: [{
    nodeId: 2,
    name: 'Living Room Switch',
    manufacturer: 'Zooz',
    productLabel: 'ZEN71',
    status: 'alive',
    ready: true,
    values: {
      '37-0-currentValue': {
        commandClass: 37,
        commandClassName: 'Binary Switch',
        endpoint: 0,
        property: 'currentValue',
        metadata: { label: 'Switch', type: 'boolean', readable: true, writeable: true },
        value: true,
      },
      '50-0-value': {
        commandClass: 50,
        commandClassName: 'Meter',
        endpoint: 0,
        property: 'value',
        propertyKey: 'Electric_kWh_Consumed',
        metadata: { label: 'Electric consumption', type: 'number', readable: true, writeable: false, unit: 'kWh' },
        value: 12.4,
      },
    },
  }],
});

tap.test('maps Z-Wave JS nodes and values to devices and entities', async () => {
  const devices = ZwaveJsMapper.toDevices(snapshot);
  const entities = ZwaveJsMapper.toEntities(snapshot);
  expect(devices.some((deviceArg) => deviceArg.id === 'zwave_js.node.2')).toBeTrue();
  expect(entities.some((entityArg) => entityArg.platform === 'switch' && entityArg.state === 'on')).toBeTrue();
  expect(entities.some((entityArg) => entityArg.platform === 'sensor' && entityArg.state === 12.4)).toBeTrue();
});

export default tap.start();
