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

tap.test('matches deCONZ mDNS, SSDP, and manual discovery records', async () => {
  const descriptor = createDeconzDiscoveryDescriptor();
  const mdnsMatcher = descriptor.getMatchers().find((matcherArg) => matcherArg.id === 'deconz-mdns-match')!;
  const ssdpMatcher = descriptor.getMatchers().find((matcherArg) => matcherArg.id === 'deconz-ssdp-match')!;
  const manualMatcher = descriptor.getMatchers().find((matcherArg) => matcherArg.id === 'deconz-manual-match')!;
  const validator = descriptor.getValidators()[0];

  const mdnsResult = await mdnsMatcher.matches({
    name: 'deCONZ-GW',
    type: '_http._tcp.local.',
    host: 'deconz.local',
    port: 80,
    txt: {
      bridgeid: '00212EFFFF00C5FB',
      modelid: 'deCONZ',
    },
  }, {});
  expect(mdnsResult.matched).toBeTrue();
  expect(mdnsResult.normalizedDeviceId).toEqual('00212EFFFF00C5FB');

  const ssdpResult = await ssdpMatcher.matches({
    ssdpLocation: 'http://192.168.1.55:80/description.xml',
    upnp: {
      manufacturer: 'Royal Philips Electronics',
      manufacturerURL: 'http://www.dresden-elektronik.de',
      modelName: 'deCONZ',
      serialNumber: '00:21:2e:ff:ff:00:c5:fb',
    },
  }, {});
  expect(ssdpResult.matched).toBeTrue();
  expect(ssdpResult.candidate?.host).toEqual('192.168.1.55');
  expect(ssdpResult.normalizedDeviceId).toEqual('00212EFFFF00C5FB');

  const manualResult = await manualMatcher.matches({
    host: '192.168.1.56',
    name: 'Phoscon Gateway',
    model: 'ConBee II',
  }, {});
  expect(manualResult.matched).toBeTrue();
  expect(manualResult.candidate?.port).toEqual(80);

  const validationResult = await validator.validate(manualResult.candidate!, {});
  expect(validationResult.matched).toBeTrue();
});

export default tap.start();
