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

tap.test('matches manual Android IP Webcam URL entries', async () => {
  const descriptor = createAndroidIpWebcamDiscoveryDescriptor();
  const matcher = descriptor.getMatchers()[0];
  const match = await matcher.matches({ url: 'http://192.168.1.20:8080', name: 'Kitchen Phone' }, {});

  expect(match.matched).toBeTrue();
  expect(match.candidate?.integrationDomain).toEqual('android_ip_webcam');
  expect(match.candidate?.host).toEqual('192.168.1.20');
  expect(match.candidate?.port).toEqual(8080);
  expect(match.candidate?.metadata?.url).toEqual('http://192.168.1.20:8080');

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

tap.test('matches manual Android IP Webcam host entries and configures flow', async () => {
  const descriptor = createAndroidIpWebcamDiscoveryDescriptor();
  const matcher = descriptor.getMatchers()[0];
  const match = await matcher.matches({ host: '192.168.1.21', name: 'Desk Phone' }, {});
  expect(match.matched).toBeTrue();
  expect(match.candidate?.port).toEqual(8080);

  const flow = new AndroidIpWebcamConfigFlow();
  const step = await flow.start(match.candidate!, {});
  const done = await step.submit!({ username: 'user', password: 'pass' });
  expect(done.kind).toEqual('done');
  expect(done.config?.host).toEqual('192.168.1.21');
  expect(done.config?.port).toEqual(8080);
  expect(done.config?.username).toEqual('user');
});

export default tap.start();
