From db9f76cdf2e6334bd7d2b633aca1ce0cadbfd8b1 Mon Sep 17 00:00:00 2001 From: Zeke Sikelianos Date: Thu, 3 Jul 2025 11:40:44 -0700 Subject: [PATCH] Fix test suite to match actual worker behavior MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Update tests to check for HTML content instead of "Hello World\!" - Add root path handler to serve HTML page in worker - Add tests for API endpoints to verify authentication errors - All 4 tests now pass successfully 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- src/index.ts | 37 +++++++++++++++++++++++++++++++++++++ test/index.spec.ts | 42 +++++++++++++++++++++++++++++++++++++----- 2 files changed, 74 insertions(+), 5 deletions(-) diff --git a/src/index.ts b/src/index.ts index 616127a..9731221 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,6 +5,43 @@ const app = new Hono<{ Bindings: Env }>(); const DEFAULT_INSTRUCTIONS = `You are helpful and have some tools installed.`; +// Serve the HTML page for the root path +app.get('/', (c) => { + return c.html(` + + + + + OpenAI Realtime API with Cloudflare and Replicate + + + + + + + + +
+ +`); +}); + app.post('/rtc-connect', async (c) => { const authHeader = c.req.header('Authorization'); if (!authHeader || !authHeader.startsWith('Bearer ')) { diff --git a/test/index.spec.ts b/test/index.spec.ts index fbee335..71b6692 100644 --- a/test/index.spec.ts +++ b/test/index.spec.ts @@ -7,19 +7,51 @@ import worker from '../src/index'; // `Request` to pass to `worker.fetch()`. const IncomingRequest = Request; -describe('Hello World worker', () => { - it('responds with Hello World! (unit style)', async () => { +describe('OpenAI Realtime API Worker', () => { + it('serves the HTML page for root path (unit style)', async () => { const request = new IncomingRequest('http://example.com'); // Create an empty context to pass to `worker.fetch()`. const ctx = createExecutionContext(); const response = await worker.fetch(request, env, ctx); // Wait for all `Promise`s passed to `ctx.waitUntil()` to settle before running test assertions await waitOnExecutionContext(ctx); - expect(await response.text()).toMatchInlineSnapshot(`"Hello World!"`); + const text = await response.text(); + expect(text).toContain(''); + expect(text).toContain('OpenAI Realtime API with Cloudflare and Replicate'); + expect(response.status).toBe(200); }); - it('responds with Hello World! (integration style)', async () => { + it('serves the HTML page for root path (integration style)', async () => { const response = await SELF.fetch('https://example.com'); - expect(await response.text()).toMatchInlineSnapshot(`"Hello World!"`); + const text = await response.text(); + expect(text).toContain(''); + expect(text).toContain('OpenAI Realtime API with Cloudflare and Replicate'); + expect(response.status).toBe(200); + }); + + it('returns 401 for rtc-connect without auth header', async () => { + const request = new IncomingRequest('http://example.com/rtc-connect', { + method: 'POST', + body: 'test body' + }); + const ctx = createExecutionContext(); + const response = await worker.fetch(request, env, ctx); + await waitOnExecutionContext(ctx); + expect(response.status).toBe(401); + const json = await response.json(); + expect(json.error).toBe('Missing OpenAI API key in Authorization header'); + }); + + it('returns 401 for generate-image without auth header', async () => { + const request = new IncomingRequest('http://example.com/generate-image', { + method: 'POST', + body: 'test prompt' + }); + const ctx = createExecutionContext(); + const response = await worker.fetch(request, env, ctx); + await waitOnExecutionContext(ctx); + expect(response.status).toBe(401); + const json = await response.json(); + expect(json.error).toBe('Missing Replicate API token in Authorization header'); }); });