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

main
ts 301 lines 10.3 KB
Raw
1 import crypto from "node:crypto";
2 import * as Cloudflare from "@alchemy.run/cloudflare";
3 import { CloudflareEnvironment } from "@alchemy.run/cloudflare/CloudflareEnvironment";
4 import { expect } from "@alchemy.run/test";
5 import * as Test from "@alchemy.run/test/Alchemy";
6 import * as accounts from "@distilled.cloud/cloudflare/accounts";
7 import * as pipelines from "@distilled.cloud/cloudflare/pipelines";
8 import * as user from "@distilled.cloud/cloudflare/user";
9 import * as workers from "@distilled.cloud/cloudflare/workers";
10 import * as Provider from "alchemy/Provider";
11 import * as Effect from "effect/Effect";
12 import * as Redacted from "effect/Redacted";
13 import { MinimumLogLevel } from "effect/References";
14 import * as Schedule from "effect/Schedule";
15
16 const { test } = Test.make({ providers: Cloudflare.providers() });
17
18 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
19
20 // The scoped API token the test harness mints propagates eventually-
21 // consistently across Cloudflare's edge — ride out 403 blips
22 // (`Forbidden`, declared in the distilled error unions) on the test's
23 // own out-of-band verification calls.
24 const forbiddenBlips = {
25 while: (e: { _tag: string }) => e._tag === "Forbidden",
26 schedule: Schedule.exponential("500 millis"),
27 times: 8,
28 } as const;
29
30 // The same blip can hit engine-side calls mid-deploy. Reconcile is
31 // idempotent (observe→ensure→sync), so retrying the whole deploy is safe.
32 const retryAuthBlip = <A, E, R>(eff: Effect.Effect<A, E, R>) =>
33 eff.pipe(
34 Effect.retry({
35 while: (e) => String(e).includes("Unable to authenticate request"),
36 schedule: Schedule.exponential("1 second"),
37 times: 5,
38 }),
39 );
40
41 // R2 S3-compatible credentials are derived from the active API token:
42 // the access key id is the token id and the secret is the SHA-256 hex
43 // digest of the token value.
44 const r2Credentials = Effect.gen(function* () {
45 const creds = yield* yield* CloudflareEnvironment;
46 if (creds.type !== "apiToken") {
47 return yield* Effect.die(
48 new Error(
49 "LegacyPipeline test requires an apiToken profile (R2 S3 credentials are derived from the token)",
50 ),
51 );
52 }
53 const token = Redacted.value(creds.apiToken);
54 // Account-owned tokens verify against the account route.
55 const verified = token.startsWith("cfat_")
56 ? yield* retryAuthBlip(accounts.verifyToken({ accountId: creds.accountId }))
57 : yield* retryAuthBlip(user.verifyToken({}));
58 const secretAccessKey = yield* Effect.sync(() =>
59 crypto.createHash("sha256").update(token).digest("hex"),
60 );
61 return {
62 accessKeyId: Redacted.make(verified.id),
63 secretAccessKey: Redacted.make(secretAccessKey),
64 };
65 });
66
67 const getLegacyPipeline = (accountId: string, pipelineName: string) =>
68 pipelines.getPipeline({ accountId, pipelineName }).pipe(Effect.retry(forbiddenBlips));
69
70 const expectLegacyPipelineGone = (accountId: string, pipelineName: string) =>
71 getLegacyPipeline(accountId, pipelineName).pipe(
72 Effect.flatMap(() => Effect.fail({ _tag: "PipelineNotDeleted" } as const)),
73 Effect.catchTag("PipelineNotExists", () => Effect.void),
74 Effect.retry({
75 while: (e) => e._tag === "PipelineNotDeleted",
76 schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(10)]),
77 }),
78 );
79
80 interface LegacyOpts {
81 prefix?: string;
82 corsOrigins?: string[];
83 name?: string;
84 }
85
86 const legacy = (
87 creds: {
88 accessKeyId: Redacted.Redacted<string>;
89 secretAccessKey: Redacted.Redacted<string>;
90 },
91 opts: LegacyOpts = {},
92 ) =>
93 Effect.gen(function* () {
94 const bucket = yield* Cloudflare.R2.Bucket("LegacyBucket", {
95 forceDestroy: true,
96 });
97 const pipeline = yield* Cloudflare.Pipelines.LegacyPipeline("Legacy", {
98 name: opts.name,
99 source: [
100 {
101 type: "http",
102 ...(opts.corsOrigins ? { cors: { origins: opts.corsOrigins } } : {}),
103 },
104 { type: "binding" },
105 ],
106 destination: {
107 bucket: bucket.bucketName,
108 credentials: creds,
109 batch: { maxDurationS: 5 },
110 ...(opts.prefix ? { prefix: opts.prefix } : {}),
111 },
112 });
113 return { bucket, pipeline };
114 });
115
116 const asyncWorkerScript = `export default {
117 async fetch() {
118 return new Response("ok");
119 },
120 };
121 `;
122
123 test.provider(
124 "binds a legacy pipeline to a worker by name",
125 (stack) =>
126 Effect.gen(function* () {
127 const { accountId } = yield* yield* CloudflareEnvironment;
128 const creds = yield* r2Credentials;
129
130 yield* stack.destroy();
131
132 const { pipeline, worker } = yield* retryAuthBlip(
133 stack.deploy(
134 Effect.gen(function* () {
135 const { pipeline } = yield* legacy(creds);
136 const worker = yield* Cloudflare.Worker("legacy-pipeline-binding-worker", {
137 script: asyncWorkerScript,
138 env: {
139 LEGACY: pipeline,
140 },
141 });
142 return { pipeline, worker };
143 }),
144 ),
145 );
146
147 const settings = yield* workers
148 .getScriptScriptAndVersionSetting({
149 accountId,
150 scriptName: worker.workerName,
151 })
152 .pipe(Effect.retry(forbiddenBlips));
153
154 const binding = (settings.bindings ?? []).find((b) => b.name === "LEGACY");
155
156 expect(binding).toMatchObject({
157 type: "pipelines",
158 name: "LEGACY",
159 pipeline: pipeline.pipelineId,
160 });
161
162 yield* stack.destroy();
163 yield* expectLegacyPipelineGone(accountId, pipeline.name);
164 }).pipe(logLevel),
165 {
166 tags: [
167 "provider:cloudflare",
168 "provider:cloudflare:pipelines",
169 "provider:cloudflare:r2",
170 "provider:cloudflare:worker",
171 "live",
172 ],
173 timeout: 420_000,
174 },
175 );
176
177 test.provider(
178 "legacy pipeline: create, in-place update, replace on name change",
179 (stack) =>
180 Effect.gen(function* () {
181 const { accountId } = yield* yield* CloudflareEnvironment;
182 const creds = yield* r2Credentials;
183
184 yield* stack.destroy();
185
186 // Create — engine-generated name, http + binding sources.
187 const initial = yield* retryAuthBlip(stack.deploy(legacy(creds)));
188
189 expect(initial.pipeline.pipelineId).toBeTruthy();
190 expect(initial.pipeline.accountId).toEqual(accountId);
191 expect(initial.pipeline.endpoint).toBeTruthy();
192 expect(initial.pipeline.bucket).toEqual(initial.bucket.bucketName);
193
194 const live = yield* getLegacyPipeline(accountId, initial.pipeline.name);
195 expect(live.id).toEqual(initial.pipeline.pipelineId);
196 expect(live.destination.path.bucket).toEqual(initial.bucket.bucketName);
197 expect(live.source.map((s) => s.type).sort()).toEqual(["binding", "http"]);
198
199 // Redeploying identical props is a no-op.
200 const noop = yield* retryAuthBlip(stack.deploy(legacy(creds)));
201 expect(noop.pipeline.pipelineId).toEqual(initial.pipeline.pipelineId);
202 expect(noop.pipeline.version).toEqual(initial.pipeline.version);
203
204 // In-place update — prefix and CORS change via PUT, same id/name.
205 const updated = yield* retryAuthBlip(
206 stack.deploy(
207 legacy(creds, {
208 prefix: "ingest",
209 corsOrigins: ["https://example.com"],
210 }),
211 ),
212 );
213
214 expect(updated.pipeline.pipelineId).toEqual(initial.pipeline.pipelineId);
215 expect(updated.pipeline.name).toEqual(initial.pipeline.name);
216
217 const liveUpdated = yield* getLegacyPipeline(accountId, updated.pipeline.name);
218 expect(liveUpdated.destination.path.prefix).toEqual("ingest");
219 const httpSource = liveUpdated.source.find((s) => s.type === "http");
220 expect(httpSource && "cors" in httpSource ? httpSource.cors?.origins : undefined).toEqual([
221 "https://example.com",
222 ]);
223
224 // Name change — the legacy API addresses pipelines by name, so this
225 // is a replacement.
226 const replacementName = `${initial.pipeline.name}-r`;
227 const replaced = yield* retryAuthBlip(
228 stack.deploy(
229 legacy(creds, {
230 name: replacementName,
231 prefix: "ingest",
232 corsOrigins: ["https://example.com"],
233 }),
234 ),
235 );
236
237 expect(replaced.pipeline.name).toEqual(replacementName);
238 expect(replaced.pipeline.pipelineId).not.toEqual(initial.pipeline.pipelineId);
239 yield* expectLegacyPipelineGone(accountId, initial.pipeline.name);
240
241 yield* stack.destroy();
242 yield* expectLegacyPipelineGone(accountId, replaced.pipeline.name);
243
244 // Destroy again — delete must be idempotent.
245 yield* stack.destroy();
246 }).pipe(logLevel),
247 {
248 tags: [
249 "provider:cloudflare",
250 "provider:cloudflare:pipelines",
251 "provider:cloudflare:r2",
252 "live",
253 ],
254 timeout: 420_000,
255 },
256 );
257
258 // The legacy list endpoint no longer enumerates legacy pipelines: while the
259 // deployed pipeline exists (getPipeline by name succeeds), polling
260 // `GET /accounts/{account_id}/pipelines?per_page=50` every 2s returns
261 // `{"result":[],"result_info":{"count":0,"total_count":0}}` (observed
262 // 2026-10-09), so `list()` decodes fine but cannot find it. Platform
263 // behavior of the deprecated API, not a provider bug — gated; set
264 // CLOUDFLARE_TEST_LEGACY_PIPELINE_LIST=1 to re-check.
265 test.provider.skipIf(!process.env.CLOUDFLARE_TEST_LEGACY_PIPELINE_LIST)(
266 "list enumerates the deployed legacy pipeline",
267 (stack) =>
268 Effect.gen(function* () {
269 const { accountId } = yield* yield* CloudflareEnvironment;
270 const creds = yield* r2Credentials;
271
272 yield* stack.destroy();
273
274 const deployed = yield* retryAuthBlip(stack.deploy(legacy(creds)));
275 expect(deployed.pipeline.pipelineId).toBeTruthy();
276
277 // Account collection: list() exhaustively paginates every legacy
278 // pipeline in the account and hydrates each into the read
279 // Attributes shape.
280 const provider = yield* Provider.findProvider(Cloudflare.Pipelines.LegacyPipeline);
281 const all = yield* provider.list();
282
283 const match = all.find((p) => p.pipelineId === deployed.pipeline.pipelineId);
284 expect(match).toBeDefined();
285 expect(match?.name).toEqual(deployed.pipeline.name);
286 expect(match?.accountId).toEqual(accountId);
287 expect(match?.bucket).toEqual(deployed.bucket.bucketName);
288 expect(match?.endpoint).toEqual(deployed.pipeline.endpoint);
289
290 yield* stack.destroy();
291 }).pipe(logLevel),
292 {
293 tags: [
294 "provider:cloudflare",
295 "provider:cloudflare:pipelines",
296 "provider:cloudflare:r2",
297 "live",
298 ],
299 timeout: 300_000,
300 },
301 );