import { expect, tap } from '@git.zone/tstest/tapbundle';
import { HomeAssistantDevoloHomeNetworkIntegration, type IDevoloCommand, type IDevoloConfig } from '../../ts/integrations/devolo_home_network/index.js';

const config: IDevoloConfig = {
  host: '192.168.1.20',
  name: 'Office Adapter',
  serialNumber: 'S12345',
  features: ['led'],
  switches: { leds: true },
};

tap.test('does not fake devolo command success without an injected executor', async () => {
  const runtime = await new HomeAssistantDevoloHomeNetworkIntegration().setup(config, {});
  const result = await runtime.callService!({
    domain: 'switch',
    service: 'turn_off',
    target: { entityId: 'switch.office_adapter_enable_leds' },
  });

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

tap.test('executes devolo commands through an injected executor', async () => {
  let command: IDevoloCommand | undefined;
  const runtime = await new HomeAssistantDevoloHomeNetworkIntegration().setup({
    ...config,
    commandExecutor: async (commandArg) => {
      command = commandArg;
      return { success: true, data: { accepted: true } };
    },
  }, {});
  const result = await runtime.callService!({
    domain: 'switch',
    service: 'turn_off',
    target: { entityId: 'switch.office_adapter_enable_leds' },
  });

  expect(result.success).toBeTrue();
  expect(command?.type).toEqual('device.set_leds');
  expect(command?.enabled).toEqual(false);
  await runtime.destroy();
});

export default tap.start();
