Files
gstack/test/land-and-deploy-postfail.test.ts
T
garrytan ff54f3c955 test(W7h): drop issue-number pins in land-and-deploy-postfail, match guards case-insensitively
W7h (t09 F12, F13). One test file.

Removed assertion groups:
- `cli/cli#3442` / `cli/cli#13380` pins (t09 F12). Invariant: none of its
  own (upstream archaeology; the IDs stay in the template, only the pin goes).
  The behavior they decorated, "after any non-zero `gh pr merge` exit, query
  authoritative state before retrying", is now a kept free meaning check.
- `Universal invariant` label pin. Invariant: query-before-retry. Remaining
  protection: the new case-insensitive check
  `/non-zero exit from `gh pr merge`, query authoritative PR state before retrying/i`
  (stronger: the mutation that inverts the rule now goes red; the old test
  stayed green).

Loosened to case-insensitive meaning checks (kept, W7 keep list, data-loss
and destructive-op guards): STOP worktree cleanup without removing, no
`--force`, primary working tree never removed, CLOSED -> stop, never replay a
merge after MERGED, one direct fallback, no fallback from a direct attempt,
the OPEN/no-auto/no-queue fallback precondition, branch-absent vs check-failed
messages. Gate-tier E2E also covering the flow: land-and-deploy-workflow
(gate), judge `land-and-deploy/SKILL.md workflow` (gate lane).

Cosmetic pins dropped (no control needed): capitals on STOP / Do NOT.

Mutation log: .context/mutation-land-and-deploy-postfail.md (12 mutations,
0 unexpected: reorder, field delete and 7 safety deletes red; 3 rewordings green).
2026-10-03 01:02:23 +00:00

146 lines
6.7 KiB
TypeScript

