sailfork / t3code-github / .repos/ alchemy-effect/ packages/ integration-tests/ test/ Cloudflare/ Email/ SendEmail.test.ts

import * as Cloudflare from "@alchemy.run/cloudflare";
import { expect } from "@alchemy.run/test";
import * as Test from "@alchemy.run/test/Alchemy";
import * as Alchemy from "alchemy";
import * as Effect from "effect/Effect";
import * as HttpClient from "effect/http/HttpClient";
import { MinimumLogLevel } from "effect/References";
import * as Schedule from "effect/Schedule";
import SendEmailWorker from "./fixtures/worker.ts";

const { test, beforeAll, afterAll, deploy, destroy } = Test.make({
  providers: Cloudflare.providers(),
});

const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");

const FROM = process.env.CLOUDFLARE_TEST_EMAIL_FROM;
const TO = process.env.CLOUDFLARE_TEST_EMAIL_TO;
const skip = !FROM || !TO;

const Stack = Alchemy.Stack(
  "SendEmailBindingStack",
  {
    providers: Cloudflare.providers(),
    state: Cloudflare.state(),
  },
  Effect.gen(function* () {
    const worker = yield* SendEmailWorker;
    return { url: worker.url.as<string>() };
  }),
);

const stack = beforeAll(deploy(Stack));
afterAll.skipIf(!!process.env.NO_DESTROY || skip)(destroy(Stack));

// The send_email binding only delivers from a domain with Email Routing
// enabled to a verified destination address — supply such a pair via
// CLOUDFLARE_TEST_EMAIL_FROM / CLOUDFLARE_TEST_EMAIL_TO to run this test.
test.skipIf(skip)(
  "sends an email through the Worker send_email binding",
  Effect.gen(function* () {
    const { url } = yield* stack;
    const client = yield* HttpClient.HttpClient;

    const sendUrl = `${url}/send?from=${encodeURIComponent(FROM!)}&to=${encodeURIComponent(TO!)}`;
    const res = yield* client.get(sendUrl).pipe(
      Effect.flatMap((res) => (res.status === 200 ? Effect.succeed(res) : Effect.fail(res))),
      Effect.retry({
        schedule: Schedule.exponential("500 millis"),
        times: 10,
      }),
    );
    const body = (yield* res.json) as { ok: boolean; message?: string };
    if (!body.ok) {
      throw new Error(`send_email failed: ${body.message}`);
    }
    expect(body.ok).toBe(true);
  }).pipe(logLevel),
  {
    tags: [
      "provider:cloudflare",
      "provider:cloudflare:email",
      "provider:cloudflare:worker",
      "live",
    ],
    timeout: 180_000,
  },
);