|
| 1 | +import { spawnSync } from 'node:child_process' |
| 2 | +import { readFileSync } from 'node:fs' |
| 3 | +import { runInNewContext } from 'node:vm' |
| 4 | +import { describe, expect, it } from 'vitest' |
| 5 | + |
| 6 | +const ci = readFileSync(new URL('../.github/workflows/ci.yml', import.meta.url), 'utf8') |
| 7 | +const tasks = readFileSync( |
| 8 | + new URL('../.github/workflows/deploy-trigger-dev.yml', import.meta.url), |
| 9 | + 'utf8' |
| 10 | +) |
| 11 | + |
| 12 | +/** Evaluate the checked-in job condition for concrete GitHub job outcomes. */ |
| 13 | +function eligible(job: string, branch: string, results: Record<string, string> = {}) { |
| 14 | + const body = ci.split(`\n ${job}:\n`)[1]?.split(/^ {2}[\w-]+:/m)[0] |
| 15 | + const expression = body?.match(/ {4}if: >-\n((?: {6}.*\n)+)/)?.[1] |
| 16 | + if (!expression) throw new Error(`Missing job condition: ${job}`) |
| 17 | + const needs = Object.fromEntries( |
| 18 | + Object.entries({ |
| 19 | + migrate: 'success', |
| 20 | + 'build-amd64': 'success', |
| 21 | + 'migrate-dev': 'success', |
| 22 | + 'build-dev': 'success', |
| 23 | + 'prepare-trigger': 'success', |
| 24 | + 'promote-images': 'success', |
| 25 | + ...results, |
| 26 | + }).map(([name, result]) => [name, { result, outputs: { promoted: 'true' } }]) |
| 27 | + ) |
| 28 | + return runInNewContext(expression.replace(/needs\.([\w-]+)/g, 'needs["$1"]'), { |
| 29 | + github: { event_name: 'push', ref: `refs/heads/${branch}` }, |
| 30 | + needs, |
| 31 | + cancelled: () => false, |
| 32 | + }) |
| 33 | +} |
| 34 | + |
| 35 | +const promotion = tasks.split(' - name: Promote current dev preview')[1] |
| 36 | +const shell = promotion?.match(/ {8}run: \|\n((?: {10}.*\n)+)/)?.[1] |
| 37 | +if (!shell) throw new Error('Missing dev task promotion step') |
| 38 | +const script = shell.replace(/^ {10}/gm, '') |
| 39 | + |
| 40 | +/** Run the real promotion shell with fake GitHub and Trigger commands. */ |
| 41 | +function promote(env: Record<string, string> = {}) { |
| 42 | + return spawnSync( |
| 43 | + 'bash', |
| 44 | + [ |
| 45 | + '-e', |
| 46 | + '-c', |
| 47 | + `gh() { printf '%s\\n' "$HEAD_SHA"; return "$GH_EXIT"; } |
| 48 | +bunx() { printf 'PROMOTE %s\\n' "$*"; return "$TRIGGER_EXIT"; } |
| 49 | +${script}`, |
| 50 | + ], |
| 51 | + { |
| 52 | + encoding: 'utf8', |
| 53 | + env: { |
| 54 | + PATH: process.env.PATH, |
| 55 | + GITHUB_REPOSITORY: 'example/sim', |
| 56 | + GITHUB_SHA: 'current', |
| 57 | + HEAD_SHA: 'current', |
| 58 | + VERSION: '20260922.1', |
| 59 | + GH_EXIT: '0', |
| 60 | + TRIGGER_EXIT: '0', |
| 61 | + ...env, |
| 62 | + }, |
| 63 | + } |
| 64 | + ) |
| 65 | +} |
| 66 | + |
| 67 | +describe('dev deployment independence', () => { |
| 68 | + it('promotes dev images with the skipped Trigger jobs and no cutover waiter', () => { |
| 69 | + expect(eligible('prepare-trigger', 'dev')).toBe(false) |
| 70 | + expect(eligible('promote-trigger', 'dev')).toBe(false) |
| 71 | + expect(eligible('promote-images', 'dev', { 'prepare-trigger': 'skipped' })).toBe(true) |
| 72 | + }) |
| 73 | + |
| 74 | + it.each(['migrate-dev', 'build-dev'])('still requires successful %s', (job) => { |
| 75 | + expect(eligible('promote-images', 'dev', { [job]: 'failure' })).toBe(false) |
| 76 | + }) |
| 77 | + |
| 78 | + it.each(['staging', 'main'])('preserves the coordinated release gates for %s', (branch) => { |
| 79 | + expect(eligible('prepare-trigger', branch)).toBe(true) |
| 80 | + expect(eligible('promote-trigger', branch)).toBe(true) |
| 81 | + expect(eligible('promote-images', branch)).toBe(true) |
| 82 | + for (const job of ['prepare-trigger', 'migrate', 'build-amd64']) { |
| 83 | + expect(eligible('promote-images', branch, { [job]: 'failure' })).toBe(false) |
| 84 | + } |
| 85 | + expect(eligible('promote-trigger', branch, { 'promote-images': 'failure' })).toBe(false) |
| 86 | + }) |
| 87 | + |
| 88 | + it('promotes the current task version only to the dev preview', () => { |
| 89 | + const result = promote() |
| 90 | + expect(result.status).toBe(0) |
| 91 | + expect(result.stdout).toContain( |
| 92 | + 'PROMOTE trigger.dev@4.5.12 promote 20260922.1 --env preview --branch dev-sim' |
| 93 | + ) |
| 94 | + }) |
| 95 | + |
| 96 | + it('does not roll tasks back when an older push is retried', () => { |
| 97 | + const result = promote({ HEAD_SHA: 'newer' }) |
| 98 | + expect(result.status).toBe(0) |
| 99 | + expect(result.stdout).not.toContain('PROMOTE ') |
| 100 | + }) |
| 101 | + |
| 102 | + it('fails closed if the current branch cannot be read', () => { |
| 103 | + const result = promote({ GH_EXIT: '42' }) |
| 104 | + expect(result.status).toBe(42) |
| 105 | + expect(result.stdout).not.toContain('PROMOTE ') |
| 106 | + }) |
| 107 | + |
| 108 | + it('rejects an invalid task version', () => { |
| 109 | + const result = promote({ VERSION: '' }) |
| 110 | + expect(result.status).toBe(1) |
| 111 | + expect(result.stdout).not.toContain('PROMOTE ') |
| 112 | + }) |
| 113 | + |
| 114 | + it('reports task promotion failures in the independent workflow', () => { |
| 115 | + expect(promote({ TRIGGER_EXIT: '43' }).status).toBe(43) |
| 116 | + }) |
| 117 | +}) |
0 commit comments