import { expect, tap } from '@git.zone/tstest/tapbundle';
import { MadvrClient, MadvrCommandModeler } from '../../ts/integrations/madvr/index.js';

tap.test('parses madVR notification snapshots from the IP-control stream', async () => {
  const raw = MadvrClient.parseNotificationText([
    'MacAddress AA:BB:CC:DD:EE:FF',
    'Temperatures 55 40 45 35',
    'IncomingSignalInfo 3840x2160 23.976 2D 444 10bit HDR HDR10 TV 16:9',
    'OutgoingSignalInfo 4096x2160 23.976 2D RGB 12bit HDR 2020 PC',
    'AspectRatio 3840x2160 1.78 178 Cinema',
    'MaskingRatio 3840x1600 2.40 240',
  ].join('\r\n'));

  expect(raw.mac_address).toEqual('AA:BB:CC:DD:EE:FF');
  expect(raw.temp_gpu).toEqual('55');
  expect(raw.incoming_res).toEqual('3840x2160');
  expect(raw.hdr_flag).toBeTrue();
  expect(raw.outgoing_colorimetry).toEqual('2020');
  expect(raw.aspect_name).toEqual('Cinema');
});

tap.test('models safe madVR commands and rejects unsupported parameters', async () => {
  const keyModel = MadvrCommandModeler.model('KeyPress, MENU');
  expect(keyModel.transport).toEqual('tcp');
  expect(keyModel.wireCommand).toEqual('KeyPress MENU\r\n');

  const profileModel = MadvrCommandModeler.model({ command: 'ActivateProfile', args: ['DISPLAY', '2'] });
  expect(profileModel.wireCommand).toEqual('ActivateProfile DISPLAY 2\r\n');

  let errorMessage = '';
  try {
    MadvrCommandModeler.model('KeyPress, VOLUMEUP');
  } catch (errorArg) {
    errorMessage = errorArg instanceof Error ? errorArg.message : String(errorArg);
  }
  expect(errorMessage).toContain('Unsupported madVR remote key');
});

tap.test('does not fake TCP command success without host or executor', async () => {
  const client = new MadvrClient({
    snapshot: {
      deviceInfo: { name: 'Cinema Envy' },
      processor: { isOn: true },
      display: {},
      available: true,
    },
  });

  let errorMessage = '';
  try {
    await client.execute({ command: 'PowerOff' });
  } catch (errorArg) {
    errorMessage = errorArg instanceof Error ? errorArg.message : String(errorArg);
  }

  expect(errorMessage).toContain('host is required');
});

export default tap.start();
