import { expect, tap } from '@git.zone/tstest/tapbundle';
import { GlancesMapper, type IGlancesRawData } from '../../ts/integrations/glances/index.js';

const rawData: IGlancesRawData = {
  system: { hostname: 'nas', os_name: 'Linux', platform: 'Linux', hr_name: 'Ubuntu 24.04' },
  quicklook: { cpu: 12.5 },
  percpu: [{ cpu_number: 0, total: 10 }],
  mem: { percent: 55.5, used: 2147483648, free: 1073741824 },
  memswap: { percent: 10, used: 1073741824, free: 9663676416 },
  fs: [{ mnt_point: '/', used: 107374182400, size: 214748364800, free: 107374182400, percent: 50 }],
  diskio: [{ disk_name: 'sda', read_bytes: 1000, write_bytes: 2000, time_since_update: 2 }],
  network: [{ interface_name: 'eth0', bytes_recv_rate_per_sec: 1234, bytes_sent_rate_per_sec: 5678, is_up: true, speed: 1073741824 }],
  load: { min1: 0.12, min5: 0.24, min15: 0.42 },
  processcount: { running: 3, total: 144, thread: 300, sleeping: 141 },
  sensors: [{ label: 'CPU Core', type: 'temperature_core', value: 48.2 }],
};

tap.test('maps raw Glances API snapshots to CPU memory disk network load temperature and process sensors', async () => {
  const snapshot = GlancesMapper.toSnapshot({ config: { name: 'NAS' }, rawData, apiVersion: 4, online: true, source: 'manual' });
  const entities = GlancesMapper.toEntities(snapshot);
  const devices = GlancesMapper.toDevices(snapshot);

  expect(snapshot.sensorData.mem?.memory_use).toEqual(2048);
  expect(snapshot.sensorData.fs?.['/']?.disk_size).toEqual(200);
  expect(snapshot.sensorData.network?.eth0?.rx).toEqual(1234);
  expect(devices[0].id).toEqual('glances.host.nas');
  expect(entities.find((entityArg) => entityArg.attributes?.key === 'cpu_use_percent')?.state).toEqual(12.5);
  expect(entities.find((entityArg) => entityArg.attributes?.key === 'memory_use_percent')?.state).toEqual(55.5);
  expect(entities.find((entityArg) => entityArg.attributes?.key === 'disk_glances_use_percent')?.state).toEqual(50);
  expect(entities.find((entityArg) => entityArg.attributes?.key === 'network_eth0_rx')?.state).toEqual(1234);
  expect(entities.find((entityArg) => entityArg.attributes?.key === 'processor_load')?.state).toEqual(0.42);
  expect(entities.find((entityArg) => entityArg.attributes?.key === 'sensor_cpu_core_temperature_core')?.attributes?.deviceClass).toEqual('temperature');
  expect(entities.find((entityArg) => entityArg.attributes?.key === 'process_running')?.state).toEqual(3);
});

tap.test('maps manual HA sensor data without inventing absent values', async () => {
  const snapshot = GlancesMapper.toSnapshot({
    config: {
      name: 'Manual Glances',
      haSensorData: {
        cpu: { cpu_use_percent: 7 },
        processcount: { process_total: 9 },
      },
    },
    online: true,
    source: 'manual',
  });
  const entities = GlancesMapper.toEntities(snapshot);

  expect(entities.find((entityArg) => entityArg.attributes?.key === 'cpu_use_percent')?.state).toEqual(7);
  expect(entities.find((entityArg) => entityArg.attributes?.key === 'process_total')?.state).toEqual(9);
  expect(entities.some((entityArg) => entityArg.attributes?.key === 'memory_use_percent')).toBeFalse();
});

export default tap.start();
