import { expect, tap } from '@git.zone/tstest/tapbundle';
import { AsuswrtMapper, type IAsuswrtSnapshot } from '../../ts/integrations/asuswrt/index.js';

const snapshot: IAsuswrtSnapshot = {
  connected: true,
  updatedAt: '2026-01-01T00:00:00.000Z',
  router: {
    host: '192.168.1.1',
    protocol: 'https',
    name: 'Main Router',
    model: 'RT-AX88U',
    firmware: '3.0.0.4',
    labelMac: 'AA:BB:CC:DD:EE:FF',
    actions: ['reboot'],
  },
  devices: [
    {
      mac: '11:22:33:44:55:66',
      name: 'Kitchen Phone',
      ipAddress: '192.168.1.40',
      connected: true,
      connectedTo: 'wl0.1',
      actions: ['reconnect'],
    },
  ],
  interfaces: [
    {
      name: 'eth0',
      label: 'WAN',
      connected: true,
      rxBytes: 2_000_000_000,
      txBytes: 1_000_000_000,
      rxRate: 250_000,
      txRate: 125_000,
    },
  ],
  sensors: {
    sensor_connected_device: 1,
    sensor_rx_bytes: 2_000_000_000,
    sensor_tx_bytes: 1_000_000_000,
    sensor_rx_rates: 250_000,
    sensor_tx_rates: 125_000,
    sensor_load_avg1: 0.12,
    '2.4GHz': 42,
    CPU: 61,
    mem_usage_perc: 35,
    mem_free: 262_144,
    sensor_uptime: 3600,
  },
  actions: [],
};

tap.test('maps ASUSWRT router, tracker, interface, and traffic sensors', async () => {
  const devices = AsuswrtMapper.toDevices(snapshot);
  const entities = AsuswrtMapper.toEntities(snapshot);

  expect(devices.some((deviceArg) => deviceArg.id === 'asuswrt.router.aa_bb_cc_dd_ee_ff')).toBeTrue();
  expect(devices.some((deviceArg) => deviceArg.id === 'asuswrt.client.11_22_33_44_55_66')).toBeTrue();
  expect(entities.find((entityArg) => entityArg.id === 'sensor.main_router_download')?.state).toEqual(2);
  expect(entities.find((entityArg) => entityArg.id === 'sensor.main_router_download_speed')?.state).toEqual(2);
  expect(entities.find((entityArg) => entityArg.id === 'sensor.main_router_wan_download')?.state).toEqual(2);
  expect(entities.find((entityArg) => entityArg.id === 'sensor.main_router_wan_upload_speed')?.state).toEqual(1);
  expect(entities.find((entityArg) => entityArg.id === 'binary_sensor.kitchen_phone_connected')?.state).toEqual('on');
  expect(entities.find((entityArg) => entityArg.id === 'sensor.main_router_memory_free')?.state).toEqual(256);
});

tap.test('maps only explicitly represented ASUSWRT actions to commands', async () => {
  const rebootCommand = AsuswrtMapper.commandForService(snapshot, {
    domain: 'asuswrt',
    service: 'reboot',
    target: {},
  });
  const reconnectCommand = AsuswrtMapper.commandForService(snapshot, {
    domain: 'asuswrt',
    service: 'reconnect_device',
    target: {},
    data: { mac: '11-22-33-44-55-66' },
  });
  const unsupportedBlock = AsuswrtMapper.commandForService(snapshot, {
    domain: 'asuswrt',
    service: 'block_device',
    target: {},
    data: { mac: '11:22:33:44:55:66' },
  });

  expect(rebootCommand?.type).toEqual('router.reboot');
  expect(reconnectCommand?.type).toEqual('client.action');
  expect(reconnectCommand?.mac).toEqual('11:22:33:44:55:66');
  expect(unsupportedBlock).toBeUndefined();
});

export default tap.start();
