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

tap.test('matches manual madVR Envy candidates and builds config flow output', async () => {
  const descriptor = createMadvrDiscoveryDescriptor();
  const matcher = descriptor.getMatchers()[0];
  const result = await matcher.matches({
    host: '192.168.1.90',
    name: 'Cinema Envy',
    manufacturer: 'madVR',
    model: 'Envy Extreme',
    macAddress: 'AA:BB:CC:DD:EE:FF',
  }, {});

  expect(result.matched).toBeTrue();
  expect(result.candidate?.port).toEqual(44077);
  expect(result.normalizedDeviceId).toEqual('AA:BB:CC:DD:EE:FF');

  const integration = new MadvrIntegration();
  const step = await integration.configFlow.start(result.candidate!, {});
  const submitted = await step.submit!({ host: '192.168.1.91', port: '44078', name: 'Rack Envy' });
  expect(submitted.kind).toEqual('done');
  expect(submitted.config?.host).toEqual('192.168.1.91');
  expect(submitted.config?.port).toEqual(44078);
  expect(submitted.config?.macAddress).toEqual('AA:BB:CC:DD:EE:FF');
});

tap.test('validates madVR candidates and rejects unrelated records', async () => {
  const descriptor = createMadvrDiscoveryDescriptor();
  const validator = descriptor.getValidators()[0];
  const valid = await validator.validate({
    source: 'manual',
    integrationDomain: 'madvr',
    host: '192.168.1.90',
    model: 'Envy Pro',
  }, {});
  expect(valid.matched).toBeTrue();
  expect(valid.confidence).toEqual('high');

  const matcher = descriptor.getMatchers()[0];
  const unrelated = await matcher.matches({ manufacturer: 'Other', model: 'Receiver' }, {});
  expect(unrelated.matched).toBeFalse();
});

export default tap.start();
