import { expect, tap } from '@git.zone/tstest/tapbundle';
import { DlnaDmsMapper, type IDlnaDmsSnapshot } from '../../ts/integrations/dlna_dms/index.js';

const snapshot: IDlnaDmsSnapshot = {
  online: true,
  source: 'snapshot',
  updatedAt: '2026-05-07T00:00:00.000Z',
  device: {
    location: 'http://192.168.1.60:8200/rootDesc.xml',
    udn: 'uuid:server-1',
    usn: 'uuid:server-1::urn:schemas-upnp-org:device:MediaServer:1',
    deviceType: 'urn:schemas-upnp-org:device:MediaServer:1',
    friendlyName: 'Media Server',
    manufacturer: 'Example',
    modelName: 'DMS 1',
    sourceId: 'media_server',
    services: {},
  },
  status: {
    online: true,
    contentDirectoryAvailable: true,
    connectionManagerAvailable: true,
    systemUpdateId: 7,
    searchCapabilities: ['dc:title'],
    sortCapabilities: ['dc:title'],
    sourceProtocolInfo: ['http-get:*:audio/mpeg:*'],
    updatedAt: '2026-05-07T00:00:00.000Z',
  },
  contentDirectory: {
    root: {
      id: '0',
      type: 'container',
      title: 'Media Server',
      upnpClass: 'object.container',
      mediaClass: 'directory',
      childCount: 2,
      canPlay: false,
      canExpand: true,
      resources: [],
    },
    children: [
      {
        id: 'music',
        parentId: '0',
        type: 'container',
        title: 'Music',
        upnpClass: 'object.container.album.musicAlbum',
        mediaClass: 'album',
        childCount: 42,
        canPlay: false,
        canExpand: true,
        resources: [],
      },
      {
        id: 'videos',
        parentId: '0',
        type: 'container',
        title: 'Videos',
        upnpClass: 'object.container',
        mediaClass: 'directory',
        childCount: 3,
        canPlay: false,
        canExpand: true,
        resources: [],
      },
    ],
    totalMatches: 2,
    numberReturned: 2,
    updateId: 7,
  },
  libraries: [],
};
snapshot.libraries = snapshot.contentDirectory.children;

tap.test('maps DLNA DMS snapshots to a server device', async () => {
  const devices = DlnaDmsMapper.toDevices(snapshot);
  expect(devices[0].id).toEqual('dlna_dms.server.uuid_server_1');
  expect(devices[0].online).toBeTrue();
  expect(devices[0].state.find((stateArg) => stateArg.featureId === 'library_count')?.value).toEqual(2);
  expect(devices[0].state.find((stateArg) => stateArg.featureId === 'library_music')?.value).toEqual(42);
});

tap.test('maps DLNA DMS status, library, and content directory sensors', async () => {
  const entities = DlnaDmsMapper.toEntities(snapshot);
  const status = entities.find((entityArg) => entityArg.uniqueId === 'dlna_dms_uuid_server_1_status');
  const libraries = entities.find((entityArg) => entityArg.uniqueId === 'dlna_dms_uuid_server_1_libraries');
  const contentDirectory = entities.find((entityArg) => entityArg.uniqueId === 'dlna_dms_uuid_server_1_content_directory');
  const music = entities.find((entityArg) => entityArg.uniqueId === 'dlna_dms_uuid_server_1_library_music');

  expect(status?.state).toEqual('online');
  expect(status?.attributes?.contentDirectoryAvailable).toBeTrue();
  expect(libraries?.state).toEqual(2);
  expect(contentDirectory?.state).toEqual(2);
  expect(contentDirectory?.attributes?.systemUpdateId).toEqual(7);
  expect(music?.state).toEqual(42);
});

export default tap.start();
