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

tap.test('matches Bond zeroconf records', async () => {
  const descriptor = createBondDiscoveryDescriptor();
  const matcher = descriptor.getMatchers().find((matcherArg) => matcherArg.id === 'bond-zeroconf-match');
  const result = await matcher!.matches({
    name: 'ZZ12345._bond._tcp.local.',
    type: '_bond._tcp.local.',
    host: 'bond-zz12345.local',
    port: 80,
    txt: {
      bondid: 'ZZ12345',
      target: 'zermatt',
    },
  }, {});

  expect(result.matched).toBeTrue();
  expect(result.normalizedDeviceId).toEqual('ZZ12345');
  expect(result.candidate?.host).toEqual('bond-zz12345.local');
});

tap.test('matches Bond DHCP leases from Home Assistant manifest patterns', async () => {
  const descriptor = createBondDiscoveryDescriptor();
  const matcher = descriptor.getMatchers().find((matcherArg) => matcherArg.id === 'bond-dhcp-match');
  const result = await matcher!.matches({
    hostname: 'bond-zz12345',
    ip: '192.168.1.45',
    macaddress: '3c:6a:2c:1a:2b:3c',
  }, {});

  expect(result.matched).toBeTrue();
  expect(result.normalizedDeviceId).toEqual('ZZ12345');
  expect(result.candidate?.source).toEqual('dhcp');
});

tap.test('validates manual Bond candidates with local endpoints', async () => {
  const descriptor = createBondDiscoveryDescriptor();
  const matcher = descriptor.getMatchers().find((matcherArg) => matcherArg.id === 'bond-manual-match');
  const validator = descriptor.getValidators()[0];
  const match = await matcher!.matches({ host: '192.168.1.46', accessToken: 'token', name: 'Patio Bond' }, {});
  const validation = await validator.validate(match.candidate!, {});

  expect(match.matched).toBeTrue();
  expect(validation.matched).toBeTrue();
  expect(match.candidate?.metadata?.accessToken).toEqual('token');
});

export default tap.start();
