// tests/infra/scan_cycle.test.js import { describe, it, expect, vi } from 'vitest'; import { runDeviceScanCycle } from '../../lib/infra/scan_cycle.js'; function fakeRepo() { return { upsertScan: vi.fn(async r => r.length), markAbsent: vi.fn(async () => 1), prune: vi.fn(async () => 2) }; } const noHosts = { all: async () => [] }; describe('runDeviceScanCycle', () => { it('scan→upsert→markAbsent→prune on a non-empty scan', async () => { const repo = fakeRepo(); const scan = vi.fn(async () => [{ mac: 'aa:bb:cc:dd:ee:ff', ip: '1.2.3.4', vendor: 'x', randomized: false }]); const res = await runDeviceScanCycle({ scan, repo, hosts: noHosts }); expect(repo.upsertScan).toHaveBeenCalledOnce(); expect(repo.markAbsent).toHaveBeenCalledWith(['aa:bb:cc:dd:ee:ff']); expect(repo.prune).toHaveBeenCalledOnce(); expect(res).toEqual({ seen: 1, pruned: 2 }); }); it('skips upsert/prune when the scan returns nothing', async () => { const repo = fakeRepo(); const res = await runDeviceScanCycle({ scan: async () => [], repo, hosts: noHosts }); expect(repo.upsertScan).not.toHaveBeenCalled(); expect(repo.prune).not.toHaveBeenCalled(); expect(res).toEqual({ seen: 0 }); }); it('excludes homelab guests (network_hosts inventory + bc:24:11 OUI)', async () => { const repo = fakeRepo(); const hosts = { all: async () => [{ mac: 'BC:24:11:9B:B7:3A' }, { mac: '00:E0:4C:0F:36:00' }] }; const scan = async () => [ { mac: 'bc:24:11:9b:b7:3a', ip: '192.168.1.216', vendor: '', randomized: false }, // in inventory { mac: 'bc:24:11:de:ad:00', ip: '192.168.1.99', vendor: '', randomized: false }, // proxmox OUI { mac: '00:e0:4c:0f:36:00', ip: '192.168.1.124', vendor: '', randomized: false }, // PVE host in inventory { mac: 'd8:eb:46:77:37:a8', ip: '192.168.1.25', vendor: 'Google', randomized: false } // real device ]; const res = await runDeviceScanCycle({ scan, repo, hosts }); expect(res.seen).toBe(1); expect(repo.upsertScan).toHaveBeenCalledWith([expect.objectContaining({ mac: 'd8:eb:46:77:37:a8' })]); expect(repo.markAbsent).toHaveBeenCalledWith(['d8:eb:46:77:37:a8']); }); });