| @@ -0,0 +1,200 @@ |
| 1 | +import { describe, expect, it } from 'bun:test' |
| 2 | +import { generateUnifiedWorkflow } from '../src/setup' |
| 3 | + |
| 4 | +interface Job { if?: string, needs?: string | string[] } |
| 5 | +interface Workflow { jobs: Record<string, Job> } |
| 6 | + |
| 7 | +const workflow = Bun.YAML.parse(generateUnifiedWorkflow(false)) as Workflow |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | +function evaluate(expression: string | undefined, ctx: Record<string, unknown>): boolean { |
| 18 | + if (!expression) |
| 19 | + return true |
| 20 | + |
| 21 | + const body = expression |
| 22 | + .replace(/^\$\{\{/, '') |
| 23 | + .replace(/\}\}$/, '') |
| 24 | + .replace(/always\(\)/g, 'true') |
| 25 | + .replace(/!cancelled\(\)/g, 'true') |
| 26 | + .replace(/needs\.determine-jobs\./g, 'c.needs["determine-jobs"].') |
| 27 | + .replace(/(?<!c\.)\bneeds\./g, 'c.needs.') |
| 28 | + .replace(/(?<!c\.)\bgithub\./g, 'c.github.') |
| 29 | + |
| 30 | + |
| 31 | + return Boolean(new Function('c', `return (${body})`)(ctx)) |
| 32 | +} |
| 33 | + |
| 34 | + |
| 35 | + |
| 36 | + |
| 37 | + |
| 38 | + |
| 39 | + |
| 40 | + |
| 41 | +function context(github: Record<string, unknown>, outputs: Record<string, string>) { |
| 42 | + const full = { |
| 43 | + run_check: 'false', |
| 44 | + run_update: 'false', |
| 45 | + run_dashboard: 'false', |
| 46 | + run_command: 'false', |
| 47 | + run_review: 'false', |
| 48 | + run_fixci: 'false', |
| 49 | + run_report: 'false', |
| 50 | + ...outputs, |
| 51 | + } |
| 52 | + |
| 53 | + const ctx = { |
| 54 | + github, |
| 55 | + needs: { |
| 56 | + 'determine-jobs': { |
| 57 | + outputs: { ...full, run_any: Object.values(full).includes('true') ? 'true' : 'false' }, |
| 58 | + result: 'success', |
| 59 | + }, |
| 60 | + 'setup': { result: 'success' }, |
| 61 | + }, |
| 62 | + } |
| 63 | + |
| 64 | + return ctx |
| 65 | +} |
| 66 | + |
| 67 | + |
| 68 | +function runs(name: string, ctx: Record<string, unknown>): boolean { |
| 69 | + const job = workflow.jobs[name] |
| 70 | + const needs = job.needs ? [job.needs].flat() : [] |
| 71 | + const gated = evaluate(job.if, ctx) |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | + const skipsWithNeeds = !/always\(\)|!cancelled\(\)/.test(job.if ?? '') |
| 76 | + const needsRun = needs.every(dep => dep === 'determine-jobs' || runs(dep, ctx)) |
| 77 | + |
| 78 | + return gated && (needsRun || !skipsWithNeeds) |
| 79 | +} |
| 80 | + |
| 81 | + |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | + |
| 90 | + |
| 91 | +describe('generated workflow reachability', () => { |
| 92 | + const cases: Array<{ event: string, github: Record<string, unknown>, outputs: Record<string, string>, expect: string[] }> = [ |
| 93 | + { |
| 94 | + event: 'pull request opened', |
| 95 | + github: { event_name: 'pull_request', event: { action: 'opened', pull_request: { draft: false, merged: false } } }, |
| 96 | + outputs: { run_review: 'true' }, |
| 97 | + expect: ['review', 'gate'], |
| 98 | + }, |
| 99 | + { |
| 100 | + event: 'pull request synchronized', |
| 101 | + github: { event_name: 'pull_request', event: { action: 'synchronize', pull_request: { draft: false, merged: false } } }, |
| 102 | + outputs: { run_review: 'true' }, |
| 103 | + expect: ['review', 'gate'], |
| 104 | + }, |
| 105 | + { |
| 106 | + event: 'buddy pull request edited', |
| 107 | + github: { event_name: 'pull_request', event: { action: 'edited', pull_request: { draft: false, merged: false } } }, |
| 108 | + outputs: { run_check: 'true' }, |
| 109 | + expect: ['check', 'touch', 'gate'], |
| 110 | + }, |
| 111 | + { |
| 112 | + event: 'pull request merged', |
| 113 | + github: { event_name: 'pull_request', event: { action: 'closed', pull_request: { draft: false, merged: true } } }, |
| 114 | + outputs: {}, |
| 115 | + expect: ['post-merge'], |
| 116 | + }, |
| 117 | + { |
| 118 | + event: '@buddy comment', |
| 119 | + github: { event_name: 'issue_comment', event: { action: 'created' } }, |
| 120 | + outputs: { run_command: 'true' }, |
| 121 | + expect: ['command'], |
| 122 | + }, |
| 123 | + { |
| 124 | + event: 'failing run on a buddy branch', |
| 125 | + github: { event_name: 'workflow_run', event: { action: 'completed', workflow_run: { conclusion: 'failure' } } }, |
| 126 | + outputs: { run_fixci: 'true' }, |
| 127 | + expect: ['fix-ci'], |
| 128 | + }, |
| 129 | + { |
| 130 | + event: 'issue opened', |
| 131 | + github: { event_name: 'issues', event: { action: 'opened' } }, |
| 132 | + outputs: {}, |
| 133 | + expect: ['issue-links'], |
| 134 | + }, |
| 135 | + { |
| 136 | + event: 'scheduled update', |
| 137 | + github: { event_name: 'schedule', event: { schedule: '0 9 * * 1,3,5' } }, |
| 138 | + outputs: { run_update: 'true', run_dashboard: 'true' }, |
| 139 | + expect: ['dependency-update', 'dashboard-update'], |
| 140 | + }, |
| 141 | + { |
| 142 | + event: 'weekly health report', |
| 143 | + github: { event_name: 'schedule', event: { schedule: '0 9 * * 1' } }, |
| 144 | + outputs: { run_report: 'true' }, |
| 145 | + expect: ['report'], |
| 146 | + }, |
| 147 | + ] |
| 148 | + |
| 149 | + for (const testCase of cases) { |
| 150 | + it(`success case - ${testCase.event} reaches ${testCase.expect.join(', ')}`, () => { |
| 151 | + const ctx = context(testCase.github, testCase.outputs) |
| 152 | + |
| 153 | + |
| 154 | + |
| 155 | + expect(runs('setup', ctx)).toBe(true) |
| 156 | + |
| 157 | + for (const job of testCase.expect) |
| 158 | + expect({ job, runs: runs(job, ctx) }).toEqual({ job, runs: true }) |
| 159 | + }) |
| 160 | + } |
| 161 | + |
| 162 | + it('failure case - ordinary discussion starts nothing', () => { |
| 163 | + |
| 164 | + |
| 165 | + const ctx = context( |
| 166 | + { event_name: 'issue_comment', event: { action: 'created' } }, |
| 167 | + {}, |
| 168 | + ) |
| 169 | + |
| 170 | + expect(runs('setup', ctx)).toBe(false) |
| 171 | + expect(runs('command', ctx)).toBe(false) |
| 172 | + }) |
| 173 | + |
| 174 | + it('failure case - a green run starts nothing', () => { |
| 175 | + const ctx = context( |
| 176 | + { event_name: 'workflow_run', event: { action: 'completed', workflow_run: { conclusion: 'success' } } }, |
| 177 | + {}, |
| 178 | + ) |
| 179 | + |
| 180 | + expect(runs('setup', ctx)).toBe(false) |
| 181 | + expect(runs('fix-ci', ctx)).toBe(false) |
| 182 | + }) |
| 183 | + |
| 184 | + it('success case - every job that needs setup is reachable on some event', () => { |
| 185 | + |
| 186 | + |
| 187 | + const dependents = Object.entries(workflow.jobs) |
| 188 | + .filter(([, job]) => [job.needs ?? []].flat().includes('setup')) |
| 189 | + .map(([name]) => name) |
| 190 | + |
| 191 | + const reachable = new Set( |
| 192 | + cases.flatMap(testCase => { |
| 193 | + const ctx = context(testCase.github, testCase.outputs) |
| 194 | + return dependents.filter(name => runs(name, ctx)) |
| 195 | + }), |
| 196 | + ) |
| 197 | + |
| 198 | + expect([...dependents].filter(name => !reachable.has(name))).toEqual([]) |
| 199 | + }) |
| 200 | +}) |