The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 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 user from "@distilled.cloud/cloudflare/user"; |
| 8 | import * as Output from "alchemy/Output"; |
| 9 | import * as Provider from "alchemy/Provider"; |
| 10 | import * as Effect from "effect/Effect"; |
| 11 | import * as Redacted from "effect/Redacted"; |
| 12 | import { MinimumLogLevel } from "effect/References"; |
| 13 | import * as Schedule from "effect/Schedule"; |
| 14 | |
| 15 | const { test } = Test.make({ providers: Cloudflare.providers() }); |
| 16 | |
| 17 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 18 | |
| 19 | const retryAuthBlip = <A, E, R>(eff: Effect.Effect<A, E, R>) => |
| 20 | eff.pipe( |
| 21 | Effect.retry({ |
| 22 | while: (e) => String(e).includes("Unable to authenticate request"), |
| 23 | schedule: Schedule.exponential("1 second"), |
| 24 | times: 5, |
| 25 | }), |
| 26 | ); |
| 27 | |
| 28 | // R2 S3-compatible credentials are derived from the active API token: |
| 29 | // the access key id is the token id and the secret is the SHA-256 hex |
| 30 | // digest of the token value. |
| 31 | const r2Credentials = Effect.gen(function* () { |
| 32 | const creds = yield* yield* CloudflareEnvironment; |
| 33 | if (creds.type !== "apiToken") { |
| 34 | return yield* Effect.die( |
| 35 | new Error( |
| 36 | "Pipelines list test requires an apiToken profile (R2 S3 credentials are derived from the token)", |
| 37 | ), |
| 38 | ); |
| 39 | } |
| 40 | const token = Redacted.value(creds.apiToken); |
| 41 | // Account-owned tokens verify against the account route. |
| 42 | const verified = token.startsWith("cfat_") |
| 43 | ? yield* retryAuthBlip(accounts.verifyToken({ accountId: creds.accountId })) |
| 44 | : yield* retryAuthBlip(user.verifyToken({})); |
| 45 | const secretAccessKey = yield* Effect.sync(() => |
| 46 | crypto.createHash("sha256").update(token).digest("hex"), |
| 47 | ); |
| 48 | return { |
| 49 | accessKeyId: Redacted.make(verified.id), |
| 50 | secretAccessKey: Redacted.make(secretAccessKey), |
| 51 | }; |
| 52 | }); |
| 53 | |
| 54 | // A pipeline references a stream + sink by name in its SQL, so the |
| 55 | // smallest deployable unit is the full R2 topology. |
| 56 | const etl = (creds: { |
| 57 | accessKeyId: Redacted.Redacted<string>; |
| 58 | secretAccessKey: Redacted.Redacted<string>; |
| 59 | }) => |
| 60 | Effect.gen(function* () { |
| 61 | const bucket = yield* Cloudflare.R2.Bucket("SinkBucket", { |
| 62 | forceDestroy: true, |
| 63 | }); |
| 64 | const stream = yield* Cloudflare.Pipelines.Stream("Stream", {}); |
| 65 | const sink = yield* Cloudflare.Pipelines.Sink("Sink", { |
| 66 | type: "r2", |
| 67 | config: { |
| 68 | bucket: bucket.bucketName, |
| 69 | credentials: creds, |
| 70 | rollingPolicy: { intervalSeconds: 10 }, |
| 71 | }, |
| 72 | }); |
| 73 | const pipeline = yield* Cloudflare.Pipelines.Pipeline("Etl", { |
| 74 | sql: Output.interpolate`INSERT INTO ${sink.name} SELECT * FROM ${stream.name}`, |
| 75 | }); |
| 76 | return { bucket, stream, sink, pipeline }; |
| 77 | }); |
| 78 | |
| 79 | test.provider( |
| 80 | "list enumerates the deployed pipeline", |
| 81 | (stack) => |
| 82 | Effect.gen(function* () { |
| 83 | yield* stack.destroy(); |
| 84 | |
| 85 | const creds = yield* r2Credentials; |
| 86 | const deployed = yield* retryAuthBlip(stack.deploy(etl(creds))); |
| 87 | |
| 88 | const provider = yield* Provider.findProvider(Cloudflare.Pipelines.Pipeline); |
| 89 | const all = yield* provider.list(); |
| 90 | |
| 91 | // Each element is the full `read` Attributes shape, usable by delete. |
| 92 | expect(all.some((p) => p.pipelineId === deployed.pipeline.pipelineId)).toBe(true); |
| 93 | const found = all.find((p) => p.pipelineId === deployed.pipeline.pipelineId)!; |
| 94 | expect(found.name).toEqual(deployed.pipeline.name); |
| 95 | expect(found.sql).toEqual(deployed.pipeline.sql); |
| 96 | expect(found.accountId).toEqual(deployed.pipeline.accountId); |
| 97 | |
| 98 | yield* stack.destroy(); |
| 99 | }).pipe(logLevel), |
| 100 | { |
| 101 | tags: [ |
| 102 | "provider:cloudflare", |
| 103 | "provider:cloudflare:pipelines", |
| 104 | "provider:cloudflare:r2", |
| 105 | "live", |
| 106 | ], |
| 107 | timeout: 600_000, |
| 108 | }, |
| 109 | ); |