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