From 349f598936021a90fb5190ea71fe458847ab8926 Mon Sep 17 00:00:00 2001 From: Day Dreamer <87640074+HugoXK@users.noreply.github.com> Date: Fri, 20 Nov 2026 14:14:29 +0800 Subject: [PATCH] test: cover the T1 trigger's write path and print each round (#3253) The decision function had tests; the code that presses the queue did not, so nothing caught a write-path regression. The CLI is now driven end to end with a stand-in `gh` on PATH that serves the reads and records every argv: a process is spawned, but nothing reaches the network, needs a token, or writes into the checkout. The five cases are execute mode's merge plus one dispatch with the argv pinned to the whole tip, a head that moved after the decision pressing nothing, `already queued to merge` counted as success without a retry or a second dispatch, a failed dispatch reported while the merge still counts, and report mode pressing nothing at all. The round's summary also goes to stdout now, byte for byte what it writes to the step summary, because the API exposes a run's log but not its step summary: the first real round after #3252 merged left its decision on the run page and nowhere quotable. The dead assertion that compared an expression with itself is replaced by a comparison against the expected commands, and `SELF_PATHS` records why the test file is not in it. Docs-not-needed: `.github/**` is not an owned path of any subsystem page. --- .github/scripts/auto-queue-t1.mjs | 10 +- .github/scripts/auto-queue-t1.test.mjs | 173 ++++++++++++++++++++++++- 2 files changed, 180 insertions(+), 3 deletions(-) diff --git a/.github/scripts/auto-queue-t1.mjs b/.github/scripts/auto-queue-t1.mjs index 75590315e..610f4444a 100644 --- a/.github/scripts/auto-queue-t1.mjs +++ b/.github/scripts/auto-queue-t1.mjs @@ -20,7 +20,10 @@ import { fileURLToPath } from 'node:url' import { createGate, loadRules } from './risk-gate.mjs' // A change to either of these is never queued by this trigger: the code that decides must not be the -// code that decides whether it may decide (#3248, owner constraint 3). +// code that decides whether it may decide (#3248, owner constraint 3). The test file is deliberately +// absent: it is not this workflow's own code path, and `.github/**` scores T3 today, so a change to it +// still needs a PASS. That protection is the tier rule, not this list (#3253) — if the tier loosens, +// nothing else here stops a pull request from editing the test that this list's behaviour rests on. export const SELF_PATHS = Object.freeze([ '.github/scripts/auto-queue-t1.mjs', '.github/workflows/auto-queue-t1.yml', @@ -317,7 +320,10 @@ function main() { const out = arg('--summary') if (out) appendFileSync(out, summary) if (process.env.GITHUB_STEP_SUMMARY) appendFileSync(process.env.GITHUB_STEP_SUMMARY, summary) - if (!out && !process.env.GITHUB_STEP_SUMMARY) process.stdout.write(summary) + // The decision also goes to the log, always: the REST API hands back a run's log text but not its step + // summary, so without this line a round that a person did not open the run page for leaves nothing + // quotable behind (#3253). Same bytes as the file, so a reader can compare the two. + process.stdout.write(summary) } if (process.argv[1] === fileURLToPath(import.meta.url)) { diff --git a/.github/scripts/auto-queue-t1.test.mjs b/.github/scripts/auto-queue-t1.test.mjs index e13fe72ba..7180e5ece 100644 --- a/.github/scripts/auto-queue-t1.test.mjs +++ b/.github/scripts/auto-queue-t1.test.mjs @@ -5,6 +5,11 @@ // the button in that shape. import { test } from 'node:test' import assert from 'node:assert/strict' +import { spawnSync } from 'node:child_process' +import { mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import { fileURLToPath } from 'node:url' import { SELF_PATHS, decidePull, guardMode, renderSummary, resolveMode } from './auto-queue-t1.mjs' const REPO = 'ThinkFlowLab/FlyBot' @@ -247,8 +252,11 @@ test('#3248: no decision can carry an admin or force flag', () => { decide({ queued: true }), decide({ files: files('core/client.js'), tier: 'T1' }), ] + // The whole command, in order: the two enqueues carry the runbook line and the two refusals carry + // nothing. Comparing the string itself is what makes the `doesNotMatch` checks below mean something — + // a command that arrived reformatted, or a refusal that arrived with a command, fails here (#3253). + assert.deepEqual(all.map((r) => r.command), [COMMAND, undefined, undefined, COMMAND]) for (const r of all) { - assert.equal(r.command === undefined ? 'none' : r.command.replace(/\s+/g, ' '), r.command === undefined ? 'none' : r.command) if (r.command !== undefined) { assert.doesNotMatch(r.command, /admin/, 'no admin flag, ever') assert.doesNotMatch(r.command, /force|--no-verify/, 'no force flag, ever') @@ -295,3 +303,166 @@ test('#3248: a snapshot can be reported on but never acted from', () => { assert.equal(guardMode({ mode: 'execute', snapshot: false }), 'execute') assert.throws(() => guardMode({ mode: 'execute', snapshot: true }), /report-only/) }) +// The write path (#3253) +// +// `act()` is the only code here that presses anything, and until this section nothing exercised it. The +// CLI is driven end to end with a stand-in `gh` first on PATH: it answers the reads from the environment +// and records every argv, so these tests spawn a process but never reach GitHub, never need a token and +// never write into the checkout. Execute mode is the live path — a snapshot is refused there by design — +// so the pull request, its files and its checks are fixtures served by the same stand-in. +const script = fileURLToPath(new URL('./auto-queue-t1.mjs', import.meta.url)) + +const FAKE_GH = `#!/bin/sh +printf '%s\\n' "$*" >> "$GH_LOG" +case "$1" in + api) + case "$*" in + *graphql*) printf '%s\\n' "$GH_QUEUE"; exit 0 ;; + *"/pulls?state=open"*) printf '%s\\n' "$GH_PULLS"; exit 0 ;; + *"/files"*) printf '%s\\n' "$GH_FILES"; exit 0 ;; + *"/check-runs"*) printf '%s\\n' "$GH_CHECKS"; exit 0 ;; + *"/pulls/"*) printf '%s\\n' "$GH_HEAD"; exit 0 ;; + esac + exit 0 ;; + pr) [ -n "$GH_MERGE_ERR" ] && { printf '%s\\n' "$GH_MERGE_ERR" >&2; exit 1; }; exit 0 ;; + workflow) [ -n "$GH_DISPATCH_ERR" ] && { printf '%s\\n' "$GH_DISPATCH_ERR" >&2; exit 1; }; exit 0 ;; +esac +exit 0 +` + +const pullJson = () => JSON.stringify({ + number: 4001, + draft: false, + labels: [{ name: 'agent-pr' }], + base: { ref: 'main' }, + head: { sha: HEAD, repo: { full_name: REPO } }, + user: { login: 'HugoXK' }, +}) + +const CHECKS = [ + { name: 'tests-ok', conclusion: 'success', head_sha: HEAD }, + { name: 'merge-gate', conclusion: 'success', head_sha: HEAD }, + { name: 'enqueue-guard', conclusion: 'success', head_sha: HEAD }, +] + +// One call to the real CLI with a stand-in `gh` on PATH. `writes()` is the only part of the argv record +// a decision may act on; the reads above it are served from the environment. +function roundWith({ head = HEAD, mergeErr = '', dispatchErr = '' } = {}, { mode = 'execute' } = {}) { + const dir = mkdtempSync(join(tmpdir(), 'auto-queue-t1-gh-')) + const log = join(dir, 'calls.log') + const summary = join(dir, 'summary.md') + writeFileSync(join(dir, 'gh'), FAKE_GH, { mode: 0o755 }) + writeFileSync(log, '') + const env = { + ...process.env, + PATH: `${dir}:${process.env.PATH ?? ''}`, + GH_LOG: log, + GH_HEAD: head, + GH_PULLS: pullJson(), + GH_FILES: JSON.stringify({ filename: 'changelog.d/3248.added.md', additions: 3, deletions: 0 }), + GH_CHECKS: JSON.stringify(CHECKS[0]) + '\n' + JSON.stringify(CHECKS[1]) + '\n' + JSON.stringify(CHECKS[2]), + GH_QUEUE: '{"data":{"repository":{"pullRequest":{"mergeQueueEntry":null}}}}', + GITHUB_STEP_SUMMARY: '', + } + if (mergeErr) env.GH_MERGE_ERR = mergeErr + if (dispatchErr) env.GH_DISPATCH_ERR = dispatchErr + const run = spawnSync(process.execPath, [script, '--mode', mode, '--repo', REPO, '--summary', summary], { encoding: 'utf8', env }) + const calls = () => readFileSync(log, 'utf8').split('\n').filter(Boolean) + const writes = () => calls().filter((line) => line.startsWith('pr merge') || line.startsWith('workflow run')) + return { run, dir, summary, calls, writes, text: run.stdout } +} + +test('#3253: execute mode writes one merge and one dispatch, and the argv is verbatim', () => { + const { run, dir, calls, writes, text } = roundWith() + try { + assert.equal(run.status, 0, run.stderr) + assert.deepEqual(writes(), [ + `pr merge 4001 --auto --squash --match-head-commit ${HEAD}`, + 'workflow run test.yml --ref main', + ], 'exactly two writes: the merge, then the dispatch') + assert.match(writes()[0], /--match-head-commit [0-9a-f]{40}$/, 'the tip is pinned, whole') + assert.doesNotMatch(writes()[0], /--admin|--force|--no-verify/, 'no admin flag and no force flag, ever') + assert.ok(calls().includes(`api repos/${REPO}/pulls/4001 --jq .head.sha`), 'the head is re-read before the merge') + assert.match(text, /- #4001 \(T1, 4a1c2d3e\): enqueued/) + assert.match(text, /auto-queue-t1: mode=execute enqueued=1 refused=0/) + } finally { + rmSync(dir, { recursive: true, force: true }) + } +}) + +test('#3253: a head that moved after the decision writes nothing and is dropped, not chased', () => { + const later = 'ffffffffffffffffffffffffffffffffffffffff' + const { run, dir, writes, text } = roundWith({ head: later }) + try { + assert.equal(run.status, 0, run.stderr) + assert.deepEqual(writes(), [], 'the head is re-read, the merge is not sent') + assert.match(text, /: moved — the head moved to ffffffff after the decision; the next round decides again/) + assert.match(text, /auto-queue-t1: mode=execute enqueued=0 refused=0 moved=1/) + } finally { + rmSync(dir, { recursive: true, force: true }) + } +}) + +test('#3253: "already queued to merge" is success, and the round neither retries nor dispatches twice', () => { + const { run, dir, writes, text } = roundWith({ mergeErr: 'GraphQL: This pull request is already queued to merge.' }) + try { + assert.equal(run.status, 0, run.stderr) + assert.equal(writes().filter((line) => line.startsWith('pr merge')).length, 1, 'one attempt, no retry') + assert.equal(writes().filter((line) => line.startsWith('workflow run')).length, 1, 'one dispatch for one enqueue') + assert.match(text, /: enqueued/) + assert.match(text, /auto-queue-t1: mode=execute enqueued=1/) + } finally { + rmSync(dir, { recursive: true, force: true }) + } +}) + +test('#3253: a failed dispatch is reported in the summary, and the merge still counts', () => { + const { run, dir, writes, text } = roundWith({ dispatchErr: 'HTTP 403: Resource not accessible by integration' }) + try { + assert.equal(run.status, 0, run.stderr) + assert.match(text, /: enqueued/, 'the merge landed') + assert.match(text, /Could not dispatch `Test` on main \(HTTP 403: Resource not accessible by integration\)/) + assert.equal(writes().filter((line) => line.startsWith('workflow run')).length, 1) + } finally { + rmSync(dir, { recursive: true, force: true }) + } +}) + +test('#3253: report mode presses nothing, not even the head re-read or a dispatch', () => { + const { run, dir, writes, text } = roundWith({}, { mode: 'report' }) + try { + assert.equal(run.status, 0, run.stderr) + assert.deepEqual(writes(), [], 'no write of any kind') + assert.match(text, /- #4001 \(T1, 4a1c2d3e\): would enqueue/) + } finally { + rmSync(dir, { recursive: true, force: true }) + } +}) + +test('#3253: the round prints the summary to stdout, byte for byte what it writes to the file', () => { + const dir = mkdtempSync(join(tmpdir(), 'auto-queue-t1-out-')) + const snapshot = join(dir, 'snapshot.json') + const file = join(dir, 'summary.md') + const step = join(dir, 'step-summary.md') + const pull = { + pr: { number: 4002, draft: false, labels: ['agent-pr'], base: { ref: 'main' }, head: { sha: HEAD, repo: { full_name: REPO } }, user: { login: 'HugoXK' } }, + files: [{ filename: 'plugins/timeline/client.js', additions: 3, deletions: 0 }], + checks: [], + queued: false, + } + writeFileSync(snapshot, JSON.stringify({ repo: REPO, pulls: [pull] })) + try { + // A read-only round: the snapshot path presses nothing, so no `gh` is needed and nothing is touched. + const run = spawnSync(process.execPath, [ + script, '--snapshot', snapshot, '--mode', 'report', '--repo', REPO, '--summary', file, + ], { encoding: 'utf8', env: { ...process.env, GITHUB_STEP_SUMMARY: step } }) + assert.equal(run.status, 0, run.stderr) + const written = readFileSync(file, 'utf8') + assert.equal(run.stdout, written, 'the log line is the file line') + assert.equal(readFileSync(step, 'utf8'), written, 'the step summary is the same text') + assert.match(run.stdout, /auto-queue-t1: mode=report would-enqueue=0 refused=1/) + assert.match(run.stdout, /#4002 \(T2, 4a1c2d3e\): refused — tier T2 is judged by a reviewer/) + } finally { + rmSync(dir, { recursive: true, force: true }) + } +})