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

tap.test('matches Dune HD SSDP records', async () => {
  const descriptor = createDunehdDiscoveryDescriptor();
  const matcher = descriptor.getMatchers()[0];
  const result = await matcher.matches({
    st: 'urn:schemas-upnp-org:device:MediaRenderer:1',
    usn: 'uuid:dune-udn-123::urn:schemas-upnp-org:device:MediaRenderer:1',
    location: 'http://192.168.1.70:80/description.xml',
    headers: {
      manufacturer: 'Dune HD',
      modelName: 'Dune HD Pro 4K',
    },
  }, {});

  expect(result.matched).toBeTrue();
  expect(result.candidate?.host).toEqual('192.168.1.70');
  expect(result.candidate?.port).toEqual(80);
  expect(result.normalizedDeviceId).toEqual('dune-udn-123');
});

tap.test('matches manual Dune HD setup entries', async () => {
  const descriptor = createDunehdDiscoveryDescriptor();
  const matcher = descriptor.getMatchers()[2];
  const result = await matcher.matches({
    host: 'dunehd.local',
    name: 'Cinema Dune',
    model: 'Dune HD Real Vision',
  }, {});

  expect(result.matched).toBeTrue();
  expect(result.confidence).toEqual('high');
  expect(result.candidate?.integrationDomain).toEqual('dunehd');
});

tap.test('validates Dune HD candidates', async () => {
  const descriptor = createDunehdDiscoveryDescriptor();
  const validator = descriptor.getValidators()[0];
  const result = await validator.validate({
    source: 'manual',
    integrationDomain: 'dunehd',
    host: '192.168.1.70',
    manufacturer: 'Dune',
  }, {});

  expect(result.matched).toBeTrue();
  expect(result.confidence).toEqual('high');
});

export default tap.start();
