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

const snapshot = {
  deviceInfo: {
    name: 'Living Room Google TV',
    host: '192.168.1.61',
    apiPort: 6466,
    pairPort: 6467,
    macAddress: 'AA:BB:CC:DD:EE:FF',
    manufacturer: 'Google',
    model: 'Chromecast',
  },
  state: {
    available: true,
    isOn: true,
    mediaState: 'playing',
    currentApp: 'com.netflix.ninja',
    volumeInfo: {
      level: 7,
      max: 10,
      muted: false,
    },
  },
  apps: [
    { id: 'com.netflix.ninja' },
    { id: 'com.plexapp.android', name: 'Plex', icon: 'https://example.invalid/plex.png' },
  ],
};

tap.test('maps Android TV Remote snapshots to media devices and entities', async () => {
  const devices = AndroidtvRemoteMapper.toDevices(snapshot);
  const entities = AndroidtvRemoteMapper.toEntities(snapshot);

  expect(devices[0].id).toEqual('androidtv_remote.device.aa_bb_cc_dd_ee_ff');
  expect(devices[0].metadata?.protocol).toEqual('androidtvremote2');
  expect(devices[0].state.some((stateArg) => stateArg.featureId === 'volume' && stateArg.value === 70)).toBeTrue();
  expect(devices[0].state.some((stateArg) => stateArg.featureId === 'activity' && stateArg.value === 'Netflix')).toBeTrue();
  expect(entities[0].platform).toEqual('media_player');
  expect(entities[0].state).toEqual('playing');
  expect(entities[0].attributes?.source).toEqual('Netflix');
  expect(entities[0].attributes?.volumeLevel).toEqual(0.7);
  expect((entities[0].attributes?.activityList as string[]).includes('Plex')).toBeTrue();
});

tap.test('maps Android TV Remote power-off snapshots to off state', async () => {
  const entities = AndroidtvRemoteMapper.toEntities({
    ...snapshot,
    state: {
      available: true,
      isOn: false,
      mediaState: 'off',
    },
  });

  expect(entities[0].state).toEqual('off');
});

export default tap.start();
