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

tap.test('matches Volumio zeroconf records', async () => {
  const descriptor = createVolumioDiscoveryDescriptor();
  const matcher = descriptor.getMatchers()[0];
  const result = await matcher.matches({
    type: '_Volumio._tcp.local.',
    name: 'Kitchen._Volumio._tcp.local.',
    host: 'kitchen-volumio.local',
    port: 3000,
    txt: {
      volumioName: 'Kitchen Volumio',
      UUID: 'volumio-uuid-123',
    },
  }, {});

  expect(result.matched).toBeTrue();
  expect(result.confidence).toEqual('certain');
  expect(result.normalizedDeviceId).toEqual('volumio-uuid-123');
  expect(result.candidate?.integrationDomain).toEqual('volumio');
  expect(result.candidate?.host).toEqual('kitchen-volumio.local');
  expect(result.candidate?.port).toEqual(3000);
  expect(result.candidate?.name).toEqual('Kitchen Volumio');
});

tap.test('matches manual Volumio host entries and validates candidates', async () => {
  const descriptor = createVolumioDiscoveryDescriptor();
  const matcher = descriptor.getMatchers()[1];
  const matched = await matcher.matches({
    host: '192.168.1.81',
    name: 'Office Volumio',
    uuid: 'manual-volumio-1',
  }, {});

  expect(matched.matched).toBeTrue();
  expect(matched.candidate?.port).toEqual(3000);

  const validator = descriptor.getValidators()[0];
  const validated = await validator.validate(matched.candidate!, {});
  expect(validated.matched).toBeTrue();
  expect(validated.confidence).toEqual('high');
});

tap.test('rejects unrelated mDNS records', async () => {
  const descriptor = createVolumioDiscoveryDescriptor();
  const matcher = descriptor.getMatchers()[0];
  const result = await matcher.matches({
    type: '_http._tcp.local.',
    name: 'Office Printer',
    host: 'printer.local',
  }, {});

  expect(result.matched).toBeFalse();
});

export default tap.start();
