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

tap.test('matches Home Assistant SoundTouch zeroconf records', async () => {
  const descriptor = createSoundtouchDiscoveryDescriptor();
  const result = await descriptor.getMatchers()[0].matches({
    name: 'Living Room SoundTouch',
    type: '_soundtouch._tcp.local.',
    host: 'living-room.local',
    port: 8090,
    txt: { deviceId: 'DEVICE-123', type: 'SoundTouch 20', mac: '00:11:22:33:44:55' },
  }, {});

  expect(result.matched).toBeTrue();
  expect(result.confidence).toEqual('certain');
  expect(result.normalizedDeviceId).toEqual('DEVICE-123');
  expect(result.candidate?.host).toEqual('living-room.local');
  expect(result.candidate?.port).toEqual(8090);
  expect(result.candidate?.manufacturer).toEqual('Bose Corporation');
});

tap.test('matches SoundTouch manual candidates and validates them', async () => {
  const descriptor = createSoundtouchDiscoveryDescriptor();
  const manual = await descriptor.getMatchers()[1].matches({ host: '192.168.1.50', name: 'Living Room' }, {});
  expect(manual.matched).toBeTrue();
  expect(manual.candidate?.port).toEqual(8090);

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

tap.test('rejects unrelated mDNS records', async () => {
  const descriptor = createSoundtouchDiscoveryDescriptor();
  const result = await descriptor.getMatchers()[0].matches({
    name: 'Printer',
    type: '_ipp._tcp.local.',
    host: 'printer.local',
  }, {});
  expect(result.matched).toBeFalse();
});

export default tap.start();
