sailfork / t3code-github / .repos/ alchemy-effect/ packages/ integration-tests/ test/ Cloudflare/ Workers/ BrowserLocal.test.ts

import * as Cloudflare from "@alchemy.run/cloudflare";
import { expect } from "@alchemy.run/test";
import * as Test from "@alchemy.run/test/Alchemy";
import { Action } from "alchemy/Action";
import * as Effect from "effect/Effect";
import { MinimumLogLevel } from "effect/References";

const { test } = Test.make({ providers: Cloudflare.providers() });

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

// Driving Cloudflare Browser Rendering inside an Action via `BrowserLocal` —
// the local (current-credentials) implementation of the `Browser` binding.
// Browser Rendering is account-scoped and has no backing cloud resource, so the
// Action runs the REST quick actions directly against a stable page.
test.provider(
  "BrowserLocal: render a page to markdown and content from an Action",
  (stack) =>
    Effect.gen(function* () {
      yield* stack.destroy();

      const out = yield* stack.deploy(
        Effect.gen(function* () {
          const Render = Action(
            "Render",
            Effect.gen(function* () {
              const browser = yield* Cloudflare.Browser("BROWSER");

              return Effect.fn(function* () {
                const md = yield* browser.markdown({
                  url: "https://example.com",
                });
                const html = yield* browser.content({
                  url: "https://example.com",
                });

                return { markdown: md.result, content: html.result };
              });
            }).pipe(Effect.provide(Cloudflare.Workers.BrowserLocal)),
          );

          return yield* Render({});
        }),
      );

      expect(out.markdown.toLowerCase()).toContain("example");
      expect(out.content.toLowerCase()).toContain("example");

      yield* stack.destroy();
    }).pipe(logLevel),
  {
    tags: ["provider:cloudflare", "provider:cloudflare:worker", "live"],
    timeout: 120_000,
  },
);