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

tap.test('recognizes static host and MAC Wake on LAN candidates', async () => {
  const descriptor = createWakeOnLanDiscoveryDescriptor();
  const matcher = descriptor.getMatchers().find((matcherArg) => matcherArg.id === 'wake-on-lan-static-host-match');
  const result = await matcher?.matches({
    host: '192.168.1.20',
    macAddress: 'AA-BB-CC-DD-EE-FF',
    name: 'Gaming PC',
  }, {});

  expect(result?.matched).toBeTrue();
  expect(result?.candidate?.integrationDomain).toEqual('wake_on_lan');
  expect(result?.candidate?.host).toEqual('192.168.1.20');
  expect(result?.candidate?.macAddress).toEqual('aa:bb:cc:dd:ee:ff');
  expect(result?.candidate?.metadata?.entityMode).toEqual('switch');
});

tap.test('builds config flow output and validates MAC/snapshot input', async () => {
  const flow = new WakeOnLanConfigFlow();
  const step = await flow.start({
    source: 'manual',
    integrationDomain: 'wake_on_lan',
    host: '192.168.1.20',
    macAddress: 'AA-BB-CC-DD-EE-FF',
    name: 'Gaming PC',
  }, {});

  const done = await step.submit?.({ macAddress: 'aabbccddeeff', entityMode: 'both', broadcastPort: '7' });
  expect(done?.kind).toEqual('done');
  expect(done?.config?.macAddress).toEqual('aa:bb:cc:dd:ee:ff');
  expect(done?.config?.entityTypes).toEqual(['button', 'switch']);
  expect(done?.config?.broadcastPort).toEqual(7);

  const invalidMac = await step.submit?.({ macAddress: 'not-a-mac' });
  expect(invalidMac?.kind).toEqual('error');

  const invalidSnapshot = await step.submit?.({ macAddress: 'aabbccddeeff', snapshotJson: '{"connected":true}' });
  expect(invalidSnapshot?.kind).toEqual('error');
});

export default tap.start();
