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 { GetPromotionCode } 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 Schedule from "effect/Schedule"; |
| 10 | |
| 11 | const { test } = Test.make({ providers: Stripe.providers() }); |
| 12 | |
| 13 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 14 | |
| 15 | const waitUntilInactive = (id: string) => |
| 16 | GetPromotionCode({ promotion_code: id }).pipe( |
| 17 | Effect.map((promo) => (promo.active ? ("active" as const) : ("inactive" as const))), |
| 18 | Effect.catchIf(isMissingStripeResource, () => Effect.succeed("gone" as const)), |
| 19 | Effect.repeat({ |
| 20 | schedule: Schedule.spaced("1 second"), |
| 21 | until: (status) => status === "inactive" || status === "gone", |
| 22 | times: 8, |
| 23 | }), |
| 24 | ); |
| 25 | |
| 26 | test.provider( |
| 27 | "create, update, and deactivate a promotion code", |
| 28 | (stack) => |
| 29 | Effect.gen(function* () { |
| 30 | yield* stack.destroy(); |
| 31 | |
| 32 | const created = yield* stack.deploy( |
| 33 | Effect.gen(function* () { |
| 34 | const coupon = yield* Stripe.Coupon("WelcomeCoupon", { |
| 35 | percentOff: 15, |
| 36 | duration: "once", |
| 37 | name: "Alchemy Promo CRUD", |
| 38 | }); |
| 39 | return yield* Stripe.PromotionCode("WelcomeCode", { |
| 40 | coupon: coupon.id, |
| 41 | metadata: { env: "test" }, |
| 42 | }); |
| 43 | }), |
| 44 | ); |
| 45 | |
| 46 | expect(created.id).toEqual(expect.any(String)); |
| 47 | expect(created.code).toEqual(expect.any(String)); |
| 48 | expect(created.active).toEqual(true); |
| 49 | expect(created.couponId).toEqual(expect.any(String)); |
| 50 | expect(created.metadata).toMatchObject({ env: "test" }); |
| 51 | expect(created.livemode).toEqual(false); |
| 52 | |
| 53 | const fetched = yield* GetPromotionCode({ |
| 54 | promotion_code: created.id, |
| 55 | }); |
| 56 | expect(fetched.id).toEqual(created.id); |
| 57 | expect(fetched.code).toEqual(created.code); |
| 58 | expect(fetched.active).toEqual(true); |
| 59 | expect(fetched.metadata?.env).toEqual("test"); |
| 60 | expect(fetched.metadata?.[Stripe.alchemyMetadataKeys.stack]).toBeDefined(); |
| 61 | expect(fetched.metadata?.[Stripe.alchemyMetadataKeys.stage]).toBeDefined(); |
| 62 | expect(fetched.metadata?.[Stripe.alchemyMetadataKeys.id]).toBeDefined(); |
| 63 | |
| 64 | const updated = yield* stack.deploy( |
| 65 | Effect.gen(function* () { |
| 66 | const coupon = yield* Stripe.Coupon("WelcomeCoupon", { |
| 67 | percentOff: 15, |
| 68 | duration: "once", |
| 69 | name: "Alchemy Promo CRUD", |
| 70 | }); |
| 71 | return yield* Stripe.PromotionCode("WelcomeCode", { |
| 72 | coupon: coupon.id, |
| 73 | metadata: { env: "prod", campaign: "spring" }, |
| 74 | }); |
| 75 | }), |
| 76 | ); |
| 77 | |
| 78 | expect(updated.id).toEqual(created.id); |
| 79 | expect(updated.metadata).toMatchObject({ |
| 80 | env: "prod", |
| 81 | campaign: "spring", |
| 82 | }); |
| 83 | |
| 84 | const refetched = yield* GetPromotionCode({ |
| 85 | promotion_code: updated.id, |
| 86 | }); |
| 87 | expect(refetched.metadata?.env).toEqual("prod"); |
| 88 | expect(refetched.metadata?.campaign).toEqual("spring"); |
| 89 | expect(refetched.active).toEqual(true); |
| 90 | |
| 91 | yield* stack.destroy(); |
| 92 | |
| 93 | const inactive = yield* waitUntilInactive(created.id); |
| 94 | expect(inactive === "inactive" || inactive === "gone").toEqual(true); |
| 95 | if (inactive === "inactive") { |
| 96 | const deactivated = yield* GetPromotionCode({ |
| 97 | promotion_code: created.id, |
| 98 | }); |
| 99 | expect(deactivated.active).toEqual(false); |
| 100 | } |
| 101 | }).pipe(logLevel), |
| 102 | { |
| 103 | tags: ["provider:stripe", "provider:stripe:coupon", "provider:stripe:promotioncode", "live"], |
| 104 | timeout: 120_000, |
| 105 | }, |
| 106 | ); |
| 107 | |
| 108 | test.provider( |
| 109 | "list enumerates the deployed promotion code", |
| 110 | (stack) => |
| 111 | Effect.gen(function* () { |
| 112 | yield* stack.destroy(); |
| 113 | |
| 114 | const deployed = yield* stack.deploy( |
| 115 | Effect.gen(function* () { |
| 116 | const coupon = yield* Stripe.Coupon("ListCoupon", { |
| 117 | percentOff: 10, |
| 118 | duration: "once", |
| 119 | name: "Alchemy Promo List", |
| 120 | }); |
| 121 | return yield* Stripe.PromotionCode("ListCode", { |
| 122 | coupon: coupon.id, |
| 123 | }); |
| 124 | }), |
| 125 | ); |
| 126 | |
| 127 | const provider = yield* Provider.findProvider(Stripe.PromotionCode); |
| 128 | const all = yield* provider.list(); |
| 129 | const found = all.find((item) => item.id === deployed.id); |
| 130 | expect(found).toBeDefined(); |
| 131 | expect(found?.code).toEqual(deployed.code); |
| 132 | expect(found?.active).toEqual(true); |
| 133 | |
| 134 | yield* stack.destroy(); |
| 135 | |
| 136 | // Deactivated codes are effectively deleted and must not re-enter nuke. |
| 137 | const after = yield* provider.list(); |
| 138 | expect(after.find((item) => item.id === deployed.id)).toBeUndefined(); |
| 139 | }).pipe(logLevel), |
| 140 | { |
| 141 | tags: ["provider:stripe", "provider:stripe:coupon", "provider:stripe:promotioncode", "live"], |
| 142 | timeout: 120_000, |
| 143 | }, |
| 144 | ); |