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

tap.test('matches manual motionEye URL entries and configures flow', async () => {
  const descriptor = createMotionEyeDiscoveryDescriptor();
  const matcher = descriptor.getMatchers()[0];
  const match = await matcher.matches({ url: 'http://192.168.1.40:8765', name: 'Camera Hub' }, {});

  expect(match.matched).toBeTrue();
  expect(match.candidate?.integrationDomain).toEqual('motioneye');
  expect(match.candidate?.host).toEqual('192.168.1.40');
  expect(match.candidate?.port).toEqual(8765);

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

  const flow = new MotionEyeConfigFlow();
  const step = await flow.start(match.candidate!, {});
  const done = await step.submit!({ adminUsername: 'admin', adminPassword: 'secret', surveillanceUsername: 'viewer', surveillancePassword: 'view' });
  expect(done.kind).toEqual('done');
  expect(done.config?.url).toEqual('http://192.168.1.40:8765');
  expect(done.config?.adminUsername).toEqual('admin');
  expect(done.config?.surveillanceUsername).toEqual('viewer');
});

tap.test('matches local HTTP URL candidates on the default motionEye port', async () => {
  const descriptor = createMotionEyeDiscoveryDescriptor();
  const matcher = descriptor.getMatchers()[1];
  const match = await matcher.matches({ url: 'http://192.168.1.41:8765', metadata: { source: 'manual-url-list' } }, {});

  expect(match.matched).toBeTrue();
  expect(match.candidate?.source).toEqual('http');
  expect(match.candidate?.metadata?.url).toEqual('http://192.168.1.41:8765');
});

export default tap.start();
