import test from 'node:test'; import assert from 'node:assert/strict'; import fs from 'node:fs'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; import { buildMppProbeView } from '../integrations/cisco-mpp-phone/aggregateProbe.js'; import { formatMicCertForDisplay } from '../integrations/cisco-mpp-phone/downloadStatusJson.js'; import { parseDownloadStatusJson, downloadStatusWarnings } from '../integrations/cisco-mpp-phone/downloadStatusJson.js'; import { formatUptime, formatHeaderStatusLine, headerIconForLine, isLineRegistered, } from '../services/renderers/mppPhoneFormatters.js'; import { renderMppPhoneDiagnosticsMarkdown } from '../services/renderers/mppPhoneDiagnosticsRenderer.js'; const __dirname = path.dirname(fileURLToPath(import.meta.url)); const STATUS = fs.readFileSync(path.join(__dirname, 'fixtures/mpp/status-782-live.json'), 'utf8'); const DOWNLOAD = fs.readFileSync(path.join(__dirname, 'fixtures/mpp/download-status-782.json'), 'utf8'); const NS = fs.readFileSync(path.join(__dirname, 'fixtures/mpp/ns-782.json'), 'utf8'); const SYSTEM = fs.readFileSync(path.join(__dirname, 'fixtures/mpp/system-782.json'), 'utf8'); function okResult() { const mpp = buildMppProbeView({ statusJson: STATUS, downloadStatusJson: DOWNLOAD, nsJson: NS, systemJson: SYSTEM, probes: [ { path: '/Status.json', status: 200, sizeBytes: 12564 }, { path: '/ns.json', status: 200, sizeBytes: 9474 }, ], }); return { ok: true, phone: { name: 'Store 00782 CP-7841', ip: '10.43.206.157', mac: 'CC98914F6799' }, parsed: mpp.parsed, verdict: mpp.verdict, mpp, probes: mpp.probes, }; } test('formatMicCertForDisplay: short message without URL', () => { const parsed = parseDownloadStatusJson(DOWNLOAD); const line = formatMicCertForDisplay(parsed.micCert); assert.match(line, /MIC cert download failed — .*File not found/i); assert.doesNotMatch(line, /sudirenewal\.cisco\.com/); assert.match(line, /07\/28\/2026/); }); test('formatUptime: compresses day-hour strings', () => { assert.equal(formatUptime('22 days and 07:20:35'), '22d 7h'); assert.equal(formatUptime('2 days and 12:01:51'), '2d 12h'); }); test('headerIconForLine: registered vs not', () => { assert.equal(headerIconForLine({ registrationState: 'Registered' }), '✅'); assert.equal(headerIconForLine({ registrationState: 'Not Registered' }), '❌'); assert.equal(isLineRegistered({ registrationState: 'Registered' }), true); }); test('renderMppPhoneDiagnosticsMarkdown: section layout default tier', () => { const md = renderMppPhoneDiagnosticsMarkdown([okResult()], { storeNum: '782', footer: false }); assert.match(md, /\*\*MPP Desk Phone Diagnostics — Store 782\*\*/); assert.match(md, /\*\*Registration\*\*/); assert.match(md, /\*\*Switch\*\*/); assert.match(md, /\*\*Provisioning\*\*/); assert.match(md, /\*\*Issues\*\*/); assert.doesNotMatch(md, /\*\*Network\*\*/); assert.match(md, /_Uptime 22d 7h · 100M Full_/); assert.match(md, /✅ \*\*Store 00782 CP-7841\*\*/); assert.match(md, /Registered/); assert.match(md, /SW00782R/); assert.match(md, /Port 45/); assert.match(md, /Resync: cisco\.sipflash\.com · OK/); assert.match(md, /MIC cert download failed — .*File not found/i); assert.doesNotMatch(md, /sudirenewal\.cisco\.com/); assert.doesNotMatch(md, /802\.1X/i); assert.doesNotMatch(md, /Web server/i); assert.doesNotMatch(md, /VLAN/i); assert.doesNotMatch(md, /proxy/i); assert.doesNotMatch(md, /Probe paths/); assert.doesNotMatch(md, /SIP counters/); }); test('renderMppPhoneDiagnosticsMarkdown: verbose includes probe table and SIP', () => { const md = renderMppPhoneDiagnosticsMarkdown([okResult()], { storeNum: '782', verbose: true, footer: false }); assert.match(md, /\*\*Probe paths\*\*/); assert.match(md, /\/Status\.json/); assert.match(md, /\*\*Extensions\*\*/); assert.match(md, /Ext 2:/); assert.match(md, /SIP counters/); }); test('renderMppPhoneDiagnosticsMarkdown: unregistered shows X header', () => { const r = okResult(); r.mpp.parsed.lines[0].registrationState = 'Not Registered'; const md = renderMppPhoneDiagnosticsMarkdown([r], { storeNum: '782', footer: false }); assert.match(md, /❌ \*\*Store 00782 CP-7841\*\*/); }); test('renderMppPhoneDiagnosticsMarkdown: relay failure line', () => { const md = renderMppPhoneDiagnosticsMarkdown([{ ok: false, phone: { name: 'Phone', ip: '10.1.1.1' }, error: { message: 'relay offline', hint: 'check agent' }, }], { storeNum: '782', footer: false }); assert.match(md, /probe failed: relay offline/); assert.match(md, /check agent/); }); test('buildMppProbeView: merges MIC warning into verdict', () => { const mpp = buildMppProbeView({ statusJson: STATUS, downloadStatusJson: DOWNLOAD, nsJson: NS, }); assert.equal(mpp.verdict.healthy, false); assert.ok(mpp.verdict.warnings.includes('mic_cert_failed')); }); test('formatHeaderStatusLine: uptime and link from phone status', () => { const line = formatHeaderStatusLine( { elapsedTime: '22 days and 07:20:35', linkSpeed: '100M Full' }, {}, ); assert.equal(line, 'Uptime 22d 7h · 100M Full'); });