Skip to content

mcp: Add unit tests with node:test runner#11

Open
AlexNajem wants to merge 3 commits into
mainfrom
add-unit-tests
Open

mcp: Add unit tests with node:test runner#11
AlexNajem wants to merge 3 commits into
mainfrom
add-unit-tests

Conversation

@AlexNajem

Copy link
Copy Markdown
Contributor

Summary

  • Adds unit tests using Node's built-in node:test runner (no new dev dependencies) covering PexClient and tools/utils.
  • PexClient tests cover constructor env validation, auth header, query-param serialization (incl. array join), JSON vs text response handling, error message mapping (401/403/404/429/500), retry behavior on 502/503, and no-retry on 4xx.
  • tools/utils tests cover paginateArray (clamping, edge cases), paginateNestedArray, and the toolResponse/toolError helpers.
  • Wires npm test into the CI pipeline and enables PR validation against main (was pr: none).
  • Makes PexClient retry baseDelay injectable so retry tests pass 0 and the suite stays fast (~145ms total instead of ~3s of real backoff).

Generated with Claude Code - claude-opus-4-7

- Adds node:test suites for PexClient (env validation, headers, query
  serialization, error mapping, retry behavior) and tools/utils
  (pagination, response/error helpers)
- Wires npm test into CI and enables PR validation against main
- Makes PexClient retry baseDelay injectable so retry tests don't
  spend real wall-clock time on backoff

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@devin-ai-integration

Copy link
Copy Markdown
Contributor

Code review

LGTM overall — tests are well-organized, fast (~345ms locally, 28/28 pass), the build & typecheck pass, the PexClient constructor change is backward compatible, and nothing in the diff is out of scope for "add unit tests". Coverage hits the meaningful seams: env validation, base-URL normalization, auth header, query-string serialization (incl. array join), JSON vs text body handling, status → message mapping, retry-on-5xx, no-retry-on-4xx, pagination clamping, and tool response/error helpers.

Notes & suggestions

1. The failing pex-mcp-server (optional) check is preexisting, not caused by this PR.
The failure is npm audit --audit-level=high exiting 1. I ran the same audit against origin/main and the exact same advisories are present there — a high in fast-uri plus moderates in hono, @hono/node-server, and ip-address (transitive via @modelcontextprotocol/sdk). It only surfaces here because this PR is what flips pr: nonepr: { branches: { include: main } }, so this is the first PR run to actually evaluate it.

Recommend addressing in a separate follow-up PR (e.g. npm audit fix or bump @modelcontextprotocol/sdk) rather than rolling into this one.

2. AGENTS.md is now stale. It still says:

There are no tests yet. Do not add a npm test step to scripts without being asked.

Worth updating in this PR so future agents have correct guidance — describe the npm test command, mention the node:test + tsx --test setup, and the src/**/*.test.ts convention.

3. Optional follow-up coverage (non-blocking):

  • 504 retry — isRetryableStatus covers 502/503/504 but only 502 and 503 are tested.
  • Network-error retry path (isNetworkError) in fetchWithRetry.
  • Timeout → PexClientError("Request timed out after 30s", 0) mapping in get().
  • paginateArray with page past totalPages (should return empty data, hasMore: false).

4. Tiny nit on stubFetch typing in src/pex-client.test.ts:

function stubFetch(handler: FetchHandler): void {
  globalThis.fetch = ((url: string, init: RequestInit) =>
    handler(url, init)) as typeof fetch;
}

The real fetch is (input: RequestInfo | URL, init?: RequestInit). The as typeof fetch cast is fine in practice because PexClient always calls fetch(string, RequestInit), but widening the handler signature to (input: RequestInfo | URL, init?: RequestInit) would be more honest if anyone later stubs from outside PexClient. Not a blocker.

Security / backward compat / performance:

  • No new credentials, endpoints, or runtime deps. Test stubs globalThis.fetch and restores it in afterEach correctly.
  • Constructor remains zero-arg compatible (new PexClient() still works); existing src/index.ts callsite unaffected.
  • Default retryBaseDelayMs is 500 (unchanged behavior); tests pass 0 for speed.

Devin session: https://app.devin.ai/sessions/743e380c4b1544619fe242606d32a40f

Upgrades the SDK from ^1.12.1 to ^1.29.0 and runs npm audit fix to
pull patched versions of fast-uri, hono, @hono/node-server, ip-address,
and express-rate-limit. Resolves 1 high + 4 moderate advisories that
were failing the Security audit CI step.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@devin-ai-integration

Copy link
Copy Markdown
Contributor

Re-review of 445661d

Looks good. The new commit bumps @modelcontextprotocol/sdk from ^1.12.1 to ^1.29.0 and runs npm audit fix, which is what addressed point #1 from my prior review.

Verification:

  • npm audit --audit-level=high now reports 0 vulnerabilities locally (was 1 high + 4 moderate).
  • npm ci && npm run build && npm test → 28/28 tests pass, typecheck clean.
  • CI is green: pex-mcp-server ✅ (job 75447432992).
  • SDK 1.12.1 → 1.29.0 is a minor bump within 1.x and the 1.29.0 release notes explicitly state "Breaking Changes: None". All SDK imports in this repo (server/mcp.js, server/stdio.js, types.js) are stable across that range, and the build confirms it.
  • No new dependencies; this is purely a version bump on existing deps.

Scope: package.json + package-lock.json only — appropriately tight for an audit-fix commit. No unrelated changes.

Still open from prior review (all non-blocking, your call whether to roll into this PR):

  • AGENTS.md update — the "There are no tests yet. Do not add a npm test step to scripts without being asked." note is now incorrect. Most valuable of the three.
  • Optional additional test coverage (504 retry, network-error retry, timeout, paginateArray past last page).
  • Minor stubFetch typing widen.

LGTM as-is. Ready to come out of draft / merge whenever.

Devin session: https://app.devin.ai/sessions/743e380c4b1544619fe242606d32a40f

- Update AGENTS.md to document the test runner and conventions; remove
  the stale "no tests yet" guidance
- Add coverage for 504 retry, network-error retry, request-timeout
  mapping (DOMException TimeoutError → PexClientError status 0), and
  paginateArray with a page past totalPages
- Widen the stubFetch handler signature to (RequestInfo | URL, init?)
  to match the real fetch signature

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@AlexNajem AlexNajem marked this pull request as ready for review May 11, 2026 22:57
@AlexNajem AlexNajem requested review from vzdendiaksj and removed request for vzdendiaksj May 11, 2026 22:57

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no potential bugs to report.

View in Devin Review to see 3 additional findings.

Open in Devin Review

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.

2 participants