|
| 1 | +// SPDX-FileCopyrightText: Copyright 2026 Stacklok, Inc. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +import assert from 'node:assert/strict'; |
| 5 | +import { spawnSync } from 'node:child_process'; |
| 6 | +import fs from 'node:fs'; |
| 7 | +import os from 'node:os'; |
| 8 | +import path from 'node:path'; |
| 9 | +import test from 'node:test'; |
| 10 | +import { parse } from 'yaml'; |
| 11 | + |
| 12 | +const workflow = parse( |
| 13 | + fs.readFileSync( |
| 14 | + new URL('../.github/workflows/pr-screenshot-summary.yaml', import.meta.url), |
| 15 | + 'utf8' |
| 16 | + ) |
| 17 | +); |
| 18 | +const resolveStep = workflow.jobs['update-description'].steps.find( |
| 19 | + (step) => step.id === 'visual' |
| 20 | +); |
| 21 | + |
| 22 | +function resolveArtifacts(t, artifacts, { unavailable = false } = {}) { |
| 23 | + const root = fs.mkdtempSync(path.join(os.tmpdir(), 'visual-job-')); |
| 24 | + t.after(() => fs.rmSync(root, { recursive: true, force: true })); |
| 25 | + const bin = path.join(root, 'bin'); |
| 26 | + fs.mkdirSync(bin); |
| 27 | + fs.writeFileSync( |
| 28 | + path.join(root, 'jobs.json.fixture'), |
| 29 | + JSON.stringify({ |
| 30 | + jobs: [ |
| 31 | + { |
| 32 | + name: 'Static checks / Lint and format checks', |
| 33 | + conclusion: 'success', |
| 34 | + }, |
| 35 | + { |
| 36 | + name: 'Visual regression / Playwright visual snapshots', |
| 37 | + conclusion: 'failure', |
| 38 | + html_url: 'https://github.com/example/docs/actions/runs/1/job/2', |
| 39 | + started_at: '2026-10-30T17:00:00Z', |
| 40 | + completed_at: '2026-10-30T17:05:00Z', |
| 41 | + }, |
| 42 | + ], |
| 43 | + }) |
| 44 | + ); |
| 45 | + fs.writeFileSync( |
| 46 | + path.join(root, 'artifacts.json.fixture'), |
| 47 | + typeof artifacts === 'string' ? artifacts : JSON.stringify({ artifacts }) |
| 48 | + ); |
| 49 | + fs.writeFileSync( |
| 50 | + path.join(bin, 'gh'), |
| 51 | + `#!/bin/sh |
| 52 | +case "$2" in |
| 53 | + */jobs?*) cat "$RUNNER_TEMP/jobs.json.fixture" ;; |
| 54 | + */artifacts?*) |
| 55 | + if [ "$ARTIFACTS_UNAVAILABLE" = true ]; then exit 1; fi |
| 56 | + cat "$RUNNER_TEMP/artifacts.json.fixture" ;; |
| 57 | + *) exit 1 ;; |
| 58 | +esac |
| 59 | +`, |
| 60 | + { mode: 0o755 } |
| 61 | + ); |
| 62 | + const outputFile = path.join(root, 'outputs'); |
| 63 | + const result = spawnSync('bash', ['-eo', 'pipefail', '-c', resolveStep.run], { |
| 64 | + encoding: 'utf8', |
| 65 | + env: { |
| 66 | + ...process.env, |
| 67 | + PATH: `${bin}${path.delimiter}${process.env.PATH}`, |
| 68 | + RUNNER_TEMP: root, |
| 69 | + GITHUB_OUTPUT: outputFile, |
| 70 | + REPO: 'example/docs', |
| 71 | + RUN_ID: '1', |
| 72 | + ARTIFACTS_UNAVAILABLE: String(unavailable), |
| 73 | + }, |
| 74 | + }); |
| 75 | + const outputs = Object.fromEntries( |
| 76 | + fs |
| 77 | + .readFileSync(outputFile, 'utf8') |
| 78 | + .trim() |
| 79 | + .split('\n') |
| 80 | + .map((line) => { |
| 81 | + const separator = line.indexOf('='); |
| 82 | + return [line.slice(0, separator), line.slice(separator + 1)]; |
| 83 | + }) |
| 84 | + ); |
| 85 | + assert.equal(outputs.conclusion, 'failure'); |
| 86 | + assert.equal( |
| 87 | + outputs.job_url, |
| 88 | + 'https://github.com/example/docs/actions/runs/1/job/2' |
| 89 | + ); |
| 90 | + return { result, outputs }; |
| 91 | +} |
| 92 | + |
| 93 | +const fullReport = { |
| 94 | + name: 'playwright-report', |
| 95 | + expired: false, |
| 96 | + id: 10, |
| 97 | + size_in_bytes: 55_544_517, |
| 98 | + created_at: '2026-10-30T17:04:00Z', |
| 99 | +}; |
| 100 | +const compactReport = { |
| 101 | + name: 'visual-failure-details', |
| 102 | + expired: false, |
| 103 | + id: 20, |
| 104 | + size_in_bytes: 21_006, |
| 105 | + created_at: '2026-10-30T17:03:00Z', |
| 106 | +}; |
| 107 | + |
| 108 | +test('selects compact details ahead of a large full report', (t) => { |
| 109 | + const { result, outputs } = resolveArtifacts(t, [fullReport, compactReport]); |
| 110 | + assert.equal(result.status, 0, result.stderr); |
| 111 | + assert.equal(outputs.artifact_id, '20'); |
| 112 | + assert.equal(outputs.artifact_size, '21006'); |
| 113 | +}); |
| 114 | + |
| 115 | +test('falls back to the full report when compact details are expired', (t) => { |
| 116 | + const { result, outputs } = resolveArtifacts(t, [ |
| 117 | + { ...compactReport, expired: true }, |
| 118 | + fullReport, |
| 119 | + ]); |
| 120 | + assert.equal(result.status, 0, result.stderr); |
| 121 | + assert.equal(outputs.artifact_id, '10'); |
| 122 | +}); |
| 123 | + |
| 124 | +test('preserves the failure conclusion when no artifact is available', (t) => { |
| 125 | + const { result, outputs } = resolveArtifacts(t, []); |
| 126 | + assert.equal(result.status, 0, result.stderr); |
| 127 | + assert.equal(outputs.artifact_id, ''); |
| 128 | + assert.equal(outputs.artifact_size, '0'); |
| 129 | +}); |
| 130 | + |
| 131 | +test('preserves the failure conclusion when artifact discovery fails', (t) => { |
| 132 | + const { result, outputs } = resolveArtifacts(t, [], { unavailable: true }); |
| 133 | + assert.notEqual(result.status, 0); |
| 134 | + assert.equal(outputs.artifact_id, undefined); |
| 135 | +}); |
| 136 | + |
| 137 | +test('preserves the failure conclusion when artifact parsing fails', (t) => { |
| 138 | + const { result, outputs } = resolveArtifacts(t, '{malformed'); |
| 139 | + assert.notEqual(result.status, 0); |
| 140 | + assert.equal(outputs.artifact_id, undefined); |
| 141 | +}); |
| 142 | + |
| 143 | +test('uses the current full report ahead of prior-attempt compact details', (t) => { |
| 144 | + const { result, outputs } = resolveArtifacts(t, [ |
| 145 | + { ...compactReport, created_at: '2026-10-30T16:55:00Z' }, |
| 146 | + fullReport, |
| 147 | + ]); |
| 148 | + assert.equal(result.status, 0, result.stderr); |
| 149 | + assert.equal(outputs.artifact_id, '10'); |
| 150 | +}); |
| 151 | + |
| 152 | +test('uses the fallback when a rerun produces no new artifacts', (t) => { |
| 153 | + const { result, outputs } = resolveArtifacts(t, [ |
| 154 | + { ...compactReport, created_at: '2026-10-30T16:55:00Z' }, |
| 155 | + { ...fullReport, created_at: '2026-10-30T16:56:00Z' }, |
| 156 | + ]); |
| 157 | + assert.equal(result.status, 0, result.stderr); |
| 158 | + assert.equal(outputs.artifact_id, ''); |
| 159 | +}); |
| 160 | + |
| 161 | +test('selects current compact details when older compact details appear first', (t) => { |
| 162 | + const { result, outputs } = resolveArtifacts(t, [ |
| 163 | + { ...compactReport, id: 19, created_at: '2026-10-30T16:55:00Z' }, |
| 164 | + compactReport, |
| 165 | + ]); |
| 166 | + assert.equal(result.status, 0, result.stderr); |
| 167 | + assert.equal(outputs.artifact_id, '20'); |
| 168 | +}); |
| 169 | + |
| 170 | +test('rejects artifacts uploaded after the current visual job finished', (t) => { |
| 171 | + const { result, outputs } = resolveArtifacts(t, [ |
| 172 | + { ...compactReport, created_at: '2026-10-30T17:10:00Z' }, |
| 173 | + ]); |
| 174 | + assert.equal(result.status, 0, result.stderr); |
| 175 | + assert.equal(outputs.artifact_id, ''); |
| 176 | +}); |
| 177 | + |
| 178 | +test('rejects artifacts with no creation timestamp', (t) => { |
| 179 | + const { result, outputs } = resolveArtifacts(t, [ |
| 180 | + { ...compactReport, created_at: undefined }, |
| 181 | + ]); |
| 182 | + assert.equal(result.status, 0, result.stderr); |
| 183 | + assert.equal(outputs.artifact_id, ''); |
| 184 | +}); |
0 commit comments