import { expect, tap } from '@git.zone/tstest/tapbundle';
import { HomeAssistantFritzIntegration, type IFritzCommand, type IFritzConfig } from '../../ts/integrations/fritz/index.js';

const config: IFritzConfig = {
  host: '192.168.178.1',
  snapshot: {
    connected: true,
    router: {
      name: 'Home Fritz',
      host: '192.168.178.1',
      serialNumber: 'AABBCCDDEEFF',
      actions: ['reboot'],
    },
    devices: [],
    interfaces: [],
    connection: {},
    sensors: {},
    wifiNetworks: [],
    portForwards: [],
    callDeflections: [],
  },
};

tap.test('does not fake FRITZ TR-064 command success without injected executor', async () => {
  const runtime = await new HomeAssistantFritzIntegration().setup(config, {});
  const result = await runtime.callService!({ domain: 'fritz', service: 'reboot', target: {} });

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

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

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

export default tap.start();
