The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Stripe from "@alchemy.run/stripe"; |
| 2 | import { isMissingStripeResource } from "@alchemy.run/stripe/missing"; |
| 3 | import { expect } from "@alchemy.run/test"; |
| 4 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 5 | import { DeleteAccount, GetAccountByAccount, CreateAccount } from "@distilled.cloud/stripe/stripe"; |
| 6 | import * as Provider from "alchemy/Provider"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import { MinimumLogLevel } from "effect/References"; |
| 9 | import * as Result from "effect/Result"; |
| 10 | import * as Schedule from "effect/Schedule"; |
| 11 | |
| 12 | const { test } = Test.make({ providers: Stripe.providers() }); |
| 13 | |
| 14 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 15 | |
| 16 | /** Opt-in: the testing Stripe account must be a Connect platform. */ |
| 17 | const CONNECT_ENABLED = process.env.STRIPE_TEST_CONNECT === "1"; |
| 18 | |
| 19 | const isMissing = isMissingStripeResource; |
| 20 | |
| 21 | const waitUntilGone = (id: string) => |
| 22 | GetAccountByAccount({ account: id }).pipe( |
| 23 | Effect.as("found" as const), |
| 24 | Effect.catchIf(isMissing, () => Effect.succeed("gone" as const)), |
| 25 | Effect.repeat({ |
| 26 | schedule: Schedule.spaced("1 second"), |
| 27 | until: (status) => status === "gone", |
| 28 | times: 10, |
| 29 | }), |
| 30 | ); |
| 31 | |
| 32 | const deleteAccount = (account: string) => |
| 33 | DeleteAccount({ account }).pipe(Effect.catchIf(isMissing, () => Effect.void)); |
| 34 | |
| 35 | const probeCreate = () => |
| 36 | CreateAccount({ |
| 37 | type: "express", |
| 38 | country: "US", |
| 39 | email: "alchemy.account.probe@example.com", |
| 40 | }); |
| 41 | |
| 42 | test.provider( |
| 43 | "connect accounts entitlement probe", |
| 44 | (stack) => |
| 45 | Effect.gen(function* () { |
| 46 | yield* stack.destroy(); |
| 47 | |
| 48 | const probe = yield* probeCreate().pipe(Effect.result); |
| 49 | if (Result.isFailure(probe)) { |
| 50 | expect(probe.failure._tag).not.toEqual("UnknownStripeError"); |
| 51 | expect(probe.failure._tag).toEqual("InvalidRequestError"); |
| 52 | expect(probe.failure.message ?? "").toContain("signed up for Connect"); |
| 53 | yield* stack.destroy(); |
| 54 | return; |
| 55 | } |
| 56 | |
| 57 | yield* deleteAccount(probe.success.id); |
| 58 | yield* stack.destroy(); |
| 59 | }).pipe(logLevel), |
| 60 | { |
| 61 | tags: ["provider:stripe", "provider:stripe:account", "live"], |
| 62 | timeout: 60_000, |
| 63 | }, |
| 64 | ); |
| 65 | |
| 66 | test.provider.skipIf(!CONNECT_ENABLED)( |
| 67 | "create, update, and delete a connected account", |
| 68 | (stack) => |
| 69 | Effect.gen(function* () { |
| 70 | yield* stack.destroy(); |
| 71 | |
| 72 | const created = yield* stack.deploy( |
| 73 | Effect.gen(function* () { |
| 74 | return yield* Stripe.Account("CatalogAccount", { |
| 75 | type: "express", |
| 76 | country: "US", |
| 77 | email: "alchemy.catalog.account@example.com", |
| 78 | businessType: "company", |
| 79 | defaultCurrency: "usd", |
| 80 | businessProfile: { |
| 81 | name: "Alchemy Catalog Merchant", |
| 82 | url: "https://alchemy.run", |
| 83 | mcc: "5734", |
| 84 | supportEmail: "support@alchemy.run", |
| 85 | }, |
| 86 | capabilities: { |
| 87 | card_payments: { requested: true }, |
| 88 | transfers: { requested: true }, |
| 89 | }, |
| 90 | settings: { |
| 91 | payouts: { schedule: { interval: "manual" } }, |
| 92 | }, |
| 93 | metadata: { tier: "gold" }, |
| 94 | }); |
| 95 | }), |
| 96 | ); |
| 97 | |
| 98 | expect(created.id).toMatch(/^acct_/); |
| 99 | expect(created.type).toEqual("express"); |
| 100 | expect(created.country).toEqual("US"); |
| 101 | expect(created.email).toEqual("alchemy.catalog.account@example.com"); |
| 102 | expect(created.businessType).toEqual("company"); |
| 103 | expect(created.defaultCurrency).toEqual("usd"); |
| 104 | expect(created.businessProfileName).toEqual("Alchemy Catalog Merchant"); |
| 105 | expect(created.businessProfileUrl).toEqual("https://alchemy.run"); |
| 106 | expect(created.metadata).toMatchObject({ tier: "gold" }); |
| 107 | expect(Object.keys(created.capabilities)).toEqual( |
| 108 | expect.arrayContaining(["card_payments", "transfers"]), |
| 109 | ); |
| 110 | |
| 111 | const fetched = yield* GetAccountByAccount({ account: created.id }); |
| 112 | expect(fetched.id).toEqual(created.id); |
| 113 | expect(fetched.email).toEqual(created.email); |
| 114 | expect(fetched.business_profile?.name).toEqual("Alchemy Catalog Merchant"); |
| 115 | expect(fetched.business_profile?.url).toEqual("https://alchemy.run"); |
| 116 | expect(fetched.settings?.payouts?.schedule?.interval).toEqual("manual"); |
| 117 | expect(fetched.metadata?.tier).toEqual("gold"); |
| 118 | expect(fetched.metadata?.[Stripe.alchemyMetadataKeys.stack]).toBeDefined(); |
| 119 | expect(fetched.metadata?.[Stripe.alchemyMetadataKeys.stage]).toBeDefined(); |
| 120 | expect(fetched.metadata?.[Stripe.alchemyMetadataKeys.id]).toBeDefined(); |
| 121 | |
| 122 | const updated = yield* stack.deploy( |
| 123 | Effect.gen(function* () { |
| 124 | return yield* Stripe.Account("CatalogAccount", { |
| 125 | type: "express", |
| 126 | country: "US", |
| 127 | email: "alchemy.catalog.account.updated@example.com", |
| 128 | businessType: "company", |
| 129 | defaultCurrency: "usd", |
| 130 | businessProfile: { |
| 131 | name: "Alchemy Catalog Merchant Updated", |
| 132 | url: "https://www.alchemy.run", |
| 133 | mcc: "5734", |
| 134 | supportEmail: "support@alchemy.run", |
| 135 | }, |
| 136 | capabilities: { |
| 137 | card_payments: { requested: true }, |
| 138 | transfers: { requested: true }, |
| 139 | }, |
| 140 | settings: { |
| 141 | payouts: { schedule: { interval: "manual" } }, |
| 142 | }, |
| 143 | metadata: { tier: "platinum", sku: "acct-1" }, |
| 144 | }); |
| 145 | }), |
| 146 | ); |
| 147 | |
| 148 | expect(updated.id).toEqual(created.id); |
| 149 | expect(updated.email).toEqual("alchemy.catalog.account.updated@example.com"); |
| 150 | expect(updated.businessProfileName).toEqual("Alchemy Catalog Merchant Updated"); |
| 151 | expect(updated.metadata).toEqual({ tier: "platinum", sku: "acct-1" }); |
| 152 | |
| 153 | const refetched = yield* GetAccountByAccount({ account: updated.id }); |
| 154 | expect(refetched.email).toEqual(updated.email); |
| 155 | expect(refetched.business_profile?.name).toEqual("Alchemy Catalog Merchant Updated"); |
| 156 | expect(refetched.business_profile?.url).toEqual("https://www.alchemy.run"); |
| 157 | expect(refetched.metadata?.tier).toEqual("platinum"); |
| 158 | expect(refetched.metadata?.sku).toEqual("acct-1"); |
| 159 | expect(refetched.metadata?.[Stripe.alchemyMetadataKeys.id]).toBeDefined(); |
| 160 | |
| 161 | yield* stack.destroy(); |
| 162 | |
| 163 | const gone = yield* waitUntilGone(created.id); |
| 164 | expect(gone).toEqual("gone"); |
| 165 | }).pipe(logLevel), |
| 166 | { |
| 167 | tags: ["provider:stripe", "provider:stripe:account", "live"], |
| 168 | timeout: 120_000, |
| 169 | }, |
| 170 | ); |
| 171 | |
| 172 | test.provider.skipIf(!CONNECT_ENABLED)( |
| 173 | "list enumerates the deployed connected account", |
| 174 | (stack) => |
| 175 | Effect.gen(function* () { |
| 176 | yield* stack.destroy(); |
| 177 | |
| 178 | const deployed = yield* stack.deploy( |
| 179 | Effect.gen(function* () { |
| 180 | return yield* Stripe.Account("ListAccount", { |
| 181 | type: "express", |
| 182 | country: "US", |
| 183 | email: "alchemy.list.account@example.com", |
| 184 | metadata: { kind: "list" }, |
| 185 | }); |
| 186 | }), |
| 187 | ); |
| 188 | |
| 189 | const provider = yield* Provider.findProvider(Stripe.Account); |
| 190 | const all = yield* provider.list(); |
| 191 | const found = all.find((account) => account.id === deployed.id); |
| 192 | expect(found).toBeDefined(); |
| 193 | expect(found?.email).toEqual(deployed.email); |
| 194 | expect(found?.metadata).toMatchObject({ kind: "list" }); |
| 195 | |
| 196 | yield* stack.destroy(); |
| 197 | |
| 198 | const gone = yield* waitUntilGone(deployed.id); |
| 199 | expect(gone).toEqual("gone"); |
| 200 | }).pipe(logLevel), |
| 201 | { |
| 202 | tags: ["provider:stripe", "provider:stripe:account", "live"], |
| 203 | timeout: 120_000, |
| 204 | }, |
| 205 | ); |