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

tap.test('matches manual OpenTherm Gateway TCP entries', async () => {
  const descriptor = createOpenthermGwDiscoveryDescriptor();
  const matcher = descriptor.getMatchers()[0];
  const result = await matcher.matches({ host: 'otgw.local', port: 25238, name: 'Boiler OTGW' }, {});
  expect(result.matched).toBeTrue();
  expect(result.candidate?.integrationDomain).toEqual('opentherm_gw');
  expect(result.candidate?.host).toEqual('otgw.local');
  expect(result.candidate?.port).toEqual(25238);
});

tap.test('validates manual OpenTherm Gateway candidates', async () => {
  const descriptor = createOpenthermGwDiscoveryDescriptor();
  const matcher = descriptor.getMatchers()[0];
  const result = await matcher.matches({ host: '192.168.1.40', metadata: { otgw: true } }, {});
  const validator = descriptor.getValidators()[0];
  const validation = await validator.validate(result.candidate!, {});
  expect(validation.matched).toBeTrue();
  expect(validation.normalizedDeviceId).toEqual('192.168.1.40:23');
});

tap.test('rejects unrelated manual entries', async () => {
  const descriptor = createOpenthermGwDiscoveryDescriptor();
  const matcher = descriptor.getMatchers()[0];
  const result = await matcher.matches({ name: 'Kitchen Speaker', model: 'MPD' }, {});
  expect(result.matched).toBeFalse();
});

export default tap.start();
