The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Cloudflare from "@alchemy.run/cloudflare"; |
| 2 | import * as Stripe from "@alchemy.run/stripe"; |
| 3 | import { expect } from "@alchemy.run/test"; |
| 4 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 5 | import * as Alchemy from "alchemy"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as HttpClient from "effect/http/HttpClient"; |
| 8 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 9 | import * as Layer from "effect/Layer"; |
| 10 | import { MinimumLogLevel } from "effect/References"; |
| 11 | import * as Schedule from "effect/Schedule"; |
| 12 | import StripeEventSourceWorker from "./fixtures/event-source-worker.ts"; |
| 13 | |
| 14 | const { test, beforeAll, afterAll, deploy, destroy } = Test.make({ |
| 15 | providers: Layer.mergeAll(Cloudflare.providers(), Stripe.providers()), |
| 16 | }); |
| 17 | const { executeWhenReady, getWhenReady } = Test; |
| 18 | |
| 19 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 20 | |
| 21 | const Stack = Alchemy.Stack( |
| 22 | "StripeEventSourceTestStack", |
| 23 | { |
| 24 | providers: Layer.mergeAll(Cloudflare.providers(), Stripe.providers()), |
| 25 | state: Cloudflare.state(), |
| 26 | }, |
| 27 | Effect.gen(function* () { |
| 28 | const worker = yield* StripeEventSourceWorker; |
| 29 | return { url: worker.url.as<string>() }; |
| 30 | }), |
| 31 | ); |
| 32 | |
| 33 | const stack = beforeAll(deploy(Stack)); |
| 34 | afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack)); |
| 35 | |
| 36 | // Real delivery to a fresh workers.dev URL is slow async provisioning |
| 37 | // (edge propagation + Stripe retry backoff + KV eventual consistency, |
| 38 | // ~1-4 min under full-suite load), so it is opt-in. The stripe-billing |
| 39 | // example integ exercises the same end-to-end delivery in its own run. |
| 40 | test.skipIf(process.env.STRIPE_TEST_REAL_DELIVERY !== "1")( |
| 41 | "consumeEvents records a CustomerCreated delivery", |
| 42 | Effect.gen(function* () { |
| 43 | const { url } = yield* stack; |
| 44 | const base = url.replace(/\/+$/, ""); |
| 45 | yield* getWhenReady(`${base}/last`); |
| 46 | |
| 47 | // Ride out binding cold-start: the Stripe CreateCustomer token may not |
| 48 | // be visible on the first request after a fresh deploy. |
| 49 | const created = yield* executeWhenReady(HttpClientRequest.post(`${base}/customers`)); |
| 50 | expect(created.status).toBe(201); |
| 51 | const body = (yield* created.json) as { id: string }; |
| 52 | expect(body.id).toMatch(/^cus_/); |
| 53 | |
| 54 | // Stripe delivers asynchronously to the fresh workers.dev URL and KV |
| 55 | // reads are eventually consistent, so poll up to ~3 minutes, absorbing |
| 56 | // transient transport errors during edge propagation. |
| 57 | const id = yield* Effect.gen(function* () { |
| 58 | const res = yield* HttpClient.execute(HttpClientRequest.get(`${base}/last/${body.id}`)); |
| 59 | if (res.status !== 200) return null; |
| 60 | const json = (yield* res.json) as { id: string | null }; |
| 61 | return json.id; |
| 62 | }).pipe( |
| 63 | Effect.catch(() => Effect.succeed(null)), |
| 64 | Effect.repeat({ |
| 65 | schedule: Schedule.spaced("5 seconds"), |
| 66 | until: (value) => value === body.id, |
| 67 | times: 36, |
| 68 | }), |
| 69 | ); |
| 70 | expect(id).toEqual(body.id); |
| 71 | }).pipe(logLevel), |
| 72 | { |
| 73 | tags: [ |
| 74 | "provider:cloudflare", |
| 75 | "provider:cloudflare:kv", |
| 76 | "provider:cloudflare:worker", |
| 77 | "provider:stripe", |
| 78 | "provider:stripe:customer", |
| 79 | "live", |
| 80 | ], |
| 81 | timeout: 240_000, |
| 82 | }, |
| 83 | ); |
| 84 | |
| 85 | test( |
| 86 | "invalid Stripe-Signature is rejected", |
| 87 | Effect.gen(function* () { |
| 88 | const { url } = yield* stack; |
| 89 | const base = url.replace(/\/+$/, ""); |
| 90 | yield* getWhenReady(`${base}/last`); |
| 91 | const res = yield* HttpClient.execute( |
| 92 | HttpClientRequest.post(`${base}/webhooks/stripe`).pipe( |
| 93 | HttpClientRequest.setHeader("stripe-signature", "t=1,v1=00"), |
| 94 | HttpClientRequest.bodyText("{}"), |
| 95 | ), |
| 96 | ); |
| 97 | expect(res.status).toBe(401); |
| 98 | }).pipe(logLevel), |
| 99 | { |
| 100 | tags: [ |
| 101 | "provider:cloudflare", |
| 102 | "provider:cloudflare:kv", |
| 103 | "provider:cloudflare:worker", |
| 104 | "provider:stripe", |
| 105 | "provider:stripe:customer", |
| 106 | "live", |
| 107 | ], |
| 108 | timeout: 120_000, |
| 109 | }, |
| 110 | ); |