Files
orca/config/scripts/ci/workflow-contracts/hourly-preflight-workflow.test.mjs

78 lines
2.4 KiB
JavaScript

import { mkdtempSync, readFileSync, rmSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { describe, expect, it } from 'vitest'
import { parse } from 'yaml'
import { runProcess } from '@orca/process-host'
const workflow = parse(
readFileSync(
new URL('../../../../.github/workflows/hourly-mac-build.yml', import.meta.url),
'utf8'
)
)
const preflight = workflow.jobs.preflight
const freshness = preflight.steps.find((step) => step.id === 'freshness')
const head = 'abcdef0123'.repeat(4)
async function checkFreshness(overrides = {}) {
const directory = mkdtempSync(join(tmpdir(), 'hourly-preflight-'))
const output = join(directory, 'output')
try {
const result = await runProcess({
program: 'bash',
args: [
'-c',
`gh() {
case "$1 $2" in
"api "*) printf '%s\\n' "$HEAD_SHA" ;;
"release list") printf '%s\\n' "$LAST_TAG" ;;
"release view") printf '%s\\n' "$LAST_SHA" ;;
*) return 1 ;;
esac
}
${freshness.run}`
],
env: {
...process.env,
GITHUB_OUTPUT: output,
GITHUB_REPOSITORY: 'stablyai/orca',
MAIN_REPO_TOKEN: 'main-token',
HOURLY_REPO: 'stablyai/orca-hourly',
HEAD_SHA: head,
LAST_TAG: 'previous-hourly',
LAST_SHA: head.slice(0, 12),
FORCED: 'false',
...overrides
}
})
return {
exitCode: result.code,
stderr: result.stderr,
stdout: result.stdout,
output: result.code === 0 ? readFileSync(output, 'utf8') : ''
}
} finally {
rmSync(directory, { recursive: true, force: true })
}
}
describe('hourly build preflight', () => {
it.each([
['unchanged', {}, false],
['changed', { LAST_SHA: '123456789012' }, true],
['forced', { FORCED: 'true' }, true],
['first build', { LAST_TAG: '' }, true],
['missing prior identity', { LAST_SHA: '' }, true]
])('%s main selects the expected build decision', async (_name, env, shouldBuild) => {
const result = await checkFreshness(env)
expect(result.exitCode, `${result.stdout} ${result.stderr}`).toBe(0)
expect(result.output).toBe(`head_sha=${head}\nshould_build=${shouldBuild}\n`)
})
it('fails closed when main cannot be resolved, even when forced', async () => {
const result = await checkFreshness({ HEAD_SHA: '', FORCED: 'true' })
expect(result.exitCode).not.toBe(0)
})
})