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

tap.test('matches Bluetooth LE advertisements and manual BLE entries', async () => {
  const descriptor = createBluetoothLeTrackerDiscoveryDescriptor();
  const bluetoothMatcher = descriptor.getMatchers().find((matcherArg) => matcherArg.id === 'bluetooth-le-tracker-bluetooth-match');
  const manualMatcher = descriptor.getMatchers().find((matcherArg) => matcherArg.id === 'bluetooth-le-tracker-manual-match');

  const bluetoothResult = await bluetoothMatcher!.matches({
    address: 'AA-BB-CC-DD-EE-FF',
    name: 'Backpack Tag\x00',
    rssi: -61,
    connectable: false,
    serviceUuids: ['0000180f-0000-1000-8000-00805f9b34fb'],
    manufacturerData: { '76': [1, 2, 3] },
  }, {});
  const manualResult = await manualMatcher!.matches({
    mac: 'BLE_11:22:33:44:55:66',
    name: 'Keys Beacon',
    track: true,
    trackBattery: true,
  }, {});

  expect(bluetoothResult.matched).toBeTrue();
  expect(bluetoothResult.normalizedDeviceId).toEqual('aa:bb:cc:dd:ee:ff');
  expect(bluetoothResult.candidate?.metadata?.haMac).toEqual('BLE_AA:BB:CC:DD:EE:FF');
  expect(manualResult.matched).toBeTrue();
  expect(manualResult.candidate?.macAddress).toEqual('11:22:33:44:55:66');
});

tap.test('validates Bluetooth LE tracker candidates', async () => {
  const validator = createBluetoothLeTrackerDiscoveryDescriptor().getValidators()[0];
  const result = await validator.validate({
    source: 'bluetooth',
    id: 'aabbccddeeff',
    name: 'BLE tracker tag',
    metadata: { sourceType: 'bluetooth_le' },
  }, {});

  expect(result.matched).toBeTrue();
  expect(result.confidence).toEqual('high');
  expect(result.candidate?.integrationDomain).toEqual('bluetooth_le_tracker');
});

export default tap.start();
