// tests/paloalto.topology.test.js import test from 'node:test'; import assert from 'node:assert/strict'; import { readFileSync } from 'node:fs'; import { fileURLToPath } from 'node:url'; import path from 'node:path'; import { normalizeTunnelRow, extractVpnLinkIdsFromAlarmInfo, siteIdsFromTunnelRaw, } from '../integrations/paloalto/topology.js'; import { normalizeWanInterfaceStatus } from '../integrations/paloalto/sites.js'; const __dirname = path.dirname(fileURLToPath(import.meta.url)); const FIX = path.join(__dirname, 'fixtures/prisma'); test('normalizeWanInterfaceStatus: fixture operational/admin up', () => { const raw = JSON.parse(readFileSync(path.join(FIX, 'waninterface-status.json'), 'utf8')); const s = normalizeWanInterfaceStatus(raw); assert.equal(s.operationalUp, true); assert.equal(s.adminUp, true); assert.equal(s.elementId, 'el-fixture-1'); }); test('normalizeTunnelRow: topology link fixture', () => { const topo = JSON.parse(readFileSync(path.join(FIX, 'topology-links.json'), 'utf8')); const up = normalizeTunnelRow(topo.items[0], null, { localSiteId: 'site-A' }); assert.equal(up.id, 'topo-link-1'); assert.equal(up.up, true); assert.equal(up.relatedInterfaceId, 'wi-mpls'); assert.equal(up.peerSiteId, 'site-HUB'); assert.match(up.peerLabel, /HUB/); assert.deepEqual(up.vpnLinkIds, ['vpn-link-1']); const down = normalizeTunnelRow(topo.items[1], null, { localSiteId: 'site-A' }); assert.equal(down.up, false); assert.equal(down.relatedInterfaceId, 'wi-bb'); }); test('normalizeTunnelRow: merges vpnlink status', () => { const status = JSON.parse(readFileSync(path.join(FIX, 'vpnlink-status.json'), 'utf8')); const row = normalizeTunnelRow({ id: 'vpn-link-1', target_site_name: 'Hub' }, status); assert.equal(row.up, true); assert.equal(row.relatedInterfaceId, 'wi-mpls'); }); test('siteIdsFromTunnelRaw: ep1/ep2 style', () => { const ids = siteIdsFromTunnelRaw({ id: 'x', ep1_site_id: 'site-A', ep2_site_id: 'site-HUB', }); assert.deepEqual(ids.sort(), ['site-A', 'site-HUB'].sort()); }); test('normalizeTunnelRow: picks peer opposite local site', () => { const row = normalizeTunnelRow({ id: 'vpn-1', ep1_site_id: 'site-A', ep2_site_id: 'site-HUB', admin_up: true, al_state: 'up', }, null, { localSiteId: 'site-A' }); assert.equal(row.peerSiteId, 'site-HUB'); assert.equal(row.up, true); }); test('normalizeTunnelRow: live vpnlink shape (al_id/vep, no site fields)', () => { const vpn = JSON.parse(readFileSync(path.join(FIX, 'vpnlinks-by-al.json'), 'utf8')).items[0]; const row = normalizeTunnelRow(vpn, null, { localSiteId: '16239359004050019', peerSiteIdOverride: 'site-HUB', peerLabelOverride: 'CG00001-HUB', }); assert.equal(row.id, 'vpn-inet-1'); assert.equal(row.anynetId, 'al-hub-1'); assert.equal(row.peerLabel, 'CG00001-HUB'); assert.equal(row.vep1Id, 'vep-local-1'); assert.equal(siteIdsFromTunnelRaw(vpn).length, 0, 'live vpnlinks carry no site ids'); }); test('normalizeTunnelRow: anynetlink fixture has ep1/ep2 site endpoints', () => { const al = JSON.parse(readFileSync(path.join(FIX, 'anynetlinks.json'), 'utf8')).items[0]; const ids = siteIdsFromTunnelRaw(al); assert.ok(ids.includes('16239359004050019')); assert.ok(ids.includes('site-HUB')); const row = normalizeTunnelRow(al, null, { localSiteId: '16239359004050019' }); assert.equal(row.peerSiteId, 'site-HUB'); assert.equal(row.relatedInterfaceId, '16239359007870072'); assert.equal(row.adminUp, true); }); test('extractVpnLinkIdsFromAlarmInfo: nested vpn_reasons', () => { const info = JSON.parse(readFileSync(path.join(FIX, 'alarm-info-vpn.json'), 'utf8')); assert.deepEqual(extractVpnLinkIdsFromAlarmInfo(info), ['vpn-link-2']); });