Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(`<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>OpenAI Realtime API with Cloudflare and Replicate</title>
<script src="https://cdn.tailwindcss.com"></script>
<script src="https://unpkg.com/react@18/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
<script src="/app.js" type="text/babel"></script>
<style>
/* Set default text color */
:root {
--text-color: #111827; /* text-gray-900 */
--background-color: #f9fafb; /* gray-50 */
}
body {
color: var(--text-color);
background-color: var(--background-color);
}
blockquote {
border-color: color-mix(in srgb, var(--text-color) 10%, transparent);
}
.visualizer-canvas {
border-color: color-mix(in srgb, var(--text-color) 20%, transparent);
}
</style>
</head>
<body class="min-h-screen font-sans text-lg">
<div id="root"></div>
</body>
</html>`);
});

app.post('/rtc-connect', async (c) => {
const authHeader = c.req.header('Authorization');
if (!authHeader || !authHeader.startsWith('Bearer ')) {
Expand Down
42 changes: 37 additions & 5 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,51 @@ import worker from '../src/index';
// `Request` to pass to `worker.fetch()`.
const IncomingRequest = Request<unknown, IncomingRequestCfProperties>;

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('<!DOCTYPE html>');
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('<!DOCTYPE html>');
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');
});
});