-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy pathvisual-report.js
More file actions
700 lines (611 loc) · 20.5 KB
/
Copy pathvisual-report.js
File metadata and controls
700 lines (611 loc) · 20.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
import fs from 'fs';
import path from 'path';
const SLASH_REGEX = /\//g;
// `notRun` means vitest never reported on the test at all, e.g. because it
// belongs to a project this CI job did not run.
const PASSED = 'passed';
const FAILED = 'failed';
const SKIPPED = 'skipped';
const NOT_RUN = 'notRun';
// Screenshot-only: the test failed before capturing this screenshot.
const NOT_CAPTURED = 'notCaptured';
// The assertion `visualTest()` registers inside each test's describe block.
const VISUAL_TEST_ASSERTION = 'matches expected screenshots';
const VITEST_STATUS = {
passed: PASSED,
failed: FAILED,
skipped: SKIPPED,
pending: SKIPPED,
todo: SKIPPED,
disabled: SKIPPED
};
const STATUS_LABEL = {
[PASSED]: 'PASS',
[FAILED]: 'FAIL',
[SKIPPED]: 'SKIPPED',
[NOT_RUN]: 'NOT RUN',
[NOT_CAPTURED]: 'NOT CAPTURED'
};
const STATUS_CLASS = {
[PASSED]: 'status-pass',
[FAILED]: 'status-fail',
[SKIPPED]: 'status-skip',
[NOT_RUN]: 'status-not-run',
[NOT_CAPTURED]: 'status-not-run'
};
// Mirrors `escapeName()` in test/unit/visual/visualTest.js, which turns suite
// names into the directory names under screenshots/.
function escapeName(name) {
return name.replace(SLASH_REGEX, '%2F');
}
function percent(value, total) {
if (total <= 0) return '0';
const exact = (value / total) * 100;
const rounded = Math.round(exact);
// Don't let rounding read as a clean 0% or 100% when it isn't one.
if ((rounded === 0 && value > 0) || (rounded === 100 && value < total)) {
return exact.toFixed(1);
}
return String(rounded);
}
function escapeHTML(text) {
return text
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"');
}
// Failure messages embed base64 data URLs of the images, which would balloon
// the report, so keep just the headline.
function summarizeFailure(messages) {
if (!messages || messages.length === 0) return null;
const firstLine = String(messages[0]).split('\n')[0].trim();
if (!firstLine) return null;
return firstLine.length > 300 ? `${firstLine.slice(0, 300)}…` : firstLine;
}
/**
* Read vitest's json reporter output, keyed by the screenshot directory each
* test corresponds to. Returns null if there are no usable results.
*/
function loadTestResults(resultsFile) {
if (!fs.existsSync(resultsFile)) {
console.warn(
`No vitest results found at ${resultsFile}. Falling back to inferring ` +
'test results from the screenshots on disk, which cannot tell a failed ' +
'test apart from one that never ran.'
);
return null;
}
let report;
try {
report = JSON.parse(fs.readFileSync(resultsFile, 'utf8'));
} catch (error) {
console.error(`Failed to read vitest results: ${resultsFile}`, error);
return null;
}
const results = new Map();
for (const fileResult of report.testResults || []) {
for (const assertion of fileResult.assertionResults || []) {
if (assertion.title !== VISUAL_TEST_ASSERTION) continue;
const ancestors = assertion.ancestorTitles || [];
if (ancestors.length === 0) continue;
// `visualTest()` saves screenshots under the escaped suite path.
const name = ancestors.map(escapeName).join('/');
const status = VITEST_STATUS[assertion.status] || SKIPPED;
// A test is reported once per project it runs in; a failure anywhere wins.
const existing = results.get(name);
if (existing && (existing.status === FAILED || status !== FAILED)) {
continue;
}
results.set(name, {
status,
failure: summarizeFailure(assertion.failureMessages)
});
}
}
console.log(
`Loaded ${results.size} visual test result(s) from ${resultsFile}`
);
return results;
}
async function generateVisualReport() {
const expectedDir = path.join(process.cwd(), 'test/unit/visual/screenshots');
const actualDir = path.join(
process.cwd(),
'test/unit/visual/actual-screenshots'
);
const resultsFile =
process.env.VISUAL_TEST_RESULTS ||
path.join(process.cwd(), 'test/unit/visual/test-results.json');
const outputFile = path.join(
process.cwd(),
'test/unit/visual/visual-report.html'
);
// Make sure the output directory exists
const outputDir = path.dirname(outputFile);
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true });
}
const testResults = loadTestResults(resultsFile);
// Function to read image file and convert to data URL
function imageToDataURL(filePath) {
try {
const data = fs.readFileSync(filePath);
const base64 = data.toString('base64');
return `data:image/png;base64,${base64}`;
} catch (error) {
console.error(`Failed to read image: ${filePath}`, error);
return null;
}
}
// Create a lookup map for actual screenshots
function createActualScreenshotMap() {
const actualMap = new Map();
if (!fs.existsSync(actualDir)) {
console.warn(`Actual screenshots directory does not exist: ${actualDir}`);
return actualMap;
}
const files = fs.readdirSync(actualDir);
for (const file of files) {
if (file.endsWith('.png') && !file.endsWith('-diff.png')) {
actualMap.set(file, path.join(actualDir, file));
}
}
return actualMap;
}
const actualScreenshotMap = createActualScreenshotMap();
// Recursively find all test cases
function findTestCases(dir, prefix = '') {
const testCases = [];
if (!fs.existsSync(path.join(dir, prefix))) {
console.warn(`Directory does not exist: ${path.join(dir, prefix)}`);
return testCases;
}
const entries = fs.readdirSync(path.join(dir, prefix), {
withFileTypes: true
});
for (const entry of entries) {
const fullPath = path.join(prefix, entry.name);
if (entry.isDirectory()) {
// Recursively search subdirectories
testCases.push(...findTestCases(dir, fullPath));
} else if (entry.name === 'metadata.json') {
// Found a test case
const metadataPath = path.join(dir, fullPath);
let metadata;
try {
metadata = JSON.parse(fs.readFileSync(metadataPath, 'utf8'));
} catch (error) {
console.error(`Failed to read metadata: ${metadataPath}`, error);
continue;
}
const testDir = path.dirname(fullPath);
// The test names vitest reports always use forward slashes.
const testName = testDir.split(path.sep).join('/');
// When vitest results exist they are the source of truth: a test they
// never mention never ran. Otherwise fall back to inferring from disk.
const result = testResults ? testResults.get(testName) : null;
const reportedStatus = testResults
? result
? result.status
: NOT_RUN
: null;
const test = {
name: testName,
numScreenshots: metadata.numScreenshots || 0,
status: reportedStatus,
failure: result ? result.failure : null,
screenshots: []
};
// Create flattened name for lookup
const flattenedName = testName.replace(SLASH_REGEX, '-');
// Collect all screenshots for this test
for (let i = 0; i < test.numScreenshots; i++) {
const screenshotName = i.toString().padStart(3, '0') + '.png';
const expectedPath = path.join(dir, testDir, screenshotName);
// Use flattened name for actual screenshots
const actualScreenshotName = `${flattenedName}-${i.toString().padStart(3, '0')}.png`;
const actualPath =
actualScreenshotMap.get(actualScreenshotName) || null;
// Use flattened name for diff image
const diffScreenshotName = `${flattenedName}-${i.toString().padStart(3, '0')}-diff.png`;
const diffPath = path.join(actualDir, diffScreenshotName);
const hasExpected = fs.existsSync(expectedPath);
const hasActual = actualPath && fs.existsSync(actualPath);
const hasDiff = fs.existsSync(diffPath);
let status;
if (reportedStatus === null) {
// No vitest results: the old disk-only heuristic.
status = hasExpected && hasActual && !hasDiff ? PASSED : FAILED;
} else if (
reportedStatus === SKIPPED ||
reportedStatus === NOT_RUN ||
reportedStatus === PASSED
) {
status = reportedStatus;
} else if (hasDiff) {
status = FAILED;
} else if (hasExpected && hasActual) {
// This screenshot matched; the test failed for some other reason.
status = PASSED;
} else if (!hasActual) {
// The test bailed out before getting this far.
status = NOT_CAPTURED;
} else {
status = FAILED;
}
const screenshot = {
index: i,
expectedImage: hasExpected ? imageToDataURL(expectedPath) : null,
actualImage: hasActual ? imageToDataURL(actualPath) : null,
// A leftover diff from an earlier run says nothing about a test
// vitest reported as passing, and a missing actual only means
// something when the test actually tried to produce one.
diffImage:
hasDiff && status === FAILED ? imageToDataURL(diffPath) : null,
showMissingActual: status === FAILED || status === NOT_CAPTURED,
status,
passed: status === PASSED
};
test.screenshots.push(screenshot);
}
if (test.status === null) {
test.status = test.screenshots.every(s => s.status === PASSED)
? PASSED
: FAILED;
}
// Don't add tests with no screenshots
if (test.screenshots.length > 0) {
testCases.push(test);
}
}
}
return testCases;
}
// Find all test cases from the expected directory
const testCases = findTestCases(expectedDir);
if (testCases.length === 0) {
console.warn(
'No test cases found. Check if the expected directory is correct.'
);
}
// Count tests and screenshots per status
const totalTests = testCases.length;
const tests = { passed: 0, failed: 0, skipped: 0, notRun: 0 };
const screenshots = {
passed: 0,
failed: 0,
notCaptured: 0,
skipped: 0,
notRun: 0
};
let totalScreenshots = 0;
for (const test of testCases) {
tests[test.status]++;
totalScreenshots += test.screenshots.length;
for (const screenshot of test.screenshots) {
screenshots[screenshot.status]++;
}
}
// Percentages only make sense against the tests that actually ran.
const executedTests = tests.passed + tests.failed;
const executedScreenshots = screenshots.passed + screenshots.failed;
const fallbackNotice = testResults
? ''
: `<div class="warning-notice">
No vitest results were found at <code>${escapeHTML(path.relative(process.cwd(), resultsFile))}</code>,
so statuses below were inferred from the screenshots on disk. Tests that
were skipped or never ran are indistinguishable from failures in this mode.
Run <code>npm test</code> to generate the results, then regenerate this report.
</div>`;
// Generate HTML
const html = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>p5.js Visual Test Results</title>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
line-height: 1.6;
color: #333;
max-width: 1200px;
margin: 0 auto;
padding: 20px;
}
header {
margin-bottom: 30px;
}
.summary {
background-color: #f5f5f5;
padding: 15px;
border-radius: 5px;
margin-bottom: 30px;
}
.summary h2 {
margin-top: 0;
}
.test-group {
border: 1px solid #ddd;
border-radius: 5px;
margin-bottom: 30px;
overflow: hidden;
}
.test-header {
background-color: #f5f5f5;
padding: 10px 15px;
border-bottom: 1px solid #ddd;
font-weight: bold;
display: flex;
justify-content: space-between;
align-items: center;
}
.test-status {
display: inline-block;
padding: 3px 8px;
border-radius: 3px;
font-size: 14px;
font-weight: normal;
}
.status-pass {
background-color: #dff0d8;
color: #3c763d;
}
.status-fail {
background-color: #f2dede;
color: #a94442;
}
.status-skip {
background-color: #fff3cd;
color: #856404;
}
.status-not-run {
background-color: #e9ecef;
color: #555;
}
.failure-message {
padding: 10px 15px;
background-color: #f2dede;
color: #a94442;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 13px;
border-bottom: 1px solid #ddd;
}
.screenshots {
padding: 20px;
}
.screenshot-set {
margin-bottom: 30px;
border-bottom: 1px solid #eee;
padding-bottom: 20px;
position: relative;
}
.screenshot-set:last-child {
margin-bottom: 0;
border-bottom: none;
padding-bottom: 0;
}
.screenshot-header {
margin-bottom: 15px;
font-weight: 500;
display: flex;
justify-content: space-between;
align-items: center;
}
.screenshot-status {
display: inline-block;
padding: 3px 8px;
border-radius: 3px;
font-size: 14px;
}
.screenshot-images {
display: flex;
flex-wrap: wrap;
gap: 20px;
}
.image-container {
flex: 1;
min-width: 300px;
}
.image-header {
margin-bottom: 5px;
font-weight: 500;
}
img {
max-width: 100%;
border: 1px solid #ddd;
background-color: #f8f8f8;
}
.toggle-btn {
background-color: #f8f9fa;
border: 1px solid #ddd;
padding: 5px 10px;
border-radius: 3px;
cursor: pointer;
margin-right: 5px;
}
.toggle-btn.active {
background-color: #e9ecef;
font-weight: bold;
}
.hidden {
display: none;
}
.filters {
margin-bottom: 20px;
}
.missing-notice {
padding: 10px;
background-color: #fff3cd;
color: #856404;
border-radius: 4px;
margin-top: 5px;
}
.warning-notice {
padding: 10px 15px;
background-color: #fff3cd;
color: #856404;
border-radius: 4px;
margin-bottom: 20px;
}
code {
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 13px;
}
</style>
</head>
<body>
<header>
<h1>p5.js Visual Test Results</h1>
<div class="filters">
<button class="toggle-btn active" data-filter="all">Show All</button>
<button class="toggle-btn" data-filter="failed">Show Only Failed</button>
<button class="toggle-btn" data-filter="passed">Show Only Passed</button>
<button class="toggle-btn" data-filter="skipped">Show Only Skipped</button>
<button class="toggle-btn" data-filter="notRun">Show Only Not Run</button>
</div>
</header>
${fallbackNotice}
<div class="summary">
<h2>Summary</h2>
<p>
<strong>Total Tests:</strong> ${totalTests}<br>
<strong>Tests Run:</strong> ${executedTests}<br>
<strong>Passed Tests:</strong> ${tests.passed} (${percent(tests.passed, executedTests)}% of tests run)<br>
<strong>Failed Tests:</strong> ${tests.failed} (${percent(tests.failed, executedTests)}% of tests run)<br>
<strong>Skipped Tests:</strong> ${tests.skipped}<br>
<strong>Tests Not Run:</strong> ${tests.notRun}<br>
<br>
<strong>Total Screenshots:</strong> ${totalScreenshots}<br>
<strong>Passed Screenshots:</strong> ${screenshots.passed} (${percent(screenshots.passed, executedScreenshots)}% of screenshots compared)<br>
<strong>Failed Screenshots:</strong> ${screenshots.failed} (${percent(screenshots.failed, executedScreenshots)}% of screenshots compared)<br>
<strong>Screenshots Not Captured:</strong> ${screenshots.notCaptured}<br>
<strong>Skipped Screenshots:</strong> ${screenshots.skipped}<br>
<strong>Screenshots Not Run:</strong> ${screenshots.notRun}<br>
<br>
<strong>Report Generated:</strong> ${new Date().toLocaleString()}
</p>
</div>
<div id="test-results">
${testCases
.map(
test => `
<div class="test-group" data-status="${test.status}">
<div class="test-header">
<span>${escapeHTML(test.name)}</span>
<span class="test-status ${STATUS_CLASS[test.status]}">${STATUS_LABEL[test.status]}</span>
</div>
${
test.failure
? `<div class="failure-message">${escapeHTML(test.failure)}</div>`
: ''
}
<div class="screenshots">
${test.screenshots
.map(
screenshot => `
<div class="screenshot-set">
<div class="screenshot-header">
<span>Screenshot #${screenshot.index + 1}</span>
<span class="screenshot-status ${STATUS_CLASS[screenshot.status]}">
${STATUS_LABEL[screenshot.status]}
</span>
</div>
<div class="screenshot-images">
<div class="image-container">
<div class="image-header">Expected</div>
${
screenshot.expectedImage
? `<img src="${screenshot.expectedImage}" alt="Expected Result">`
: `<div class="missing-notice">No expected image found</div>`
}
</div>
${
screenshot.actualImage || screenshot.showMissingActual
? `
<div class="image-container">
<div class="image-header">Actual</div>
${
screenshot.actualImage
? `<img src="${screenshot.actualImage}" alt="Actual Result">`
: `<div class="missing-notice">No actual image found</div>`
}
</div>
`
: ''
}
${
screenshot.diffImage
? `
<div class="image-container">
<div class="image-header">Diff</div>
<img src="${screenshot.diffImage}" alt="Difference">
</div>
`
: ''
}
</div>
</div>
`
)
.join('')}
</div>
</div>
`
)
.join('')}
</div>
<script>
// Filter functionality
const buttons = document.querySelectorAll('.toggle-btn');
const testGroups = document.querySelectorAll('.test-group');
buttons.forEach(button => {
button.addEventListener('click', function() {
const filter = this.dataset.filter;
testGroups.forEach(el => {
el.style.display =
filter === 'all' || el.dataset.status === filter ? 'block' : 'none';
});
buttons.forEach(other => other.classList.remove('active'));
this.classList.add('active');
});
});
</script>
</body>
</html>
`;
// Write HTML to file
fs.writeFileSync(outputFile, html);
console.log(`Visual test report generated: ${outputFile}`);
console.log(
`${tests.passed} passed, ${tests.failed} failed, ${tests.skipped} skipped, ` +
`${tests.notRun} not run (of ${totalTests} total)`
);
return {
totalTests,
executedTests,
passedTests: tests.passed,
failedTests: tests.failed,
skippedTests: tests.skipped,
notRunTests: tests.notRun,
totalScreenshots,
executedScreenshots,
passedScreenshots: screenshots.passed,
failedScreenshots: screenshots.failed,
notCapturedScreenshots: screenshots.notCaptured,
skippedScreenshots: screenshots.skipped,
notRunScreenshots: screenshots.notRun,
usedTestResults: testResults !== null,
reportPath: outputFile
};
}
// Run the function if this script is executed directly
if (import.meta.main === true) {
generateVisualReport().catch(error => {
console.error('Failed to generate report:', error);
process.exit(1);
});
}
export { generateVisualReport };