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

main
ts 116 lines 3.88 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
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 s3 from "@distilled.cloud/aws/s3";
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(AWS.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 "AwsExternalHostStack",
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 access key and the role's trust policy
36 // take a while to propagate: retry until the route answers `ok`, then report
37 // the last answer so a real failure shows its AWS 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("3 seconds"), times: 30 }),
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 "AWS bindings on a Cloudflare Worker",
55 { tags: ["provider:aws", "provider:cloudflare", "live"] },
56 () => {
57 // `test.provider` supplies the AWS credentials for the out-of-band read.
58 test.provider(
59 "S3 PutObject + GetObject",
60 (_stack) =>
61 Effect.gen(function* () {
62 const body = yield* call("/s3?key=s3-roundtrip");
63 expectOk(body);
64 expect(body.result.contentLength).toBe("from-worker".length);
65
66 const { bucketName } = yield* stack;
67 const object = yield* s3.getObject({ Bucket: bucketName!, Key: "s3-roundtrip" });
68 expect(object.ContentLength).toBe("from-worker".length);
69 }),
70 { timeout: 180_000 },
71 );
72
73 test(
74 "S3 PresignGetObject",
75 Effect.gen(function* () {
76 const body = yield* call("/s3/presign?key=s3-presign");
77 expectOk(body);
78 const text = yield* HttpClient.get(body.result.url).pipe(
79 Effect.flatMap((response) => response.text),
80 );
81 expect(text).toBe("presigned");
82 }),
83 { timeout: 180_000 },
84 );
85
86 test(
87 "DynamoDB PutItem + GetItem",
88 Effect.gen(function* () {
89 const body = yield* call("/dynamodb?key=ddb-roundtrip");
90 expectOk(body);
91 expect(body.result.found).toBe(true);
92 }),
93 { timeout: 180_000 },
94 );
95
96 test(
97 "SQS SendMessage + ReceiveMessage",
98 Effect.gen(function* () {
99 const body = yield* call("/sqs?key=sqs-roundtrip");
100 expectOk(body);
101 expect(body.result.bodies).toContain("sqs-roundtrip");
102 }),
103 { timeout: 180_000 },
104 );
105
106 test(
107 "AccessAnalyzer ValidatePolicy (account-level)",
108 Effect.gen(function* () {
109 const body = yield* call("/access-analyzer");
110 expectOk(body);
111 expect(Array.isArray(body.result.findings)).toBe(true);
112 }),
113 { timeout: 180_000 },
114 );
115 },
116 );