const { findMatchingClient, getClientStatus, formatLastSeen, buildMerakiClientLink, } = require('../utils/merakiMatcher'); describe('merakiMatcher', () => { const sampleClients = [ { id: 'c1', description: 'Register 305', status: 'Online', lastSeen: '2025-01-01T10:00:00Z' }, { id: 'c2', description: 'Printer Front', status: 'Offline', lastSeen: Date.now() - 5 * 60 * 1000, }, { id: 'c3', description: '192.168.10.45 - Terminal', status: 'Online', lastSeen: null }, { id: 'c4', description: 'SRV-042', status: 'Online' }, ]; describe('findMatchingClient', () => { it('matches by exact or substring on description', () => { expect(findMatchingClient(sampleClients, { name: 'Register 305' })).toBe(sampleClients[0]); expect(findMatchingClient(sampleClients, { name: 'printer' })).toBe(sampleClients[1]); }); it('matches using multiple identifier fields', () => { const match = findMatchingClient(sampleClients, { deviceName: 'Terminal', ip_address: '192.168.10.45', }); expect(match).toBe(sampleClients[2]); }); it('matches MDM style UserName / DeviceFriendlyName', () => { const match = findMatchingClient(sampleClients, { UserName: 'SRV-042', DeviceFriendlyName: 'Server 042', }); expect(match).toBe(sampleClients[3]); }); it('returns null when no clients or no match', () => { expect(findMatchingClient([], { name: 'foo' })).toBeNull(); expect(findMatchingClient(sampleClients, { name: 'nonexistent' })).toBeNull(); }); }); describe('getClientStatus', () => { it('returns correct emojis and fallback', () => { expect(getClientStatus({ status: 'Online' })).toBe('✅ Online'); expect(getClientStatus({ status: 'Offline' })).toBe('❌ Offline'); expect(getClientStatus(null)).toBe('❓ Unknown'); expect(getClientStatus(undefined)).toBe('❓ Unknown'); }); }); describe('formatLastSeen', () => { it('handles missing value', () => { expect(formatLastSeen(null)).toBe('N/A'); expect(formatLastSeen(undefined)).toBe('N/A'); }); it('formats recent times', () => { const justNow = new Date(); expect(formatLastSeen(justNow)).toBe('Just now'); const tenMin = new Date(Date.now() - 10 * 60 * 1000); expect(formatLastSeen(tenMin)).toMatch(/10 min ago/); }); }); describe('buildMerakiClientLink', () => { const network = { id: 'N_123', name: 'Store 305', url: 'https://example.com/n/ABC123' }; it('builds a client link when possible', () => { const client = { id: 'c99' }; const link = buildMerakiClientLink(network, client); expect(link).toContain('/manage/clients/c99/overview'); }); it('returns empty string on bad input', () => { expect(buildMerakiClientLink(null, {})).toBe(''); expect(buildMerakiClientLink(network, null)).toBe(''); }); }); });