|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { execFile } from 'node:child_process'; |
| 3 | +import { mkdtemp, readFile, writeFile } from 'node:fs/promises'; |
| 4 | +import { tmpdir } from 'node:os'; |
| 5 | +import { join } from 'node:path'; |
| 6 | +import { promisify } from 'node:util'; |
| 7 | +import { test } from 'vitest'; |
| 8 | + |
| 9 | +const execFileAsync = promisify(execFile); |
| 10 | +const ROOT = join(import.meta.dirname, '..', '..'); |
| 11 | +const SCRIPT = join(ROOT, 'scripts', 'size-report.mjs'); |
| 12 | +const MARKER = '<!-- agent-device-size-report -->'; |
| 13 | + |
| 14 | +// Each entry answers one fetch call, in order. `net` rejects the fetch (a |
| 15 | +// dropped connection); a number is the HTTP status; `body` is the response |
| 16 | +// text (defaults to `[]` for 200 so a list call sees no comments). |
| 17 | +type StubResponse = { status: number | 'net'; body?: string }; |
| 18 | + |
| 19 | +const FETCH_STUB = ` |
| 20 | +import fs from 'node:fs'; |
| 21 | +const script = JSON.parse(process.env.SIZE_REPORT_FETCH_SCRIPT); |
| 22 | +let index = 0; |
| 23 | +globalThis.fetch = async (url, init) => { |
| 24 | + const step = script[Math.min(index, script.length - 1)]; |
| 25 | + index += 1; |
| 26 | + fs.appendFileSync(process.env.SIZE_REPORT_FETCH_LOG, \`\${init?.method ?? 'GET'} \${url}\\n\`); |
| 27 | + if (step.status === 'net') throw new Error('ECONNRESET'); |
| 28 | + const body = step.body ?? (step.status === 200 ? '[]' : \`{"message":"status \${step.status}"}\`); |
| 29 | + return new Response(body, { status: step.status }); |
| 30 | +}; |
| 31 | +`; |
| 32 | + |
| 33 | +async function runPostComment(script: StubResponse[]) { |
| 34 | + const dir = await mkdtemp(join(tmpdir(), 'size-report-post-comment-')); |
| 35 | + const stubPath = join(dir, 'fetch-stub.mjs'); |
| 36 | + const reportPath = join(dir, 'report.md'); |
| 37 | + const logPath = join(dir, 'fetch.log'); |
| 38 | + const summaryPath = join(dir, 'summary.md'); |
| 39 | + await Promise.all([ |
| 40 | + writeFile(stubPath, FETCH_STUB), |
| 41 | + writeFile(reportPath, `${MARKER}\n# report\n`), |
| 42 | + writeFile(logPath, ''), |
| 43 | + writeFile(summaryPath, ''), |
| 44 | + ]); |
| 45 | + const env = { |
| 46 | + ...process.env, |
| 47 | + GITHUB_TOKEN: 'token', |
| 48 | + GITHUB_REPOSITORY: 'owner/repo', |
| 49 | + GITHUB_PR_NUMBER: '1', |
| 50 | + GITHUB_STEP_SUMMARY: summaryPath, |
| 51 | + SIZE_REPORT_RETRY_BASE_MS: '1', |
| 52 | + SIZE_REPORT_FETCH_SCRIPT: JSON.stringify(script), |
| 53 | + SIZE_REPORT_FETCH_LOG: logPath, |
| 54 | + }; |
| 55 | + let exitCode = 0; |
| 56 | + let stdout = ''; |
| 57 | + let stderr = ''; |
| 58 | + try { |
| 59 | + ({ stdout, stderr } = await execFileAsync( |
| 60 | + process.execPath, |
| 61 | + ['--import', stubPath, SCRIPT, '--post-comment', reportPath], |
| 62 | + { env }, |
| 63 | + )); |
| 64 | + } catch (error) { |
| 65 | + const failure = error as { code?: number; stdout?: string; stderr?: string }; |
| 66 | + exitCode = failure.code ?? 1; |
| 67 | + stdout = failure.stdout ?? ''; |
| 68 | + stderr = failure.stderr ?? ''; |
| 69 | + } |
| 70 | + const calls = (await readFile(logPath, 'utf8')).trim().split('\n').filter(Boolean); |
| 71 | + const summary = await readFile(summaryPath, 'utf8'); |
| 72 | + return { exitCode, stdout, stderr, calls, summary }; |
| 73 | +} |
| 74 | + |
| 75 | +const COMMENTS_URL = 'https://api.github.com/repos/owner/repo/issues/1/comments'; |
| 76 | +const EXISTING_COMMENT_URL = `${COMMENTS_URL}/42`; |
| 77 | +const existingMarkerComment = JSON.stringify([ |
| 78 | + { url: EXISTING_COMMENT_URL, body: `${MARKER}\nold` }, |
| 79 | +]); |
| 80 | + |
| 81 | +test('a create whose response was lost is reconciled into an update, not a duplicate', async () => { |
| 82 | + const result = await runPostComment([ |
| 83 | + { status: 200 }, // list: nothing yet |
| 84 | + { status: 'net' }, // create: connection dropped, but it landed server-side |
| 85 | + { status: 200, body: existingMarkerComment }, // re-list: marker comment now exists |
| 86 | + { status: 200 }, // update |
| 87 | + ]); |
| 88 | + assert.equal(result.exitCode, 0, result.stderr); |
| 89 | + assert.deepEqual(result.calls, [ |
| 90 | + `GET ${COMMENTS_URL}?per_page=100`, |
| 91 | + `POST ${COMMENTS_URL}`, |
| 92 | + `GET ${COMMENTS_URL}?per_page=100`, |
| 93 | + `PATCH ${EXISTING_COMMENT_URL}`, |
| 94 | + ]); |
| 95 | +}); |
| 96 | + |
| 97 | +test('transient failures that outlast the retries warn and exit 0', async () => { |
| 98 | + const result = await runPostComment([{ status: 503 }]); |
| 99 | + assert.equal(result.exitCode, 0, result.stderr); |
| 100 | + assert.match( |
| 101 | + result.stdout, |
| 102 | + /^::warning::Skipping PR size comment after transient GitHub failure: .*503/m, |
| 103 | + ); |
| 104 | + assert.match(result.summary, /Skipping PR size comment/); |
| 105 | + assert.equal(result.calls.length, 4, 'one list call per attempt'); |
| 106 | +}); |
| 107 | + |
| 108 | +test('a non-transient 4xx is fatal and is not retried', async () => { |
| 109 | + const result = await runPostComment([{ status: 200 }, { status: 401 }]); |
| 110 | + assert.notEqual(result.exitCode, 0); |
| 111 | + assert.match(result.stderr, /Failed to create PR comment: 401/); |
| 112 | + assert.doesNotMatch(result.stdout, /::warning::/); |
| 113 | + assert.equal(result.calls.length, 2, 'no retry after a fatal status'); |
| 114 | +}); |
0 commit comments