@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 97 lines 3.06 KB
Raw
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 Effect from "effect/Effect";
5 import * as HttpClient from "effect/http/HttpClient";
6 import * as Schedule from "effect/Schedule";
7 import * as pathe from "pathe";
8 import Stack from "./fixtures/url-stack.ts";
9
10 const { test, beforeAll, afterAll, deploy, destroy } = Test.make({
11 providers: Cloudflare.providers(),
12 });
13
14 const stack = beforeAll(deploy(Stack));
15 afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack));
16
17 // Fresh workers.dev URLs can briefly serve Cloudflare's placeholder page with
18 // a 200, so retry until the body parses as JSON (the placeholder is HTML).
19 const getJson = (url: string) =>
20 HttpClient.HttpClient.pipe(
21 Effect.flatMap((client) => client.get(url)),
22 Effect.flatMap((res) => res.json),
23 Effect.retry({
24 schedule: Schedule.exponential("1 second"),
25 times: 12,
26 }),
27 Effect.map((body) => body as Record<string, string>),
28 );
29
30 test(
31 "effectful worker reads its own URL via yield* Worker.URL",
32 Effect.gen(function* () {
33 const { effectUrl } = yield* stack;
34 const body = yield* getJson(`${effectUrl}/`);
35 expect(body.url).toBe(effectUrl);
36 }),
37 {
38 tags: ["provider:cloudflare", "provider:cloudflare:worker", "live"],
39 timeout: 180_000,
40 },
41 );
42
43 test(
44 "async worker receives its own URL via env: { PUBLIC_URL: Worker.URL }",
45 Effect.gen(function* () {
46 const { asyncUrl } = yield* stack;
47 const body = yield* getJson(`${asyncUrl}/`);
48 expect(body.url).toBe(asyncUrl);
49 }),
50 {
51 tags: ["provider:cloudflare", "provider:cloudflare:worker", "live"],
52 timeout: 180_000,
53 },
54 );
55
56 // The canonical VITE_PUBLIC_URL use case: the URL is resolved before the vite
57 // build, so it reaches the bundle two ways — inlined into `import.meta.env`
58 // via `define` at build time, and as a plain_text env binding at deploy time.
59 test.provider.skipIf(!!process.env.FAST)(
60 "vite worker inlines VITE_PUBLIC_URL at build time and binds it at runtime",
61 (stack) =>
62 Effect.gen(function* () {
63 yield* stack.destroy();
64
65 const rootDir = pathe.resolve(import.meta.dirname, "fixtures/vite-url-fixture");
66 const site = yield* stack.deploy(
67 Effect.gen(function* () {
68 return yield* Cloudflare.Worker("UrlViteWorker", {
69 vite: {
70 rootDir,
71 memo: {
72 include: ["index.html", "package.json", "vite.config.ts", "src/**"],
73 },
74 },
75 compatibility: {
76 date: "2024-09-23",
77 flags: ["nodejs_compat"],
78 },
79 env: {
80 VITE_PUBLIC_URL: Cloudflare.Worker.URL,
81 },
82 });
83 }),
84 );
85
86 expect(site.url).toBeDefined();
87 const body = yield* getJson(`${site.url!}/self-url`);
88 expect(body.inlined).toBe(site.url);
89 expect(body.env).toBe(site.url);
90
91 yield* stack.destroy();
92 }),
93 {
94 tags: ["provider:cloudflare", "provider:cloudflare:worker", "live"],
95 timeout: 360_000,
96 },
97 );