jest.mock('../config', () => ({ logLevel: 'error' })); jest.mock('../integrations/atlas/atlasDevices', () => ({ getAtlasDevicesForStore: jest.fn(), })); const { getAtlasDevicesForStore } = require('../integrations/atlas/atlasDevices'); const { collectAvStatus, shapeAtlasDevice, deriveOnline } = require('../services/avService'); describe('deriveOnline', () => { it('returns true for common "online" keywords', () => { expect(deriveOnline({ connection_status: 'online' })).toBe(true); expect(deriveOnline({ connection_status: 'CONNECTED' })).toBe(true); expect(deriveOnline({ state: { connection_status: 'active' } })).toBe(true); expect(deriveOnline({ status: 'up' })).toBe(true); }); it('returns false for common "offline" keywords', () => { expect(deriveOnline({ connection_status: 'offline' })).toBe(false); expect(deriveOnline({ connection_status: 'Disconnected' })).toBe(false); expect(deriveOnline({ status: 'inactive' })).toBe(false); }); it('returns null when no recognisable status field is present', () => { expect(deriveOnline({})).toBeNull(); expect(deriveOnline({ status: 'idk' })).toBeNull(); expect(deriveOnline({ status: '' })).toBeNull(); }); it('honors explicit booleans', () => { expect(deriveOnline({ connection_status: true })).toBe(true); expect(deriveOnline({ connection_status: false })).toBe(false); }); }); describe('shapeAtlasDevice', () => { it('promotes state.IpAddress / state.MacAddress to top-level ip / mac', () => { const shaped = shapeAtlasDevice({ id: 'd1', name: 'US000782AMP', state: { IpAddress: '10.0.0.5', MacAddress: 'AA:BB:CC:DD:EE:FF' }, status: 'online', }); expect(shaped.ip).toBe('10.0.0.5'); expect(shaped.mac).toBe('AA:BB:CC:DD:EE:FF'); expect(shaped.online).toBe(true); }); it('handles model as a string OR an object with .name', () => { expect(shapeAtlasDevice({ model: 'Cisco AMP' }).model).toBe('Cisco AMP'); expect(shapeAtlasDevice({ model: { name: 'Cisco AMP' } }).model).toBe('Cisco AMP'); }); it('falls back to a placeholder name when none is present', () => { expect(shapeAtlasDevice({}).name).toBe('Unknown AV Device'); }); }); describe('collectAvStatus', () => { beforeEach(() => { jest.clearAllMocks(); }); it('shapes each Atlas device returned by the integration', async () => { getAtlasDevicesForStore.mockResolvedValue({ devices: [ { id: 'd1', name: 'US000782AMP', model: 'Cisco AMP', state: { IpAddress: '10.0.0.5', MacAddress: 'aa:bb:cc:dd:ee:ff' }, status: 'online', }, ], }); const result = await collectAvStatus('782'); expect(result.unavailable).toBeFalsy(); expect(result.devices).toHaveLength(1); expect(result.devices[0]).toEqual( expect.objectContaining({ id: 'd1', name: 'US000782AMP', model: 'Cisco AMP', ip: '10.0.0.5', mac: 'aa:bb:cc:dd:ee:ff', online: true, }) ); }); it('propagates an unavailable payload from the integration layer', async () => { getAtlasDevicesForStore.mockResolvedValue({ devices: [], unavailable: true, reason: 'ATLAS_AUTH_KEY is not set', }); const result = await collectAvStatus('782'); expect(result.unavailable).toBe(true); expect(result.reason).toMatch(/ATLAS_AUTH_KEY/); expect(result.devices).toEqual([]); }); it('catches unexpected throws and returns the banner contract', async () => { getAtlasDevicesForStore.mockRejectedValue(new Error('boom')); const result = await collectAvStatus('782'); expect(result.unavailable).toBe(true); expect(result.reason).toBe('boom'); expect(result.devices).toEqual([]); }); it('returns an empty list when the store has no Atlas devices', async () => { getAtlasDevicesForStore.mockResolvedValue({ devices: [] }); const result = await collectAvStatus('782'); expect(result.unavailable).toBeFalsy(); expect(result.devices).toEqual([]); }); });