/**
* Coverage for PR #1620 — Post-failure PR-state check after `gh pr merge`
* non-zero exit.
*
* The fix lives in land-and-deploy/sections/merge-and-deploy.md.tmpl as Step
* §4a-postfail (the Step 4/5 body was carved out of the skeleton into an
* on-demand section — prompt-token-load-reduction carve; the skeleton keeps
* only the STOP-Read pointer). After ANY non-zero `gh pr merge`, the skill
* must query authoritative PR state via
* GraphQL (including queue membership) and
* branch on the result instead of blindly retrying `gh pr merge`.
*
* Static invariants pin:
* - §4a-postfail header present
* - Query-before-retry invariant after any non-zero merge exit
* - All three state branches (MERGED, OPEN, CLOSED) named explicitly
* - MERGED branch: capture merge SHA via mergeCommit.oid
* - MERGED branch: non-destructive worktree cleanup with uncommitted-work guard
* - MERGED branch: continues to §4a CI watch
* - OPEN branch: checks autoMergeRequest before treating as failure
* - CLOSED branch: STOPs
* - Hard rule: no replay after MERGED; one guarded auto-to-direct fallback
* - .tmpl edit propagated to generated SKILL.md (atomic per T-Codex-3)
*/
import { describe, expect, test } from "bun:test";
import * as fs from "node:fs";
import * as path from "node:path";
const ROOT = path.resolve(import.meta.dir, "..");
const TMPL = path.join(ROOT, "land-and-deploy", "sections", "merge-and-deploy.md.tmpl");
const MD = path.join(ROOT, "land-and-deploy", "sections", "merge-and-deploy.md");
function readTmpl(): string {
return fs.readFileSync(TMPL, "utf-8");
}
function readMd(): string {
return fs.readFileSync(MD, "utf-8");
}
describe("PR #1620 §4a-postfail in land-and-deploy template", () => {
test("§4a-postfail header present in template", () => {
expect(readTmpl()).toMatch(/### 4a-postfail: Post-failure PR-state check/);
});
test("§4a-postfail comes before §4a (Merge queue detection)", () => {
const body = readTmpl();
const postfail = body.indexOf("### 4a-postfail:");
const queue = body.indexOf("### 4a: Merge queue detection");
expect(postfail).toBeGreaterThan(-1);
expect(queue).toBeGreaterThan(-1);
expect(postfail).toBeLessThan(queue);
});
test("any non-zero merge exit queries authoritative state before retrying", () => {
const body = readTmpl().replace(/\s+/g, " ");
expect(body).toMatch(/non-zero exit from `gh pr merge`, query authoritative PR state before retrying/i);
});
test("Authoritative state query includes auto request and queue membership", () => {
const body = readTmpl();
expect(body).toMatch(/gh api graphql/);
expect(body).toContain('state headRefOid baseRefName mergedAt mergeCommit { oid }');
expect(body).toContain('autoMergeRequest { enabledAt } mergeQueueEntry { id state }');
});
test("All three state branches named: MERGED, OPEN, CLOSED", () => {
const body = readTmpl();
expect(body).toMatch(/state == "MERGED"/);
expect(body).toMatch(/state == "OPEN"/);
expect(body).toMatch(/state == "CLOSED"/);
});
test("MERGED branch captures merge SHA via mergeCommit.oid", () => {
const body = readTmpl();
expect(body).toMatch(/jq -er '\.data\.repository\.pullRequest\.mergeCommit\.oid'/);
});
test("MERGED worktree cleanup is non-destructive (uncommitted-work guard)", () => {
const body = readTmpl();
expect(body).toMatch(/uncommitted work/);
expect(body).toMatch(/stop worktree cleanup without removing/i);
expect(body).toMatch(/do not use `--force`/i);
expect(body).toMatch(/do not remove the user's primary working tree/i);
});
test("MERGED branch continues to §4b CI auto-deploy detection", () => {
const body = readTmpl();
expect(body).toMatch(/continue to §4b \(CI auto-deploy detection\)/);
});
// #2656: the failed merge carried --delete-branch; the recovery path must
// reconcile the remote branch instead of silently dropping that half.
// #2696: that reconciliation must target the PR head repository, not the
// base checkout's origin, because fork branches do not exist in origin.
test("MERGED branch reconciles the PR head repository (ls-remote, confirm-first delete)", () => {
const body = readTmpl();
expect(body).toMatch(/gh pr view "\$PR_NUMBER" --repo "\$REPO" --json headRepositoryOwner,headRepository,headRefName/);
// gh leaves .headRepository.nameWithOwner empty (verified live, gh 2.83) —
// owner/name is composed from headRepositoryOwner.login + headRepository.name.
expect(body).toMatch(/headRepositoryOwner\.login/);
expect(body).not.toMatch(/\[\.headRepository\.nameWithOwner/);
expect(body).toMatch(/git ls-remote --heads "https:\/\/github\.com\/<head-repository>\.git" "<head-branch>"/);
expect(body).toMatch(/git push "https:\/\/github\.com\/<head-repository>\.git" --delete "<head-branch>"/);
expect(body).not.toMatch(/git ls-remote --heads origin/);
expect(body).not.toMatch(/git push origin --delete/);
// Confirm-first: deletion is offered, never unilateral.
expect(body).toMatch(/Delete it\?/);
});
test("MERGED branch reconciliation distinguishes branch-absent from check-failed", () => {
const body = readTmpl();
// exit 0 + empty output = already clean (idempotent re-runs)...
expect(body).toMatch(/already been cleaned up/i);
// ...non-zero exit = unknown state, never read as a clean branch.
expect(body).toMatch(/couldn't verify remote branch state/i);
expect(body).toMatch(/never read a failed check as a clean branch/i);
});
test("OPEN branch checks autoMergeRequest before treating as failure", () => {
const body = readTmpl();
expect(body).toMatch(/autoMergeRequest != null or \.mergeQueueEntry != null/);
expect(body).toMatch(/auto-merge is enabled or\s+merge queue is in use/);
});
test("CLOSED branch STOPs", () => {
const body = readTmpl();
expect(body).toMatch(/state == "CLOSED"[\s\S]{0,200}\bstop\b/i);
});
test("Hard rule: no replay after MERGED and only one guarded direct fallback", () => {
const body = readTmpl().replace(/\s+/g, " ");
expect(body).toMatch(/never replay a merge after MERGED/i);
expect(body).toMatch(/one direct fallback/i);
expect(body).toMatch(/no fallback from a direct attempt/i);
expect(body).toMatch(/confirmed OPEN, no auto request and no queue entry/i);
});
test("Generated merge-and-deploy.md carries the §4a-postfail section (atomic regen per T-Codex-3)", () => {
const md = readMd();
expect(md).toMatch(/### 4a-postfail: Post-failure PR-state check/);
expect(md).toMatch(/state == "MERGED"/);
expect(md).toMatch(/headRepositoryOwner\.login/);
expect(md).not.toMatch(/git ls-remote --heads origin/);
});
});