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

tap.test('matches OwnTone zeroconf records with supported mtd-version', async () => {
  const descriptor = createForkedDaapdDiscoveryDescriptor();
  const result = await descriptor.getMatchers()[0].matches({
    name: 'Living Room._daap._tcp.local.',
    type: '_daap._tcp.local.',
    host: 'living-room.local',
    port: 3689,
    txt: {
      'Machine Name': 'Living Room',
      'mtd-version': '28.0.0',
    },
  }, {});

  expect(result.matched).toBeTrue();
  expect(result.confidence).toEqual('certain');
  expect(result.normalizedDeviceId).toEqual('Living Room');
  expect(result.candidate?.integrationDomain).toEqual('forked_daapd');
  expect(result.candidate?.host).toEqual('living-room.local');
});

tap.test('rejects old DAAP services and validates manual OwnTone candidates', async () => {
  const descriptor = createForkedDaapdDiscoveryDescriptor();
  const oldServer = await descriptor.getMatchers()[0].matches({
    type: '_daap._tcp.local.',
    host: 'old.local',
    txt: { 'Machine Name': 'Old DAAP', 'mtd-version': '26.9.0' },
  }, {});
  expect(oldServer.matched).toBeFalse();

  const manual = await descriptor.getMatchers()[1].matches({ host: '192.168.1.40', name: 'Manual OwnTone' }, {});
  expect(manual.matched).toBeTrue();
  expect(manual.candidate?.port).toEqual(3689);

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

export default tap.start();
