|
| 1 | +// @ts-check |
| 2 | +// Lifecycle tests for scripts/sdk-reference-pr.sh: the real script runs against a fake `gh` |
| 3 | +// (a PR-state adapter persisted in a JSON file) and a fake `git`, so the assertions are on the |
| 4 | +// PR state GitHub would hold after each event, not on the resolver's decision string. |
| 5 | +import assert from 'node:assert/strict'; |
| 6 | +import { spawnSync } from 'node:child_process'; |
| 7 | +import fs from 'node:fs'; |
| 8 | +import os from 'node:os'; |
| 9 | +import path from 'node:path'; |
| 10 | +import { test } from 'node:test'; |
| 11 | +import { fileURLToPath } from 'node:url'; |
| 12 | + |
| 13 | +const SCRIPT = path.join(path.dirname(fileURLToPath(import.meta.url)), 'sdk-reference-pr.sh'); |
| 14 | + |
| 15 | +/** The fake `gh`: every PR mutation the script may issue, on a JSON state file. */ |
| 16 | +const FAKE_GH = `#!/usr/bin/env node |
| 17 | +const fs = require('node:fs'); |
| 18 | +const file = process.env.FAKE_PR_STATE; |
| 19 | +const state = JSON.parse(fs.readFileSync(file, 'utf-8')); |
| 20 | +const save = () => fs.writeFileSync(file, JSON.stringify(state, null, 1)); |
| 21 | +const args = process.argv.slice(2); |
| 22 | +const log = (line) => fs.appendFileSync(process.env.FAKE_LOG, line + '\\n'); |
| 23 | +log('gh ' + args.join(' ')); |
| 24 | +const [cmd, sub] = args; |
| 25 | +const flag = (name) => { const i = args.indexOf(name); return i === -1 ? undefined : args[i + 1]; }; |
| 26 | +const open = () => state.prs.filter((p) => p.state === 'open'); |
| 27 | +if (cmd === 'pr' && sub === 'list') { |
| 28 | + const head = flag('--head'); |
| 29 | + const hits = open().filter((p) => p.head === head); |
| 30 | + const jq = flag('--jq'); |
| 31 | + const first = hits[0]; |
| 32 | + if (jq && jq.includes('select(. != null)')) { if (first) process.stdout.write(first.number + ' ' + first.isDraft + '\\n'); process.exit(0); } |
| 33 | + process.stdout.write(JSON.stringify(hits.map((p) => ({ number: p.number, isDraft: p.isDraft })))); |
| 34 | + process.exit(0); |
| 35 | +} |
| 36 | +if (cmd === 'pr' && sub === 'create') { |
| 37 | + const number = state.next++; |
| 38 | + state.prs.push({ number, head: flag('--head'), state: 'open', isDraft: args.includes('--draft'), title: flag('--title') }); |
| 39 | + save(); process.stdout.write('https://github.com/future-agi/docs/pull/' + number + '\\n'); process.exit(0); |
| 40 | +} |
| 41 | +const pr = state.prs.find((p) => String(p.number) === args[2]); |
| 42 | +if (cmd === 'pr' && sub === 'view') { process.stdout.write(String(pr.isDraft) + '\\n'); process.exit(0); } |
| 43 | +if (cmd === 'pr' && sub === 'edit') { pr.title = flag('--title'); pr.edits = (pr.edits || 0) + 1; save(); process.exit(0); } |
| 44 | +if (cmd === 'pr' && sub === 'ready') { |
| 45 | + if (process.env.FAKE_READY_FAILS === '1') { process.stderr.write('GraphQL: forbidden\\n'); process.exit(1); } |
| 46 | + pr.isDraft = false; save(); process.exit(0); |
| 47 | +} |
| 48 | +if (cmd === 'pr' && sub === 'close') { pr.state = 'closed'; save(); process.exit(0); } |
| 49 | +process.stderr.write('fake gh: unexpected ' + args.join(' ') + '\\n'); process.exit(64); |
| 50 | +`; |
| 51 | + |
| 52 | +/** The fake `git`: `diff --quiet` answers from FAKE_DIFF, pushes are recorded, the rest is a no-op. */ |
| 53 | +const FAKE_GIT = `#!/usr/bin/env node |
| 54 | +const fs = require('node:fs'); |
| 55 | +const args = process.argv.slice(2); |
| 56 | +fs.appendFileSync(process.env.FAKE_LOG, 'git ' + args.join(' ') + '\\n'); |
| 57 | +if (args[0] === 'diff' && args.includes('--quiet')) process.exit(process.env.FAKE_DIFF === '0' ? 0 : 1); |
| 58 | +process.exit(0); |
| 59 | +`; |
| 60 | + |
| 61 | +function harness() { |
| 62 | + const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'sdk-ref-pr-')); |
| 63 | + const bin = path.join(dir, 'bin'); |
| 64 | + fs.mkdirSync(bin); |
| 65 | + fs.writeFileSync(path.join(bin, 'gh'), FAKE_GH, { mode: 0o755 }); |
| 66 | + fs.writeFileSync(path.join(bin, 'git'), FAKE_GIT, { mode: 0o755 }); |
| 67 | + const stateFile = path.join(dir, 'prs.json'); |
| 68 | + const logFile = path.join(dir, 'calls.log'); |
| 69 | + fs.writeFileSync(stateFile, JSON.stringify({ next: 100, prs: [] })); |
| 70 | + fs.writeFileSync(logFile, ''); |
| 71 | + const base = { SHA: 'a'.repeat(40), REF: 'python-v0.7.0', PY: '0.7.0', TS: '0.2.0', BACKEND: 'v1.58.0', SOURCE: 'test' }; |
| 72 | + /** @param {{STAGE: string, PUBLISH: string, BRANCH?: string, SHA?: string, diff?: boolean, readyFails?: boolean}} ev */ |
| 73 | + const run = (ev) => { |
| 74 | + const env = { |
| 75 | + ...process.env, ...base, SHA: ev.SHA ?? base.SHA, |
| 76 | + BRANCH: ev.BRANCH ?? 'sdk-reference/python-v0.7.0-typescript-v0.2.0', |
| 77 | + STAGE: ev.STAGE, PUBLISH: ev.PUBLISH, |
| 78 | + PATH: `${bin}:${process.env.PATH}`, FAKE_PR_STATE: stateFile, FAKE_LOG: logFile, |
| 79 | + FAKE_DIFF: ev.diff === false ? '0' : '1', FAKE_READY_FAILS: ev.readyFails ? '1' : '0', GH_TOKEN: 'fake', |
| 80 | + }; |
| 81 | + const result = spawnSync('bash', [SCRIPT], { env, encoding: 'utf-8' }); |
| 82 | + return { status: result.status, out: result.stdout + result.stderr }; |
| 83 | + }; |
| 84 | + const prs = () => JSON.parse(fs.readFileSync(stateFile, 'utf-8')).prs; |
| 85 | + const calls = () => fs.readFileSync(logFile, 'utf-8').trim().split('\n').filter(Boolean); |
| 86 | + const resetLog = () => fs.writeFileSync(logFile, ''); |
| 87 | + return { run, prs, calls, resetLog }; |
| 88 | +} |
| 89 | + |
| 90 | +test('candidate opens a draft; released promotes the same PR and verifies it; repeats are idempotent', () => { |
| 91 | + const h = harness(); |
| 92 | + let r = h.run({ STAGE: 'candidate', PUBLISH: 'draft' }); |
| 93 | + assert.equal(r.status, 0, r.out); |
| 94 | + assert.deepEqual(h.prs().map((p) => [p.number, p.isDraft, p.state]), [[100, true, 'open']]); |
| 95 | + assert.match(r.out, /#100 \(candidate, draft\)/); |
| 96 | + |
| 97 | + r = h.run({ STAGE: 'released', PUBLISH: 'ready' }); |
| 98 | + assert.equal(r.status, 0, r.out); |
| 99 | + assert.deepEqual(h.prs().map((p) => [p.number, p.isDraft, p.state]), [[100, false, 'open']], 'same PR, now ready'); |
| 100 | + assert.match(r.out, /#100 \(released, ready\)/); |
| 101 | + assert.ok(h.calls().some((c) => c === 'gh pr ready 100'), 'the promotion was issued'); |
| 102 | + |
| 103 | + h.resetLog(); |
| 104 | + r = h.run({ STAGE: 'released', PUBLISH: 'ready' }); |
| 105 | + assert.equal(r.status, 0, r.out); |
| 106 | + assert.equal(h.prs().length, 1, 'a repeated released event opens no second PR'); |
| 107 | + assert.ok(!h.calls().some((c) => c.startsWith('gh pr create')), 'no create on repeat'); |
| 108 | + assert.ok(!h.calls().some((c) => c === 'gh pr ready 100'), 'no redundant promotion of a ready PR'); |
| 109 | +}); |
| 110 | + |
| 111 | +test('a stale candidate after released is ignored: the ready PR keeps its state, branch and body', () => { |
| 112 | + const h = harness(); |
| 113 | + assert.equal(h.run({ STAGE: 'candidate', PUBLISH: 'draft' }).status, 0); |
| 114 | + assert.equal(h.run({ STAGE: 'released', PUBLISH: 'ready' }).status, 0); |
| 115 | + h.resetLog(); |
| 116 | + const r = h.run({ STAGE: 'candidate', PUBLISH: 'draft', SHA: 'b'.repeat(40) }); |
| 117 | + assert.equal(r.status, 0, r.out); |
| 118 | + assert.deepEqual(h.prs().map((p) => [p.number, p.isDraft]), [[100, false]], 'still ready'); |
| 119 | + assert.match(r.out, /stale candidate event ignored/); |
| 120 | + assert.match(r.out, /::notice::/); |
| 121 | + const calls = h.calls(); |
| 122 | + assert.ok(!calls.some((c) => c.startsWith('git push')), 'the released branch was not rewritten'); |
| 123 | + assert.ok(!calls.some((c) => c.startsWith('gh pr edit')), 'the released title/body were not rewritten'); |
| 124 | + assert.ok(!calls.some((c) => c.startsWith('gh pr ready') || c.startsWith('gh pr create'))); |
| 125 | +}); |
| 126 | + |
| 127 | +test('a failed promotion fails the run instead of reporting ready', () => { |
| 128 | + const h = harness(); |
| 129 | + assert.equal(h.run({ STAGE: 'candidate', PUBLISH: 'draft' }).status, 0); |
| 130 | + const r = h.run({ STAGE: 'released', PUBLISH: 'ready', readyFails: true }); |
| 131 | + assert.notEqual(r.status, 0, 'must not exit 0'); |
| 132 | + assert.match(r.out, /could not mark future-agi\/docs#100 ready/); |
| 133 | + assert.deepEqual(h.prs().map((p) => p.isDraft), [true], 'the PR is honestly still a draft'); |
| 134 | +}); |
| 135 | + |
| 136 | +test('no diff against main: nothing is opened, and an open PR for the release is closed', () => { |
| 137 | + const h = harness(); |
| 138 | + let r = h.run({ STAGE: 'released', PUBLISH: 'ready', diff: false }); |
| 139 | + assert.equal(r.status, 0, r.out); |
| 140 | + assert.equal(h.prs().length, 0); |
| 141 | + assert.match(r.out, /nothing to open/); |
| 142 | + assert.equal(h.run({ STAGE: 'candidate', PUBLISH: 'draft' }).status, 0); |
| 143 | + r = h.run({ STAGE: 'released', PUBLISH: 'ready', diff: false }); |
| 144 | + assert.equal(r.status, 0, r.out); |
| 145 | + assert.deepEqual(h.prs().map((p) => p.state), ['closed']); |
| 146 | +}); |
| 147 | + |
| 148 | +test('a preview is its own draft PR on its own branch and never touches a release PR', () => { |
| 149 | + const h = harness(); |
| 150 | + assert.equal(h.run({ STAGE: 'released', PUBLISH: 'ready' }).status, 0); |
| 151 | + const r = h.run({ STAGE: 'preview', PUBLISH: 'draft', BRANCH: 'sdk-reference/preview-bbbbbbbbbbbb', SHA: 'b'.repeat(40) }); |
| 152 | + assert.equal(r.status, 0, r.out); |
| 153 | + assert.deepEqual(h.prs().map((p) => [p.head, p.isDraft]), [ |
| 154 | + ['sdk-reference/python-v0.7.0-typescript-v0.2.0', false], |
| 155 | + ['sdk-reference/preview-bbbbbbbbbbbb', true], |
| 156 | + ]); |
| 157 | + assert.match(r.out, /#101 \(preview, draft\)/); |
| 158 | +}); |
| 159 | + |
| 160 | +test('a missing decision variable is a usage error, not a half-run', () => { |
| 161 | + const h = harness(); |
| 162 | + const r = h.run({ STAGE: 'released', PUBLISH: '' }); |
| 163 | + assert.equal(r.status, 2); |
| 164 | + assert.match(r.out, /PUBLISH is not set/); |
| 165 | + assert.equal(h.prs().length, 0); |
| 166 | +}); |
0 commit comments