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

tap.test('matches manual Homematic CCU entries', async () => {
  const descriptor = createHomematicDiscoveryDescriptor();
  const matcher = descriptor.getMatchers().find((matcherArg) => matcherArg.id === 'homematic-manual-match');
  const result = await matcher!.matches({
    host: '192.168.1.20',
    model: 'Homematic CCU3',
    manufacturer: 'eQ-3',
    interfaceName: 'rf',
  }, {});
  expect(result.matched).toBeTrue();
  expect(result.candidate?.integrationDomain).toEqual('homematic');
  expect(result.candidate?.port).toEqual(2001);
  expect(result.candidate?.metadata?.interfaceName).toEqual('rf');
});

tap.test('matches Homematic metadata records and validates candidates', async () => {
  const descriptor = createHomematicDiscoveryDescriptor();
  const metadataMatcher = descriptor.getMatchers().find((matcherArg) => matcherArg.id === 'homematic-metadata-match');
  const metadataResult = await metadataMatcher!.matches({
    domain: 'homematic',
    name: 'Homematic',
    metadata: { upstreamDomain: 'homematic' },
  }, {});
  expect(metadataResult.matched).toBeTrue();
  expect(metadataResult.candidate?.manufacturer).toEqual('eQ-3');

  const validator = descriptor.getValidators()[0];
  const validation = await validator.validate({
    source: 'manual',
    integrationDomain: 'homematic',
    host: 'ccu3.local',
    manufacturer: 'eQ-3',
    model: 'CCU3',
  }, {});
  expect(validation.matched).toBeTrue();
  expect(validation.candidate?.port).toEqual(2001);
});

export default tap.start();
