| 1 | import { Action } from "@/Action"; |
| 2 | import * as Cloudflare from "@/Cloudflare"; |
| 3 | import * as Test from "@/Test/Alchemy"; |
| 4 | import { expect } from "alchemy-test"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import { MinimumLogLevel } from "effect/References"; |
| 7 | |
| 8 | const { test } = Test.make({ providers: Cloudflare.providers() }); |
| 9 | |
| 10 | const logLevel = Effect.provideService( |
| 11 | MinimumLogLevel, |
| 12 | process.env.DEBUG ? "Debug" : "Info", |
| 13 | ); |
| 14 | |
| 15 | // Driving Cloudflare Browser Rendering inside an Action via `BrowserLocal` — |
| 16 | // the local (current-credentials) implementation of the `Browser` binding. |
| 17 | // Browser Rendering is account-scoped and has no backing cloud resource, so the |
| 18 | // Action runs the REST quick actions directly against a stable page. |
| 19 | test.provider( |
| 20 | "BrowserLocal: render a page to markdown and content from an Action", |
| 21 | (stack) => |
| 22 | Effect.gen(function* () { |
| 23 | yield* stack.destroy(); |
| 24 | |
| 25 | const out = yield* stack.deploy( |
| 26 | Effect.gen(function* () { |
| 27 | const Render = Action( |
| 28 | "Render", |
| 29 | Effect.gen(function* () { |
| 30 | const browser = yield* Cloudflare.Browser("BROWSER"); |
| 31 | |
| 32 | return Effect.fn(function* () { |
| 33 | const md = yield* browser.markdown({ |
| 34 | url: "https://example.com", |
| 35 | }); |
| 36 | const html = yield* browser.content({ |
| 37 | url: "https://example.com", |
| 38 | }); |
| 39 | |
| 40 | return { markdown: md.result, content: html.result }; |
| 41 | }); |
| 42 | }).pipe(Effect.provide(Cloudflare.Workers.BrowserLocal)), |
| 43 | ); |
| 44 | |
| 45 | return yield* Render({}); |
| 46 | }), |
| 47 | ); |
| 48 | |
| 49 | expect(out.markdown.toLowerCase()).toContain("example"); |
| 50 | expect(out.content.toLowerCase()).toContain("example"); |
| 51 | |
| 52 | yield* stack.destroy(); |
| 53 | }).pipe(logLevel), |
| 54 | { |
| 55 | tags: ["provider:cloudflare", "provider:cloudflare:worker", "live"], |
| 56 | timeout: 120_000, |
| 57 | }, |
| 58 | ); |