|
| 1 | +import { spawnSync } from 'node:child_process' |
| 2 | +import { fileURLToPath } from 'node:url' |
| 3 | +import { describe, expect, it } from 'vitest' |
| 4 | + |
| 5 | +const script = fileURLToPath(new URL('../.github/scripts/wait-for-e2e-build.sh', import.meta.url)) |
| 6 | + |
| 7 | +interface Job { |
| 8 | + name: string |
| 9 | + status: string |
| 10 | + conclusion: string | null |
| 11 | +} |
| 12 | + |
| 13 | +const job = (status: string, conclusion: string | null = null, name = 'checks / build (e2e)') => ({ |
| 14 | + name, |
| 15 | + status, |
| 16 | + conclusion, |
| 17 | +}) |
| 18 | + |
| 19 | +/** |
| 20 | + * Runs the real script against a `gh` stub whose jq filter is the script's own: each poll answers |
| 21 | + * with the next listing (the last one repeats), and `sleep` only counts polls. |
| 22 | + */ |
| 23 | +function runWait(listings: Job[][], apiStatus = 0) { |
| 24 | + return spawnSync( |
| 25 | + 'bash', |
| 26 | + [ |
| 27 | + '-c', |
| 28 | + `set -euo pipefail |
| 29 | + poll=0 |
| 30 | + gh() { |
| 31 | + [ "$1" = api ] && [ "$2" = --paginate ] || return 97 |
| 32 | + case "$3" in |
| 33 | + 'repos/example/project/actions/runs/7/jobs?filter=latest&per_page=100') ;; |
| 34 | + *) return 98 ;; |
| 35 | + esac |
| 36 | + if [ "$API_STATUS" -ne 0 ]; then return "$API_STATUS"; fi |
| 37 | + local listings count |
| 38 | + listings=$(jq -c '.' <<< "$LISTINGS") |
| 39 | + count=$(jq 'length' <<< "$listings") |
| 40 | + local index=$(( poll < count ? poll : count - 1 )) |
| 41 | + jq -r "{jobs: .[$index]} | $5" <<< "$listings" |
| 42 | + } |
| 43 | + sleep() { poll=$((poll + 1)); } |
| 44 | + source "$1"`, |
| 45 | + 'wait-for-e2e-build-fixture', |
| 46 | + script, |
| 47 | + ], |
| 48 | + { |
| 49 | + timeout: 5_000, |
| 50 | + encoding: 'utf8', |
| 51 | + env: { |
| 52 | + PATH: process.env.PATH, |
| 53 | + GITHUB_REPOSITORY: 'example/project', |
| 54 | + GITHUB_RUN_ID: '7', |
| 55 | + LISTINGS: JSON.stringify(listings), |
| 56 | + API_STATUS: String(apiStatus), |
| 57 | + }, |
| 58 | + } |
| 59 | + ) |
| 60 | +} |
| 61 | + |
| 62 | +describe('wait-for-e2e-build.sh', () => { |
| 63 | + it('returns once an in-progress build succeeds', () => { |
| 64 | + const result = runWait([ |
| 65 | + [job('queued'), job('in_progress', null, 'checks / lint')], |
| 66 | + [job('in_progress')], |
| 67 | + [job('completed', 'success')], |
| 68 | + ]) |
| 69 | + expect(result.status).toBe(0) |
| 70 | + expect(result.stdout).toContain('build (e2e) finished') |
| 71 | + }) |
| 72 | + |
| 73 | + it('accepts the unprefixed job name of a directly dispatched checks run', () => { |
| 74 | + expect(runWait([[job('completed', 'success', 'build (e2e)')]]).status).toBe(0) |
| 75 | + }) |
| 76 | + |
| 77 | + it.each(['failure', 'cancelled', 'skipped', 'timed_out'])( |
| 78 | + 'fails when the build ends %s', |
| 79 | + (end) => { |
| 80 | + const result = runWait([[job('in_progress')], [job('completed', end)]]) |
| 81 | + expect(result.status).toBe(1) |
| 82 | + expect(result.stdout).toContain(`finished with '${end}'`) |
| 83 | + } |
| 84 | + ) |
| 85 | + |
| 86 | + it('fails when the run has no build job instead of waiting forever', () => { |
| 87 | + const result = runWait([[job('completed', 'success', 'checks / build')]]) |
| 88 | + expect(result.status).toBe(1) |
| 89 | + expect(result.stdout).toContain('found 0') |
| 90 | + }) |
| 91 | + |
| 92 | + it('fails when two jobs claim the build name', () => { |
| 93 | + const result = runWait([ |
| 94 | + [job('completed', 'success'), job('completed', 'success', 'build (e2e)')], |
| 95 | + ]) |
| 96 | + expect(result.status).toBe(1) |
| 97 | + expect(result.stdout).toContain('found 2') |
| 98 | + }) |
| 99 | + |
| 100 | + it('fails on an API error rather than treating it as still building', () => { |
| 101 | + const result = runWait([[job('in_progress')]], 1) |
| 102 | + expect(result.status).not.toBe(0) |
| 103 | + expect(result.stdout).not.toContain('build (e2e) finished') |
| 104 | + }) |
| 105 | +}) |
0 commit comments