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

tap.test('matches Frontier Silicon SSDP FSAPI advertisements', async () => {
  const descriptor = createFrontierSiliconDiscoveryDescriptor();
  const matcher = descriptor.getMatchers()[0];
  const result = await matcher.matches({
    st: frontierSiliconSsdpSt,
    usn: 'uuid:radio-uuid::urn:schemas-frontier-silicon-com:undok:fsapi:1',
    location: 'http://192.168.1.90:80/device',
    headers: {
      'SPEAKER-NAME': 'Kitchen Radio',
    },
    upnp: {
      manufacturer: 'Hama',
      modelName: 'DIR3100',
    },
  }, {});

  expect(result.matched).toBeTrue();
  expect(result.confidence).toEqual('certain');
  expect(result.normalizedDeviceId).toEqual('radio-uuid');
  expect(result.candidate?.host).toEqual('192.168.1.90');
  expect(result.candidate?.port).toEqual(80);
  expect(result.candidate?.name).toEqual('Kitchen Radio');
  expect(result.candidate?.metadata?.deviceUrl).toEqual('http://192.168.1.90:80/device');

  const miss = await matcher.matches({ st: 'urn:schemas-upnp-org:device:MediaRenderer:1', location: 'http://192.168.1.91/device.xml' }, {});
  expect(miss.matched).toBeFalse();
});

tap.test('matches and validates manual Frontier Silicon candidates', async () => {
  const descriptor = createFrontierSiliconDiscoveryDescriptor();
  const manual = await descriptor.getMatchers()[1].matches({ host: '192.168.1.92', name: 'Manual FSAPI Radio', pin: '9999' }, {});
  expect(manual.matched).toBeTrue();
  expect(manual.candidate?.host).toEqual('192.168.1.92');
  expect(manual.candidate?.metadata?.frontierSilicon).toBeTrue();

  const validation = await descriptor.getValidators()[0].validate(manual.candidate!, {});
  expect(validation.matched).toBeTrue();
  expect(validation.normalizedDeviceId).toEqual('192.168.1.92:80');
});

export default tap.start();
