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

const rawData = '|/dev/sda|Samsung SSD 860|39|C||/dev/sdb|WDC WD20EFRX|102|F|';

tap.test('parses hddtemp daemon output into disk snapshots', async () => {
  const snapshot = await new HddtempClient({ rawData, name: 'HD Temperature' }).getSnapshot();

  expect(snapshot.online).toBeTrue();
  expect(snapshot.source).toEqual('manual');
  expect(snapshot.disks.length).toEqual(2);
  expect(snapshot.disks[0].device).toEqual('/dev/sda');
  expect(snapshot.disks[0].model).toEqual('Samsung SSD 860');
  expect(snapshot.disks[0].temperature).toEqual(39);
  expect(snapshot.disks[0].unit).toEqual('C');
  expect(snapshot.disks[1].temperature).toEqual(102);
  expect(snapshot.disks[1].unit).toEqual('F');
});

tap.test('maps hddtemp snapshots to one daemon device and temperature entities', async () => {
  const snapshot = HddtempMapper.toSnapshot({ config: { rawData, host: 'nas.local', name: 'NAS Disks' }, online: true, source: 'manual' });
  const devices = HddtempMapper.toDevices(snapshot);
  const entities = HddtempMapper.toEntities(snapshot);

  expect(devices[0].id).toEqual('hddtemp.daemon.nas_local_7634');
  expect(devices[0].online).toBeTrue();
  expect(devices[0].features.length).toEqual(2);
  expect(entities.find((entityArg) => entityArg.id === 'sensor.nas_disks_dev_sda')?.state).toEqual(39);
  expect(entities.find((entityArg) => entityArg.id === 'sensor.nas_disks_dev_sda')?.attributes?.device).toEqual('/dev/sda');
  expect(entities.find((entityArg) => entityArg.id === 'sensor.nas_disks_dev_sdb')?.attributes?.unit).toEqual('F');
});

tap.test('keeps configured but missing disks unavailable without inventing values', async () => {
  const snapshot = HddtempMapper.toSnapshot({ config: { rawData, disks: ['/dev/sdz'], name: 'Configured HDD' }, online: true, source: 'manual' });
  const entities = HddtempMapper.toEntities(snapshot);

  expect(snapshot.disks.length).toEqual(1);
  expect(snapshot.disks[0].device).toEqual('/dev/sdz');
  expect(snapshot.disks[0].available).toBeFalse();
  expect(entities[0].state).toEqual(null);
  expect(entities[0].available).toBeFalse();
});

export default tap.start();
