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

tap.test('matches IKEA TRADFRI HomeKit mDNS records', async () => {
  const descriptor = createTradfriDiscoveryDescriptor();
  const matcher = descriptor.getMatchers()[0];
  const result = await matcher.matches({
    type: '_hap._tcp.local.',
    name: 'TRADFRI-Gateway._hap._tcp.local.',
    host: 'tradfri-gateway.local',
    port: 8080,
    txt: {
      md: 'TRADFRI',
      id: 'AA:BB:CC:DD:EE:FF',
    },
  }, {});
  expect(result.matched).toBeTrue();
  expect(result.normalizedDeviceId).toEqual('AA:BB:CC:DD:EE:FF');
  expect(result.candidate?.host).toEqual('tradfri-gateway.local');
  expect(result.candidate?.port).toEqual(5684);
});

tap.test('matches and validates manual host/security-code entries', async () => {
  const descriptor = createTradfriDiscoveryDescriptor();
  const manualMatcher = descriptor.getMatchers()[1];
  const validator = descriptor.getValidators()[0];
  const result = await manualMatcher.matches({
    host: '192.168.1.23',
    securityCode: 'abc123',
  }, {});
  expect(result.matched).toBeTrue();
  expect(result.candidate?.metadata?.securityCode).toEqual('abc123');
  const validation = await validator.validate(result.candidate!, {});
  expect(validation.matched).toBeTrue();
});

export default tap.start();
