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

tap.test('matches Android TV Remote mDNS advertisements', async () => {
  const descriptor = createAndroidtvRemoteDiscoveryDescriptor();
  const matcher = descriptor.getMatchers()[0];
  const result = await matcher.matches({
    type: '_androidtvremote2._tcp.local.',
    name: 'Living Room TV._androidtvremote2._tcp.local.',
    host: '192.168.1.61',
    port: 6466,
    properties: {
      bt: 'AA:BB:CC:DD:EE:FF',
    },
  }, {});

  expect(result.matched).toBeTrue();
  expect(result.candidate?.integrationDomain).toEqual('androidtv_remote');
  expect(result.candidate?.host).toEqual('192.168.1.61');
  expect(result.candidate?.port).toEqual(6466);
  expect(result.candidate?.macAddress).toEqual('AA:BB:CC:DD:EE:FF');
  expect(result.normalizedDeviceId).toEqual('AA:BB:CC:DD:EE:FF');
});

tap.test('matches manual Android TV Remote host entries', async () => {
  const descriptor = createAndroidtvRemoteDiscoveryDescriptor();
  const matcher = descriptor.getMatchers()[1];
  const result = await matcher.matches({
    host: '192.168.1.62',
    deviceName: 'Bedroom Google TV',
    macAddress: '11:22:33:44:55:66',
  }, {});

  expect(result.matched).toBeTrue();
  expect(result.candidate?.host).toEqual('192.168.1.62');
  expect(result.candidate?.port).toEqual(6466);
  expect(result.candidate?.metadata?.pairPort).toEqual(6467);
});

tap.test('creates manual host config flow entries', async () => {
  const flow = new AndroidtvRemoteConfigFlow();
  const step = await flow.start({ source: 'manual', host: '192.168.1.63', name: 'Office TV' }, {});
  const done = await step.submit?.({ host: '192.168.1.63', enableIme: false });

  expect(done?.kind).toEqual('done');
  expect(done?.config?.host).toEqual('192.168.1.63');
  expect(done?.config?.apiPort).toEqual(6466);
  expect(done?.config?.pairPort).toEqual(6467);
  expect(done?.config?.enableIme).toBeFalse();
});

export default tap.start();
