-
Notifications
You must be signed in to change notification settings - Fork 3
Add unit tests for Cloudflare Worker (Phase 0) #212
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
62cb1ab
Add unit tests for Cloudflare Worker (Phase 0)
claude 80ccf53
Address review feedback: add missing CORS and Vary assertions
claude 7530ee3
Add mise task for Cloudflare Worker tests
claude c127846
Merge main: resolve wrangler version conflict
claude 569cbe5
Initial plan
Copilot 2506070
Apply review feedback: use npm ci and remove redundant festivals.json…
Copilot e1952a8
Merge pull request #213 from richardthe3rd/copilot/sub-pr-212
richardthe3rd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,185 @@ | ||
| import { describe, it, expect, beforeEach, afterEach } from 'vitest'; | ||
| import { env, createExecutionContext, waitOnExecutionContext, fetchMock } from 'cloudflare:test'; | ||
| import worker from '../worker.js'; | ||
|
|
||
| const UPSTREAM = 'https://data.cambridgebeerfestival.com'; | ||
|
|
||
| /** | ||
| * Helper to make a request to the worker. | ||
| */ | ||
| async function fetchWorker(path, origin = 'https://cambeerfestival.app') { | ||
| const request = new Request(`https://worker.example.com${path}`, { | ||
| headers: { Origin: origin }, | ||
| }); | ||
| const ctx = createExecutionContext(); | ||
| const response = await worker.fetch(request, env, ctx); | ||
| await waitOnExecutionContext(ctx); | ||
| return response; | ||
| } | ||
|
|
||
| /** | ||
| * Sample Apache-style directory listing HTML. | ||
| */ | ||
| function makeDirectoryHtml(files) { | ||
| const links = files.map((f) => `<a href="${f}">${f}</a>`).join('\n'); | ||
| return ` | ||
| <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> | ||
| <html><head><title>Index of /cbf2025</title></head> | ||
| <body><h1>Index of /cbf2025</h1> | ||
| <pre>Name Last modified Size Description | ||
| <hr> | ||
| <a href="/">Parent Directory</a> - | ||
| ${links} | ||
| <hr></pre></body></html>`; | ||
| } | ||
|
|
||
| describe('available_beverage_types endpoint', () => { | ||
| beforeEach(() => { | ||
| fetchMock.activate(); | ||
| fetchMock.disableNetConnect(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| fetchMock.deactivate(); | ||
| }); | ||
|
|
||
| it('parses directory listing into beverage types', async () => { | ||
| fetchMock.get(UPSTREAM) | ||
| .intercept({ path: '/cbf2025/' }) | ||
| .reply(200, makeDirectoryHtml([ | ||
| 'beer.json', 'cider.json', 'perry.json', 'mead.json', | ||
| ])); | ||
|
|
||
| const response = await fetchWorker('/cbf2025/available_beverage_types.json'); | ||
| expect(response.status).toBe(200); | ||
|
|
||
| const data = await response.json(); | ||
| expect(data.festival_id).toBe('cbf2025'); | ||
| expect(data.available_beverage_types).toEqual(['beer', 'cider', 'mead', 'perry']); | ||
| }); | ||
|
|
||
| it('returns types sorted alphabetically', async () => { | ||
| fetchMock.get(UPSTREAM) | ||
| .intercept({ path: '/cbf2025/' }) | ||
| .reply(200, makeDirectoryHtml([ | ||
| 'wine.json', 'beer.json', 'apple-juice.json', | ||
| ])); | ||
|
|
||
| const response = await fetchWorker('/cbf2025/available_beverage_types.json'); | ||
| const data = await response.json(); | ||
| expect(data.available_beverage_types).toEqual(['apple-juice', 'beer', 'wine']); | ||
| }); | ||
|
|
||
| it('filters out available_beverage_types.json from results', async () => { | ||
| fetchMock.get(UPSTREAM) | ||
| .intercept({ path: '/cbf2025/' }) | ||
| .reply(200, makeDirectoryHtml([ | ||
| 'beer.json', 'available_beverage_types.json', 'cider.json', | ||
| ])); | ||
|
|
||
| const response = await fetchWorker('/cbf2025/available_beverage_types.json'); | ||
| const data = await response.json(); | ||
| expect(data.available_beverage_types).toEqual(['beer', 'cider']); | ||
| expect(data.available_beverage_types).not.toContain('available_beverage_types'); | ||
| }); | ||
|
|
||
| it('returns empty array when no JSON files found', async () => { | ||
| fetchMock.get(UPSTREAM) | ||
| .intercept({ path: '/cbf2025/' }) | ||
| .reply(200, makeDirectoryHtml([])); | ||
|
|
||
| const response = await fetchWorker('/cbf2025/available_beverage_types.json'); | ||
| const data = await response.json(); | ||
| expect(data.available_beverage_types).toEqual([]); | ||
| }); | ||
|
|
||
| it('returns 404 when festival not found upstream', async () => { | ||
| fetchMock.get(UPSTREAM) | ||
| .intercept({ path: '/nonexistent/' }) | ||
| .reply(404, 'Not Found'); | ||
|
|
||
| const response = await fetchWorker('/nonexistent/available_beverage_types.json'); | ||
| expect(response.status).toBe(404); | ||
|
|
||
| const data = await response.json(); | ||
| expect(data.error).toBe('Festival not found'); | ||
| expect(data.festival_id).toBe('nonexistent'); | ||
| }); | ||
|
|
||
| it('returns 500 when upstream fetch fails', async () => { | ||
| fetchMock.get(UPSTREAM) | ||
| .intercept({ path: '/cbf2025/' }) | ||
| .replyWithError(new Error('Connection refused')); | ||
|
|
||
| const response = await fetchWorker('/cbf2025/available_beverage_types.json'); | ||
| expect(response.status).toBe(500); | ||
|
|
||
| const data = await response.json(); | ||
| expect(data.error).toBe('Failed to fetch beverage types'); | ||
| }); | ||
|
|
||
| it('includes CORS headers on 500 error', async () => { | ||
| fetchMock.get(UPSTREAM) | ||
| .intercept({ path: '/cbf2025/' }) | ||
| .replyWithError(new Error('Connection refused')); | ||
|
|
||
| const response = await fetchWorker('/cbf2025/available_beverage_types.json'); | ||
| expect(response.headers.get('Access-Control-Allow-Origin')) | ||
| .toBe('https://cambeerfestival.app'); | ||
| }); | ||
|
|
||
| it('includes CORS headers on success', async () => { | ||
| fetchMock.get(UPSTREAM) | ||
| .intercept({ path: '/cbf2025/' }) | ||
| .reply(200, makeDirectoryHtml(['beer.json'])); | ||
|
|
||
| const response = await fetchWorker('/cbf2025/available_beverage_types.json'); | ||
| expect(response.headers.get('Access-Control-Allow-Origin')) | ||
| .toBe('https://cambeerfestival.app'); | ||
| }); | ||
|
|
||
| it('includes CORS headers on 404', async () => { | ||
| fetchMock.get(UPSTREAM) | ||
| .intercept({ path: '/nonexistent/' }) | ||
| .reply(404, 'Not Found'); | ||
|
|
||
| const response = await fetchWorker('/nonexistent/available_beverage_types.json'); | ||
| expect(response.headers.get('Access-Control-Allow-Origin')) | ||
| .toBe('https://cambeerfestival.app'); | ||
| }); | ||
|
|
||
| it('sets Cache-Control to 1 hour on success', async () => { | ||
| fetchMock.get(UPSTREAM) | ||
| .intercept({ path: '/cbf2025/' }) | ||
| .reply(200, makeDirectoryHtml(['beer.json'])); | ||
|
|
||
| const response = await fetchWorker('/cbf2025/available_beverage_types.json'); | ||
| expect(response.headers.get('Cache-Control')).toBe('public, max-age=3600'); | ||
| }); | ||
|
|
||
| it('includes timestamp in response', async () => { | ||
| fetchMock.get(UPSTREAM) | ||
| .intercept({ path: '/cbf2025/' }) | ||
| .reply(200, makeDirectoryHtml(['beer.json'])); | ||
|
|
||
| const response = await fetchWorker('/cbf2025/available_beverage_types.json'); | ||
| const data = await response.json(); | ||
| expect(data.timestamp).toBeDefined(); | ||
| // Verify it's a valid ISO date | ||
| expect(new Date(data.timestamp).toISOString()).toBe(data.timestamp); | ||
| }); | ||
|
|
||
| it('handles hyphenated beverage type names', async () => { | ||
| fetchMock.get(UPSTREAM) | ||
| .intercept({ path: '/cbf2025/' }) | ||
| .reply(200, makeDirectoryHtml([ | ||
| 'international-beer.json', 'low-no.json', 'apple-juice.json', | ||
| ])); | ||
|
|
||
| const response = await fetchWorker('/cbf2025/available_beverage_types.json'); | ||
| const data = await response.json(); | ||
| expect(data.available_beverage_types).toEqual([ | ||
| 'apple-juice', 'international-beer', 'low-no', | ||
| ]); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In
test-worker, the explicitcp data/festivals.json cloudflare-worker/festivals.jsonis redundant becausenpm testwill run the worker’spretestscript, which already copies../data/festivals.jsoninto./festivals.json. Consider removing one of these copies (e.g., drop the workflow step fortest-worker) to avoid duplication and keep the source of truth in one place.