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 logpush from "@distilled.cloud/cloudflare/logpush"; |
| 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 | // Cloudflare's edge intermittently 403s ("Unable to authenticate request") |
| 20 | // even for established tokens. The blip can hit engine-side calls mid-deploy |
| 21 | // as well as the test's own out-of-band calls. Reconcile is idempotent |
| 22 | // (observe→ensure→sync), so retrying the whole deploy is safe. The message |
| 23 | // match (rather than a typed tag) is deliberate: this wraps whole deploys |
| 24 | // whose error channel is the engine's aggregate, not a single distilled op. |
| 25 | const retryAuthBlip = <A, E, R>(eff: Effect.Effect<A, E, R>) => |
| 26 | eff.pipe( |
| 27 | Effect.retry({ |
| 28 | while: (e) => String(e).includes("Unable to authenticate request"), |
| 29 | schedule: Schedule.exponential("1 second"), |
| 30 | times: 5, |
| 31 | }), |
| 32 | ); |
| 33 | |
| 34 | const r2Credentials = Effect.gen(function* () { |
| 35 | const creds = yield* yield* CloudflareEnvironment; |
| 36 | if (creds.type !== "apiToken") { |
| 37 | return yield* Effect.die( |
| 38 | new Error( |
| 39 | "Logpush R2 test requires an apiToken profile (R2 S3 credentials are derived from the token)", |
| 40 | ), |
| 41 | ); |
| 42 | } |
| 43 | const token = Redacted.value(creds.apiToken); |
| 44 | const verified = yield* user.verifyToken({}).pipe( |
| 45 | Effect.retry({ |
| 46 | while: (e) => e._tag === "Forbidden", |
| 47 | schedule: Schedule.exponential("1 second"), |
| 48 | times: 5, |
| 49 | }), |
| 50 | ); |
| 51 | const secretAccessKey = yield* Effect.sync(() => |
| 52 | crypto.createHash("sha256").update(token).digest("hex"), |
| 53 | ); |
| 54 | return { |
| 55 | accountId: creds.accountId, |
| 56 | accessKeyId: verified.id, |
| 57 | secretAccessKey, |
| 58 | }; |
| 59 | }); |
| 60 | |
| 61 | interface R2Creds { |
| 62 | accountId: string; |
| 63 | accessKeyId: string; |
| 64 | secretAccessKey: string; |
| 65 | } |
| 66 | |
| 67 | interface JobOpts { |
| 68 | dataset: Cloudflare.Logpush.Dataset; |
| 69 | enabled?: boolean; |
| 70 | maxUploadIntervalSeconds?: number; |
| 71 | outputOptions?: Cloudflare.Logpush.OutputOptions; |
| 72 | } |
| 73 | |
| 74 | // One program deploying both the R2 destination bucket and the Logpush job |
| 75 | // pushing into it. The job's destinationConf interpolates the bucket name so |
| 76 | // the engine orders job-after-bucket on deploy (and the reverse on destroy). |
| 77 | const program = (creds: R2Creds, opts: JobOpts) => |
| 78 | Effect.gen(function* () { |
| 79 | const bucket = yield* Cloudflare.R2.Bucket("LogpushBucket", { |
| 80 | forceDestroy: true, |
| 81 | }); |
| 82 | const job = yield* Cloudflare.Logpush.Job("Job", { |
| 83 | dataset: opts.dataset, |
| 84 | destinationConf: Output.interpolate`r2://${bucket.bucketName}/alchemy/{DATE}?account-id=${creds.accountId}&access-key-id=${creds.accessKeyId}&secret-access-key=${creds.secretAccessKey}`, |
| 85 | enabled: opts.enabled, |
| 86 | maxUploadIntervalSeconds: opts.maxUploadIntervalSeconds, |
| 87 | outputOptions: opts.outputOptions, |
| 88 | }); |
| 89 | return { bucket, job }; |
| 90 | }); |
| 91 | |
| 92 | // `Forbidden` is in `getJobForAccount`'s typed error union (the same edge |
| 93 | // 403 blip described above), so the retry predicate is fully inferred. |
| 94 | const getJob = (accountId: string, jobId: number) => |
| 95 | logpush.getJobForAccount({ accountId, jobId }).pipe( |
| 96 | Effect.retry({ |
| 97 | while: (e) => e._tag === "Forbidden", |
| 98 | schedule: Schedule.exponential("500 millis"), |
| 99 | times: 8, |
| 100 | }), |
| 101 | ); |
| 102 | |
| 103 | const waitForDelete = (accountId: string, jobId: number) => |
| 104 | getJob(accountId, jobId).pipe( |
| 105 | Effect.flatMap((job) => |
| 106 | job.id === jobId ? Effect.fail({ _tag: "JobNotDeleted" } as const) : Effect.void, |
| 107 | ), |
| 108 | Effect.catchTag("JobNotFound", () => Effect.void), |
| 109 | Effect.retry({ |
| 110 | while: (e) => e._tag === "JobNotDeleted", |
| 111 | schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(10)]), |
| 112 | }), |
| 113 | ); |
| 114 | |
| 115 | test.provider( |
| 116 | "create, update, and delete an account-scoped job pushing to R2", |
| 117 | (stack) => |
| 118 | Effect.gen(function* () { |
| 119 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 120 | const creds = yield* r2Credentials; |
| 121 | |
| 122 | yield* stack.destroy(); |
| 123 | |
| 124 | // Create — engine-generated name, disabled to avoid pushing noise. |
| 125 | const initial = yield* retryAuthBlip( |
| 126 | stack.deploy( |
| 127 | program(creds, { |
| 128 | dataset: "workers_trace_events", |
| 129 | enabled: false, |
| 130 | }), |
| 131 | ), |
| 132 | ); |
| 133 | |
| 134 | expect(initial.job.jobId).toBeTypeOf("number"); |
| 135 | expect(initial.job.accountId).toEqual(accountId); |
| 136 | expect(initial.job.zoneId).toBeUndefined(); |
| 137 | expect(initial.job.dataset).toEqual("workers_trace_events"); |
| 138 | expect(initial.job.enabled).toEqual(false); |
| 139 | expect(initial.job.destinationConf).toContain( |
| 140 | `r2://${initial.bucket.bucketName}/alchemy/{DATE}`, |
| 141 | ); |
| 142 | |
| 143 | const live = yield* getJob(accountId, initial.job.jobId); |
| 144 | expect(live.id).toEqual(initial.job.jobId); |
| 145 | expect(live.dataset).toEqual("workers_trace_events"); |
| 146 | expect(live.enabled).toEqual(false); |
| 147 | expect(live.name).toEqual(initial.job.name); |
| 148 | |
| 149 | // Update mutable props in place — same job id. |
| 150 | const updated = yield* retryAuthBlip( |
| 151 | stack.deploy( |
| 152 | program(creds, { |
| 153 | dataset: "workers_trace_events", |
| 154 | enabled: true, |
| 155 | maxUploadIntervalSeconds: 60, |
| 156 | outputOptions: { |
| 157 | fieldNames: ["EventTimestampMs", "Outcome", "ScriptName"], |
| 158 | timestampFormat: "rfc3339", |
| 159 | }, |
| 160 | }), |
| 161 | ), |
| 162 | ); |
| 163 | |
| 164 | expect(updated.job.jobId).toEqual(initial.job.jobId); |
| 165 | expect(updated.job.enabled).toEqual(true); |
| 166 | |
| 167 | const liveUpdated = yield* getJob(accountId, updated.job.jobId); |
| 168 | expect(liveUpdated.enabled).toEqual(true); |
| 169 | expect(liveUpdated.maxUploadIntervalSeconds).toEqual(60); |
| 170 | expect(liveUpdated.outputOptions?.fieldNames).toEqual([ |
| 171 | "EventTimestampMs", |
| 172 | "Outcome", |
| 173 | "ScriptName", |
| 174 | ]); |
| 175 | |
| 176 | yield* stack.destroy(); |
| 177 | |
| 178 | yield* waitForDelete(accountId, initial.job.jobId); |
| 179 | |
| 180 | // Destroy again — delete must be idempotent (the job is already gone). |
| 181 | yield* stack.destroy(); |
| 182 | }).pipe(logLevel), |
| 183 | { |
| 184 | tags: ["provider:cloudflare", "provider:cloudflare:logpush", "provider:cloudflare:r2", "live"], |
| 185 | timeout: 180_000, |
| 186 | }, |
| 187 | ); |
| 188 | |
| 189 | test.provider( |
| 190 | "list enumerates the deployed Logpush job", |
| 191 | (stack) => |
| 192 | Effect.gen(function* () { |
| 193 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 194 | const creds = yield* r2Credentials; |
| 195 | |
| 196 | yield* stack.destroy(); |
| 197 | |
| 198 | const deployed = yield* retryAuthBlip( |
| 199 | stack.deploy( |
| 200 | program(creds, { |
| 201 | dataset: "workers_trace_events", |
| 202 | enabled: false, |
| 203 | }), |
| 204 | ), |
| 205 | ); |
| 206 | |
| 207 | const provider = yield* Provider.findProvider(Cloudflare.Logpush.Job); |
| 208 | const all = yield* provider.list(); |
| 209 | |
| 210 | // The account-scoped job we just deployed is present in the |
| 211 | // exhaustively-paginated (account + per-zone fan-out) result. |
| 212 | const found = all.find((job) => job.jobId === deployed.job.jobId); |
| 213 | expect(found).toBeDefined(); |
| 214 | expect(found?.accountId).toEqual(accountId); |
| 215 | expect(found?.zoneId).toBeUndefined(); |
| 216 | expect(found?.dataset).toEqual("workers_trace_events"); |
| 217 | |
| 218 | yield* stack.destroy(); |
| 219 | |
| 220 | yield* waitForDelete(accountId, deployed.job.jobId); |
| 221 | |
| 222 | yield* stack.destroy(); |
| 223 | }).pipe(logLevel), |
| 224 | { |
| 225 | tags: ["provider:cloudflare", "provider:cloudflare:logpush", "provider:cloudflare:r2", "live"], |
| 226 | timeout: 180_000, |
| 227 | }, |
| 228 | ); |
| 229 | |
| 230 | // Requires entitlement for a second account-scoped dataset. On the testing |
| 231 | // account (Workers Paid) every dataset except `workers_trace_events` is |
| 232 | // rejected with: |
| 233 | // Forbidden: creating a new job (for <dataset> dataset) is not allowed: |
| 234 | // exceeded max jobs allowed |
| 235 | // (verified for audit_logs, audit_logs_v2, access_requests, gateway_dns, |
| 236 | // casb_findings, dns_firewall_logs), so a dataset-change replacement cannot |
| 237 | // be exercised here. Enable on an account with a second Logpush-entitled |
| 238 | // dataset. |
| 239 | test.provider.skip( |
| 240 | "changing the dataset triggers a replacement", |
| 241 | (stack) => |
| 242 | Effect.gen(function* () { |
| 243 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 244 | const creds = yield* r2Credentials; |
| 245 | |
| 246 | yield* stack.destroy(); |
| 247 | |
| 248 | const initial = yield* retryAuthBlip( |
| 249 | stack.deploy(program(creds, { dataset: "workers_trace_events" })), |
| 250 | ); |
| 251 | expect(initial.job.dataset).toEqual("workers_trace_events"); |
| 252 | |
| 253 | const replaced = yield* retryAuthBlip( |
| 254 | stack.deploy(program(creds, { dataset: "audit_logs_v2" })), |
| 255 | ); |
| 256 | |
| 257 | // The dataset is fixed at creation — a new physical job exists. |
| 258 | expect(replaced.job.jobId).not.toEqual(initial.job.jobId); |
| 259 | expect(replaced.job.dataset).toEqual("audit_logs_v2"); |
| 260 | |
| 261 | const liveReplaced = yield* getJob(accountId, replaced.job.jobId); |
| 262 | expect(liveReplaced.dataset).toEqual("audit_logs_v2"); |
| 263 | |
| 264 | // The old job was deleted as part of the replacement. |
| 265 | yield* waitForDelete(accountId, initial.job.jobId); |
| 266 | |
| 267 | yield* stack.destroy(); |
| 268 | |
| 269 | yield* waitForDelete(accountId, replaced.job.jobId); |
| 270 | }).pipe(logLevel), |
| 271 | { |
| 272 | tags: ["provider:cloudflare", "provider:cloudflare:logpush", "provider:cloudflare:r2", "live"], |
| 273 | timeout: 180_000, |
| 274 | }, |
| 275 | ); |