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

tap.test('matches manual Jellyfin server URLs', async () => {
  const descriptor = createJellyfinDiscoveryDescriptor();
  const matcher = descriptor.getMatchers().find((matcherArg) => matcherArg.id === 'jellyfin-manual-match');
  const result = await matcher?.matches({
    url: 'http://jellyfin.local:8096',
    name: 'Home Jellyfin',
  }, {});
  expect(result?.matched).toBeTrue();
  expect(result?.candidate?.host).toEqual('jellyfin.local');
  expect(result?.candidate?.port).toEqual(8096);
});

tap.test('matches Jellyfin SSDP records', async () => {
  const descriptor = createJellyfinDiscoveryDescriptor();
  const matcher = descriptor.getMatchers().find((matcherArg) => matcherArg.id === 'jellyfin-ssdp-match');
  const result = await matcher?.matches({
    st: 'urn:schemas-upnp-org:device:MediaServer:1',
    usn: 'uuid:jellyfin-server-1::urn:schemas-upnp-org:device:MediaServer:1',
    location: 'http://192.168.1.20:8096/dlna/server/description.xml',
    server: 'Jellyfin/10.10.7 UPnP/1.0',
    headers: {
      manufacturer: 'Jellyfin',
      modelName: 'Jellyfin Server',
    },
  }, {});
  expect(result?.matched).toBeTrue();
  expect(result?.normalizedDeviceId).toEqual('jellyfin-server-1');
  expect(result?.candidate?.host).toEqual('192.168.1.20');
});

tap.test('validates Jellyfin candidates', async () => {
  const descriptor = createJellyfinDiscoveryDescriptor();
  const validator = descriptor.getValidators()[0];
  const result = await validator.validate({
    source: 'manual',
    integrationDomain: 'jellyfin',
    host: '192.168.1.20',
    port: 8096,
  }, {});
  expect(result.matched).toBeTrue();
  expect(result.confidence).toEqual('high');
});

export default tap.start();
