jest.mock('../config', () => ({ logLevel: 'error' })); jest.mock('../integrations/atlas/atlasClient', () => { const actual = jest.requireActual('../integrations/atlas/atlasClient'); return { ...actual, atlasGet: jest.fn(), }; }); const { atlasGet, AtlasUnavailableError } = require('../integrations/atlas/atlasClient'); const devices = require('../integrations/atlas/atlasDevices'); function pageOf(items, nextPage = null) { return { items, next_page: nextPage }; } describe('atlasDevices pagination', () => { beforeEach(() => { jest.clearAllMocks(); devices.resetCacheForTests(); }); it('stops paginating when a short page is returned', async () => { // 100 items + next_page=2 → keep going; 5 items on page 2 → stop. const page1 = Array.from({ length: devices.PAGE_SIZE }, (_, i) => ({ id: `d${i}`, name: 'x' })); const page2 = Array.from({ length: 5 }, (_, i) => ({ id: `d${100 + i}`, name: 'x' })); atlasGet.mockResolvedValueOnce(pageOf(page1, 2)).mockResolvedValueOnce(pageOf(page2, 3)); const list = await devices.getAtlasDeviceList(); expect(list).toHaveLength(105); expect(atlasGet).toHaveBeenCalledTimes(2); }); it('stops paginating when next_page is missing even on a full page', async () => { const page1 = Array.from({ length: devices.PAGE_SIZE }, (_, i) => ({ id: `d${i}`, name: 'x' })); atlasGet.mockResolvedValueOnce(pageOf(page1, null)); const list = await devices.getAtlasDeviceList(); expect(list).toHaveLength(devices.PAGE_SIZE); expect(atlasGet).toHaveBeenCalledTimes(1); }); it('preserves the previous cache when a mid-pagination call fails', async () => { // First refresh succeeds with one device. atlasGet.mockResolvedValueOnce(pageOf([{ id: 'd1', name: 'US000782AMP' }], null)); await devices.getAtlasDeviceList(true); // Force a refresh that fails on page 1 → cache should not be wiped. atlasGet.mockRejectedValueOnce(new Error('boom')); const list = await devices.getAtlasDeviceList(true); expect(list).toHaveLength(1); expect(list[0].id).toBe('d1'); }); it('propagates AtlasUnavailableError so callers can render a banner', async () => { atlasGet.mockRejectedValueOnce(new AtlasUnavailableError('ATLAS_AUTH_KEY is not set')); await expect(devices.getAtlasDeviceList(true)).rejects.toBeInstanceOf(AtlasUnavailableError); }); }); describe('findAtlasDevicesForStore', () => { beforeEach(() => { jest.clearAllMocks(); devices.resetCacheForTests(); }); it('matches the zero-padded store number against device names', async () => { atlasGet.mockResolvedValueOnce( pageOf( [ { id: '1', name: 'US000782AMP' }, // match { id: '2', name: 'us000782DSP' }, // match (case-insensitive) { id: '3', name: 'US007820AMP' }, // NOT — 782 unpadded would false-positive { id: '4', name: 'US000305AMP' }, ], null ) ); const matches = await devices.findAtlasDevicesForStore('782'); expect(matches.map(m => m.id).sort()).toEqual(['1', '2']); }); it('handles already-padded store numbers and whitespace', async () => { atlasGet.mockResolvedValueOnce(pageOf([{ id: '1', name: 'US000305AMP' }], null)); const matches = await devices.findAtlasDevicesForStore(' 000305 '); expect(matches).toHaveLength(1); }); }); describe('getAtlasDevicesForStore', () => { beforeEach(() => { jest.clearAllMocks(); devices.resetCacheForTests(); }); it('returns { devices: [] } when no matches', async () => { atlasGet.mockResolvedValueOnce(pageOf([{ id: '1', name: 'US000305AMP' }], null)); const result = await devices.getAtlasDevicesForStore('999'); expect(result).toEqual({ devices: [] }); }); it('fetches detail per matched device and merges over the list summary', async () => { atlasGet // Initial list call. .mockResolvedValueOnce(pageOf([{ id: 'd1', name: 'US000782AMP', status: 'unknown' }], null)) // Detail call for d1 — adds richer state. .mockResolvedValueOnce({ id: 'd1', name: 'US000782AMP', status: 'online', state: { IpAddress: '10.0.0.5', MacAddress: 'aa:bb:cc:dd:ee:ff' }, }); const result = await devices.getAtlasDevicesForStore('782'); expect(result.devices).toHaveLength(1); expect(result.devices[0]).toEqual( expect.objectContaining({ id: 'd1', name: 'US000782AMP', status: 'online', state: { IpAddress: '10.0.0.5', MacAddress: 'aa:bb:cc:dd:ee:ff' }, }) ); }); it('returns { unavailable: true } with the auth reason when the key is missing', async () => { atlasGet.mockRejectedValueOnce(new AtlasUnavailableError('ATLAS_AUTH_KEY is not set')); const result = await devices.getAtlasDevicesForStore('782'); expect(result.unavailable).toBe(true); expect(result.reason).toMatch(/ATLAS_AUTH_KEY/); expect(result.devices).toEqual([]); }); it('returns { unavailable: true } with the transport reason on other lookup failures', async () => { atlasGet.mockRejectedValueOnce(new Error('ECONNRESET')); const result = await devices.getAtlasDevicesForStore('782'); expect(result.unavailable).toBe(true); expect(result.reason).toMatch(/Atlas lookup failed/); }); });