Skip to content

test(cloudflare): Run server tests under vitest-pool-workers#678

Merged
dcramer merged 21 commits intomainfrom
vitest-pool-workers
Mar 19, 2026
Merged

test(cloudflare): Run server tests under vitest-pool-workers#678
dcramer merged 21 commits intomainfrom
vitest-pool-workers

Conversation

@dcramer
Copy link
Copy Markdown
Member

@dcramer dcramer commented Dec 12, 2025

Move the mcp-cloudflare server tests onto @cloudflare/vitest-pool-workers so they run inside the same workerd-style runtime we ship in production.

This replaces the split test setup with a single Vitest configuration, switches HTTP mocking to Cloudflare-native fetchMock, and adds route-level coverage for the app surface that now depends on that worker runtime behavior. The branch also updates the shared mocks package so the new tests and the existing core tests can reuse the same fixture payloads.

I kept the worker-specific compatibility pieces that were needed to make the MCP SDK load correctly in tests, including the AJV stub and the worker JSON schema validator path in the handler. After rebasing onto main, I also aligned the static-route expectations and restored the mock fixtures needed for the full repo test suite to pass.

Coverage-specific setup was removed as part of this migration because it was conflicting with the worker pool setup and was not needed for the current CI path.

Comment thread packages/mcp-core/src/server.ts
Comment thread packages/mcp-server-mocks/src/payloads.ts
Comment thread packages/mcp-cloudflare/tsconfig.test.json Outdated
Comment thread packages/mcp-cloudflare/src/test-utils/fetch-mock-setup.ts
Comment thread packages/mcp-server-mocks/src/index.ts Outdated
@dcramer dcramer changed the title feat(cloudflare): add vitest-pool-workers integration tests test(cloudflare): Run server tests under vitest-pool-workers Mar 16, 2026
Comment thread packages/mcp-cloudflare/tsconfig.test.json
Comment thread packages/mcp-server-mocks/src/index.ts
Comment thread packages/mcp-server-mocks/src/payloads.ts
Comment thread packages/mcp-cloudflare/src/test-utils/fetch-mock-setup.ts Outdated
Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Comment thread packages/mcp-cloudflare/src/test-utils/fetch-mock-setup.ts
dcramer and others added 21 commits March 19, 2026 15:34
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 <noreply@anthropic.com>
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 <noreply@anthropic.com>
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 <noreply@anthropic.com>
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 <noreply@anthropic.com>
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 <noreply@anthropic.com>
- 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 <noreply@anthropic.com>
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 <noreply@anthropic.com>
Update the Cloudflare route assertions to match the current static and discovery responses after rebasing onto main.

Restore the missing mock fixtures needed by mcp-core tests and record the dependency state required to run the worker test pool locally.

Co-Authored-By: Codex <noreply@openai.com>
Replace mocked provider behavior in the callback suite with a real workers-oauth-provider instance so client, grant, and redirect validation follow the library contract.

Add worker-level /mcp coverage that authenticates through the mounted OAuth flow before issuing MCP requests. This keeps the tests closer to production behavior while still mocking the upstream Sentry boundary.

Co-Authored-By: Codex <noreply@openai.com>
Bump the shared vitest-pool-workers catalog to 0.9.14 so the lockfile no longer pulls the vulnerable wrangler 4.35.0 dependency flagged by dependency-review.

Clamp the Cloudflare test package to a single Vitest worker to avoid the workerd loopback runtime failures that showed up after the upgrade while keeping the existing test coverage intact.

Co-Authored-By: Codex GPT-5 <codex@openai.com>
Raise the workspace and Cloudflare package wrangler range to a release that includes the GHSA-36p8-mvp6-cv38 fix so dependency-review no longer flags the lockfile.

Keep the previously-stabilized vitest worker settings intact and refresh the lockfile against the safe wrangler line.

Co-Authored-By: Codex GPT-5 <codex@openai.com>
Pin vitest pool workers and wrangler to exact patched versions so the
Cloudflare test stack resolves away from vulnerable transitive packages.
Keep the lockfile on a single reviewed dependency set instead of letting
caret ranges drift into dependency-review failures.

Co-Authored-By: OpenAI Codex <noreply@openai.com>
Clear the inherited server exclude list so the test tsconfig
actually resolves its declared test inputs. This keeps the config
usable for direct typechecking instead of resolving to no files.

Co-Authored-By: OpenAI Codex <noreply@openai.com>
Restore the worker runtime fetch shim after OAuth helper tests so the new\nvitest-pool-workers suite does not leak a file-local fetch mock into other\nCloudflare tests.\n\nRegister the shared cloudflare:test fetchMock interceptors only once and\nreactivate them per test. This keeps the suite from stacking duplicate\npersistent interceptors as it runs under the worker pool.\n\nCo-Authored-By: Codex <noreply@openai.com>
Restore the OAuth helper fetch stub for the whole test file so both token refresh paths run against the intended mock in the worker runtime.

Move the shared secondary issue fixture into the mocks payload exports so the Cloudflare fetch mock setup and the MSW server use the same data instead of drifting.

Co-Authored-By: Codex <noreply@openai.com>
Re-export flamegraphFixture from both the main mocks entrypoint and the
payload-only module so consumers keep the same fixture surface after the
refactor.

Add a regression test that imports from @sentry/mcp-server-mocks to catch
future export drift at the package boundary.

Co-Authored-By: Codex GPT-5 <codex@openai.com>
Keep the MSW-free payloads entrypoint aligned with the main mocks package exports so fixture helpers are available in worker-safe consumers.

Add regression coverage for the payloads subpath to catch future export drift.

Co-Authored-By: Codex GPT-5 <codex@openai.com>
Document why the internal use_sentry server does not currently thread a JSON schema validator and call out the Cloudflare follow-up needed if elicitation is added later.

Co-Authored-By: Codex GPT-5 <codex@openai.com>
Tighten the worker fetchMock handler for the errors dataset so it validates fields and sort the same way as the shared MSW mocks and normalizes query allowlist matching.

Add a focused regression test for the worker fetch mock contract. Local workerd Vitest execution is currently failing in this environment before test output is surfaced, so verification here is limited to biome checks on the changed files.

Co-Authored-By: Codex GPT-5 <codex@openai.com>
@dcramer
Copy link
Copy Markdown
Member Author

dcramer commented Mar 19, 2026

yolo i need reliability and i think this is the path

@dcramer dcramer merged commit 007924a into main Mar 19, 2026
16 checks passed
@dcramer dcramer deleted the vitest-pool-workers branch March 19, 2026 22:47
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