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

main
ts 139 lines 4.76 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import * as Cloudflare from "@alchemy.run/cloudflare";
3 import { CloudflareEnvironment } from "@alchemy.run/cloudflare/CloudflareEnvironment";
4 import { describe, expect } from "@alchemy.run/test";
5 import * as Test from "@alchemy.run/test/Alchemy";
6 import * as kv from "@distilled.cloud/cloudflare/kv";
7 import * as r2 from "@distilled.cloud/cloudflare/r2";
8 import * as Alchemy from "alchemy";
9 import * as Data from "effect/Data";
10 import * as Effect from "effect/Effect";
11 import * as HttpClient from "effect/http/HttpClient";
12 import * as Layer from "effect/Layer";
13 import * as Schedule from "effect/Schedule";
14 import * as Stream from "effect/Stream";
15 import { drainSubscription } from "../K2/k2-test-utils.ts";
16 import ExternalHostLambdaLive, {
17 Bucket,
18 ExternalHostLambda,
19 K2Stream,
20 Namespace,
21 } from "./fixtures/handler.ts";
22
23 const providers = Layer.mergeAll(AWS.providers(), Cloudflare.providers());
24
25 const { test, beforeAll, afterAll, deploy, destroy } = Test.make({
26 providers,
27 state: Alchemy.localState(),
28 });
29
30 const Stack = Alchemy.Stack(
31 "CloudflareExternalHostStack",
32 { providers, state: Alchemy.localState() },
33 Effect.gen(function* () {
34 const fn = yield* ExternalHostLambda;
35 const bucket = yield* Bucket;
36 const namespace = yield* Namespace;
37 const stream = yield* K2Stream;
38 const verifier = yield* Cloudflare.K2.Subscription("ExternalHostVerifier", {
39 stream,
40 startAt: "earliest",
41 });
42 return {
43 streamId: stream.streamId,
44 subscriptionId: verifier.subscriptionId,
45 url: fn.functionUrl.as<string>(),
46 bucketName: bucket.bucketName,
47 namespaceId: namespace.namespaceId,
48 };
49 }).pipe(Effect.provide(ExternalHostLambdaLive)),
50 );
51
52 const stack = beforeAll(deploy(Stack), { timeout: 300_000 });
53 afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack), { timeout: 300_000 });
54
55 class NotReady extends Data.TaggedError("NotReady")<{ status: number; body: string }> {}
56
57 // A fresh function URL and the minted token take a while to start serving:
58 // retry non-2xx answers, failing with the last Cloudflare error.
59 const call = (route: string) =>
60 Effect.gen(function* () {
61 const { url } = yield* stack;
62 return yield* HttpClient.get(`${url!.replace(/\/$/, "")}${route}`).pipe(
63 Effect.timeout("25 seconds"),
64 Effect.flatMap((response) =>
65 response.text.pipe(
66 Effect.flatMap((body) =>
67 response.status === 200
68 ? Effect.succeed(JSON.parse(body) as any)
69 : Effect.fail(new NotReady({ status: response.status, body })),
70 ),
71 ),
72 ),
73 Effect.retry({ schedule: Schedule.spaced("3 seconds"), times: 40 }),
74 );
75 });
76
77 describe(
78 "Cloudflare *Http bindings on an AWS Lambda",
79 { tags: ["provider:aws", "provider:cloudflare", "live"] },
80 () => {
81 // `test.provider` supplies the Cloudflare credentials for the out-of-band reads.
82 test.provider(
83 "R2 ReadWriteBucket",
84 (_stack) =>
85 Effect.gen(function* () {
86 expect(yield* call("/r2?key=r2-roundtrip")).toEqual({ value: "from-lambda" });
87
88 const { bucketName } = yield* stack;
89 const { accountId } = yield* yield* CloudflareEnvironment;
90 const object = yield* r2.getObject({
91 accountId,
92 bucketName: bucketName!,
93 objectName: "r2-roundtrip",
94 });
95 expect(yield* object.body.pipe(Stream.decodeText, Stream.mkString)).toBe("from-lambda");
96 }),
97 { timeout: 240_000 },
98 );
99
100 test.provider(
101 "KV ReadWriteNamespace",
102 (_stack) =>
103 Effect.gen(function* () {
104 expect(yield* call("/kv?key=kv-roundtrip")).toEqual({ value: "from-lambda" });
105
106 const { namespaceId } = yield* stack;
107 const { accountId } = yield* yield* CloudflareEnvironment;
108 const value = yield* kv
109 .getNamespaceValue({ accountId, namespaceId: namespaceId!, keyName: "kv-roundtrip" })
110 .pipe(Effect.flatMap((res) => res.body.pipe(Stream.decodeText, Stream.mkString)));
111 expect(value).toBe("from-lambda");
112 }),
113 { timeout: 240_000 },
114 );
115
116 test(
117 "Queues WriteQueue",
118 Effect.gen(function* () {
119 expect(yield* call("/queue?key=queue-send")).toEqual({ sent: true });
120 }),
121 { timeout: 240_000 },
122 );
123
124 test.provider(
125 "K2 WriteStream",
126 (_stack) =>
127 Effect.gen(function* () {
128 expect(yield* call("/k2?key=k2-send")).toEqual({ sent: true });
129
130 const { streamId, subscriptionId } = yield* stack;
131 const records = yield* drainSubscription(streamId!, subscriptionId!, 1);
132 expect(records.map((record) => JSON.parse(record.text))).toContainEqual({
133 key: "k2-send",
134 });
135 }),
136 { timeout: 240_000 },
137 );
138 },
139 );