The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { existsSync } from "node:fs"; |
| 2 | import * as Cloudflare from "@alchemy.run/cloudflare"; |
| 3 | import { describe, expect } from "@alchemy.run/test"; |
| 4 | import * as Test from "@alchemy.run/test/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 FuseStack from "./fixtures/fuse/stack.ts"; |
| 10 | import LocalRemoteStack, { state } from "./fixtures/remote/local-stack.ts"; |
| 11 | |
| 12 | const { test, beforeAll, afterAll, deploy, destroy } = Test.make({ |
| 13 | providers: Cloudflare.providers(), |
| 14 | state, |
| 15 | dev: true, |
| 16 | }); |
| 17 | |
| 18 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 19 | |
| 20 | // First request has to wait for the local runtime to `docker pull` the image |
| 21 | // and boot the container, so give it plenty of room. |
| 22 | const HOOK_TIMEOUT = 300_000; |
| 23 | const TEST_TIMEOUT = 240_000; |
| 24 | |
| 25 | const readinessSchedule = Schedule.min([ |
| 26 | Schedule.exponential("500 millis"), |
| 27 | Schedule.spaced("3 seconds"), |
| 28 | ]); |
| 29 | |
| 30 | /** |
| 31 | * Remote container (`image`) under `alchemy dev`: the local provider must |
| 32 | * resolve `dev: { imageUri }` so the local runtime can `docker pull` the |
| 33 | * pre-built image (regression: it previously died with "Container requires a |
| 34 | * `main` entrypoint" because it only handled the Effect-native variant). |
| 35 | * The DO proxies a request to the echo server running inside the container. |
| 36 | */ |
| 37 | describe( |
| 38 | "local remote container (image)", |
| 39 | { |
| 40 | tags: ["provider:cloudflare", "provider:cloudflare:container", "provider:cloudflare:worker"], |
| 41 | }, |
| 42 | () => { |
| 43 | const stack = beforeAll(deploy(LocalRemoteStack), { |
| 44 | timeout: HOOK_TIMEOUT, |
| 45 | }); |
| 46 | afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(LocalRemoteStack), { |
| 47 | timeout: HOOK_TIMEOUT, |
| 48 | }); |
| 49 | |
| 50 | test( |
| 51 | "pulls the remote image and serves it over its TCP port", |
| 52 | Effect.gen(function* () { |
| 53 | const { url } = yield* stack; |
| 54 | const client = yield* HttpClient.HttpClient; |
| 55 | |
| 56 | // `new URL` (not `${url}/hello`): the dev url has a trailing slash, and |
| 57 | // a `//hello` path is protocol-relative — the local dev proxy would |
| 58 | // resolve it to host "hello" and die with a DNS error. |
| 59 | const body = yield* client.get(new URL("/hello", url)).pipe( |
| 60 | Effect.flatMap((r) => |
| 61 | r.status !== 200 |
| 62 | ? Effect.fail(new Error(`not ready: ${r.status}`)) |
| 63 | : Effect.flatMap(r.text, (text) => |
| 64 | // mendhak/http-https-echo echoes the request (method, path, |
| 65 | // headers) back as JSON. |
| 66 | text.includes("method") |
| 67 | ? Effect.succeed(text) |
| 68 | : Effect.fail(new Error(`not ready: got ${text}`)), |
| 69 | ), |
| 70 | ), |
| 71 | Effect.timeout("30 seconds"), |
| 72 | Effect.retry({ schedule: readinessSchedule, times: 30 }), |
| 73 | ); |
| 74 | expect(body).toContain("method"); |
| 75 | }).pipe(logLevel), |
| 76 | { tags: ["local"], timeout: TEST_TIMEOUT }, |
| 77 | ); |
| 78 | }, |
| 79 | ); |
| 80 | |
| 81 | /** |
| 82 | * A container that mounts a FUSE filesystem (as `s3fs` or `tigrisfs` mount R2) |
| 83 | * under `alchemy dev`, as deployed Containers can. A Linux host without |
| 84 | * /dev/fuse has no device to give the container. |
| 85 | */ |
| 86 | describe.skipIf(process.platform === "linux" && !existsSync("/dev/fuse"))( |
| 87 | "local container FUSE mount", |
| 88 | { |
| 89 | tags: ["provider:cloudflare", "provider:cloudflare:container", "provider:cloudflare:worker"], |
| 90 | }, |
| 91 | () => { |
| 92 | const stack = beforeAll(deploy(FuseStack), { timeout: HOOK_TIMEOUT }); |
| 93 | afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(FuseStack), { |
| 94 | timeout: HOOK_TIMEOUT, |
| 95 | }); |
| 96 | |
| 97 | test( |
| 98 | "mounts a FUSE filesystem inside the container", |
| 99 | Effect.gen(function* () { |
| 100 | const { url } = yield* stack; |
| 101 | const client = yield* HttpClient.HttpClient; |
| 102 | |
| 103 | const body = yield* client.get(new URL("/", url)).pipe( |
| 104 | Effect.flatMap((r) => |
| 105 | r.status !== 200 ? Effect.fail(new Error(`not ready: ${r.status}`)) : r.text, |
| 106 | ), |
| 107 | Effect.timeout("30 seconds"), |
| 108 | Effect.retry({ schedule: readinessSchedule, times: 30 }), |
| 109 | ); |
| 110 | // Without the privileges bindfs fails: "fuse: device not found". |
| 111 | expect(body.trim()).toBe("read through fuse"); |
| 112 | }).pipe(logLevel), |
| 113 | { tags: ["local"], timeout: TEST_TIMEOUT }, |
| 114 | ); |
| 115 | }, |
| 116 | ); |