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

tap.test('matches and validates manual AdGuard Home candidates', async () => {
  const descriptor = createAdguardDiscoveryDescriptor();
  const matcher = descriptor.getMatchers()[0];
  const match = await matcher.matches({
    host: '192.168.1.2',
    port: 3000,
    name: 'AdGuard Home',
  }, {});
  expect(match.matched).toBeTrue();
  expect(match.candidate?.integrationDomain).toEqual('adguard');
  expect(match.candidate?.manufacturer).toEqual('AdGuard Team');

  const validator = descriptor.getValidators()[0];
  const valid = await validator.validate({
    source: 'manual',
    integrationDomain: 'adguard',
    host: '192.168.1.2',
    port: 3000,
  }, {});
  expect(valid.matched).toBeTrue();
});

tap.test('config flow returns local HTTP AdGuard Home config', async () => {
  const integration = new AdguardIntegration();
  const step = await integration.configFlow.start({ source: 'manual', integrationDomain: 'adguard', host: 'http://192.168.1.2:3000/admin' }, {});
  const incomplete = await step.submit?.({ host: '192.168.1.2', username: 'admin' });
  expect(incomplete?.kind).toEqual('error');

  const done = await step.submit?.({ host: 'http://192.168.1.2:3000/admin', username: 'admin', password: 'secret' });
  expect(done?.kind).toEqual('done');
  expect(done?.config?.host).toEqual('192.168.1.2');
  expect(done?.config?.port).toEqual(3000);
  expect(done?.config?.ssl).toBeFalse();
  expect(done?.config?.basePath).toEqual('/admin');
});

export default tap.start();
