|
| 1 | +const assert = require('node:assert/strict'); |
| 2 | +const test = require('node:test'); |
| 3 | +const select = require('./select-odc-snapshot.cjs'); |
| 4 | + |
| 5 | +function fixture(runs, artifactsByRun = {}) { |
| 6 | + const outputs = new Map(); |
| 7 | + const visited = []; |
| 8 | + return { |
| 9 | + outputs, |
| 10 | + visited, |
| 11 | + input: { |
| 12 | + context: { repo: { owner: 'teaql', repo: 'teaql-java' } }, |
| 13 | + core: { |
| 14 | + info() {}, |
| 15 | + setOutput(name, value) { outputs.set(name, value); }, |
| 16 | + }, |
| 17 | + github: { |
| 18 | + rest: { |
| 19 | + actions: { |
| 20 | + // Match the real Octokit REST method used by actions/github-script. |
| 21 | + async listWorkflowRuns(args) { |
| 22 | + assert.equal(args.workflow_id, 'dependency-check-data.yml'); |
| 23 | + assert.equal(args.branch, 'main'); |
| 24 | + assert.equal(args.status, 'success'); |
| 25 | + return { data: { workflow_runs: runs } }; |
| 26 | + }, |
| 27 | + async listWorkflowRunArtifacts({ run_id }) { |
| 28 | + visited.push(run_id); |
| 29 | + return { data: { artifacts: artifactsByRun[run_id] ?? [] } }; |
| 30 | + }, |
| 31 | + }, |
| 32 | + }, |
| 33 | + }, |
| 34 | + }, |
| 35 | + }; |
| 36 | +} |
| 37 | + |
| 38 | +const trustedRun = { |
| 39 | + id: 42, |
| 40 | + conclusion: 'success', |
| 41 | + head_branch: 'main', |
| 42 | + event: 'schedule', |
| 43 | + head_repository: { full_name: 'teaql/teaql-java' }, |
| 44 | +}; |
| 45 | +const goodArtifact = { |
| 46 | + name: 'teaql-odc-v13.0.0-Linux', |
| 47 | + expired: false, |
| 48 | + size_in_bytes: 2_000_000, |
| 49 | +}; |
| 50 | + |
| 51 | +test('selects a successful updater artifact from main', async () => { |
| 52 | + const state = fixture([trustedRun], { 42: [goodArtifact] }); |
| 53 | + assert.equal(await select(state.input), 42); |
| 54 | + assert.equal(state.outputs.get('run_id'), '42'); |
| 55 | +}); |
| 56 | + |
| 57 | +test('rejects PR and fork provenance even if an artifact exists', async () => { |
| 58 | + const state = fixture([ |
| 59 | + { ...trustedRun, id: 1, event: 'pull_request' }, |
| 60 | + { ...trustedRun, id: 2, head_repository: { full_name: 'other/teaql-java' } }, |
| 61 | + { ...trustedRun, id: 3, head_branch: 'feature' }, |
| 62 | + ], { 1: [goodArtifact], 2: [goodArtifact], 3: [goodArtifact] }); |
| 63 | + await assert.rejects(select(state.input), /No successful main-branch/); |
| 64 | + assert.deepEqual(state.visited, []); |
| 65 | +}); |
| 66 | + |
| 67 | +test('fails closed for missing, expired or undersized artifacts', async () => { |
| 68 | + const state = fixture([trustedRun], { |
| 69 | + 42: [ |
| 70 | + { ...goodArtifact, expired: true }, |
| 71 | + { ...goodArtifact, size_in_bytes: 100 }, |
| 72 | + ], |
| 73 | + }); |
| 74 | + await assert.rejects(select(state.input), /No successful main-branch/); |
| 75 | + assert.deepEqual(state.visited, [42]); |
| 76 | + assert.equal(state.outputs.size, 0); |
| 77 | +}); |
| 78 | + |
| 79 | +test('skips an unusable recent run and selects the next trusted artifact', async () => { |
| 80 | + const newest = { ...trustedRun, id: 43, event: 'workflow_dispatch' }; |
| 81 | + const state = fixture([newest, trustedRun], { |
| 82 | + 43: [{ ...goodArtifact, expired: true }], |
| 83 | + 42: [goodArtifact], |
| 84 | + }); |
| 85 | + |
| 86 | + assert.equal(await select(state.input), 42); |
| 87 | + assert.deepEqual(state.visited, [43, 42]); |
| 88 | + assert.equal(state.outputs.get('run_id'), '42'); |
| 89 | +}); |
0 commit comments