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 Provider from "alchemy/Provider"; |
| 9 | import * as Effect from "effect/Effect"; |
| 10 | import * as Redacted from "effect/Redacted"; |
| 11 | import { MinimumLogLevel } from "effect/References"; |
| 12 | import * as Schedule from "effect/Schedule"; |
| 13 | |
| 14 | const { test } = Test.make({ providers: Cloudflare.providers() }); |
| 15 | |
| 16 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 17 | |
| 18 | // The scoped API token the harness mints propagates eventually- |
| 19 | // consistently across Cloudflare's edge — ride out auth blips. |
| 20 | const retryAuthBlip = <A, E, R>(eff: Effect.Effect<A, E, R>) => |
| 21 | eff.pipe( |
| 22 | Effect.retry({ |
| 23 | while: (e) => String(e).includes("Unable to authenticate request"), |
| 24 | schedule: Schedule.exponential("1 second"), |
| 25 | times: 5, |
| 26 | }), |
| 27 | ); |
| 28 | |
| 29 | // R2 S3-compatible credentials are derived from the active API token: |
| 30 | // the access key id is the token id and the secret is the SHA-256 hex |
| 31 | // digest of the token value. |
| 32 | const r2Credentials = Effect.gen(function* () { |
| 33 | const creds = yield* yield* CloudflareEnvironment; |
| 34 | if (creds.type !== "apiToken") { |
| 35 | return yield* Effect.die( |
| 36 | new Error( |
| 37 | "Pipelines sink test requires an apiToken profile (R2 S3 credentials are derived from the token)", |
| 38 | ), |
| 39 | ); |
| 40 | } |
| 41 | const token = Redacted.value(creds.apiToken); |
| 42 | // Account-owned tokens verify against the account route. |
| 43 | const verified = token.startsWith("cfat_") |
| 44 | ? yield* retryAuthBlip(accounts.verifyToken({ accountId: creds.accountId })) |
| 45 | : yield* retryAuthBlip( |
| 46 | user.verifyToken({}).pipe( |
| 47 | Effect.retry({ |
| 48 | while: (e) => e._tag === "Forbidden", |
| 49 | schedule: Schedule.exponential("500 millis"), |
| 50 | times: 8, |
| 51 | }), |
| 52 | ), |
| 53 | ); |
| 54 | const secretAccessKey = yield* Effect.sync(() => |
| 55 | crypto.createHash("sha256").update(token).digest("hex"), |
| 56 | ); |
| 57 | return { |
| 58 | accessKeyId: Redacted.make(verified.id), |
| 59 | secretAccessKey: Redacted.make(secretAccessKey), |
| 60 | }; |
| 61 | }); |
| 62 | |
| 63 | test.provider( |
| 64 | "list enumerates the deployed sink", |
| 65 | (stack) => |
| 66 | Effect.gen(function* () { |
| 67 | yield* stack.destroy(); |
| 68 | |
| 69 | const creds = yield* r2Credentials; |
| 70 | |
| 71 | const deployed = yield* retryAuthBlip( |
| 72 | stack.deploy( |
| 73 | Effect.gen(function* () { |
| 74 | const bucket = yield* Cloudflare.R2.Bucket("SinkBucket", { |
| 75 | forceDestroy: true, |
| 76 | }); |
| 77 | return yield* Cloudflare.Pipelines.Sink("ListSink", { |
| 78 | type: "r2", |
| 79 | config: { |
| 80 | bucket: bucket.bucketName, |
| 81 | credentials: creds, |
| 82 | rollingPolicy: { intervalSeconds: 10 }, |
| 83 | }, |
| 84 | }); |
| 85 | }), |
| 86 | ), |
| 87 | ); |
| 88 | |
| 89 | expect(deployed.sinkId).toBeTruthy(); |
| 90 | |
| 91 | // Account collection: list() exhaustively paginates every sink in |
| 92 | // the account and hydrates each into the read Attributes shape. |
| 93 | const provider = yield* Provider.findProvider(Cloudflare.Pipelines.Sink); |
| 94 | const all = yield* provider.list(); |
| 95 | |
| 96 | const match = all.find((s) => s.sinkId === deployed.sinkId); |
| 97 | expect(match).toBeDefined(); |
| 98 | expect(match?.name).toEqual(deployed.name); |
| 99 | expect(match?.type).toEqual("r2"); |
| 100 | expect(match?.accountId).toEqual(deployed.accountId); |
| 101 | |
| 102 | yield* stack.destroy(); |
| 103 | }).pipe(logLevel), |
| 104 | { |
| 105 | tags: [ |
| 106 | "provider:cloudflare", |
| 107 | "provider:cloudflare:pipelines", |
| 108 | "provider:cloudflare:r2", |
| 109 | "live", |
| 110 | ], |
| 111 | timeout: 300_000, |
| 112 | }, |
| 113 | ); |