|
| 1 | +import { writeFileSync } from 'fs'; |
| 2 | +import { join } from 'path'; |
| 3 | +import { MARKDOWN_REPORT_FILENAME } from './constants.js'; |
| 4 | +import type { ComparisonResult } from './image-comparer.js'; |
| 5 | +import type { ScannedFile } from './file-scanner.js'; |
| 6 | + |
| 7 | +// Generates a markdown report and writes it to the output directory |
| 8 | +export function generateMarkdownReport( |
| 9 | + comparisonResults: ComparisonResult[], |
| 10 | + baselineOnly: ScannedFile[], |
| 11 | + candidateOnly: ScannedFile[], |
| 12 | + outputDir: string, |
| 13 | +): void { |
| 14 | + const markdown = generateMarkdown(comparisonResults, baselineOnly, candidateOnly); |
| 15 | + writeFileSync(join(outputDir, MARKDOWN_REPORT_FILENAME), markdown); |
| 16 | +} |
| 17 | + |
| 18 | +// Builds the full markdown string from comparison data |
| 19 | +function generateMarkdown( |
| 20 | + comparisonResults: ComparisonResult[], |
| 21 | + baselineOnly: ScannedFile[], |
| 22 | + candidateOnly: ScannedFile[], |
| 23 | +): string { |
| 24 | + const withDifferences = comparisonResults.filter((r) => r.hasDifference); |
| 25 | + const withoutDifferences = comparisonResults.filter((r) => !r.hasDifference); |
| 26 | + |
| 27 | + const totalImages = comparisonResults.length + baselineOnly.length + candidateOnly.length; |
| 28 | + const diffCount = withDifferences.length; |
| 29 | + const removedCount = baselineOnly.length; |
| 30 | + const addedCount = candidateOnly.length; |
| 31 | + const identicalCount = withoutDifferences.length; |
| 32 | + |
| 33 | + const hasFailed = diffCount > 0 || removedCount > 0; |
| 34 | + const statusEmoji = hasFailed ? '❌' : '✅'; |
| 35 | + const statusText = hasFailed ? 'FAILED' : 'PASSED'; |
| 36 | + |
| 37 | + const lines: string[] = []; |
| 38 | + |
| 39 | + // Status header |
| 40 | + lines.push(`# ${statusEmoji} Visual Diff Report — ${statusText}`); |
| 41 | + lines.push(''); |
| 42 | + |
| 43 | + // Summary line |
| 44 | + const parts: string[] = []; |
| 45 | + if (diffCount > 0) parts.push(`**${diffCount}** different`); |
| 46 | + if (removedCount > 0) parts.push(`**${removedCount}** removed`); |
| 47 | + if (addedCount > 0) parts.push(`**${addedCount}** added`); |
| 48 | + if (identicalCount > 0) parts.push(`**${identicalCount}** identical`); |
| 49 | + lines.push(`**${totalImages}** images compared: ${parts.join(' · ')}`); |
| 50 | + lines.push(''); |
| 51 | + |
| 52 | + // Details section with all file lists |
| 53 | + const hasDetails = |
| 54 | + withDifferences.length > 0 || removedCount > 0 || addedCount > 0 || identicalCount > 0; |
| 55 | + |
| 56 | + if (hasDetails) { |
| 57 | + lines.push('<details>'); |
| 58 | + lines.push('<summary>Details</summary>'); |
| 59 | + lines.push(''); |
| 60 | + |
| 61 | + if (withDifferences.length > 0) { |
| 62 | + lines.push(`### Differences (${diffCount})`); |
| 63 | + lines.push(''); |
| 64 | + lines.push('| File | Diff % | Notes |'); |
| 65 | + lines.push('|------|--------|-------|'); |
| 66 | + for (const result of withDifferences) { |
| 67 | + const notes = result.dimensionMismatch |
| 68 | + ? `⚠️ Dimension mismatch (${result.dimensionMismatch.baseline} → ${result.dimensionMismatch.candidate})` |
| 69 | + : ''; |
| 70 | + lines.push(`| ${result.pair.name} | ${result.diffPercentage.toFixed(2)}% | ${notes} |`); |
| 71 | + } |
| 72 | + lines.push(''); |
| 73 | + } |
| 74 | + |
| 75 | + if (removedCount > 0) { |
| 76 | + lines.push(`### Removed Files (${removedCount})`); |
| 77 | + lines.push(''); |
| 78 | + for (const file of baselineOnly) { |
| 79 | + lines.push(`- \`${file.name}\``); |
| 80 | + } |
| 81 | + lines.push(''); |
| 82 | + } |
| 83 | + |
| 84 | + if (addedCount > 0) { |
| 85 | + lines.push(`### Added Files (${addedCount})`); |
| 86 | + lines.push(''); |
| 87 | + for (const file of candidateOnly) { |
| 88 | + lines.push(`- \`${file.name}\``); |
| 89 | + } |
| 90 | + lines.push(''); |
| 91 | + } |
| 92 | + |
| 93 | + if (identicalCount > 0) { |
| 94 | + lines.push(`### Identical Files (${identicalCount})`); |
| 95 | + lines.push(''); |
| 96 | + for (const result of withoutDifferences) { |
| 97 | + lines.push(`- \`${result.pair.name}\``); |
| 98 | + } |
| 99 | + lines.push(''); |
| 100 | + } |
| 101 | + |
| 102 | + lines.push('</details>'); |
| 103 | + lines.push(''); |
| 104 | + } |
| 105 | + |
| 106 | + return lines.join('\n'); |
| 107 | +} |
0 commit comments