|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/** |
| 4 | + * External dependencies. |
| 5 | + */ |
| 6 | +const { readFileSync, writeFileSync, existsSync } = require( 'node:fs' ); |
| 7 | +const { join } = require( 'node:path' ); |
| 8 | + |
| 9 | +process.env.WP_ARTIFACTS_PATH ??= join( process.cwd(), 'artifacts' ); |
| 10 | + |
| 11 | +const args = process.argv.slice( 2 ); |
| 12 | +const summaryFile = args[ 0 ]; |
| 13 | +const workflowRunUrl = process.env.WORKFLOW_RUN_URL || ''; |
| 14 | + |
| 15 | +const resultsFile = join( |
| 16 | + process.env.WP_ARTIFACTS_PATH, |
| 17 | + 'visual-results.json' |
| 18 | +); |
| 19 | + |
| 20 | +if ( ! existsSync( resultsFile ) ) { |
| 21 | + const errorMarkdown = |
| 22 | + '<!-- visual-regression-report -->\n## Visual Regression Report\n\n' + |
| 23 | + '**Error:** Could not find visual regression results file.\n'; |
| 24 | + console.log( errorMarkdown ); |
| 25 | + if ( summaryFile ) { |
| 26 | + writeFileSync( summaryFile, errorMarkdown ); |
| 27 | + } |
| 28 | + process.exit( 1 ); |
| 29 | +} |
| 30 | + |
| 31 | +const results = JSON.parse( readFileSync( resultsFile, 'utf8' ) ); |
| 32 | + |
| 33 | +/** |
| 34 | + * Recursively flattens all suites to collect specs. |
| 35 | + * |
| 36 | + * @param {Object} suite A Playwright JSON reporter suite object. |
| 37 | + * @return {Array} Flat array of spec objects. |
| 38 | + */ |
| 39 | +function flattenSpecs( suite ) { |
| 40 | + const specs = [ ...( suite.specs || [] ) ]; |
| 41 | + for ( const child of suite.suites || [] ) { |
| 42 | + specs.push( ...flattenSpecs( child ) ); |
| 43 | + } |
| 44 | + return specs; |
| 45 | +} |
| 46 | + |
| 47 | +const allSpecs = []; |
| 48 | +for ( const suite of results.suites || [] ) { |
| 49 | + allSpecs.push( ...flattenSpecs( suite ) ); |
| 50 | +} |
| 51 | + |
| 52 | +const changed = []; |
| 53 | +const unchanged = []; |
| 54 | +const errored = []; |
| 55 | + |
| 56 | +for ( const spec of allSpecs ) { |
| 57 | + const status = spec.ok ? 'passed' : 'failed'; |
| 58 | + const hasError = spec.tests?.some( ( t ) => |
| 59 | + [ 'timedOut', 'interrupted' ].includes( t.status ) |
| 60 | + ); |
| 61 | + |
| 62 | + if ( hasError ) { |
| 63 | + errored.push( spec.title ); |
| 64 | + } else if ( status === 'failed' ) { |
| 65 | + changed.push( spec.title ); |
| 66 | + } else { |
| 67 | + unchanged.push( spec.title ); |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +const total = changed.length + unchanged.length + errored.length; |
| 72 | + |
| 73 | +let markdown = '<!-- visual-regression-report -->\n## Visual Regression Report\n\n'; |
| 74 | + |
| 75 | +if ( changed.length === 0 && errored.length === 0 ) { |
| 76 | + markdown += `**No visual changes detected** across all ${ total } pages.\n`; |
| 77 | +} else { |
| 78 | + const runLink = workflowRunUrl |
| 79 | + ? ` | [View workflow run](${ workflowRunUrl })` |
| 80 | + : ''; |
| 81 | + markdown += `**${ changed.length } of ${ total } pages changed**${ runLink }\n`; |
| 82 | +} |
| 83 | + |
| 84 | +if ( changed.length > 0 ) { |
| 85 | + markdown += `\n<details>\n<summary>Changed pages (${ changed.length })</summary>\n\n`; |
| 86 | + for ( const name of changed ) { |
| 87 | + markdown += `- ${ name }\n`; |
| 88 | + } |
| 89 | + markdown += '\n</details>\n'; |
| 90 | +} |
| 91 | + |
| 92 | +if ( unchanged.length > 0 ) { |
| 93 | + markdown += `\n<details>\n<summary>Unchanged pages (${ unchanged.length })</summary>\n\n`; |
| 94 | + markdown += unchanged.join( ', ' ) + '\n'; |
| 95 | + markdown += '\n</details>\n'; |
| 96 | +} |
| 97 | + |
| 98 | +if ( errored.length > 0 ) { |
| 99 | + markdown += `\n<details>\n<summary>Errored pages (${ errored.length })</summary>\n\n`; |
| 100 | + for ( const name of errored ) { |
| 101 | + markdown += `- ${ name }\n`; |
| 102 | + } |
| 103 | + markdown += '\n</details>\n'; |
| 104 | +} |
| 105 | + |
| 106 | +markdown += |
| 107 | + '\n---\nDownload the **visual-regression-report** artifact from the workflow run for an HTML report with side-by-side image diffs.\n'; |
| 108 | + |
| 109 | +console.log( markdown ); |
| 110 | + |
| 111 | +if ( summaryFile ) { |
| 112 | + writeFileSync( summaryFile, markdown ); |
| 113 | +} |
0 commit comments