|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { generateMarkdownReport } from './markdown.js'; |
| 3 | +import type { AnalysisResult } from './types.js'; |
| 4 | +import type { Event, Failure, Session, SessionSummary, RawOcppMessage } from '@ocpp-debugkit/core'; |
| 5 | + |
| 6 | +// Helpers |
| 7 | +function makeEvent( |
| 8 | + id: string, |
| 9 | + messageId: string, |
| 10 | + messageType: 'Call' | 'CallResult' | 'CallError', |
| 11 | + action: string | null, |
| 12 | + timestamp: number | null = null, |
| 13 | +): Event { |
| 14 | + let rawMessage: RawOcppMessage; |
| 15 | + if (messageType === 'Call') { |
| 16 | + rawMessage = [2, messageId, action as string, {}]; |
| 17 | + } else if (messageType === 'CallResult') { |
| 18 | + rawMessage = [3, messageId, {}]; |
| 19 | + } else { |
| 20 | + rawMessage = [4, messageId, 'Error', 'desc', {}]; |
| 21 | + } |
| 22 | + return { |
| 23 | + id, |
| 24 | + messageId, |
| 25 | + timestamp, |
| 26 | + direction: 'CS_TO_CSMS', |
| 27 | + messageType, |
| 28 | + action, |
| 29 | + payload: {}, |
| 30 | + errorCode: messageType === 'CallError' ? 'Error' : null, |
| 31 | + errorDescription: messageType === 'CallError' ? 'desc' : null, |
| 32 | + rawMessage, |
| 33 | + }; |
| 34 | +} |
| 35 | + |
| 36 | +function makeSession( |
| 37 | + sessionId: string, |
| 38 | + events: Event[], |
| 39 | + transactionId: number | null = 100001, |
| 40 | +): Session { |
| 41 | + return { |
| 42 | + sessionId, |
| 43 | + stationId: 'CS-001', |
| 44 | + connectorId: 1, |
| 45 | + transactionId, |
| 46 | + startTime: events[0]?.timestamp ?? null, |
| 47 | + endTime: events[events.length - 1]?.timestamp ?? null, |
| 48 | + events, |
| 49 | + status: 'completed', |
| 50 | + }; |
| 51 | +} |
| 52 | + |
| 53 | +function makeFailure(): Failure { |
| 54 | + return { |
| 55 | + code: 'FAILED_AUTHORIZATION', |
| 56 | + description: 'Authorization rejected: idTag returned "Invalid" status', |
| 57 | + severity: 'warning', |
| 58 | + eventIds: ['evt-0001', 'evt-0002'], |
| 59 | + suggestedSteps: ['Verify the idTag is valid', 'Check the CSMS local authorization list'], |
| 60 | + }; |
| 61 | +} |
| 62 | + |
| 63 | +// --------------------------------------------------------------------------- |
| 64 | +// Tests |
| 65 | +// --------------------------------------------------------------------------- |
| 66 | + |
| 67 | +describe('generateMarkdownReport', () => { |
| 68 | + it('generates a non-empty Markdown string', () => { |
| 69 | + const result: AnalysisResult = { |
| 70 | + events: [makeEvent('evt-0001', 'msg-001', 'Call', 'BootNotification', 1000)], |
| 71 | + sessions: [], |
| 72 | + failures: [], |
| 73 | + summaries: [], |
| 74 | + warnings: [], |
| 75 | + }; |
| 76 | + |
| 77 | + const report = generateMarkdownReport(result); |
| 78 | + expect(typeof report).toBe('string'); |
| 79 | + expect(report.length).toBeGreaterThan(0); |
| 80 | + }); |
| 81 | + |
| 82 | + it('includes the main title', () => { |
| 83 | + const result: AnalysisResult = { |
| 84 | + events: [], |
| 85 | + sessions: [], |
| 86 | + failures: [], |
| 87 | + summaries: [], |
| 88 | + warnings: [], |
| 89 | + }; |
| 90 | + |
| 91 | + const report = generateMarkdownReport(result); |
| 92 | + expect(report).toContain('# OCPP DebugKit — Trace Analysis Report'); |
| 93 | + }); |
| 94 | + |
| 95 | + it('includes session overview section', () => { |
| 96 | + const events = [ |
| 97 | + makeEvent('evt-0001', 'msg-001', 'Call', 'BootNotification', 1000), |
| 98 | + makeEvent('evt-0002', 'msg-002', 'Call', 'StartTransaction', 2000), |
| 99 | + ]; |
| 100 | + const session = makeSession('session-0', events); |
| 101 | + const result: AnalysisResult = { |
| 102 | + events, |
| 103 | + sessions: [session], |
| 104 | + failures: [], |
| 105 | + summaries: [ |
| 106 | + { |
| 107 | + sessionId: 'session-0', |
| 108 | + stationId: 'CS-001', |
| 109 | + connectorId: 1, |
| 110 | + transactionId: 100001, |
| 111 | + status: 'completed', |
| 112 | + eventCount: 2, |
| 113 | + durationMs: 1000, |
| 114 | + failureCount: 0, |
| 115 | + actionSequence: ['BootNotification', 'StartTransaction'], |
| 116 | + }, |
| 117 | + ], |
| 118 | + warnings: [], |
| 119 | + }; |
| 120 | + |
| 121 | + const report = generateMarkdownReport(result); |
| 122 | + expect(report).toContain('## Session Overview'); |
| 123 | + expect(report).toContain('CS-001'); |
| 124 | + expect(report).toContain('session-0'); |
| 125 | + }); |
| 126 | + |
| 127 | + it('includes failures section with details', () => { |
| 128 | + const failure = makeFailure(); |
| 129 | + const result: AnalysisResult = { |
| 130 | + events: [], |
| 131 | + sessions: [], |
| 132 | + failures: [failure], |
| 133 | + summaries: [], |
| 134 | + warnings: [], |
| 135 | + }; |
| 136 | + |
| 137 | + const report = generateMarkdownReport(result); |
| 138 | + expect(report).toContain('## Failures'); |
| 139 | + expect(report).toContain('FAILED_AUTHORIZATION'); |
| 140 | + expect(report).toContain('Authorization rejected'); |
| 141 | + expect(report).toContain('Verify the idTag is valid'); |
| 142 | + }); |
| 143 | + |
| 144 | + it('shows no failures message when clean', () => { |
| 145 | + const result: AnalysisResult = { |
| 146 | + events: [], |
| 147 | + sessions: [], |
| 148 | + failures: [], |
| 149 | + summaries: [], |
| 150 | + warnings: [], |
| 151 | + }; |
| 152 | + |
| 153 | + const report = generateMarkdownReport(result); |
| 154 | + expect(report).toContain('## Failures'); |
| 155 | + expect(report).toContain('No failures detected'); |
| 156 | + }); |
| 157 | + |
| 158 | + it('includes timeline summary with action sequence', () => { |
| 159 | + const summary: SessionSummary = { |
| 160 | + sessionId: 'session-0', |
| 161 | + stationId: 'CS-001', |
| 162 | + connectorId: 1, |
| 163 | + transactionId: 100001, |
| 164 | + status: 'completed', |
| 165 | + eventCount: 5, |
| 166 | + durationMs: 30000, |
| 167 | + failureCount: 0, |
| 168 | + actionSequence: ['BootNotification', 'Authorize', 'StartTransaction'], |
| 169 | + }; |
| 170 | + |
| 171 | + const result: AnalysisResult = { |
| 172 | + events: [], |
| 173 | + sessions: [], |
| 174 | + failures: [], |
| 175 | + summaries: [summary], |
| 176 | + warnings: [], |
| 177 | + }; |
| 178 | + |
| 179 | + const report = generateMarkdownReport(result); |
| 180 | + expect(report).toContain('## Timeline Summary'); |
| 181 | + expect(report).toContain('BootNotification → Authorize → StartTransaction'); |
| 182 | + }); |
| 183 | + |
| 184 | + it('includes event appendix', () => { |
| 185 | + const events = [ |
| 186 | + makeEvent('evt-0001', 'msg-001', 'Call', 'BootNotification', 1000), |
| 187 | + makeEvent('evt-0002', 'msg-001', 'CallResult', null, 1500), |
| 188 | + ]; |
| 189 | + const result: AnalysisResult = { |
| 190 | + events, |
| 191 | + sessions: [], |
| 192 | + failures: [], |
| 193 | + summaries: [], |
| 194 | + warnings: [], |
| 195 | + }; |
| 196 | + |
| 197 | + const report = generateMarkdownReport(result); |
| 198 | + expect(report).toContain('## Event Appendix'); |
| 199 | + expect(report).toContain('evt-0001'); |
| 200 | + expect(report).toContain('BootNotification'); |
| 201 | + }); |
| 202 | + |
| 203 | + it('includes parse warnings', () => { |
| 204 | + const result: AnalysisResult = { |
| 205 | + events: [], |
| 206 | + sessions: [], |
| 207 | + failures: [], |
| 208 | + summaries: [], |
| 209 | + warnings: [{ index: 5, message: 'Line 6: invalid JSON' }], |
| 210 | + }; |
| 211 | + |
| 212 | + const report = generateMarkdownReport(result); |
| 213 | + expect(report).toContain('## Parse Warnings'); |
| 214 | + expect(report).toContain('Line 6: invalid JSON'); |
| 215 | + }); |
| 216 | + |
| 217 | + it('includes metadata when provided', () => { |
| 218 | + const result: AnalysisResult = { |
| 219 | + events: [], |
| 220 | + sessions: [], |
| 221 | + failures: [], |
| 222 | + summaries: [], |
| 223 | + warnings: [], |
| 224 | + metadata: { |
| 225 | + stationId: 'CS-SYNTHETIC-001', |
| 226 | + ocppVersion: '1.6', |
| 227 | + source: 'csms-log', |
| 228 | + description: 'Test trace', |
| 229 | + }, |
| 230 | + }; |
| 231 | + |
| 232 | + const report = generateMarkdownReport(result); |
| 233 | + expect(report).toContain('CS-SYNTHETIC-001'); |
| 234 | + expect(report).toContain('1.6'); |
| 235 | + expect(report).toContain('csms-log'); |
| 236 | + expect(report).toContain('Test trace'); |
| 237 | + }); |
| 238 | + |
| 239 | + it('includes suggested next steps from failures', () => { |
| 240 | + const failure = makeFailure(); |
| 241 | + const result: AnalysisResult = { |
| 242 | + events: [], |
| 243 | + sessions: [], |
| 244 | + failures: [failure], |
| 245 | + summaries: [], |
| 246 | + warnings: [], |
| 247 | + }; |
| 248 | + |
| 249 | + const report = generateMarkdownReport(result); |
| 250 | + expect(report).toContain('## Suggested Next Steps'); |
| 251 | + expect(report).toContain('Verify the idTag is valid'); |
| 252 | + expect(report).toContain('Check the CSMS local authorization list'); |
| 253 | + }); |
| 254 | + |
| 255 | + it('includes severity emoji for failures', () => { |
| 256 | + const failure: Failure = { |
| 257 | + code: 'CONNECTOR_FAULT', |
| 258 | + description: 'Connector fault detected', |
| 259 | + severity: 'critical', |
| 260 | + eventIds: ['evt-0001'], |
| 261 | + suggestedSteps: ['Inspect the connector'], |
| 262 | + }; |
| 263 | + |
| 264 | + const result: AnalysisResult = { |
| 265 | + events: [], |
| 266 | + sessions: [], |
| 267 | + failures: [failure], |
| 268 | + summaries: [], |
| 269 | + warnings: [], |
| 270 | + }; |
| 271 | + |
| 272 | + const report = generateMarkdownReport(result); |
| 273 | + expect(report).toContain('🔴'); |
| 274 | + expect(report).toContain('CONNECTOR_FAULT'); |
| 275 | + }); |
| 276 | +}); |
0 commit comments