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

tap.test('matches Kodi JSON-RPC mDNS records', async () => {
  const descriptor = createKodiDiscoveryDescriptor();
  const matcher = descriptor.getMatchers()[0];
  const result = await matcher.matches({
    type: '_xbmc-jsonrpc-h._tcp.local.',
    name: 'Living Room Kodi._xbmc-jsonrpc-h._tcp.local.',
    host: 'living-room-kodi.local',
    port: 8080,
    txt: {
      uuid: 'kodi-uuid-123',
      version: '21.0',
    },
  }, {});
  expect(result.matched).toBeTrue();
  expect(result.candidate?.host).toEqual('living-room-kodi.local');
  expect(result.candidate?.port).toEqual(8080);
  expect(result.normalizedDeviceId).toEqual('kodi-uuid-123');
});

tap.test('matches manual Kodi host entries', async () => {
  const descriptor = createKodiDiscoveryDescriptor();
  const matcher = descriptor.getMatchers()[1];
  const result = await matcher.matches({
    host: '192.168.1.70',
    name: 'Living Room Kodi',
  }, {});
  expect(result.matched).toBeTrue();
  expect(result.candidate?.host).toEqual('192.168.1.70');
  expect(result.candidate?.port).toEqual(8080);
});

tap.test('validates Kodi candidates', async () => {
  const descriptor = createKodiDiscoveryDescriptor();
  const validator = descriptor.getValidators()[0];
  const result = await validator.validate({
    source: 'mdns',
    integrationDomain: 'kodi',
    host: '192.168.1.71',
    port: 8080,
  }, {});
  expect(result.matched).toBeTrue();
  expect(result.confidence).toEqual('high');
});

export default tap.start();
