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

main
ts 86 lines 3.01 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 import * as GCP from "@alchemy.run/gcp";
3 import { describe, expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as storage from "@distilled.cloud/gcp/storage_v1";
6 import * as Alchemy from "alchemy";
7 import * as Effect from "effect/Effect";
8 import * as HttpClient from "effect/http/HttpClient";
9 import * as Layer from "effect/Layer";
10 import * as Schedule from "effect/Schedule";
11 import ExternalHostWorker, { Bucket } from "./fixtures/worker.ts";
12
13 const providers = Layer.mergeAll(GCP.providers(), Cloudflare.providers());
14
15 const { test, beforeAll, afterAll, deploy, destroy } = Test.make({
16 providers,
17 state: Alchemy.localState(),
18 });
19
20 const Stack = Alchemy.Stack(
21 "GcpExternalHostStack",
22 { providers, state: Alchemy.localState() },
23 Effect.gen(function* () {
24 const worker = yield* ExternalHostWorker;
25 const bucket = yield* Bucket;
26 return { url: worker.url.as<string>(), bucketName: bucket.bucketName };
27 }),
28 );
29
30 const stack = beforeAll(deploy(Stack), { timeout: 300_000 });
31 afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack), { timeout: 300_000 });
32
33 type Body = { ok: boolean; result?: any; error?: string };
34
35 // A fresh workers.dev URL, the minted key and the account's new role grants
36 // take a while to propagate: retry until the route answers `ok`, then report
37 // the last answer so a real failure shows its GCP error.
38 const call = (route: string) =>
39 Effect.gen(function* () {
40 const { url } = yield* stack;
41 return yield* HttpClient.get(`${url}${route}`).pipe(
42 Effect.timeout("20 seconds"),
43 Effect.flatMap((response) => response.json),
44 Effect.map((body) => body as Body),
45 Effect.flatMap((body) => (body?.ok ? Effect.succeed(body) : Effect.fail(body))),
46 Effect.retry({ schedule: Schedule.spaced("5 seconds"), times: 24 }),
47 Effect.catch((body) => Effect.succeed(body as Body)),
48 );
49 });
50
51 const expectOk = (body: Body) => expect({ ok: body.ok, error: body.error }).toEqual({ ok: true });
52
53 describe(
54 "GCP bindings on a Cloudflare Worker",
55 { tags: ["provider:gcp", "provider:cloudflare", "live"] },
56 () => {
57 // `test.provider` supplies the GCP credentials for the out-of-band read.
58 test.provider(
59 "Storage ReadWriteBucket",
60 (_stack) =>
61 Effect.gen(function* () {
62 const body = yield* call("/storage?key=storage-roundtrip");
63 expectOk(body);
64 expect(body.result.text).toBe("from-worker");
65
66 const { bucketName } = yield* stack;
67 const object = yield* storage.getObjects({
68 bucket: bucketName!,
69 object: "storage-roundtrip",
70 });
71 expect(object.size).toBe(String("from-worker".length));
72 }),
73 { timeout: 240_000 },
74 );
75
76 test(
77 "Pub/Sub WriteTopic",
78 Effect.gen(function* () {
79 const body = yield* call("/pubsub?key=pubsub-publish");
80 expectOk(body);
81 expect(body.result.messageId).toBeTruthy();
82 }),
83 { timeout: 240_000 },
84 );
85 },
86 );