import { expect, tap } from '@git.zone/tstest/tapbundle';
import { HomeAssistantOpnsenseIntegration, type IOpnsenseCommand, type IOpnsenseConfig } from '../../ts/integrations/opnsense/index.js';

const config: IOpnsenseConfig = {
  url: 'https://192.168.1.1',
  apiKey: 'key',
  apiSecret: 'secret',
  snapshot: {
    connected: true,
    router: {
      host: '192.168.1.1',
      name: 'Edge Firewall',
      macAddress: 'AA:BB:CC:DD:EE:FF',
      actions: ['reboot'],
    },
    devices: [],
    interfaces: [],
    gateways: [],
    firewall: {},
    system: {},
    telemetry: {},
    services: [],
    vpn: {},
    sensors: {},
    switches: [],
  },
};

tap.test('does not fake OPNsense live API command success without executor', async () => {
  const runtime = await new HomeAssistantOpnsenseIntegration().setup(config, {});
  const result = await runtime.callService!({ domain: 'opnsense', service: 'reboot', target: {} });

  expect(result.success).toBeFalse();
  expect(result.error || '').toInclude('not faked');
  await runtime.destroy();
});

tap.test('executes represented OPNsense commands through injected executor', async () => {
  let command: IOpnsenseCommand | undefined;
  const runtime = await new HomeAssistantOpnsenseIntegration().setup({
    ...config,
    commandExecutor: async (commandArg) => {
      command = commandArg;
      return { success: true, data: { accepted: true } };
    },
  }, {});
  const result = await runtime.callService!({ domain: 'opnsense', service: 'reboot', target: {} });

  expect(result.success).toBeTrue();
  expect(command?.type).toEqual('router.action');
  expect(command?.path).toEqual('/api/core/system/reboot');
  await runtime.destroy();
});

export default tap.start();
