Skip to content

test(github): the GitHub session's fake lives under tests/support/fakes, held to GitHub's recorded answers by a contract suite - #1716

Merged
dan-petty merged 1 commit into
release/v0.2.37from
test/983-github-fake-contract
Oct 20, 2026
Merged

dan-petty merged 1 commit into
release/v0.2.37from
test/983-github-fake-contract

Conversation

@dan-petty

Copy link
Copy Markdown
Owner

The last of #983's pull requests. The GitHub session's fake moves under tests/support/fakes/, and a contract suite holds it to GitHub's recorded answers. This closes #983.

The move.

The contract suite: tests/test_github_contract.py. Ten cases run through the real session twice:

  • over GitHubApiFake, routed as a test over the fake routes it;
  • over RecordedGitHubApi, which replays GitHub's own answers to the same requests in order through httpx2.MockTransport, offline. It fails on an unrecorded request or an unused recording.

The cases:

  • a REST read;
  • a read answered from its ETag after a 404;
  • a 404;
  • a listing over GitHub's linked pages;
  • a reader that stops early;
  • a listing read again from its first page's ETag;
  • a refused token;
  • a 422;
  • a write answered with text;
  • a write refused with a 400.

Each compares answers, errors, requests and the ledger against values written in the case. Every case fails under at least one mutant of the fake or the session.

The recordings, in tests/fixtures/github_api/recorded_answers.json, were made on 2026-10-19 through the session's own transport.

  • What was requested: reads of GitHub's public octocat/Hello-World example, a search with no query, and two Markdown renders, which change nothing. A /user read used the pinned fake token.
  • What was kept out: they hold no token, OAuth scopes, OAuth client id or request id.
  • Not recorded, each with its reason in the module docstring: rate limits, 5xx answers, refused connects, redirects and a missing scope's 403.

Fake bugs the suite found, and fixed. GitHub reports the quota with every REST answer: a 304 spends none and a 404 spends one.

  • The fake's own 304 reported the quota of the answer it stood in for, and its 404 reported none.
  • The fake now holds the last quota it reported for each resource: its 304 reports it, and its 404 spends one.
  • github_text takes headers.

Folded from #983's amendments.

  • No gh children: the github_api fixture refuses any gh command the rate limiter would run, run_gh and the quota refresh. A reader that falls back to run_gh fails offline with "A test over the fake GitHub API ran gh: " instead of starting gh with the fake token.
  • Pruned rows: the gh-output table drops seven rows gh writes for no GitHub failure, checked against gh v2.102.0's and go-gh v2.16.1's source. "Proxy Authentication Required", the only place "authentication required" reaches gh's stderr, is pinned as no class.
  • Signing-key 422: it was already confirmed against the key listing since PR 5.

Records.

Verification

  • Tests first.
    • The contract suite failed over the old fake on its stale 304 quota, its 404 with no quota and github_text with no headers.
    • The gh-fallback test hit the old fixture.
    • The pruned-phrase test found all seven phrases classified.
  • Review. An adversarial review, with each finding checked by a skeptic, drove:
    • a 422 quota check that the session's own pacing can no longer mask;
    • literal expected values;
    • dropping the page-cap case, which patched devops-cli;
    • the fake's 404 spending quota;
    • the proxy row.
  • Isolation. The full suite passed under a guard that refuses gh children: 11,041 passed, 1 skipped and 8 xfailed, with 0 gh children.
  • Gate. uv run devops ci passed (pre-push hook).

Closes #983

Fail-to-pass: not applicable: moves a test double and adds its contract suite; the src change removes table rows gh never writes, which the new table test pins

…es, held to GitHub's recorded answers by a contract suite

The GitHub session's fake moves from tests/github_session_fakes.py to tests/support/fakes/github.py, and every importer reads it from there. Its github_api fixture moves beside it out of tests/conftest.py, defined with pytest.fixture, so the support registration gives it to every test and the test selector sees it; install_async_client_fake, which keeps github_api and stub_web from sharing a test, moves to tests/web_fakes.py, since a support module imports no conftest. serve_github_api installs any double of GitHub's API as the fixture installs the fake.

tests/test_github_contract.py runs ten cases through the real session twice: over GitHubApiFake, routed as a test over the fake routes it, and over RecordedGitHubApi, which replays GitHub's own answers to the same requests in order through httpx2.MockTransport, offline, matching each request on its method, URL, If-None-Match and body. The answers were recorded on 2026-10-19 through the session's own transport (tests/fixtures/github_api/recorded_answers.json): reads of GitHub's octocat/Hello-World example repository, a search with no query, a Markdown render and a /user read with the pinned fake token, kept without any token, OAuth scopes, OAuth client or request id. The cases cover a REST read, a read answered from its ETag after a read GitHub answers 404, a 404, a listing over the pages GitHub links, a reader that stops early, a listing read again from its first page's ETag, a refused token, a 422, a write answered with text and a write refused with a 400. Each compares answers, errors, requests and the ledger with values written in the case, and each fails under a mutant of the fake or the session.

The suite found the fake's quota wrong where it makes an answer itself. GitHub reports the quota as it stands with every REST answer; a 304 spends none of it and a 404 spends one. The fake's 304 reported the quota of the answer it stood in for, and its 404 reported none. The fake now holds the quota it last reported for each resource, which a test may set: its own 304 reports it, and its own 404 spends one from it. github_text takes headers, so a text answer carries the quota GitHub sends with it. Rate limits, 5xx answers, refused connects, redirects and a missing scope's 403 are not recorded: GitHub cannot be made to send the first three without spending a quota or waiting for an outage, a redirect wants a moved repository that names no person, and the session reads a missing scope from the token's x-oauth-scopes, which the recordings remove. The page cap is the session's own rule and stays tested in tests/test_github_session.py, and the session sends no GraphQL.

The github_api fixture now refuses any gh command the rate limiter would run, run_gh and the quota refresh, so a reader that falls back to run_gh fails offline with "A test over the fake GitHub API ran gh: <argv>" instead of starting gh with the pinned fake token.

The gh-output table drops the rows gh writes for no GitHub failure: scope 'project', scope "project", project scope, requires additional permissions, requires the 'project' scope, need the 'project' scope and authentication required. gh's source at v2.102.0 and go-gh's at v2.16.1 hold none of them in any failure message: project scope is only in the help of gh's skill installer, and authentication required only in the exit-codes section of gh's man page and in an authenticating proxy's 407 reason phrase, Proxy Authentication Required, which the table now pins as no class. The session's signing-key 422 is already confirmed against the key listing since PR 5.

docs/TESTING.md and ADR 0006 name the fake's place and its contract suite, the task file records PR 7 and reads the item as delivered, its follow-up for the remaining gh-text matchers being the 2026-10-19 amendment on #891, and changelog.d/983.md describes the whole item.

Closes #983
@dan-petty
dan-petty merged commit bf9481e into release/v0.2.37 Oct 20, 2026
15 checks passed
@dan-petty
dan-petty deleted the test/983-github-fake-contract branch October 20, 2026 03:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant