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

const arpOutput = `show ip arp
Protocol  Address          Age (min)  Hardware Addr   Type   Interface
Internet  10.10.11.1       -          0027.d32d.0123  ARPA   GigabitEthernet0
Internet  10.10.11.20      0          001d.ec02.07ab  ARPA   Vlan1
Internet  10.10.11.30      1          aabb.ccdd.eeff  ARPA   Vlan1
Internet  10.10.11.40      0          Incomplete      ARPA   Vlan1
`;

tap.test('parses Cisco IOS ARP output using Home Assistant presence semantics', async () => {
  const entries = CiscoIosMapper.parseArpOutput(arpOutput);
  const tracked = CiscoIosMapper.trackedDevicesFromEntries(entries);

  expect(CiscoIosMapper.parseCiscoMacAddress('001d.ec02.07ab')).toEqual('00:1D:EC:02:07:AB');
  expect(entries.length).toEqual(3);
  expect(entries[0].macAddress).toEqual('00:27:D3:2D:01:23');
  expect(entries[0].present).toBeFalse();
  expect(entries[1].present).toBeTrue();
  expect(entries[2].present).toBeFalse();
  expect(tracked.length).toEqual(1);
  expect(tracked[0].macAddress).toEqual('00:1D:EC:02:07:AB');
});

tap.test('maps Cisco IOS snapshots to devices and entities', async () => {
  const snapshot = CiscoIosMapper.toSnapshot({ host: '192.168.1.1', name: 'Core Router', username: 'admin', arpOutput });
  const devices = CiscoIosMapper.toDevices(snapshot);
  const entities = CiscoIosMapper.toEntities(snapshot);

  expect(snapshot.connected).toBeTrue();
  expect(snapshot.source).toEqual('manual');
  expect(snapshot.device.configurationUrl).toEqual('ssh://192.168.1.1:22');
  expect(snapshot.trackedDevices.length).toEqual(1);
  expect(devices.length).toEqual(2);
  expect(devices[0].id).toEqual('cisco_ios.router.192_168_1_1');
  expect(devices[0].state.find((stateArg) => stateArg.featureId === 'tracked_devices')?.value).toEqual(1);
  expect(entities.find((entityArg) => entityArg.id === 'sensor.core_router_tracked_devices')?.state).toEqual(1);
  expect(entities.find((entityArg) => entityArg.id === 'binary_sensor.00_1d_ec_02_07_ab_present')?.state).toEqual('on');
});

tap.test('maps Cisco IOS show ip arp service commands', async () => {
  const snapshot = CiscoIosMapper.toSnapshot({ host: '192.168.1.1', name: 'Core Router', username: 'admin', arpOutput });
  const command = CiscoIosMapper.commandForService(snapshot, { domain: 'cisco_ios', service: 'show_ip_arp', target: {}, data: {} });
  const custom = CiscoIosMapper.commandForService(snapshot, { domain: 'cisco_ios', service: 'run_command', target: {}, data: { command: 'show version' } });

  expect(command?.action).toEqual('show_ip_arp');
  expect(command?.command).toEqual('show ip arp');
  expect(custom?.action).toEqual('run_command');
  expect(custom?.command).toEqual('show version');
});

export default tap.start();
