import { expect, tap } from '@git.zone/tstest/tapbundle';
import { SnapcastMapper, type ISnapcastSnapshot } from '../../ts/integrations/snapcast/index.js';

const snapshot: ISnapcastSnapshot = {
  capturedAt: '2026-01-01T00:00:00.000Z',
  source: 'manual',
  server: {
    groups: [{
      id: 'group-1',
      muted: false,
      name: 'Kitchen Group',
      stream_id: 'music',
      clients: [{
        id: 'client-1',
        connected: true,
        host: { name: 'Kitchen', ip: '192.168.1.31', mac: '00:11:22:33:44:55' },
        config: {
          latency: 25,
          name: 'Kitchen',
          volume: { muted: false, percent: 42 },
        },
        snapclient: { name: 'Snapclient', version: '0.29.0', protocolVersion: 2 },
      }],
    }],
    streams: [{
      id: 'music',
      status: 'playing',
      uri: { raw: 'pipe:///tmp/snapfifo?name=music', scheme: 'pipe', query: { name: 'music' } },
      metadata: { title: 'Example Track', artist: ['Example Artist'], album: 'Example Album' },
      properties: { position: 12 },
    }],
  },
};

tap.test('maps Snapcast clients groups and streams to canonical devices', async () => {
  const devices = SnapcastMapper.toDevices(snapshot);
  expect(devices.some((deviceArg) => deviceArg.id === 'snapcast.client.client_1')).toBeTrue();
  expect(devices.some((deviceArg) => deviceArg.id === 'snapcast.group.group_1')).toBeTrue();
  expect(devices.some((deviceArg) => deviceArg.id === 'snapcast.stream.music')).toBeTrue();
});

tap.test('maps Snapcast clients groups and streams to entities', async () => {
  const entities = SnapcastMapper.toEntities(snapshot);
  const clientEntity = entities.find((entityArg) => entityArg.id === 'media_player.kitchen_snapcast_client');
  const groupEntity = entities.find((entityArg) => entityArg.id === 'media_player.kitchen_group_snapcast_group');
  const streamEntity = entities.find((entityArg) => entityArg.id === 'sensor.music_snapcast_stream');

  expect(clientEntity?.platform).toEqual('media_player');
  expect(clientEntity?.state).toEqual('playing');
  expect(clientEntity?.attributes?.volumeLevel).toEqual(0.42);
  expect(groupEntity?.attributes?.source).toEqual('music');
  expect(streamEntity?.state).toEqual('playing');
});

export default tap.start();
