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

tap.test('matches manual RFLink serial gateway entries', async () => {
  const descriptor = createRflinkDiscoveryDescriptor();
  const matcher = descriptor.getMatchers()[0];
  const result = await matcher.matches({
    connectionType: 'serial',
    port: '/dev/serial/by-id/usb-Nodo_RFLink_Gateway',
    baudRate: 57600,
    name: 'RFLink Gateway',
  }, {});
  expect(result.matched).toBeTrue();
  expect(result.candidate?.integrationDomain).toEqual('rflink');
  expect(result.candidate?.metadata?.connectionType).toEqual('serial');
  expect(result.candidate?.metadata?.serialPort).toEqual('/dev/serial/by-id/usb-Nodo_RFLink_Gateway');
});

tap.test('matches manual RFLink TCP bridge entries', async () => {
  const descriptor = createRflinkDiscoveryDescriptor();
  const matcher = descriptor.getMatchers()[0];
  const result = await matcher.matches({
    connectionType: 'tcp',
    host: '192.168.1.34',
    port: 1234,
    metadata: { rflink: true },
  }, {});
  expect(result.matched).toBeTrue();
  expect(result.candidate?.host).toEqual('192.168.1.34');
  expect(result.candidate?.port).toEqual(1234);
  expect(result.candidate?.metadata?.connectionType).toEqual('tcp');
});

tap.test('validates RFLink gateway candidates', async () => {
  const descriptor = createRflinkDiscoveryDescriptor();
  const validator = descriptor.getValidators()[0];
  const result = await validator.validate({
    source: 'manual',
    integrationDomain: 'rflink',
    name: 'RFLink over TCP',
    host: '192.168.1.35',
    port: 1234,
    metadata: { connectionType: 'tcp' },
  }, {});
  expect(result.matched).toBeTrue();
  expect(result.normalizedDeviceId).toEqual('192.168.1.35');
});

export default tap.start();
