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

tap.test('matches Axis mDNS records by service type and OUI', async () => {
  const descriptor = createAxisDiscoveryDescriptor();
  const matcher = descriptor.getMatchers().find((matcherArg) => matcherArg.id === 'axis-mdns-match');
  const result = await matcher!.matches({
    type: '_axis-video._tcp.local.',
    name: 'AXIS P3265._axis-video._tcp.local.',
    host: 'axis-p3265.local',
    port: 80,
    txt: {
      macaddress: 'ACCC8E123456',
    },
  }, {});
  expect(result.matched).toBeTrue();
  expect(result.normalizedDeviceId).toEqual('accc8e123456');
  expect(result.candidate?.integrationDomain).toEqual('axis');
});

tap.test('matches Axis SSDP records by manufacturer', async () => {
  const descriptor = createAxisDiscoveryDescriptor();
  const matcher = descriptor.getMatchers().find((matcherArg) => matcherArg.id === 'axis-ssdp-match');
  const result = await matcher!.matches({
    manufacturer: 'AXIS',
    location: 'http://192.168.1.50:80/',
    upnp: {
      friendlyName: 'AXIS Door Station',
      serialNumber: '00408C654321',
      modelName: 'I8116-E',
    },
  }, {});
  expect(result.matched).toBeTrue();
  expect(result.candidate?.host).toEqual('192.168.1.50');
  expect(result.candidate?.model).toEqual('I8116-E');
});

tap.test('validates manual Axis candidates', async () => {
  const descriptor = createAxisDiscoveryDescriptor();
  const matcher = descriptor.getMatchers().find((matcherArg) => matcherArg.id === 'axis-manual-match');
  const match = await matcher!.matches({ host: 'axis.local', protocol: 'http', model: 'AXIS P3265' }, {});
  expect(match.matched).toBeTrue();

  const validator = descriptor.getValidators()[0];
  const validation = await validator.validate(match.candidate!, {});
  expect(validation.matched).toBeTrue();
  expect(validation.candidate?.manufacturer).toEqual('Axis Communications AB');
});

export default tap.start();
