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 * 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 { |
| 6 | DeleteAccount, |
| 7 | GetAccountExternalAccount, |
| 8 | CreateAccount, |
| 9 | CreateToken, |
| 10 | } from "@distilled.cloud/stripe/stripe"; |
| 11 | import * as Provider from "alchemy/Provider"; |
| 12 | import * as Effect from "effect/Effect"; |
| 13 | import { MinimumLogLevel } from "effect/References"; |
| 14 | import * as Result from "effect/Result"; |
| 15 | import * as Schedule from "effect/Schedule"; |
| 16 | |
| 17 | const { test } = Test.make({ providers: Stripe.providers() }); |
| 18 | |
| 19 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 20 | |
| 21 | /** Opt-in: the testing Stripe account must be a Connect platform. */ |
| 22 | const CONNECT_ENABLED = process.env.STRIPE_TEST_CONNECT === "1"; |
| 23 | |
| 24 | const isMissing = isMissingStripeResource; |
| 25 | |
| 26 | const waitUntilGone = (account: string, id: string) => |
| 27 | GetAccountExternalAccount({ account, id }).pipe( |
| 28 | Effect.as("found" as const), |
| 29 | Effect.catchIf(isMissing, () => Effect.succeed("gone" as const)), |
| 30 | Effect.repeat({ |
| 31 | schedule: Schedule.spaced("1 second"), |
| 32 | until: (status) => status === "gone", |
| 33 | times: 10, |
| 34 | }), |
| 35 | ); |
| 36 | |
| 37 | const deleteAccount = (account: string) => |
| 38 | DeleteAccount({ account }).pipe( |
| 39 | Effect.catchIf(isMissing, () => Effect.void), |
| 40 | Effect.catchIf( |
| 41 | (e) => e._tag === "InvalidRequestError", |
| 42 | () => Effect.void, |
| 43 | ), |
| 44 | Effect.asVoid, |
| 45 | ); |
| 46 | |
| 47 | const createBankToken = (holderName: string) => |
| 48 | CreateToken({ |
| 49 | bank_account: { |
| 50 | country: "US", |
| 51 | currency: "usd", |
| 52 | account_holder_name: holderName, |
| 53 | account_holder_type: "individual", |
| 54 | routing_number: "110000000", |
| 55 | account_number: "000123456789", |
| 56 | }, |
| 57 | }); |
| 58 | |
| 59 | const connectAccountProps = { |
| 60 | type: "custom" as const, |
| 61 | country: "US", |
| 62 | capabilities: { |
| 63 | transfers: { requested: true }, |
| 64 | }, |
| 65 | tosAcceptance: { |
| 66 | date: 1_609_459_200, |
| 67 | ip: "127.0.0.1", |
| 68 | }, |
| 69 | businessType: "individual" as const, |
| 70 | individual: { |
| 71 | first_name: "Alchemy", |
| 72 | last_name: "Tester", |
| 73 | }, |
| 74 | }; |
| 75 | |
| 76 | const probeConnectAccount = () => |
| 77 | CreateAccount({ |
| 78 | type: "custom", |
| 79 | country: "US", |
| 80 | email: "alchemy.account-external-account.probe@example.com", |
| 81 | capabilities: { |
| 82 | transfers: { requested: true }, |
| 83 | }, |
| 84 | tos_acceptance: { |
| 85 | date: 1_609_459_200, |
| 86 | ip: "127.0.0.1", |
| 87 | }, |
| 88 | business_type: "individual", |
| 89 | individual: { |
| 90 | first_name: "Alchemy", |
| 91 | last_name: "Tester", |
| 92 | }, |
| 93 | }); |
| 94 | |
| 95 | test.provider( |
| 96 | "connect accounts entitlement probe", |
| 97 | (stack) => |
| 98 | Effect.gen(function* () { |
| 99 | yield* stack.destroy(); |
| 100 | |
| 101 | const probe = yield* probeConnectAccount().pipe(Effect.result); |
| 102 | if (Result.isFailure(probe)) { |
| 103 | expect(probe.failure._tag).not.toEqual("UnknownStripeError"); |
| 104 | expect(probe.failure._tag).toEqual("InvalidRequestError"); |
| 105 | expect(probe.failure.message ?? "").toContain("signed up for Connect"); |
| 106 | yield* stack.destroy(); |
| 107 | return; |
| 108 | } |
| 109 | |
| 110 | yield* deleteAccount(probe.success.id); |
| 111 | yield* stack.destroy(); |
| 112 | }).pipe(logLevel), |
| 113 | { |
| 114 | tags: [ |
| 115 | "provider:stripe", |
| 116 | "provider:stripe:account", |
| 117 | "provider:stripe:accountexternalaccount", |
| 118 | "live", |
| 119 | ], |
| 120 | timeout: 60_000, |
| 121 | }, |
| 122 | ); |
| 123 | |
| 124 | test.provider.skipIf(!CONNECT_ENABLED)( |
| 125 | "create, update, and delete an account external account", |
| 126 | (stack) => |
| 127 | Effect.gen(function* () { |
| 128 | yield* stack.destroy(); |
| 129 | |
| 130 | const token = yield* createBankToken("Jenny Rosen"); |
| 131 | |
| 132 | const created = yield* stack.deploy( |
| 133 | Effect.gen(function* () { |
| 134 | const account = yield* Stripe.Account("ConnectAccount", { |
| 135 | ...connectAccountProps, |
| 136 | email: "alchemy.aea.lifecycle@example.com", |
| 137 | }); |
| 138 | const externalAccount = yield* Stripe.AccountExternalAccount("PayoutBank", { |
| 139 | account: account.id, |
| 140 | externalAccount: token.id, |
| 141 | accountHolderName: "Jenny Rosen", |
| 142 | accountHolderType: "individual", |
| 143 | defaultForCurrency: true, |
| 144 | metadata: { purpose: "payouts" }, |
| 145 | }); |
| 146 | return { account, externalAccount }; |
| 147 | }), |
| 148 | ); |
| 149 | |
| 150 | expect(created.externalAccount.id).toMatch(/^(ba_|card_)/); |
| 151 | expect(created.externalAccount.account).toEqual(created.account.id); |
| 152 | expect(created.externalAccount.object).toEqual("bank_account"); |
| 153 | expect(created.externalAccount.last4).toEqual("6789"); |
| 154 | expect(created.externalAccount.country).toEqual("US"); |
| 155 | expect(created.externalAccount.currency).toEqual("usd"); |
| 156 | expect(created.externalAccount.accountHolderName).toEqual("Jenny Rosen"); |
| 157 | expect(created.externalAccount.accountHolderType).toEqual("individual"); |
| 158 | expect(created.externalAccount.defaultForCurrency).toEqual(true); |
| 159 | expect(created.externalAccount.metadata).toMatchObject({ |
| 160 | purpose: "payouts", |
| 161 | }); |
| 162 | expect(created.externalAccount.routingNumber).toEqual("110000000"); |
| 163 | |
| 164 | const fetched = yield* GetAccountExternalAccount({ |
| 165 | account: created.externalAccount.account, |
| 166 | id: created.externalAccount.id, |
| 167 | }); |
| 168 | expect(fetched.id).toEqual(created.externalAccount.id); |
| 169 | expect(fetched.object).toEqual("bank_account"); |
| 170 | if (fetched.object === "bank_account") { |
| 171 | expect(fetched.last4).toEqual("6789"); |
| 172 | expect(fetched.account_holder_name).toEqual("Jenny Rosen"); |
| 173 | expect(fetched.account_holder_type).toEqual("individual"); |
| 174 | expect(fetched.metadata?.purpose).toEqual("payouts"); |
| 175 | expect(fetched.metadata?.[Stripe.alchemyMetadataKeys.stack]).toBeDefined(); |
| 176 | expect(fetched.metadata?.[Stripe.alchemyMetadataKeys.stage]).toBeDefined(); |
| 177 | expect(fetched.metadata?.[Stripe.alchemyMetadataKeys.id]).toBeDefined(); |
| 178 | } |
| 179 | |
| 180 | const updated = yield* stack.deploy( |
| 181 | Effect.gen(function* () { |
| 182 | const account = yield* Stripe.Account("ConnectAccount", { |
| 183 | ...connectAccountProps, |
| 184 | email: "alchemy.aea.lifecycle@example.com", |
| 185 | }); |
| 186 | const externalAccount = yield* Stripe.AccountExternalAccount("PayoutBank", { |
| 187 | account: account.id, |
| 188 | externalAccount: token.id, |
| 189 | accountHolderName: "Alchemy Tester", |
| 190 | accountHolderType: "company", |
| 191 | defaultForCurrency: true, |
| 192 | metadata: { purpose: "payroll", region: "us" }, |
| 193 | }); |
| 194 | return { account, externalAccount }; |
| 195 | }), |
| 196 | ); |
| 197 | |
| 198 | expect(updated.externalAccount.id).toEqual(created.externalAccount.id); |
| 199 | expect(updated.externalAccount.account).toEqual(created.account.id); |
| 200 | expect(updated.externalAccount.accountHolderName).toEqual("Alchemy Tester"); |
| 201 | expect(updated.externalAccount.accountHolderType).toEqual("company"); |
| 202 | expect(updated.externalAccount.metadata).toEqual({ |
| 203 | purpose: "payroll", |
| 204 | region: "us", |
| 205 | }); |
| 206 | |
| 207 | const refetched = yield* GetAccountExternalAccount({ |
| 208 | account: updated.externalAccount.account, |
| 209 | id: updated.externalAccount.id, |
| 210 | }); |
| 211 | if (refetched.object === "bank_account") { |
| 212 | expect(refetched.account_holder_name).toEqual("Alchemy Tester"); |
| 213 | expect(refetched.account_holder_type).toEqual("company"); |
| 214 | expect(refetched.metadata?.purpose).toEqual("payroll"); |
| 215 | expect(refetched.metadata?.region).toEqual("us"); |
| 216 | expect(refetched.metadata?.[Stripe.alchemyMetadataKeys.id]).toBeDefined(); |
| 217 | } |
| 218 | |
| 219 | yield* stack.destroy(); |
| 220 | |
| 221 | const gone = yield* waitUntilGone( |
| 222 | created.externalAccount.account, |
| 223 | created.externalAccount.id, |
| 224 | ); |
| 225 | expect(gone).toEqual("gone"); |
| 226 | }).pipe(logLevel), |
| 227 | { |
| 228 | tags: [ |
| 229 | "provider:stripe", |
| 230 | "provider:stripe:account", |
| 231 | "provider:stripe:accountexternalaccount", |
| 232 | "live", |
| 233 | ], |
| 234 | timeout: 120_000, |
| 235 | }, |
| 236 | ); |
| 237 | |
| 238 | test.provider.skipIf(!CONNECT_ENABLED)( |
| 239 | "list enumerates the deployed account external account", |
| 240 | (stack) => |
| 241 | Effect.gen(function* () { |
| 242 | yield* stack.destroy(); |
| 243 | |
| 244 | const token = yield* createBankToken("List Holder"); |
| 245 | |
| 246 | const deployed = yield* stack.deploy( |
| 247 | Effect.gen(function* () { |
| 248 | const account = yield* Stripe.Account("ListConnectAccount", { |
| 249 | ...connectAccountProps, |
| 250 | email: "alchemy.aea.list@example.com", |
| 251 | }); |
| 252 | const externalAccount = yield* Stripe.AccountExternalAccount("ListPayoutBank", { |
| 253 | account: account.id, |
| 254 | externalAccount: token.id, |
| 255 | accountHolderName: "List Holder", |
| 256 | metadata: { kind: "list" }, |
| 257 | }); |
| 258 | return { account, externalAccount }; |
| 259 | }), |
| 260 | ); |
| 261 | |
| 262 | const provider = yield* Provider.findProvider(Stripe.AccountExternalAccount); |
| 263 | const all = yield* provider.list(); |
| 264 | const found = all.find((ea) => ea.id === deployed.externalAccount.id); |
| 265 | expect(found).toBeDefined(); |
| 266 | expect(found?.account).toEqual(deployed.account.id); |
| 267 | expect(found?.metadata).toMatchObject({ kind: "list" }); |
| 268 | expect(found?.last4).toEqual("6789"); |
| 269 | |
| 270 | yield* stack.destroy(); |
| 271 | |
| 272 | const gone = yield* waitUntilGone( |
| 273 | deployed.externalAccount.account, |
| 274 | deployed.externalAccount.id, |
| 275 | ); |
| 276 | expect(gone).toEqual("gone"); |
| 277 | |
| 278 | const after = yield* provider.list(); |
| 279 | expect(after.find((ea) => ea.id === deployed.externalAccount.id)).toBeUndefined(); |
| 280 | }).pipe(logLevel), |
| 281 | { |
| 282 | tags: [ |
| 283 | "provider:stripe", |
| 284 | "provider:stripe:account", |
| 285 | "provider:stripe:accountexternalaccount", |
| 286 | "live", |
| 287 | ], |
| 288 | timeout: 120_000, |
| 289 | }, |
| 290 | ); |