|
| 1 | +/** |
| 2 | + * Integration-style test for Report extraction to ensure workflow YAML is parsed |
| 3 | + * and data sets (listeners, permissions, runsOn, secrets, vars, uses) are populated. |
| 4 | + * This is written BEFORE the underlying bug fix (yaml Promise) so it will fail initially (TDD). |
| 5 | + */ |
| 6 | +import {jest} from '@jest/globals' |
| 7 | +import Report from '../../src/report/report.js' |
| 8 | + |
| 9 | +// Minimal mock logger with required interface (spinner-compatible methods) |
| 10 | +const mockLogger = () => ({ |
| 11 | + debug: jest.fn(), |
| 12 | + warn: jest.fn(), |
| 13 | + error: jest.fn(), |
| 14 | + start: jest.fn(), |
| 15 | + stopAndPersist: jest.fn(), |
| 16 | + fail: jest.fn(), |
| 17 | + isDebug: true, |
| 18 | + text: '', |
| 19 | + set text(_) {}, |
| 20 | +}) |
| 21 | + |
| 22 | +// Minimal mock cache (disabled path interactions for this test) |
| 23 | +const mockCache = () => ({ |
| 24 | + path: '', |
| 25 | + exists: jest.fn().mockResolvedValue(false), |
| 26 | + load: jest.fn(), |
| 27 | + save: jest.fn(), |
| 28 | +}) |
| 29 | + |
| 30 | +// Sample workflow YAML content containing all extractable elements |
| 31 | +const WORKFLOW_YAML = `name: CI Full |
| 32 | +on: |
| 33 | + push: |
| 34 | + branches: [ main ] |
| 35 | +permissions: |
| 36 | + contents: read |
| 37 | +jobs: |
| 38 | + build: |
| 39 | + runs-on: ubuntu-latest |
| 40 | + steps: |
| 41 | + - name: Checkout |
| 42 | + uses: actions/checkout@v4 |
| 43 | + - name: Setup |
| 44 | + uses: actions/setup-node@v4 |
| 45 | + with: |
| 46 | + node-version: 20 |
| 47 | + - name: Env usage |
| 48 | + run: echo "+\${{ secrets.MY_SECRET }}+\${{ vars.MY_VAR }}+" |
| 49 | +` |
| 50 | + |
| 51 | +// Fake workflow object as produced by Repository.getWorkflows / Workflow.getWorkflow (after fix) |
| 52 | +function createWorkflow() { |
| 53 | + return { |
| 54 | + node_id: 'WF_node', |
| 55 | + path: '.github/workflows/ci.yml', |
| 56 | + language: 'YAML', |
| 57 | + text: WORKFLOW_YAML, |
| 58 | + yaml: { |
| 59 | + name: 'CI Full', |
| 60 | + on: {push: {branches: ['main']}}, |
| 61 | + permissions: {contents: 'read'}, |
| 62 | + jobs: { |
| 63 | + build: { |
| 64 | + 'runs-on': 'ubuntu-latest', |
| 65 | + steps: [ |
| 66 | + {name: 'Checkout', uses: 'actions/checkout@v4'}, |
| 67 | + {name: 'Setup', uses: 'actions/setup-node@v4', with: {'node-version': 20}}, |
| 68 | + {name: 'Env usage', run: 'echo "+${{ secrets.MY_SECRET }}+${{ vars.MY_VAR }}+"'}, |
| 69 | + ], |
| 70 | + }, |
| 71 | + }, |
| 72 | + }, |
| 73 | + state: 'active', |
| 74 | + created_at: new Date().toISOString(), |
| 75 | + updated_at: new Date().toISOString(), |
| 76 | + last_run_at: new Date().toISOString(), |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +describe('Report extraction end-to-end (regression)', () => { |
| 81 | + test('should populate extraction sets from workflow YAML', async () => { |
| 82 | + const flags = { |
| 83 | + token: 'TEST', |
| 84 | + repository: 'o/r', |
| 85 | + csv: null, |
| 86 | + json: null, |
| 87 | + md: null, |
| 88 | + listeners: true, |
| 89 | + permissions: true, |
| 90 | + runsOn: true, |
| 91 | + secrets: true, |
| 92 | + vars: true, |
| 93 | + uses: true, |
| 94 | + unique: 'false', |
| 95 | + } |
| 96 | + |
| 97 | + const report = new Report(flags, mockLogger(), mockCache()) |
| 98 | + |
| 99 | + const repoData = { |
| 100 | + workflows: [createWorkflow()], |
| 101 | + } |
| 102 | + |
| 103 | + const data = await report.createReport(repoData) |
| 104 | + expect(data).toHaveLength(1) |
| 105 | + const wf = data[0] |
| 106 | + |
| 107 | + // These expectations will fail until yaml Promise bug is fixed |
| 108 | + expect(wf.name).toBe('CI Full') |
| 109 | + expect(wf.listeners instanceof Set && wf.listeners.size).toBeGreaterThan(0) |
| 110 | + expect(wf.permissions instanceof Set && wf.permissions.size).toBeGreaterThan(0) |
| 111 | + expect(wf.runsOn instanceof Set && wf.runsOn.size).toBeGreaterThan(0) |
| 112 | + expect(wf.secrets instanceof Set && wf.secrets.size).toBeGreaterThan(0) |
| 113 | + expect(wf.vars instanceof Set && wf.vars.size).toBeGreaterThan(0) |
| 114 | + expect(wf.uses instanceof Set && wf.uses.size).toBeGreaterThan(0) |
| 115 | + }) |
| 116 | +}) |
0 commit comments