import { expect, tap } from '@git.zone/tstest/tapbundle';
import { VolumioMapper, type IVolumioSnapshot } from '../../ts/integrations/volumio/index.js';

const snapshot: IVolumioSnapshot = {
  deviceInfo: {
    uuid: 'volumio-uuid-123',
    name: 'Kitchen Volumio',
    host: '192.168.1.81',
    port: 3000,
    manufacturer: 'Volumio',
    hardware: 'Raspberry Pi',
    systemVersion: '3.661',
  },
  systemInfo: {
    id: 'volumio-uuid-123',
    name: 'Kitchen Volumio',
  },
  systemVersion: {
    hardware: 'Raspberry Pi',
    systemversion: '3.661',
  },
  state: {
    status: 'play',
    title: 'Test Track',
    artist: 'Test Artist',
    album: 'Test Album',
    albumart: 'http://192.168.1.81:3000/albumart?cacheid=1',
    uri: 'music-library/NAS/test.flac',
    trackType: 'flac',
    seek: 123000,
    duration: 245,
    volume: 42,
    mute: false,
    random: true,
    repeat: false,
    service: 'mpd',
    samplerate: '44100',
    bitdepth: '16',
  },
  playlists: [{ name: 'Morning' }, { name: 'Evening' }],
  online: true,
  updatedAt: '2026-01-01T00:00:00.000Z',
};

tap.test('maps Volumio snapshots to media devices', async () => {
  const devices = VolumioMapper.toDevices(snapshot);
  expect(devices[0].id).toEqual('volumio.device.volumio_uuid_123');
  expect(devices[0].protocol).toEqual('http');
  expect(devices[0].state.some((stateArg) => stateArg.featureId === 'playback' && stateArg.value === 'playing')).toBeTrue();
  expect(devices[0].state.some((stateArg) => stateArg.featureId === 'volume' && stateArg.value === 42)).toBeTrue();
  expect(devices[0].state.some((stateArg) => stateArg.featureId === 'current_title' && stateArg.value === 'Test Track')).toBeTrue();
});

tap.test('maps Volumio snapshots to media player entities', async () => {
  const entities = VolumioMapper.toEntities(snapshot);
  expect(entities[0].id).toEqual('media_player.kitchen_volumio');
  expect(entities[0].platform).toEqual('media_player');
  expect(entities[0].state).toEqual('playing');
  expect(entities[0].attributes?.volumeLevel).toEqual(0.42);
  expect(entities[0].attributes?.mediaTitle).toEqual('Test Track');
  expect(entities[0].attributes?.mediaArtist).toEqual('Test Artist');
  expect(entities[0].attributes?.mediaPosition).toEqual(123);
  expect(entities[0].attributes?.mediaDuration).toEqual(245);
  expect(entities[0].attributes?.sourceList).toEqual(['Morning', 'Evening']);
});

export default tap.start();
