|
1 | 1 | import assert from "node:assert"; |
2 | 2 | import test, { describe } from "node:test"; |
| 3 | +import { Miniflare } from "miniflare"; |
3 | 4 | import { experimental_startMixedModeSession } from "wrangler"; |
4 | 5 |
|
5 | | -describe("startMixedModeSession", () => { |
6 | | - test("no-op mixed-mode proxyServerWorker", async (t) => { |
7 | | - if ( |
8 | | - !process.env.CLOUDFLARE_ACCOUNT_ID || |
9 | | - !process.env.CLOUDFLARE_API_TOKEN |
10 | | - ) { |
11 | | - return t.skip(); |
12 | | - } |
| 6 | +process.env.CLOUDFLARE_ACCOUNT_ID = process.env.TEST_CLOUDFLARE_ACCOUNT_ID; |
| 7 | +process.env.CLOUDFLARE_API_TOKEN = process.env.TEST_CLOUDFLARE_API_TOKEN; |
13 | 8 |
|
14 | | - const mixedModeSession = await experimental_startMixedModeSession({}); |
| 9 | +describe("startMixedModeSession", () => { |
| 10 | + test("simple AI request to the proxyServerWorker", async () => { |
| 11 | + const mixedModeSession = await experimental_startMixedModeSession({ |
| 12 | + AI: { |
| 13 | + type: "ai", |
| 14 | + }, |
| 15 | + }); |
15 | 16 | const proxyServerUrl = |
16 | 17 | mixedModeSession.mixedModeConnectionString.toString(); |
17 | 18 | assert.match(proxyServerUrl, /http:\/\/localhost:\d{4,5}\//); |
18 | | - assert.strictEqual( |
19 | | - await (await fetch(proxyServerUrl)).text(), |
20 | | - "no-op mixed-mode proxyServerWorker" |
| 19 | + assert.match( |
| 20 | + await ( |
| 21 | + await fetch(proxyServerUrl, { |
| 22 | + headers: { |
| 23 | + "MF-Binding": "AI", |
| 24 | + "MF-URL": "https://workers-binding.ai/ai-api/models/search", |
| 25 | + }, |
| 26 | + }) |
| 27 | + ).text(), |
| 28 | + // Assert the catalog _at least_ contains a LLama model |
| 29 | + /Llama/ |
| 30 | + ); |
| 31 | + await mixedModeSession.ready; |
| 32 | + await mixedModeSession.dispose(); |
| 33 | + }); |
| 34 | + test("AI mixed mode binding", async () => { |
| 35 | + const mixedModeSession = await experimental_startMixedModeSession({ |
| 36 | + AI: { |
| 37 | + type: "ai", |
| 38 | + }, |
| 39 | + }); |
| 40 | + |
| 41 | + const mf = new Miniflare({ |
| 42 | + compatibilityDate: "2025-01-01", |
| 43 | + modules: true, |
| 44 | + script: /* javascript */ ` |
| 45 | + export default { |
| 46 | + async fetch(request, env) { |
| 47 | + const messages = [ |
| 48 | + { |
| 49 | + role: "user", |
| 50 | + // Doing snapshot testing against AI responses can be flaky, but this prompt generates the same output relatively reliably |
| 51 | + content: "Respond with the exact text 'This is a response from Workers AI.'. Do not include any other text", |
| 52 | + }, |
| 53 | + ]; |
| 54 | +
|
| 55 | + const content = await env.AI.run("@hf/thebloke/zephyr-7b-beta-awq", { |
| 56 | + messages, |
| 57 | + }); |
| 58 | +
|
| 59 | + return new Response(content.response); |
| 60 | + } |
| 61 | + } |
| 62 | + `, |
| 63 | + ai: { |
| 64 | + binding: "AI", |
| 65 | + mixedModeConnectionString: mixedModeSession.mixedModeConnectionString, |
| 66 | + }, |
| 67 | + }); |
| 68 | + assert.match( |
| 69 | + await (await mf.dispatchFetch("http://example.com")).text(), |
| 70 | + /This is a response from Workers AI/ |
21 | 71 | ); |
| 72 | + await mf.dispose(); |
| 73 | + |
22 | 74 | await mixedModeSession.ready; |
23 | 75 | await mixedModeSession.dispose(); |
24 | 76 | }); |
|
0 commit comments