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

tap.test('matches Yamaha MusicCast mDNS records', async () => {
  const descriptor = createYamahaMusiccastDiscoveryDescriptor();
  const matcher = descriptor.getMatchers()[0];
  const result = await matcher.matches({
    name: 'Living Room MusicCast',
    type: '_http._tcp.local.',
    host: 'yamaha-rx.local',
    port: 80,
    txt: {
      manufacturer: 'Yamaha Corporation',
      model: 'RX-V685',
      system_id: '03E88CF3',
      device_id: '4C1B86A6CBF5',
    },
  }, {});

  expect(result.matched).toBeTrue();
  expect(result.normalizedDeviceId).toEqual('03E88CF3');
  expect(result.candidate?.integrationDomain).toEqual('yamaha_musiccast');
  expect(result.candidate?.host).toEqual('yamaha-rx.local');
});

tap.test('matches manual Yamaha MusicCast entries and validates candidates', async () => {
  const descriptor = createYamahaMusiccastDiscoveryDescriptor();
  const matcher = descriptor.getMatchers()[2];
  const matched = await matcher.matches({
    host: '192.168.1.70',
    name: 'Kitchen MusicCast',
    model: 'WX-021',
    systemId: 'ABCD1234',
  }, {});

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

  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 = createYamahaMusiccastDiscoveryDescriptor();
  const matcher = descriptor.getMatchers()[0];
  const result = await matcher.matches({
    name: 'Office Printer',
    type: '_ipp._tcp.local.',
    host: 'printer.local',
    txt: { manufacturer: 'Brother' },
  }, {});

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

export default tap.start();
