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

tap.test('returns a clear unsupported response for live scans without injected data', async () => {
  const runtime = await new BluetoothLeTrackerIntegration().setup({}, {});
  const result = await runtime.callService!({
    domain: 'bluetooth_le_tracker',
    service: 'scan',
    target: {},
  });

  expect(result.success).toBeFalse();
  expect(String(result.error).includes('Live scanning is not implemented') || String(result.error).includes('live scanning is not implemented')).toBeTrue();
});

tap.test('accepts injected scan data and refreshes runtime devices', async () => {
  const runtime = await new BluetoothLeTrackerIntegration().setup({ minSeenNew: 1 }, {});
  const result = await runtime.callService!({
    domain: 'bluetooth_le_tracker',
    service: 'scan',
    target: {},
    data: {
      advertisements: [{ address: 'AA:BB:CC:DD:EE:FF', name: 'Backpack Tag', rssi: -59, time: Date.now() }],
    },
  });
  const devices = await runtime.devices();
  const entities = await runtime.entities();

  expect(result.success).toBeTrue();
  expect(devices.some((deviceArg) => deviceArg.id === 'bluetooth_le_tracker.device.aa_bb_cc_dd_ee_ff')).toBeTrue();
  expect(entities.some((entityArg) => entityArg.uniqueId === 'bluetooth_le_tracker_presence_aa_bb_cc_dd_ee_ff' && entityArg.state === 'on')).toBeTrue();
});

export default tap.start();
