|
| 1 | +"""Tests for pr-state.sh's local-writer section — the two-writers detector. |
| 2 | +
|
| 3 | +This is the one thing GitHub cannot tell you. A branch with two writers looks |
| 4 | +completely normal through the API: the divergence exists only between a local |
| 5 | +checkout and the remote, and it collapses into an ordinary merge commit the |
| 6 | +moment somebody reconciles it. |
| 7 | +
|
| 8 | +PR #619 is why this exists. One writer took the branch at 08:09 and worked from |
| 9 | +that base; another pushed 23031e78 at 09:34. The reviewer reviewed 23031e78 |
| 10 | +three times, twice with blocking findings, while the first line never held that |
| 11 | +commit at all. Fifteen hours later it landed as `Merge remote-tracking branch |
| 12 | +'origin/fix/...' into fix/...` — a branch merged into itself. |
| 13 | +
|
| 14 | +So the scenario below is built for real: a bare "origin", two clones that both |
| 15 | +commit to one branch, and the assertion that the script names it. |
| 16 | +""" |
| 17 | + |
| 18 | +import json |
| 19 | +import os |
| 20 | +import stat |
| 21 | +import subprocess |
| 22 | +from pathlib import Path |
| 23 | + |
| 24 | +import pytest |
| 25 | + |
| 26 | +REPO_ROOT = Path(__file__).resolve().parents[2] |
| 27 | +SCRIPT = REPO_ROOT / "scripts" / "pr-state.sh" |
| 28 | + |
| 29 | +GIT_ENV = { |
| 30 | + "PATH": "/usr/bin:/bin:/usr/local/bin:/opt/homebrew/bin", |
| 31 | + "GIT_CONFIG_GLOBAL": "/dev/null", |
| 32 | + "GIT_CONFIG_SYSTEM": "/dev/null", |
| 33 | + "GIT_AUTHOR_NAME": "t", |
| 34 | + "GIT_AUTHOR_EMAIL": "t@t", |
| 35 | + "GIT_COMMITTER_NAME": "t", |
| 36 | + "GIT_COMMITTER_EMAIL": "t@t", |
| 37 | +} |
| 38 | + |
| 39 | +BRANCH = "fix/issue-592-vpp-idle-at-floor" |
| 40 | + |
| 41 | + |
| 42 | +def _git(cwd: Path, *args: str) -> str: |
| 43 | + proc = subprocess.run( |
| 44 | + ["git", *args], |
| 45 | + cwd=cwd, |
| 46 | + check=True, |
| 47 | + capture_output=True, |
| 48 | + text=True, |
| 49 | + env={**GIT_ENV, "HOME": str(cwd)}, |
| 50 | + ) |
| 51 | + return proc.stdout.strip() |
| 52 | + |
| 53 | + |
| 54 | +def _commit(repo: Path, name: str) -> None: |
| 55 | + (repo / name).write_text(name) |
| 56 | + _git(repo, "add", name) |
| 57 | + _git(repo, "commit", "-q", "-m", name) |
| 58 | + |
| 59 | + |
| 60 | +@pytest.fixture |
| 61 | +def two_writers(tmp_path: Path) -> Path: |
| 62 | + """A checkout whose branch has diverged from its own remote. |
| 63 | +
|
| 64 | + Writer B pushes; writer A, which branched earlier, commits locally without |
| 65 | + pulling. That is #619's shape exactly. |
| 66 | + """ |
| 67 | + origin = tmp_path / "origin.git" |
| 68 | + origin.mkdir() |
| 69 | + _git(origin, "init", "-q", "--bare", "-b", "main") |
| 70 | + |
| 71 | + writer_b = tmp_path / "writer-b" |
| 72 | + _git(tmp_path, "clone", "-q", str(origin), "writer-b") |
| 73 | + _commit(writer_b, "seed") |
| 74 | + _git(writer_b, "push", "-q", "origin", "main") |
| 75 | + _git(writer_b, "checkout", "-q", "-b", BRANCH) |
| 76 | + _commit(writer_b, "from-b") |
| 77 | + _git(writer_b, "push", "-q", "origin", BRANCH) |
| 78 | + |
| 79 | + writer_a = tmp_path / "writer-a" |
| 80 | + _git(tmp_path, "clone", "-q", str(origin), "writer-a") |
| 81 | + _git(writer_a, "checkout", "-q", "-b", BRANCH, "--no-track", "origin/main") |
| 82 | + _commit(writer_a, "from-a") |
| 83 | + # A never pulled B's push, so the two lines have no common tip. |
| 84 | + return writer_a |
| 85 | + |
| 86 | + |
| 87 | +def _run(cwd: Path, tmp_path: Path, prs: list[dict]) -> str: |
| 88 | + bin_dir = tmp_path / "bin" |
| 89 | + bin_dir.mkdir(exist_ok=True) |
| 90 | + (bin_dir / "prs.json").write_text(json.dumps(prs)) |
| 91 | + gh = bin_dir / "gh" |
| 92 | + gh.write_text(f"#!/bin/sh\ncat '{bin_dir}/prs.json'\n") |
| 93 | + gh.chmod(gh.stat().st_mode | stat.S_IEXEC) |
| 94 | + |
| 95 | + proc = subprocess.run( |
| 96 | + ["bash", str(SCRIPT)], |
| 97 | + capture_output=True, |
| 98 | + text=True, |
| 99 | + cwd=cwd, |
| 100 | + env=dict(os.environ, PATH=f"{bin_dir}:{os.environ['PATH']}"), |
| 101 | + ) |
| 102 | + assert proc.returncode == 0, proc.stderr |
| 103 | + return proc.stdout |
| 104 | + |
| 105 | + |
| 106 | +def _pr(number: int, branch: str) -> dict: |
| 107 | + return { |
| 108 | + "number": number, |
| 109 | + "title": f"pr {number}", |
| 110 | + "isDraft": True, |
| 111 | + "mergeable": "MERGEABLE", |
| 112 | + "mergeStateStatus": "CLEAN", |
| 113 | + "headRefName": branch, |
| 114 | + "updatedAt": "2026-08-17T00:00:00Z", |
| 115 | + "reviews": [], |
| 116 | + "commits": [{"committedDate": "2026-08-17T00:00:00Z"}], |
| 117 | + "comments": [ |
| 118 | + {"body": "@claude-bot review", "createdAt": "2026-08-18T00:00:00Z"} |
| 119 | + ], |
| 120 | + "statusCheckRollup": [ |
| 121 | + {"name": "Fast tests", "status": "COMPLETED", "conclusion": "SUCCESS"} |
| 122 | + ], |
| 123 | + } |
| 124 | + |
| 125 | + |
| 126 | +def test_two_writers_on_one_branch_are_named(two_writers: Path, tmp_path: Path) -> None: |
| 127 | + """The #619 shape. Local has a commit the remote lacks AND the remote has a |
| 128 | + commit local lacks — which cannot happen with a single writer.""" |
| 129 | + out = _run(two_writers, tmp_path, [_pr(619, BRANCH)]) |
| 130 | + |
| 131 | + assert "DIVERGED" in out |
| 132 | + assert "TWO WRITERS" in out |
| 133 | + assert "#619" in out |
| 134 | + |
| 135 | + |
| 136 | +def test_a_branch_in_sync_is_not_flagged(two_writers: Path, tmp_path: Path) -> None: |
| 137 | + """Guards against a detector that shouts on every branch — which would get |
| 138 | + it ignored, the way a CONFLICTING PR with no checks got ignored.""" |
| 139 | + _git(two_writers, "fetch", "-q", "origin") |
| 140 | + _git(two_writers, "reset", "-q", "--hard", f"origin/{BRANCH}") |
| 141 | + out = _run(two_writers, tmp_path, [_pr(619, BRANCH)]) |
| 142 | + |
| 143 | + assert "DIVERGED" not in out |
| 144 | + assert "in sync" in out |
| 145 | + |
| 146 | + |
| 147 | +def test_a_branch_with_no_open_pr_is_not_reported( |
| 148 | + two_writers: Path, tmp_path: Path |
| 149 | +) -> None: |
| 150 | + """The fleet has ~47 worktrees and most have no open PR. Listing them all |
| 151 | + would bury the one line that matters.""" |
| 152 | + out = _run(two_writers, tmp_path, [_pr(999, "some/other-branch")]) |
| 153 | + |
| 154 | + assert BRANCH not in out.split("Local writers")[-1] |
0 commit comments