Skip to content

Conversation

@dcramer
Copy link
Member

@dcramer dcramer commented Dec 12, 2025

Summary

Consolidates mcp-cloudflare to a unified test infrastructure using vitest-pool-workers with fetchMock from cloudflare:test. All tests now run in the workerd runtime for production parity.

Key Changes

  • Unified vitest config - Single vitest.config.ts using @cloudflare/vitest-pool-workers
  • fetchMock for HTTP mocking - Replaces MSW (which doesn't work in workerd) with Cloudflare's fetchMock
  • ajv workaround - Stub for ajv CJS compatibility in workerd runtime (MCP SDK imports ajv at module level)
  • Real MCP handler tests - Tests exercise actual mcp-handler.ts code, not stubs
  • Removed redundant tests - Deleted tests that verified third-party library behavior

Test Coverage

  • 94 tests across 16 files
  • MCP handler authentication, URL constraints, protocol handling
  • OAuth routes (authorize, callback, helpers)
  • API routes (auth, chat, search, metadata)
  • Static routes and MCP discovery

Files

File Purpose
vitest.config.ts Unified config with ajv alias and ssr.noExternal
wrangler.test.jsonc Test bindings aligned with production flags
src/test-setup.ts Global fetchMock setup for Sentry API
src/test-utils/fetch-mock-setup.ts Reusable fetchMock interceptors
src/test-utils/ajv-stub.ts Stub for ajv CJS compatibility

🤖 Generated with Claude Code

Comment on lines +110 to +116
const server = new McpServer(
{
name: MCP_SERVER_NAME,
version: LIB_VERSION,
},
{ jsonSchemaValidator },
);

This comment was marked as outdated.

dcramer and others added 5 commits December 15, 2025 09:10
Set up @cloudflare/vitest-pool-workers for testing mcp-cloudflare routes
with real Cloudflare bindings via Miniflare. Tests are organized into
separate modules per route group:

- static.integration.test.ts - robots.txt, llms.txt, SSE deprecation
- mcp-discovery.integration.test.ts - /.mcp discovery endpoints
- api-metadata.integration.test.ts - /api/metadata auth
- api-search.integration.test.ts - /api/search validation and AI binding
- api-auth.integration.test.ts - /api/auth/* status, logout
- chat-oauth.integration.test.ts - OAuth callback error handling
- api-chat.integration.test.ts - /api/chat auth

Key configuration:
- wrangler.test.jsonc with observability disabled for OTel compatibility
- vitest.integration.config.ts using vitest-pool-workers
- tsconfig.test.json with cloudflare:test module types

Co-Authored-By: Claude Code <[email protected]>
Tests the MCP endpoint behavior through OAuthProvider without Sentry wrapper:
- Unauthenticated requests return 401
- Invalid Bearer tokens return 401
- OAuth discovery endpoint returns proper metadata
- Dynamic client registration works at /oauth/register

Uses OAuthProvider directly to avoid vitest-pool-workers compatibility
issues with @sentry/cloudflare dependencies.

Co-Authored-By: Claude Code <[email protected]>
Replace the two-config approach (MSW for Node.js, workers pool for workerd)
with a unified vitest-pool-workers config using fetchMock from cloudflare:test.

Changes:
- Create payloads.ts in mcp-server-mocks for MSW-free fixture exports
- Add fetch-mock-setup.ts with comprehensive Sentry API mocking
- Consolidate vitest.config.ts to use workers pool for all tests
- Remove vitest.integration.config.ts (no longer needed)
- Rename integration tests to standard .test.ts naming
- Extract inline payload constants to JSON fixture files

Key fix: fetchMock requires explicit Content-Type headers in reply()
for the API client to properly parse JSON responses.

Note: mcp-handler.test.ts excluded from workers pool due to CJS
dependency (ajv) incompatibility with workerd runtime.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
Refactor mcp-handler.test.ts to properly test the MCP handler without
mocking agents/mcp. Tests now exercise the real handler with fetchMock
for Sentry API mocking only.

Key changes:
- Add ajv-stub.ts to bypass CJS require() issues in workerd
- Configure vitest with resolve.alias and ssr.noExternal for ajv
- Add jsonSchemaValidator option to buildServer() for edge runtimes
- Use CfWorkerJsonSchemaValidator in mcp-handler for workerd compat
- Parse SSE responses in tests (MCP handler returns SSE format)
- Fix Accept header and initialize params for MCP protocol

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <[email protected]>
Delete mcp-endpoint.test.ts as it tested third-party library behavior
(@cloudflare/workers-oauth-provider), not our code.

Per docs/testing.md: "Don't test third-party library behavior".

The tests verified:
- OAuthProvider returns 401 for unauthenticated requests
- OAuthProvider returns 401 for invalid tokens
- OAuthProvider exposes OAuth discovery metadata
- OAuthProvider handles dynamic client registration

mcp-handler.test.ts already covers our authentication logic:
- Handler rejects requests without auth context
- Handler rejects legacy tokens without grantedSkills
- Handler rejects tokens with no valid skills

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
Comment on lines +99 to +109
export {
createDefaultEvent,
createGenericEvent,
createUnknownEvent,
createPerformanceEvent,
createPerformanceIssue,
createRegressedIssue,
createUnsupportedIssue,
createCspIssue,
createCspEvent,
} from "./fixtures";

This comment was marked as outdated.

dcramer and others added 2 commits December 15, 2025 09:54
- Remove codecov.yml configuration file
- Remove codecov steps from test.yml workflow
- Remove codecov steps from eval.yml workflow
- Remove codecov badge from README.md

Co-Authored-By: Claude Code <[email protected]>
Fixed undici MockAgent interceptor ordering issues where general patterns
were registered before specific handlers, causing wrong fixtures to be
returned.

Changes:
- Move PERF-N1-001/events/latest handler before general /events/latest
  handler so performance events return the correct fixture
- Add PERF-N1 and PEATED to org issues exclusion pattern to prevent
  incorrect list responses for these specific issues
- Add section comments throughout file for maintainability
- Add documentation about undici's first-match-wins behavior

Co-Authored-By: Claude Code <[email protected]>
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