Cover the pm-github failure surface against a local HTTP GitHub server
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
maxRetriesand throwing - 404 and 422 as non-retryable — throwing immediately with no retry
computeBackoffMs:Retry-Afterseconds→ms capped at 60s, the primary rate-limit reset window, exponential fallback (1s, 2s, 4s… capped), and a non-numericRetry-Afterfalling through
Credential safety
- a same-origin redirect forwards the token
- a cross-origin redirect DROPS the token, so a credential leak through
Locationis 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 --applycontinues past a mid-batch 422 to partial success (exit 0)- a non-empty batch that writes nothing exits non-zero
runSynctolerates a 404 reading upstream state (skipped, not aborted)- the search provider degrades to no hits when GitHub is unreachable
runValidateflags 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 aPM_GITHUB_API_BASEoverride 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.requestOncedispatches on the URL scheme rather than hard-codinghttps, because a local test server speakshttpand the entire point is to run the real client against it.FetchResult,fetchJSON,fetchAllIssuesandfetchCommentsare 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
| metric | before | after | threshold |
|---|---|---|---|
| lines | 76.xx | 89.91 | 76 → 88 |
| branches | 78.xx | 80.75 | 78 → 79 |
| functions | 75.xx | 90.86 | 75 → 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 readPM_GITHUB_API_BASEat call time; GraphQL URL derives from the same base. requestOncenow dispatches by URL scheme (http:vshttps:); exportedFetchResult,fetchJSON,fetchAllIssues,fetchComments, andgithubApiBaseare marked@internalwithstripInternalenabled.- Renamed redirect variable to
redirectUrlto avoid shadowing.
- Added
-
Bug Fixes
- Enforced token drop on cross‑origin redirects; added tests.
- Constrained
PM_GITHUB_API_BASE: allowhttpsanywhere orhttponly on loopback; strip trailing slashes; invalid values throw. - Ensured all test
pmspawns usePM_SPAWN_OPTSso Windows runs succeed.