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 Alchemy from "alchemy"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import * as HttpClient from "effect/http/HttpClient"; |
| 7 | import { MinimumLogLevel } from "effect/References"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 9 | import SendEmailWorker from "./fixtures/worker.ts"; |
| 10 | |
| 11 | const { test, beforeAll, afterAll, deploy, destroy } = Test.make({ |
| 12 | providers: Cloudflare.providers(), |
| 13 | }); |
| 14 | |
| 15 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 16 | |
| 17 | const FROM = process.env.CLOUDFLARE_TEST_EMAIL_FROM; |
| 18 | const TO = process.env.CLOUDFLARE_TEST_EMAIL_TO; |
| 19 | const skip = !FROM || !TO; |
| 20 | |
| 21 | const Stack = Alchemy.Stack( |
| 22 | "SendEmailBindingStack", |
| 23 | { |
| 24 | providers: Cloudflare.providers(), |
| 25 | state: Cloudflare.state(), |
| 26 | }, |
| 27 | Effect.gen(function* () { |
| 28 | const worker = yield* SendEmailWorker; |
| 29 | return { url: worker.url.as<string>() }; |
| 30 | }), |
| 31 | ); |
| 32 | |
| 33 | const stack = beforeAll(deploy(Stack)); |
| 34 | afterAll.skipIf(!!process.env.NO_DESTROY || skip)(destroy(Stack)); |
| 35 | |
| 36 | // The send_email binding only delivers from a domain with Email Routing |
| 37 | // enabled to a verified destination address — supply such a pair via |
| 38 | // CLOUDFLARE_TEST_EMAIL_FROM / CLOUDFLARE_TEST_EMAIL_TO to run this test. |
| 39 | test.skipIf(skip)( |
| 40 | "sends an email through the Worker send_email binding", |
| 41 | Effect.gen(function* () { |
| 42 | const { url } = yield* stack; |
| 43 | const client = yield* HttpClient.HttpClient; |
| 44 | |
| 45 | const sendUrl = `${url}/send?from=${encodeURIComponent(FROM!)}&to=${encodeURIComponent(TO!)}`; |
| 46 | const res = yield* client.get(sendUrl).pipe( |
| 47 | Effect.flatMap((res) => (res.status === 200 ? Effect.succeed(res) : Effect.fail(res))), |
| 48 | Effect.retry({ |
| 49 | schedule: Schedule.exponential("500 millis"), |
| 50 | times: 10, |
| 51 | }), |
| 52 | ); |
| 53 | const body = (yield* res.json) as { ok: boolean; message?: string }; |
| 54 | if (!body.ok) { |
| 55 | throw new Error(`send_email failed: ${body.message}`); |
| 56 | } |
| 57 | expect(body.ok).toBe(true); |
| 58 | }).pipe(logLevel), |
| 59 | { |
| 60 | tags: [ |
| 61 | "provider:cloudflare", |
| 62 | "provider:cloudflare:email", |
| 63 | "provider:cloudflare:worker", |
| 64 | "live", |
| 65 | ], |
| 66 | timeout: 180_000, |
| 67 | }, |
| 68 | ); |