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 { GetIssuingCards, GetIssuingCard } 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 ISSUING_ENABLED = process.env.STRIPE_TEST_ISSUING === "1"; |
| 15 | |
| 16 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 17 | |
| 18 | const billing = { |
| 19 | address: { |
| 20 | line1: "123 Main Street", |
| 21 | city: "San Francisco", |
| 22 | state: "CA", |
| 23 | postalCode: "94111", |
| 24 | country: "US", |
| 25 | }, |
| 26 | } as const; |
| 27 | |
| 28 | const waitUntilCanceled = (id: string) => |
| 29 | GetIssuingCard({ card: id }).pipe( |
| 30 | Effect.map((card) => (card.status === "canceled" ? ("canceled" as const) : ("live" as const))), |
| 31 | Effect.catchIf(isMissingStripeResource, () => Effect.succeed("canceled" as const)), |
| 32 | Effect.repeat({ |
| 33 | schedule: Schedule.spaced("1 second"), |
| 34 | until: (status) => status === "canceled", |
| 35 | times: 10, |
| 36 | }), |
| 37 | ); |
| 38 | |
| 39 | test.provider( |
| 40 | "issuing cards entitlement probe", |
| 41 | (stack) => |
| 42 | Effect.gen(function* () { |
| 43 | yield* stack.destroy(); |
| 44 | |
| 45 | const result = yield* GetIssuingCards({ limit: 1 }).pipe(Effect.result); |
| 46 | |
| 47 | if (Result.isSuccess(result)) { |
| 48 | expect(Array.isArray(result.success.data)).toBe(true); |
| 49 | } else { |
| 50 | expect(result.failure._tag).not.toEqual("UnknownStripeError"); |
| 51 | expect(["InvalidRequestError", "Forbidden", "Unauthorized"]).toContain(result.failure._tag); |
| 52 | if (result.failure._tag === "InvalidRequestError") { |
| 53 | expect(result.failure.message).toContain("not set up to use Issuing"); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | yield* stack.destroy(); |
| 58 | }).pipe(logLevel), |
| 59 | { |
| 60 | tags: ["provider:stripe", "provider:stripe:issuingcard", "live"], |
| 61 | timeout: 60_000, |
| 62 | }, |
| 63 | ); |
| 64 | |
| 65 | test.provider.skipIf(!ISSUING_ENABLED)( |
| 66 | "create, update, and cancel an issuing card", |
| 67 | (stack) => |
| 68 | Effect.gen(function* () { |
| 69 | yield* stack.destroy(); |
| 70 | |
| 71 | const created = yield* stack.deploy( |
| 72 | Effect.gen(function* () { |
| 73 | const cardholder = yield* Stripe.IssuingCardholder("ExpenseHolder", { |
| 74 | name: "Alchemy Card Holder", |
| 75 | type: "individual", |
| 76 | email: "alchemy.issuing.card@example.com", |
| 77 | phoneNumber: "+15555550100", |
| 78 | billing, |
| 79 | individual: { firstName: "Alchemy", lastName: "Tester" }, |
| 80 | }); |
| 81 | const card = yield* Stripe.IssuingCard("ExpenseCard", { |
| 82 | cardholder: cardholder.id, |
| 83 | currency: "usd", |
| 84 | type: "virtual", |
| 85 | status: "inactive", |
| 86 | metadata: { team: "ops" }, |
| 87 | }); |
| 88 | return { card, cardholder }; |
| 89 | }), |
| 90 | ); |
| 91 | |
| 92 | expect(created.card.id).toMatch(/^ic_/); |
| 93 | expect(created.card.cardholder).toEqual(created.cardholder.id); |
| 94 | expect(created.card.currency).toEqual("usd"); |
| 95 | expect(created.card.type).toEqual("virtual"); |
| 96 | expect(created.card.status).toEqual("inactive"); |
| 97 | expect(created.card.brand).toEqual(expect.any(String)); |
| 98 | expect(created.card.last4).toEqual(expect.any(String)); |
| 99 | expect(created.card.expMonth).toEqual(expect.any(Number)); |
| 100 | expect(created.card.expYear).toEqual(expect.any(Number)); |
| 101 | expect(created.card.metadata).toMatchObject({ team: "ops" }); |
| 102 | expect(created.card.livemode).toEqual(false); |
| 103 | |
| 104 | const fetched = yield* GetIssuingCard({ card: created.card.id }); |
| 105 | expect(fetched.id).toEqual(created.card.id); |
| 106 | expect(fetched.cardholder.id).toEqual(created.cardholder.id); |
| 107 | expect(fetched.currency).toEqual("usd"); |
| 108 | expect(fetched.type).toEqual("virtual"); |
| 109 | expect(fetched.status).toEqual("inactive"); |
| 110 | expect(fetched.metadata?.team).toEqual("ops"); |
| 111 | expect(fetched.metadata?.[Stripe.alchemyMetadataKeys.stack]).toBeDefined(); |
| 112 | expect(fetched.metadata?.[Stripe.alchemyMetadataKeys.stage]).toBeDefined(); |
| 113 | expect(fetched.metadata?.[Stripe.alchemyMetadataKeys.id]).toBeDefined(); |
| 114 | |
| 115 | const updated = yield* stack.deploy( |
| 116 | Effect.gen(function* () { |
| 117 | const cardholder = yield* Stripe.IssuingCardholder("ExpenseHolder", { |
| 118 | name: "Alchemy Card Holder", |
| 119 | type: "individual", |
| 120 | email: "alchemy.issuing.card@example.com", |
| 121 | phoneNumber: "+15555550100", |
| 122 | billing, |
| 123 | individual: { firstName: "Alchemy", lastName: "Tester" }, |
| 124 | }); |
| 125 | const card = yield* Stripe.IssuingCard("ExpenseCard", { |
| 126 | cardholder: cardholder.id, |
| 127 | currency: "usd", |
| 128 | type: "virtual", |
| 129 | status: "inactive", |
| 130 | spendingControls: { |
| 131 | spendingLimits: [{ amount: 50_000, interval: "monthly" }], |
| 132 | }, |
| 133 | metadata: { team: "finance", sku: "ic-2" }, |
| 134 | }); |
| 135 | return { card, cardholder }; |
| 136 | }), |
| 137 | ); |
| 138 | |
| 139 | expect(updated.card.id).toEqual(created.card.id); |
| 140 | expect(updated.card.status).toEqual("inactive"); |
| 141 | expect(updated.card.metadata).toEqual({ team: "finance", sku: "ic-2" }); |
| 142 | expect(updated.card.spendingControls?.spendingLimits).toEqual([ |
| 143 | { amount: 50_000, interval: "monthly" }, |
| 144 | ]); |
| 145 | |
| 146 | const refetched = yield* GetIssuingCard({ card: updated.card.id }); |
| 147 | expect(refetched.id).toEqual(updated.card.id); |
| 148 | expect(refetched.status).toEqual("inactive"); |
| 149 | expect(refetched.metadata?.team).toEqual("finance"); |
| 150 | expect(refetched.metadata?.sku).toEqual("ic-2"); |
| 151 | expect(refetched.metadata?.[Stripe.alchemyMetadataKeys.id]).toBeDefined(); |
| 152 | expect(refetched.spending_controls.spending_limits).toEqual( |
| 153 | expect.arrayContaining([ |
| 154 | expect.objectContaining({ |
| 155 | amount: 50_000, |
| 156 | interval: "monthly", |
| 157 | }), |
| 158 | ]), |
| 159 | ); |
| 160 | |
| 161 | yield* stack.destroy(); |
| 162 | |
| 163 | const canceled = yield* waitUntilCanceled(created.card.id); |
| 164 | expect(canceled).toEqual("canceled"); |
| 165 | }).pipe(logLevel), |
| 166 | { |
| 167 | tags: [ |
| 168 | "provider:stripe", |
| 169 | "provider:stripe:issuingcard", |
| 170 | "provider:stripe:issuingcardholder", |
| 171 | "live", |
| 172 | ], |
| 173 | timeout: 120_000, |
| 174 | }, |
| 175 | ); |
| 176 | |
| 177 | test.provider.skipIf(!ISSUING_ENABLED)( |
| 178 | "list enumerates the deployed issuing card", |
| 179 | (stack) => |
| 180 | Effect.gen(function* () { |
| 181 | yield* stack.destroy(); |
| 182 | |
| 183 | const deployed = yield* stack.deploy( |
| 184 | Effect.gen(function* () { |
| 185 | const cardholder = yield* Stripe.IssuingCardholder("ListHolder", { |
| 186 | name: "Alchemy List Holder", |
| 187 | type: "individual", |
| 188 | email: "alchemy.issuing.list@example.com", |
| 189 | phoneNumber: "+15555550100", |
| 190 | billing, |
| 191 | individual: { firstName: "Alchemy", lastName: "Lister" }, |
| 192 | }); |
| 193 | const card = yield* Stripe.IssuingCard("ListCard", { |
| 194 | cardholder: cardholder.id, |
| 195 | currency: "usd", |
| 196 | type: "virtual", |
| 197 | status: "inactive", |
| 198 | metadata: { kind: "list" }, |
| 199 | }); |
| 200 | return { card, cardholder }; |
| 201 | }), |
| 202 | ); |
| 203 | |
| 204 | const provider = yield* Provider.findProvider(Stripe.IssuingCard); |
| 205 | const all = yield* provider.list(); |
| 206 | const found = all.find((card) => card.id === deployed.card.id); |
| 207 | expect(found).toBeDefined(); |
| 208 | expect(found?.cardholder).toEqual(deployed.cardholder.id); |
| 209 | expect(found?.type).toEqual("virtual"); |
| 210 | expect(found?.metadata).toMatchObject({ kind: "list" }); |
| 211 | |
| 212 | yield* stack.destroy(); |
| 213 | |
| 214 | const canceled = yield* waitUntilCanceled(deployed.card.id); |
| 215 | expect(canceled).toEqual("canceled"); |
| 216 | |
| 217 | const after = yield* provider.list(); |
| 218 | expect(after.find((card) => card.id === deployed.card.id)).toBeUndefined(); |
| 219 | }).pipe(logLevel), |
| 220 | { |
| 221 | tags: [ |
| 222 | "provider:stripe", |
| 223 | "provider:stripe:issuingcard", |
| 224 | "provider:stripe:issuingcardholder", |
| 225 | "live", |
| 226 | ], |
| 227 | timeout: 120_000, |
| 228 | }, |
| 229 | ); |