Add unit tests for Cloudflare Worker (Phase 0) - #212
Conversation
Set up Vitest + @cloudflare/vitest-pool-workers test infrastructure for the existing data proxy worker. This establishes the Worker testing pattern before building the ratings Worker and catches regressions in security-relevant CORS logic. Test coverage (55 tests across 4 files): - cors.test.js: Origin matching for all allowed origins (production, staging, Pages previews, localhost, tunnel) and rejection of unknown origins. Preflight handling with correct max-age (300s production, 10s staging/dev). - festivals.test.js: Embedded festivals.json serving, cache headers, content type, CORS headers, and /festivals alias. - beverage-types.test.js: HTML directory listing parsing, alphabetical sorting, filtering of self-referential files, 404/500 error handling, cache headers. - proxy.test.js: Health check endpoint, upstream proxying with charset enforcement, error handling (502), CORS headers on all responses. CI integration: - Add test-worker job to deploy-worker.yml that runs on worker or festivals changes - Both validate-worker and deploy-worker now depend on test-worker passing https://claude.ai/code/session_01Gc9AXJisQdxqBwg73A447L
- Add test for CORS headers on beverage-types 500 error response (ensures browsers can read error details cross-origin) - Add Vary header assertion on rejected origins in CORS tests (verifies no cache-related headers leak for unknown origins) https://claude.ai/code/session_01Gc9AXJisQdxqBwg73A447L
- test:worker: Install deps and run worker tests (npm install && npm test) - Uses sources for caching: skips when worker code/tests/deps unchanged https://claude.ai/code/session_01Gc9AXJisQdxqBwg73A447L
Take wrangler ^4.59.1 from main alongside test dependencies. https://claude.ai/code/session_01Gc9AXJisQdxqBwg73A447L
There was a problem hiding this comment.
Pull request overview
Adds a Vitest-based unit test harness for the existing Cloudflare data-proxy Worker, and wires it into CI so CORS/proxy behavior regressions are caught early.
Changes:
- Add Vitest +
@cloudflare/vitest-pool-workersconfiguration for running Worker tests underworkerd. - Introduce a test suite covering CORS, festivals registry serving, beverage type discovery parsing, and upstream proxy behavior.
- Add a
test-workerCI job and make worker validation/deploy depend on it; add amisetask to run Worker tests locally.
Reviewed changes
Copilot reviewed 8 out of 9 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
mise.toml |
Adds a test:worker task for running Worker tests locally. |
cloudflare-worker/vitest.config.js |
Configures Vitest to run Worker tests via @cloudflare/vitest-pool-workers. |
cloudflare-worker/test/cors.test.js |
Tests allowed/rejected origins and preflight max-age behavior. |
cloudflare-worker/test/festivals.test.js |
Tests embedded festivals.json serving, headers, and /festivals alias. |
cloudflare-worker/test/beverage-types.test.js |
Tests directory listing parsing into beverage types, sorting, filtering, and error paths. |
cloudflare-worker/test/proxy.test.js |
Tests /health, upstream proxying behavior, charset enforcement, and error handling. |
cloudflare-worker/package.json |
Adds Worker test scripts and dev dependencies for Vitest + workers pool. |
cloudflare-worker/package-lock.json |
Locks new test dependencies. |
.github/workflows/deploy-worker.yml |
Adds test-worker job and makes validate/deploy depend on tests passing. |
| description = "Run Cloudflare Worker tests (Vitest + workerd)" | ||
| dir = "cloudflare-worker" | ||
| sources = ['cloudflare-worker/package.json', 'cloudflare-worker/package-lock.json', 'cloudflare-worker/worker.js', 'cloudflare-worker/test/**/*.js', 'cloudflare-worker/vitest.config.js', 'data/festivals.json'] | ||
| run = 'npm install && npm test' |
There was a problem hiding this comment.
The mise test:worker task runs npm install, which is non-deterministic and can update package-lock.json during the task run. Since the worker has a lockfile and CI uses npm ci, prefer npm ci here as well to keep local runs reproducible and avoid dirty working trees.
| run = 'npm install && npm test' | |
| run = 'npm ci && npm test' |
| - name: Copy festivals.json to worker directory | ||
| run: cp data/festivals.json cloudflare-worker/festivals.json | ||
|
|
There was a problem hiding this comment.
In test-worker, the explicit cp data/festivals.json cloudflare-worker/festivals.json is redundant because npm test will run the worker’s pretest script, which already copies ../data/festivals.json into ./festivals.json. Consider removing one of these copies (e.g., drop the workflow step for test-worker) to avoid duplication and keep the source of truth in one place.
| - name: Copy festivals.json to worker directory | |
| run: cp data/festivals.json cloudflare-worker/festivals.json |
|
@copilot open a new pull request to apply changes based on the comments in this thread |
|
@richardthe3rd I've opened a new pull request, #213, to work on those changes. Once the pull request is ready, I'll request review from you. |
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
… copy Co-authored-by: richardthe3rd <573334+richardthe3rd@users.noreply.github.com>
🚀 Cloudflare Pages PreviewYour preview deployment is ready! Preview URL: https://claude-cloudflare-workers-te.staging-cambeerfestival.pages.dev This preview will be automatically updated when you push new commits to this PR. |
Apply review feedback: use npm ci and remove redundant festivals.json copy
🚀 Cloudflare Pages PreviewYour preview deployment is ready! Preview URL: https://claude-cloudflare-workers-te.staging-cambeerfestival.pages.dev This preview will be automatically updated when you push new commits to this PR. |
Set up Vitest + @cloudflare/vitest-pool-workers test infrastructure for the
existing data proxy worker. This establishes the Worker testing pattern before
building the ratings Worker and catches regressions in security-relevant CORS
logic.
Test coverage (55 tests across 4 files):
Pages previews, localhost, tunnel) and rejection of unknown origins. Preflight
handling with correct max-age (300s production, 10s staging/dev).
type, CORS headers, and /festivals alias.
filtering of self-referential files, 404/500 error handling, cache headers.
enforcement, error handling (502), CORS headers on all responses.
CI integration:
changes
https://claude.ai/code/session_01Gc9AXJisQdxqBwg73A447L