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

main
ts 157 lines 5.76 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 import { CloudflareEnvironment } from "@alchemy.run/cloudflare/CloudflareEnvironment";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as r2 from "@distilled.cloud/cloudflare/r2";
6 import * as Effect from "effect/Effect";
7 import { MinimumLogLevel } from "effect/References";
8 import * as Stream from "effect/Stream";
9 import * as pathe from "pathe";
10 import PresignLocalWorker, { PresignLocalBucket } from "./fixtures/presign/local-worker.ts";
11 import PresignRemoteWorker, { PresignRemoteBucket } from "./fixtures/presign/remote-worker.ts";
12 import { presignRoundTrip } from "./fixtures/presign/roundtrip.ts";
13
14 // `dev: true` runs local providers behind the RPC sidecar proxy by default,
15 // matching the process topology of the real `alchemy dev` command.
16 const { test } = Test.make({
17 providers: Cloudflare.providers(),
18 dev: true,
19 });
20
21 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
22
23 /**
24 * Under `alchemy dev` a locally-emulated bucket is served on the Worker's
25 * local S3 endpoint with no extra configuration: the same
26 * `PresignPutObject`/`PresignGetObject` code mints URLs that point at the dev
27 * server, an unauthenticated client uploads through them, and the Worker's
28 * native binding reads the object back from the same local simulator.
29 */
30 test.provider(
31 "presigned URLs round-trip against a locally-emulated bucket",
32 (stack) =>
33 Effect.gen(function* () {
34 yield* stack.destroy();
35
36 const deployed = yield* stack.deploy(
37 Effect.gen(function* () {
38 const bucket = yield* PresignLocalBucket;
39 const worker = yield* PresignLocalWorker;
40 return { bucket, worker };
41 }),
42 );
43
44 // `dev:` name + localhost URL — proof no cloud call ran
45 expect(deployed.bucket.bucketName).toMatch(/^dev:/);
46 expect(deployed.worker.url).toMatch(/^http:\/\/localhost:\d+$/);
47
48 const { putUrl, getUrl } = yield* presignRoundTrip(
49 deployed.worker.url!,
50 "uploads/local file.txt",
51 );
52 const prefix = `${deployed.worker.url}/cdn-cgi/local/r2/s3/${encodeURIComponent(deployed.bucket.bucketName)}/`;
53 expect(putUrl.startsWith(prefix)).toBe(true);
54 expect(getUrl.startsWith(prefix)).toBe(true);
55
56 yield* stack.destroy();
57 }).pipe(logLevel),
58 {
59 tags: ["provider:cloudflare", "provider:cloudflare:r2", "provider:cloudflare:worker", "local"],
60 timeout: 120_000,
61 },
62 );
63
64 /**
65 * Async (non-Effect) Workers get the same per-mode wiring through
66 * `Cloudflare.R2.S3Credentials` on `env`: in dev it resolves to the Worker's
67 * local S3 endpoint and local credentials, and `aws4fetch` presigns against
68 * it.
69 */
70 test.provider(
71 "async Worker presigns with S3Credentials against a locally-emulated bucket",
72 (stack) =>
73 Effect.gen(function* () {
74 yield* stack.destroy();
75
76 const deployed = yield* stack.deploy(
77 Effect.gen(function* () {
78 // The declaration form, as in async Worker modules
79 const Bucket = Cloudflare.R2.Bucket("PresignAsyncBucket", {
80 forceDestroy: true,
81 });
82 const worker = yield* Cloudflare.Worker("PresignAsyncWorker", {
83 main: pathe.resolve(import.meta.dirname, "fixtures/presign/async-worker.ts"),
84 env: {
85 BUCKET: Bucket,
86 BUCKET_S3: Cloudflare.R2.S3Credentials(Bucket, {
87 access: "read-write",
88 }),
89 },
90 });
91 const bucket = yield* Bucket;
92 return { bucket, worker };
93 }),
94 );
95
96 expect(deployed.bucket.bucketName).toMatch(/^dev:/);
97 expect(deployed.worker.url).toMatch(/^http:\/\/localhost:\d+$/);
98
99 const { putUrl } = yield* presignRoundTrip(deployed.worker.url!, "uploads/async.txt");
100 expect(putUrl.startsWith(`${deployed.worker.url}/cdn-cgi/local/r2/s3/`)).toBe(true);
101
102 yield* stack.destroy();
103 }).pipe(logLevel),
104 {
105 tags: ["provider:cloudflare", "provider:cloudflare:r2", "provider:cloudflare:worker", "local"],
106 timeout: 120_000,
107 },
108 );
109
110 /**
111 * `Alchemy.remote()` keeps the bucket on real Cloudflare during dev: the
112 * presign binding mints a scoped API token and signs URLs for R2's S3
113 * endpoint, the upload lands in the real bucket (verified out-of-band), and
114 * destroy removes it.
115 */
116 test.provider(
117 "Alchemy.remote() bucket presigns against real R2 in dev",
118 (stack) =>
119 Effect.gen(function* () {
120 yield* stack.destroy();
121
122 const deployed = yield* stack.deploy(
123 Effect.gen(function* () {
124 const bucket = yield* PresignRemoteBucket;
125 const worker = yield* PresignRemoteWorker;
126 return { bucket, worker };
127 }),
128 );
129
130 expect(deployed.bucket.bucketName).not.toMatch(/^dev:/);
131
132 const { putUrl } = yield* presignRoundTrip(deployed.worker.url!, "uploads/remote.txt");
133 expect(new URL(putUrl).hostname).toMatch(/\.r2\.cloudflarestorage\.com$/);
134
135 // Out-of-band: the upload is in the real bucket
136 const { accountId } = yield* yield* CloudflareEnvironment;
137 const object = yield* r2.getObject({
138 accountId,
139 bucketName: deployed.bucket.bucketName,
140 objectName: "uploads/remote.txt",
141 });
142 const text = yield* object.body.pipe(Stream.decodeText, Stream.mkString);
143 expect(text).toBe("uploaded via presigned url");
144
145 yield* stack.destroy();
146
147 const gone = yield* r2.getBucket({ accountId, bucketName: deployed.bucket.bucketName }).pipe(
148 Effect.as(false),
149 Effect.catchTag("NoSuchBucket", () => Effect.succeed(true)),
150 );
151 expect(gone).toBe(true);
152 }).pipe(logLevel),
153 {
154 tags: ["provider:cloudflare", "provider:cloudflare:r2", "provider:cloudflare:worker", "live"],
155 timeout: 180_000,
156 },
157 );