The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Cloudflare from "@alchemy.run/cloudflare"; |
| 2 | import { expect } from "@alchemy.run/test"; |
| 3 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import * as HttpClient from "effect/http/HttpClient"; |
| 6 | import { MinimumLogLevel } from "effect/References"; |
| 7 | import * as Schedule from "effect/Schedule"; |
| 8 | import Stack from "./fixtures/bindings-stack.ts"; |
| 9 | |
| 10 | const { test, beforeAll, afterAll, deploy, destroy } = Test.make({ |
| 11 | providers: Cloudflare.providers(), |
| 12 | }); |
| 13 | |
| 14 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 15 | |
| 16 | const stack = beforeAll(deploy(Stack), { timeout: 120_000 }); |
| 17 | afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack), { timeout: 120_000 }); |
| 18 | |
| 19 | // Deploying the Worker succeeding at all proves Cloudflare accepted both the |
| 20 | // `ai_search` and `ai_search_namespace` bindings. The `/bindings` route then |
| 21 | // confirms they are injected and shaped correctly at runtime. |
| 22 | test( |
| 23 | "worker deploys with ai_search + ai_search_namespace bindings injected", |
| 24 | Effect.gen(function* () { |
| 25 | const { url } = yield* stack; |
| 26 | expect(url).toBeTypeOf("string"); |
| 27 | |
| 28 | const client = yield* HttpClient.HttpClient; |
| 29 | const res = yield* client.get(`${url}/bindings`).pipe( |
| 30 | Effect.flatMap((res) => |
| 31 | res.status === 200 |
| 32 | ? Effect.succeed(res) |
| 33 | : Effect.fail(new Error(`Worker not ready: ${res.status}`)), |
| 34 | ), |
| 35 | // Cap the backoff at 3s so a fresh worker that takes a while to start |
| 36 | // serving 200s keeps getting polled steadily, rather than the |
| 37 | // unbounded exponential delay overshooting the test timeout. |
| 38 | Effect.retry({ |
| 39 | schedule: Schedule.min([Schedule.exponential("500 millis"), Schedule.spaced("3 seconds")]), |
| 40 | times: 40, |
| 41 | }), |
| 42 | ); |
| 43 | |
| 44 | const body = (yield* res.json) as { |
| 45 | search: string; |
| 46 | searchChatCompletions: string; |
| 47 | searchSearch: string; |
| 48 | ns: string; |
| 49 | nsGet: string; |
| 50 | }; |
| 51 | |
| 52 | // Single-instance `ai_search` binding resolves to an `AiSearchInstance` |
| 53 | // object exposing `search()` / `chatCompletions()`. |
| 54 | expect(body.search).toBe("object"); |
| 55 | expect(body.searchChatCompletions).toBe("function"); |
| 56 | expect(body.searchSearch).toBe("function"); |
| 57 | // `ai_search_namespace` binding resolves to an `AiSearchNamespace` with |
| 58 | // `.get()`. |
| 59 | expect(body.ns).toBe("object"); |
| 60 | expect(body.nsGet).toBe("function"); |
| 61 | }).pipe(logLevel), |
| 62 | { |
| 63 | tags: [ |
| 64 | "provider:cloudflare", |
| 65 | "provider:cloudflare:ai", |
| 66 | "provider:cloudflare:r2", |
| 67 | "provider:cloudflare:worker", |
| 68 | "live", |
| 69 | ], |
| 70 | timeout: 240_000, |
| 71 | }, |
| 72 | ); |
| 73 | |
| 74 | // The Effect worker attaches the same two binding flavors via |
| 75 | // `Cloudflare.AI.Search.Search(...)` / `Cloudflare.AI.SearchNamespace(...)` |
| 76 | // and reads them through the Effect-native client. Resolving each client's |
| 77 | // `raw` runtime handle proves the Effect-first path wires through to the |
| 78 | // live runtime bindings. |
| 79 | test( |
| 80 | "effect worker resolves ai_search + ai_search_namespace via Effect clients", |
| 81 | Effect.gen(function* () { |
| 82 | const { effectUrl } = yield* stack; |
| 83 | expect(effectUrl).toBeTypeOf("string"); |
| 84 | |
| 85 | const client = yield* HttpClient.HttpClient; |
| 86 | const res = yield* client.get(`${effectUrl}/bindings`).pipe( |
| 87 | Effect.flatMap((res) => |
| 88 | res.status === 200 |
| 89 | ? Effect.succeed(res) |
| 90 | : Effect.fail(new Error(`Worker not ready: ${res.status}`)), |
| 91 | ), |
| 92 | // Cap the backoff at 3s so a fresh worker that takes a while to start |
| 93 | // serving 200s keeps getting polled steadily, rather than the |
| 94 | // unbounded exponential delay overshooting the test timeout. |
| 95 | Effect.retry({ |
| 96 | schedule: Schedule.min([Schedule.exponential("500 millis"), Schedule.spaced("3 seconds")]), |
| 97 | times: 40, |
| 98 | }), |
| 99 | ); |
| 100 | |
| 101 | const body = (yield* res.json) as { |
| 102 | mode: string; |
| 103 | searchRaw: string; |
| 104 | searchChatCompletions: string; |
| 105 | searchSearch: string; |
| 106 | nsRaw: string; |
| 107 | nsChatCompletions: string; |
| 108 | }; |
| 109 | |
| 110 | expect(body.mode).toBe("effect"); |
| 111 | // `Cloudflare.AI.Search.Search(...).raw` resolves to the runtime |
| 112 | // `AiSearchInstance` exposing `search()` / `chatCompletions()`. |
| 113 | expect(body.searchRaw).toBe("object"); |
| 114 | expect(body.searchChatCompletions).toBe("function"); |
| 115 | expect(body.searchSearch).toBe("function"); |
| 116 | // `Cloudflare.AI.SearchNamespace(...).raw` resolves to the runtime |
| 117 | // `AiSearchNamespace`; `.get(name)` scopes to an instance exposing |
| 118 | // `chatCompletions()`. The namespace handle may be a callable runtime |
| 119 | // proxy (`typeof` `"function"`) or an object. |
| 120 | expect(["object", "function"]).toContain(body.nsRaw); |
| 121 | expect(body.nsChatCompletions).toBe("function"); |
| 122 | }).pipe(logLevel), |
| 123 | { |
| 124 | tags: [ |
| 125 | "provider:cloudflare", |
| 126 | "provider:cloudflare:ai", |
| 127 | "provider:cloudflare:r2", |
| 128 | "provider:cloudflare:worker", |
| 129 | "live", |
| 130 | ], |
| 131 | timeout: 240_000, |
| 132 | }, |
| 133 | ); |