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

tap.test('matches ONVIF WS-Discovery camera records', async () => {
  const descriptor = createOnvifDiscoveryDescriptor();
  const matcher = descriptor.getMatchers()[0];
  const result = await matcher.matches({
    epr: 'urn:uuid:camera-001',
    xaddrs: ['http://192.168.1.50:8899/onvif/device_service'],
    types: ['dn:NetworkVideoTransmitter'],
    scopes: [
      'onvif://www.onvif.org/Profile/Streaming',
      'onvif://www.onvif.org/name/Driveway%20Camera',
      'onvif://www.onvif.org/hardware/IPC-123',
      'onvif://www.onvif.org/mac/AA-BB-CC-11-22-33',
    ],
  }, {});

  expect(result.matched).toBeTrue();
  expect(result.candidate?.host).toEqual('192.168.1.50');
  expect(result.candidate?.port).toEqual(8899);
  expect(result.candidate?.name).toEqual('Driveway Camera');
  expect(result.normalizedDeviceId).toEqual('aa:bb:cc:11:22:33');
});

tap.test('matches ONVIF mDNS camera records', async () => {
  const descriptor = createOnvifDiscoveryDescriptor();
  const matcher = descriptor.getMatchers()[1];
  const result = await matcher.matches({
    type: '_onvif._tcp.local.',
    name: 'Porch Camera._onvif._tcp.local.',
    host: 'porch-camera.local',
    port: 80,
    txt: {
      model: 'IPC-321',
      mac: '00:11:22:33:44:55',
    },
  }, {});

  expect(result.matched).toBeTrue();
  expect(result.candidate?.integrationDomain).toEqual('onvif');
  expect(result.normalizedDeviceId).toEqual('00:11:22:33:44:55');
});

tap.test('validates manual ONVIF candidates', async () => {
  const descriptor = createOnvifDiscoveryDescriptor();
  const manualMatcher = descriptor.getMatchers()[2];
  const validator = descriptor.getValidators()[0];
  const manual = await manualMatcher.matches({
    host: '192.168.1.51',
    port: 80,
    name: 'Garage Camera',
    deviceInfo: {
      manufacturer: 'ExampleCam',
      model: 'Model S',
      serialNumber: 'SN123',
    },
    profiles: [],
  }, {});
  const validated = await validator.validate(manual.candidate!, {});

  expect(manual.matched).toBeTrue();
  expect(validated.matched).toBeTrue();
  expect(validated.metadata?.manualSupported).toEqual(true);
});

export default tap.start();
