import { expect, tap } from '@git.zone/tstest/tapbundle';
import { HomeAssistantAsuswrtIntegration, type IAsuswrtCommand, type IAsuswrtConfig } from '../../ts/integrations/asuswrt/index.js';

const config: IAsuswrtConfig = {
  host: '192.168.1.1',
  protocol: 'ssh',
  snapshot: {
    connected: true,
    router: {
      name: 'SSH Router',
      host: '192.168.1.1',
      protocol: 'ssh',
      labelMac: 'AA:BB:CC:DD:EE:FF',
      actions: ['reboot'],
    },
    devices: [],
    interfaces: [],
    sensors: {},
  },
};

tap.test('does not fake SSH/Telnet command success without injected executor', async () => {
  const runtime = await new HomeAssistantAsuswrtIntegration().setup(config, {});
  const result = await runtime.callService!({ domain: 'asuswrt', service: 'reboot', target: {} });

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

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

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

export default tap.start();
