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

tap.test('matches WiZ mDNS, UDP, and manual discovery records', async () => {
  const descriptor = createWizDiscoveryDescriptor();
  const mdnsMatcher = descriptor.getMatchers()[0];
  const udpMatcher = descriptor.getMatchers()[1];
  const manualMatcher = descriptor.getMatchers()[2];

  const mdnsResult = await mdnsMatcher.matches({
    type: '_http._tcp.local.',
    name: 'wiz_a8bb50a4f94d._http._tcp.local.',
    host: 'wiz-a4f94d.local',
    txt: {
      mac: 'a8bb50a4f94d',
      name: 'Desk Lamp',
    },
  }, {});
  const udpResult = await udpMatcher.matches({
    host: '192.168.1.51',
    response: {
      method: 'registration',
      result: {
        mac: 'a8bb50a4f94d',
      },
    },
  }, {});
  const manualResult = await manualMatcher.matches({
    host: '192.168.1.52',
    name: 'Counter Plug',
    deviceInfo: {
      model: 'WiZ Smart Plug',
    },
  }, {});

  expect(mdnsResult.matched).toBeTrue();
  expect(mdnsResult.normalizedDeviceId).toEqual('a8:bb:50:a4:f9:4d');
  expect(udpResult.matched).toBeTrue();
  expect(udpResult.candidate?.metadata?.discoveryProtocol).toEqual('udp');
  expect(manualResult.matched).toBeTrue();
  expect(manualResult.candidate?.port).toEqual(38899);
});

tap.test('validates WiZ candidates from DHCP-style metadata', async () => {
  const validator = createWizDiscoveryDescriptor().getValidators()[0];
  const result = await validator.validate({
    source: 'dhcp',
    host: 'wiz_a4f94d',
    macAddress: 'A8:BB:50:A4:F9:4D',
    port: 38899,
  }, {});
  expect(result.matched).toBeTrue();
  expect(result.confidence).toEqual('certain');
});

export default tap.start();
