Cover the pm-github failure surface against a local HTTP GitHub server

Merged
UN
4mo ago
+1896-27

Why

pm-github is what the ecosystem relies on for two-way GitHub issue sync, so its correctness decides whether each repo's pm items can be trusted as the source of truth. The coverage gate was passing at 76 lines / 78 branches / 75 functions — but only because coverageGate.thresholds was pinned to that measured floor rather than toward the coverage mandate.

The uncovered quarter of a 4332-line engine was its failure surface, which for a sync tool is exactly where the damage lives. The existing suite (atomic, comments-sync, dryrun-preview, import-lock, link-deps, projects, smoke) covered the main flows; nothing covered what happens when GitHub says 429, or when a batch fails halfway.

Approach: a real HTTP boundary, not a stubbed function

test/helpers/mock-github-server.ts runs a real http.createServer on an ephemeral 127.0.0.1 port and the client is pointed at it. Request building, response decoding, retry, backoff, redirect handling and pagination all actually execute — a test that replaced fetchJSON with a fake would prove none of that.

What is now covered

Retry and backoff

  • 429 with Retry-After, 5xx transient, and 403 primary rate limit (remaining=0) each retried and then succeeding
  • a persistent 429 giving up at maxRetries and throwing
  • 404 and 422 as non-retryable — throwing immediately with no retry
  • computeBackoffMs: Retry-After seconds→ms capped at 60s, the primary rate-limit reset window, exponential fallback (1s, 2s, 4s… capped), and a non-numeric Retry-After falling through

Credential safety

  • a same-origin redirect forwards the token
  • a cross-origin redirect DROPS the token, so a credential leak through Location is now a test failure rather than a latent bug
  • redirect loops rejected instead of spinning

Pagination and transport

  • Link-header pagination with no silent truncation, plus the empty-page and single-page cases
  • a malformed JSON body
  • a transport error (connection refused) surfacing as a rejection

Partial-failure semantics — the ones that matter most for a sync tool

  • runExport --apply continues past a mid-batch 422 to partial success (exit 0)
  • a non-empty batch that writes nothing exits non-zero
  • runSync tolerates a 404 reading upstream state (skipped, not aborted)
  • the search provider degrades to no hits when GitHub is unreachable
  • runValidate flags an inaccessible repo, and warns when the rate-limit budget is low

Production changes (deliberately minimal)

Two were required to make the stack reachable from a test:

  • githubApiBase() reads a PM_GITHUB_API_BASE override at call time, not module-eval time, so a test can retarget per case without import-order coupling. Production is byte-identical when the variable is unset.
  • requestOnce dispatches on the URL scheme rather than hard-coding https, because a local test server speaks http and the entire point is to run the real client against it.
  • FetchResult, fetchJSON, fetchAllIssues and fetchComments are exported so tests drive the stack from its real entry points.

On the env override: it only changes the API base, and the redirect path already computes sameOrigin(url, target) ? token : undefined, so the token is never forwarded to a different origin — verified by a dedicated test rather than by inspection. sameOrigin compares full URL.origin (scheme + host + port), not just hostname.

Coverage

metricbeforeafterthreshold
lines76.xx89.9176 → 88
branches78.xx80.7578 → 79
functions75.xx90.8675 → 89

0 failing tests. No threshold lowered, nothing added to coverageGate.ignore.

Verified locally (not claimed)

npm run check     -> exit 0
npm run coverage  -> exit 0, fail 0, 89.91 / 80.75 / 90.86

No test contacts the real GitHub API and none mutates a real issue. The only github.com strings in the new tests are html_url fixtures.

pm item

Summary by Sourcery

Strengthen pm-github’s GitHub integration by making the HTTP client and handlers testable against a local HTTP GitHub server and raising coverage thresholds over the failure surface.

Enhancements:

  • Introduce configurable GitHub API base and GraphQL endpoint resolution via PM_GITHUB_API_BASE, keeping production behavior unchanged by default.
  • Update the HTTP request layer to dispatch based on URL scheme so both https (real GitHub) and http (local test server) are supported.
  • Export fetchJSON, fetchAllIssues, fetchComments, and FetchResult to allow tests to exercise the real HTTP and pagination stack end-to-end.

Tests:

  • Add http-boundary tests that drive the real HTTP client against a local mock server to cover retry/backoff behavior, redirects, pagination, transport errors, and failure mappings in runImport.
  • Add handler-level failure-surface tests that run registered github commands, search provider, and Projects v2 flows through the SDK harness against the mock server to cover partial failures, token resolution, validation errors, and GraphQL paths.
  • Raise coverageGate thresholds for lines, branches, and functions to reflect the expanded test coverage of pm-github’s failure surface.

Summary by cubic

Add a local HTTP GitHub server test suite that drives the real client (retry, backoff, redirects, pagination, partial failures), harden the API base override to prevent token leaks, and keep test-only exports out of published types. Coverage is 90.04/80.95/90.86 with gates at 88/79/89; no real GitHub calls.

  • Refactors

    • Added githubApiBase() to read PM_GITHUB_API_BASE at call time; GraphQL URL derives from the same base.
    • requestOnce now dispatches by URL scheme (http: vs https:); exported FetchResult, fetchJSON, fetchAllIssues, fetchComments, and githubApiBase are marked @internal with stripInternal enabled.
    • Renamed redirect variable to redirectUrl to avoid shadowing.
  • Bug Fixes

    • Enforced token drop on cross‑origin redirects; added tests.
    • Constrained PM_GITHUB_API_BASE: allow https anywhere or http only on loopback; strip trailing slashes; invalid values throw.
    • Ensured all test pm spawns use PM_SPAWN_OPTS so Windows runs succeed.