| 1 | import * as Provider from "@/Provider"; |
| 2 | import * as Stripe from "@/Stripe"; |
| 3 | import * as Test from "@/Test/Alchemy"; |
| 4 | import { |
| 5 | GetIssuingPersonalizationDesigns, |
| 6 | GetIssuingPersonalizationDesign, |
| 7 | GetIssuingPhysicalBundles, |
| 8 | } from "@distilled.cloud/stripe/stripe"; |
| 9 | import { expect } from "alchemy-test"; |
| 10 | import * as Effect from "effect/Effect"; |
| 11 | import { MinimumLogLevel } from "effect/References"; |
| 12 | import * as Result from "effect/Result"; |
| 13 | import { isMissingStripeResource } from "@/Stripe/missing.ts"; |
| 14 | |
| 15 | const { test } = Test.make({ providers: Stripe.providers() }); |
| 16 | |
| 17 | const logLevel = Effect.provideService( |
| 18 | MinimumLogLevel, |
| 19 | process.env.DEBUG ? "Debug" : "Info", |
| 20 | ); |
| 21 | |
| 22 | const ISSUING_ENABLED = process.env.STRIPE_TEST_ISSUING === "1"; |
| 23 | |
| 24 | const pickPhysicalBundle = Effect.gen(function* () { |
| 25 | const standard = yield* GetIssuingPhysicalBundles({ |
| 26 | type: "standard", |
| 27 | status: "active", |
| 28 | limit: 1, |
| 29 | }); |
| 30 | if (standard.data[0] !== undefined) return standard.data[0]; |
| 31 | const active = yield* GetIssuingPhysicalBundles({ |
| 32 | status: "active", |
| 33 | limit: 1, |
| 34 | }); |
| 35 | if (active.data[0] !== undefined) return active.data[0]; |
| 36 | const any = yield* GetIssuingPhysicalBundles({ limit: 1 }); |
| 37 | return any.data[0]; |
| 38 | }); |
| 39 | |
| 40 | test.provider( |
| 41 | "issuing personalization designs entitlement probe", |
| 42 | (stack) => |
| 43 | Effect.gen(function* () { |
| 44 | yield* stack.destroy(); |
| 45 | |
| 46 | const result = yield* GetIssuingPersonalizationDesigns({ limit: 1 }).pipe( |
| 47 | Effect.result, |
| 48 | ); |
| 49 | |
| 50 | if (Result.isFailure(result)) { |
| 51 | // Unentitled — distilled must fail with a typed tag, never |
| 52 | // UnknownStripeError. Exact error: |
| 53 | // InvalidRequestError: "Your account is not set up to use Issuing. |
| 54 | // Please visit https://dashboard.stripe.com/issuing/overview to get |
| 55 | // started." |
| 56 | expect(result.failure._tag).not.toEqual("UnknownStripeError"); |
| 57 | expect(["InvalidRequestError", "Forbidden"]).toContain( |
| 58 | result.failure._tag, |
| 59 | ); |
| 60 | if (result.failure._tag === "InvalidRequestError") { |
| 61 | expect(result.failure.message).toContain("not set up to use Issuing"); |
| 62 | } |
| 63 | yield* stack.destroy(); |
| 64 | return; |
| 65 | } |
| 66 | |
| 67 | expect(Array.isArray(result.success.data)).toBe(true); |
| 68 | yield* stack.destroy(); |
| 69 | }).pipe(logLevel), |
| 70 | { |
| 71 | tags: [ |
| 72 | "provider:stripe", |
| 73 | "provider:stripe:issuingpersonalizationdesign", |
| 74 | "live", |
| 75 | ], |
| 76 | timeout: 60_000, |
| 77 | }, |
| 78 | ); |
| 79 | |
| 80 | test.provider.skipIf(!ISSUING_ENABLED)( |
| 81 | "create, update, and destroy a personalization design", |
| 82 | (stack) => |
| 83 | Effect.gen(function* () { |
| 84 | yield* stack.destroy(); |
| 85 | |
| 86 | const bundle = yield* pickPhysicalBundle; |
| 87 | expect(bundle).toBeDefined(); |
| 88 | const physicalBundle = bundle!.id; |
| 89 | |
| 90 | const created = yield* stack.deploy( |
| 91 | Effect.gen(function* () { |
| 92 | return yield* Stripe.IssuingPersonalizationDesign("CardDesign", { |
| 93 | physicalBundle, |
| 94 | name: "Alchemy Card Design", |
| 95 | metadata: { line: "test" }, |
| 96 | }); |
| 97 | }), |
| 98 | ); |
| 99 | |
| 100 | expect(created.id).toMatch(/^ipcd_/); |
| 101 | expect(created.name).toEqual("Alchemy Card Design"); |
| 102 | expect(created.physicalBundle).toEqual(physicalBundle); |
| 103 | expect(created.lookupKey).toEqual(expect.any(String)); |
| 104 | expect(created.isDefault).toEqual(false); |
| 105 | expect(created.metadata).toMatchObject({ line: "test" }); |
| 106 | expect(created.created).toEqual(expect.any(Number)); |
| 107 | expect(created.livemode).toEqual(false); |
| 108 | expect(["active", "inactive", "rejected", "review"]).toContain( |
| 109 | created.status, |
| 110 | ); |
| 111 | |
| 112 | const fetched = yield* GetIssuingPersonalizationDesign({ |
| 113 | personalization_design: created.id, |
| 114 | }); |
| 115 | expect(fetched.id).toEqual(created.id); |
| 116 | expect(fetched.name).toEqual("Alchemy Card Design"); |
| 117 | expect( |
| 118 | typeof fetched.physical_bundle === "string" |
| 119 | ? fetched.physical_bundle |
| 120 | : fetched.physical_bundle.id, |
| 121 | ).toEqual(physicalBundle); |
| 122 | expect(fetched.metadata?.line).toEqual("test"); |
| 123 | expect( |
| 124 | fetched.metadata?.[Stripe.alchemyMetadataKeys.stack], |
| 125 | ).toBeDefined(); |
| 126 | expect( |
| 127 | fetched.metadata?.[Stripe.alchemyMetadataKeys.stage], |
| 128 | ).toBeDefined(); |
| 129 | expect(fetched.metadata?.[Stripe.alchemyMetadataKeys.id]).toBeDefined(); |
| 130 | |
| 131 | const updated = yield* stack.deploy( |
| 132 | Effect.gen(function* () { |
| 133 | return yield* Stripe.IssuingPersonalizationDesign("CardDesign", { |
| 134 | physicalBundle, |
| 135 | name: "Alchemy Card Design Updated", |
| 136 | metadata: { line: "test", version: "2" }, |
| 137 | }); |
| 138 | }), |
| 139 | ); |
| 140 | |
| 141 | expect(updated.id).toEqual(created.id); |
| 142 | expect(updated.name).toEqual("Alchemy Card Design Updated"); |
| 143 | expect(updated.physicalBundle).toEqual(physicalBundle); |
| 144 | expect(updated.metadata).toEqual({ line: "test", version: "2" }); |
| 145 | |
| 146 | const refetched = yield* GetIssuingPersonalizationDesign({ |
| 147 | personalization_design: updated.id, |
| 148 | }); |
| 149 | expect(refetched.id).toEqual(updated.id); |
| 150 | expect(refetched.name).toEqual("Alchemy Card Design Updated"); |
| 151 | expect(refetched.metadata?.line).toEqual("test"); |
| 152 | expect(refetched.metadata?.version).toEqual("2"); |
| 153 | expect(refetched.metadata?.[Stripe.alchemyMetadataKeys.id]).toBeDefined(); |
| 154 | |
| 155 | yield* stack.destroy(); |
| 156 | |
| 157 | // No delete/archive API — destroy is a no-op and the design remains. |
| 158 | const residue = yield* GetIssuingPersonalizationDesign({ |
| 159 | personalization_design: created.id, |
| 160 | }).pipe( |
| 161 | Effect.catchIf(isMissingStripeResource, () => |
| 162 | Effect.succeed(undefined), |
| 163 | ), |
| 164 | ); |
| 165 | expect(residue).toBeDefined(); |
| 166 | expect(residue?.id).toEqual(created.id); |
| 167 | }).pipe(logLevel), |
| 168 | { |
| 169 | tags: [ |
| 170 | "provider:stripe", |
| 171 | "provider:stripe:issuingpersonalizationdesign", |
| 172 | "live", |
| 173 | ], |
| 174 | timeout: 120_000, |
| 175 | }, |
| 176 | ); |
| 177 | |
| 178 | test.provider.skipIf(!ISSUING_ENABLED)( |
| 179 | "list enumerates the deployed personalization design", |
| 180 | (stack) => |
| 181 | Effect.gen(function* () { |
| 182 | yield* stack.destroy(); |
| 183 | |
| 184 | const bundle = yield* pickPhysicalBundle; |
| 185 | expect(bundle).toBeDefined(); |
| 186 | const physicalBundle = bundle!.id; |
| 187 | |
| 188 | const deployed = yield* stack.deploy( |
| 189 | Effect.gen(function* () { |
| 190 | return yield* Stripe.IssuingPersonalizationDesign("ListDesign", { |
| 191 | physicalBundle, |
| 192 | name: "Alchemy List Card Design", |
| 193 | metadata: { kind: "list" }, |
| 194 | }); |
| 195 | }), |
| 196 | ); |
| 197 | |
| 198 | const provider = yield* Provider.findProvider( |
| 199 | Stripe.IssuingPersonalizationDesign, |
| 200 | ); |
| 201 | const all = yield* provider.list(); |
| 202 | const found = all.find((design) => design.id === deployed.id); |
| 203 | expect(found).toBeDefined(); |
| 204 | expect(found?.name).toEqual(deployed.name); |
| 205 | expect(found?.physicalBundle).toEqual(physicalBundle); |
| 206 | expect(found?.metadata).toMatchObject({ kind: "list" }); |
| 207 | |
| 208 | yield* stack.destroy(); |
| 209 | |
| 210 | // Residue stays listed — destroy cannot untag or delete the object. |
| 211 | const after = yield* provider.list(); |
| 212 | expect(after.find((design) => design.id === deployed.id)).toBeDefined(); |
| 213 | }).pipe(logLevel), |
| 214 | { |
| 215 | tags: [ |
| 216 | "provider:stripe", |
| 217 | "provider:stripe:issuingpersonalizationdesign", |
| 218 | "live", |
| 219 | ], |
| 220 | timeout: 120_000, |
| 221 | }, |
| 222 | ); |