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

tap.test('matches Samsung TV SSDP MainTVAgent records', async () => {
  const descriptor = createSamsungtvDiscoveryDescriptor();
  const matcher = descriptor.getMatchers()[0];
  const result = await matcher.matches({
    st: 'urn:samsung.com:service:MainTVAgent2:1',
    usn: 'uuid:tv-udn-123::urn:samsung.com:service:MainTVAgent2:1',
    location: 'http://192.168.1.55:8001/api/v2/',
    headers: {
      manufacturer: 'Samsung Electronics',
      modelName: 'QN90A',
    },
  }, {});

  expect(result.matched).toBeTrue();
  expect(result.candidate?.host).toEqual('192.168.1.55');
  expect(result.normalizedDeviceId).toEqual('tv-udn-123');
  expect(result.candidate?.metadata?.ssdpMainTvAgentLocation).toEqual('http://192.168.1.55:8001/api/v2/');
});

tap.test('matches Samsung TV mDNS AirPlay records', async () => {
  const descriptor = createSamsungtvDiscoveryDescriptor();
  const matcher = descriptor.getMatchers()[1];
  const result = await matcher.matches({
    type: '_airplay._tcp.local.',
    name: 'Living Room TV',
    host: 'living-room-tv.local',
    port: 7000,
    properties: {
      manufacturer: 'Samsung Electronics',
      deviceid: 'AA:BB:CC:DD:EE:FF',
      model: 'Tizen TV',
    },
  }, {});

  expect(result.matched).toBeTrue();
  expect(result.candidate?.host).toEqual('living-room-tv.local');
  expect(result.candidate?.port).toEqual(8001);
  expect(result.normalizedDeviceId).toEqual('AA:BB:CC:DD:EE:FF');
});

tap.test('validates Samsung TV candidates', async () => {
  const descriptor = createSamsungtvDiscoveryDescriptor();
  const validator = descriptor.getValidators()[0];
  const result = await validator.validate({
    source: 'manual',
    integrationDomain: 'samsungtv',
    host: '192.168.1.55',
    manufacturer: 'Samsung',
  }, {});

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

export default tap.start();
