|
| 1 | +import { describe, it } from 'node:test'; |
| 2 | +import assert from 'node:assert/strict'; |
| 3 | + |
| 4 | +import { buildReport } from '../audit-unlighthouse.mts'; |
| 5 | +import type { CiResult } from '../audit-unlighthouse.mts'; |
| 6 | + |
| 7 | +// --------------------------------------------------------------------------- |
| 8 | +// Fixtures |
| 9 | +// --------------------------------------------------------------------------- |
| 10 | + |
| 11 | +const DEFAULT_METRICS: CiResult['summary']['metrics'] = { |
| 12 | + 'largest-contentful-paint': { displayValue: '2.5 s' }, |
| 13 | + 'cumulative-layout-shift': { displayValue: '0.01' }, |
| 14 | + 'first-contentful-paint': { displayValue: '1.2 s' }, |
| 15 | + 'total-blocking-time': { displayValue: '100 ms' }, |
| 16 | + 'max-potential-fid': { displayValue: '200 ms' }, |
| 17 | + interactive: { displayValue: '3.5 s' }, |
| 18 | +}; |
| 19 | + |
| 20 | +function makeCiResult(overrides: { |
| 21 | + score?: number; |
| 22 | + performance?: number; |
| 23 | + accessibility?: number; |
| 24 | + 'best-practices'?: number; |
| 25 | + seo?: number; |
| 26 | + metrics?: CiResult['summary']['metrics']; |
| 27 | +} = {}): CiResult { |
| 28 | + return { |
| 29 | + summary: { |
| 30 | + score: overrides.score ?? 0.85, |
| 31 | + categories: { |
| 32 | + performance: { score: overrides.performance ?? 0.80 }, |
| 33 | + accessibility: { score: overrides.accessibility ?? 0.92 }, |
| 34 | + 'best-practices': { score: overrides['best-practices'] ?? 1.0 }, |
| 35 | + seo: { score: overrides.seo ?? 0.90 }, |
| 36 | + }, |
| 37 | + metrics: overrides.metrics ?? { ...DEFAULT_METRICS }, |
| 38 | + }, |
| 39 | + }; |
| 40 | +} |
| 41 | + |
| 42 | +const BASE = makeCiResult(); |
| 43 | + |
| 44 | +// --------------------------------------------------------------------------- |
| 45 | +// Report heading |
| 46 | +// --------------------------------------------------------------------------- |
| 47 | + |
| 48 | +describe('report heading', () => { |
| 49 | + it('contains the audit heading', () => { |
| 50 | + const markdown = buildReport(BASE, BASE); |
| 51 | + |
| 52 | + assert.ok(markdown.includes('## Unlighthouse Audit'), 'Missing main heading'); |
| 53 | + }); |
| 54 | + |
| 55 | + it('appends branch label when branch is given', () => { |
| 56 | + const markdown = buildReport(BASE, BASE, 'canary'); |
| 57 | + |
| 58 | + assert.ok( |
| 59 | + markdown.includes('## Unlighthouse Audit — `canary`'), |
| 60 | + 'Missing branch label in heading', |
| 61 | + ); |
| 62 | + }); |
| 63 | + |
| 64 | + it('handles branch names with slashes', () => { |
| 65 | + const markdown = buildReport(BASE, BASE, 'integrations/makeswift'); |
| 66 | + |
| 67 | + assert.ok(markdown.includes('`integrations/makeswift`')); |
| 68 | + }); |
| 69 | + |
| 70 | + it('omits branch label when none provided', () => { |
| 71 | + const markdown = buildReport(BASE, BASE); |
| 72 | + |
| 73 | + assert.ok(!markdown.includes(' — '), 'Should not contain a branch label separator'); |
| 74 | + }); |
| 75 | + |
| 76 | + it('contains the description text', () => { |
| 77 | + const markdown = buildReport(BASE, BASE); |
| 78 | + |
| 79 | + assert.ok(markdown.includes('Unlighthouse scores for the latest commit on this branch.')); |
| 80 | + }); |
| 81 | +}); |
| 82 | + |
| 83 | +// --------------------------------------------------------------------------- |
| 84 | +// Summary Score section |
| 85 | +// --------------------------------------------------------------------------- |
| 86 | + |
| 87 | +describe('Summary Score section', () => { |
| 88 | + it('contains the Summary Score heading', () => { |
| 89 | + const markdown = buildReport(BASE, BASE); |
| 90 | + |
| 91 | + assert.ok(markdown.includes('### Summary Score')); |
| 92 | + }); |
| 93 | + |
| 94 | + it('contains the aggregate score note', () => { |
| 95 | + const markdown = buildReport(BASE, BASE); |
| 96 | + |
| 97 | + assert.ok(markdown.includes('Aggregate score across all categories as reported by Unlighthouse.')); |
| 98 | + }); |
| 99 | + |
| 100 | + it('renders scores as integers on a 1-100 scale', () => { |
| 101 | + const desktop = makeCiResult({ score: 0.85 }); |
| 102 | + const mobile = makeCiResult({ score: 0.72 }); |
| 103 | + const markdown = buildReport(desktop, mobile); |
| 104 | + |
| 105 | + assert.ok(markdown.includes('| Score | 85 | 72 |')); |
| 106 | + }); |
| 107 | + |
| 108 | + it('rounds fractional scores correctly', () => { |
| 109 | + const desktop = makeCiResult({ score: 0.856 }); // rounds to 86 |
| 110 | + const markdown = buildReport(desktop, BASE); |
| 111 | + |
| 112 | + assert.ok(markdown.includes('86'), 'Score 0.856 should round to 86'); |
| 113 | + }); |
| 114 | + |
| 115 | + it('contains the two-column header', () => { |
| 116 | + const markdown = buildReport(BASE, BASE); |
| 117 | + |
| 118 | + assert.ok(markdown.includes('| | Desktop | Mobile |')); |
| 119 | + }); |
| 120 | +}); |
| 121 | + |
| 122 | +// --------------------------------------------------------------------------- |
| 123 | +// Category Scores section |
| 124 | +// --------------------------------------------------------------------------- |
| 125 | + |
| 126 | +describe('Category Scores section', () => { |
| 127 | + it('contains the Category Scores heading', () => { |
| 128 | + const markdown = buildReport(BASE, BASE); |
| 129 | + |
| 130 | + assert.ok(markdown.includes('### Category Scores')); |
| 131 | + }); |
| 132 | + |
| 133 | + it('renders all four categories', () => { |
| 134 | + const markdown = buildReport(BASE, BASE); |
| 135 | + |
| 136 | + assert.ok(markdown.includes('Performance')); |
| 137 | + assert.ok(markdown.includes('Accessibility')); |
| 138 | + assert.ok(markdown.includes('Best Practices')); |
| 139 | + assert.ok(markdown.includes('SEO')); |
| 140 | + }); |
| 141 | + |
| 142 | + it('renders desktop and mobile scores independently', () => { |
| 143 | + const desktop = makeCiResult({ performance: 0.80 }); |
| 144 | + const mobile = makeCiResult({ performance: 0.93 }); |
| 145 | + const markdown = buildReport(desktop, mobile); |
| 146 | + |
| 147 | + assert.ok( |
| 148 | + markdown.includes('| Performance | 80 | 93 |'), |
| 149 | + 'Performance row should show desktop then mobile score', |
| 150 | + ); |
| 151 | + }); |
| 152 | + |
| 153 | + it('shows all four categories independently', () => { |
| 154 | + const desktop = makeCiResult({ seo: 0.88, accessibility: 0.75 }); |
| 155 | + const mobile = makeCiResult({ seo: 0.91, accessibility: 0.82 }); |
| 156 | + const markdown = buildReport(desktop, mobile); |
| 157 | + |
| 158 | + assert.ok(markdown.includes('| SEO | 88 | 91 |')); |
| 159 | + assert.ok(markdown.includes('| Accessibility | 75 | 82 |')); |
| 160 | + }); |
| 161 | +}); |
| 162 | + |
| 163 | +// --------------------------------------------------------------------------- |
| 164 | +// Core Web Vitals section |
| 165 | +// --------------------------------------------------------------------------- |
| 166 | + |
| 167 | +describe('Core Web Vitals section', () => { |
| 168 | + it('contains the Core Web Vitals heading', () => { |
| 169 | + const markdown = buildReport(BASE, BASE); |
| 170 | + |
| 171 | + assert.ok(markdown.includes('### Core Web Vitals')); |
| 172 | + }); |
| 173 | + |
| 174 | + it('renders all six metrics', () => { |
| 175 | + const markdown = buildReport(BASE, BASE); |
| 176 | + |
| 177 | + assert.ok(markdown.includes('LCP')); |
| 178 | + assert.ok(markdown.includes('CLS')); |
| 179 | + assert.ok(markdown.includes('FCP')); |
| 180 | + assert.ok(markdown.includes('TBT')); |
| 181 | + assert.ok(markdown.includes('Max Potential FID')); |
| 182 | + assert.ok(markdown.includes('Time to Interactive')); |
| 183 | + }); |
| 184 | + |
| 185 | + it('passes displayValue through unchanged', () => { |
| 186 | + const desktop = makeCiResult({ |
| 187 | + metrics: { ...DEFAULT_METRICS, 'largest-contentful-paint': { displayValue: '4.8 s' } }, |
| 188 | + }); |
| 189 | + const markdown = buildReport(desktop, BASE); |
| 190 | + |
| 191 | + assert.ok(markdown.includes('4.8 s')); |
| 192 | + }); |
| 193 | + |
| 194 | + it('shows — for a metric missing from a result', () => { |
| 195 | + const desktopMissingMetric = makeCiResult({ metrics: {} }); |
| 196 | + const markdown = buildReport(desktopMissingMetric, BASE); |
| 197 | + |
| 198 | + assert.ok(markdown.includes('—'), 'Missing metric should show —'); |
| 199 | + }); |
| 200 | + |
| 201 | + it('shows desktop and mobile displayValues per metric row', () => { |
| 202 | + const desktop = makeCiResult({ metrics: { ...DEFAULT_METRICS, 'total-blocking-time': { displayValue: '80 ms' } } }); |
| 203 | + const mobile = makeCiResult({ metrics: { ...DEFAULT_METRICS, 'total-blocking-time': { displayValue: '320 ms' } } }); |
| 204 | + const markdown = buildReport(desktop, mobile); |
| 205 | + |
| 206 | + assert.ok(markdown.includes('| TBT | 80 ms | 320 ms |')); |
| 207 | + }); |
| 208 | +}); |
0 commit comments