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 { GetCoupon } 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 waitUntilGone = (id: string) => |
| 16 | GetCoupon({ coupon: id }).pipe( |
| 17 | Effect.as("found" as const), |
| 18 | Effect.catchIf(isMissingStripeResource, () => Effect.succeed("gone" as const)), |
| 19 | Effect.repeat({ |
| 20 | schedule: Schedule.spaced("1 second"), |
| 21 | until: (status) => status === "gone", |
| 22 | times: 10, |
| 23 | }), |
| 24 | ); |
| 25 | |
| 26 | test.provider( |
| 27 | "create, update, and delete a coupon", |
| 28 | (stack) => |
| 29 | Effect.gen(function* () { |
| 30 | yield* stack.destroy(); |
| 31 | |
| 32 | const created = yield* stack.deploy( |
| 33 | Effect.gen(function* () { |
| 34 | return yield* Stripe.Coupon("WelcomeCoupon", { |
| 35 | percentOff: 20, |
| 36 | duration: "forever", |
| 37 | name: "Alchemy Welcome 20%", |
| 38 | metadata: { campaign: "welcome" }, |
| 39 | }); |
| 40 | }), |
| 41 | ); |
| 42 | |
| 43 | expect(created.id).toEqual(expect.any(String)); |
| 44 | expect(created.percentOff).toEqual(20); |
| 45 | expect(created.amountOff).toBeUndefined(); |
| 46 | expect(created.duration).toEqual("forever"); |
| 47 | expect(created.name).toEqual("Alchemy Welcome 20%"); |
| 48 | expect(created.valid).toEqual(true); |
| 49 | expect(created.livemode).toEqual(false); |
| 50 | expect(created.metadata).toMatchObject({ campaign: "welcome" }); |
| 51 | expect(created.created).toEqual(expect.any(Number)); |
| 52 | |
| 53 | const fetched = yield* GetCoupon({ coupon: created.id }); |
| 54 | expect(fetched.id).toEqual(created.id); |
| 55 | expect(fetched.percent_off).toEqual(20); |
| 56 | expect(fetched.duration).toEqual("forever"); |
| 57 | expect(fetched.name).toEqual("Alchemy Welcome 20%"); |
| 58 | expect(fetched.metadata?.campaign).toEqual("welcome"); |
| 59 | expect(fetched.metadata?.[Stripe.alchemyMetadataKeys.stack]).toBeDefined(); |
| 60 | expect(fetched.metadata?.[Stripe.alchemyMetadataKeys.stage]).toBeDefined(); |
| 61 | expect(fetched.metadata?.[Stripe.alchemyMetadataKeys.id]).toBeDefined(); |
| 62 | |
| 63 | const updated = yield* stack.deploy( |
| 64 | Effect.gen(function* () { |
| 65 | return yield* Stripe.Coupon("WelcomeCoupon", { |
| 66 | percentOff: 20, |
| 67 | duration: "forever", |
| 68 | name: "Alchemy Welcome 20% Updated", |
| 69 | metadata: { campaign: "spring", sku: "welcome-2" }, |
| 70 | }); |
| 71 | }), |
| 72 | ); |
| 73 | |
| 74 | expect(updated.id).toEqual(created.id); |
| 75 | expect(updated.percentOff).toEqual(20); |
| 76 | expect(updated.duration).toEqual("forever"); |
| 77 | expect(updated.name).toEqual("Alchemy Welcome 20% Updated"); |
| 78 | expect(updated.metadata).toEqual({ |
| 79 | campaign: "spring", |
| 80 | sku: "welcome-2", |
| 81 | }); |
| 82 | |
| 83 | const refetched = yield* GetCoupon({ coupon: updated.id }); |
| 84 | expect(refetched.id).toEqual(updated.id); |
| 85 | expect(refetched.name).toEqual("Alchemy Welcome 20% Updated"); |
| 86 | expect(refetched.percent_off).toEqual(20); |
| 87 | expect(refetched.metadata?.campaign).toEqual("spring"); |
| 88 | expect(refetched.metadata?.sku).toEqual("welcome-2"); |
| 89 | expect(refetched.metadata?.[Stripe.alchemyMetadataKeys.id]).toBeDefined(); |
| 90 | |
| 91 | yield* stack.destroy(); |
| 92 | |
| 93 | const gone = yield* waitUntilGone(created.id); |
| 94 | expect(gone).toEqual("gone"); |
| 95 | }).pipe(logLevel), |
| 96 | { |
| 97 | tags: ["provider:stripe", "provider:stripe:coupon", "live"], |
| 98 | timeout: 120_000, |
| 99 | }, |
| 100 | ); |
| 101 | |
| 102 | test.provider( |
| 103 | "replace when the discount changes", |
| 104 | (stack) => |
| 105 | Effect.gen(function* () { |
| 106 | yield* stack.destroy(); |
| 107 | |
| 108 | const created = yield* stack.deploy( |
| 109 | Effect.gen(function* () { |
| 110 | return yield* Stripe.Coupon("ReplaceCoupon", { |
| 111 | percentOff: 10, |
| 112 | duration: "once", |
| 113 | name: "Alchemy Replace Coupon", |
| 114 | }); |
| 115 | }), |
| 116 | ); |
| 117 | |
| 118 | expect(created.percentOff).toEqual(10); |
| 119 | expect(created.duration).toEqual("once"); |
| 120 | |
| 121 | const replaced = yield* stack.deploy( |
| 122 | Effect.gen(function* () { |
| 123 | return yield* Stripe.Coupon("ReplaceCoupon", { |
| 124 | amountOff: 500, |
| 125 | currency: "usd", |
| 126 | duration: "once", |
| 127 | name: "Alchemy Replace Coupon", |
| 128 | }); |
| 129 | }), |
| 130 | ); |
| 131 | |
| 132 | expect(replaced.id).not.toEqual(created.id); |
| 133 | expect(replaced.amountOff).toEqual(500); |
| 134 | expect(replaced.currency).toEqual("usd"); |
| 135 | expect(replaced.percentOff).toBeUndefined(); |
| 136 | expect(replaced.duration).toEqual("once"); |
| 137 | |
| 138 | const fetched = yield* GetCoupon({ coupon: replaced.id }); |
| 139 | expect(fetched.id).toEqual(replaced.id); |
| 140 | expect(fetched.amount_off).toEqual(500); |
| 141 | expect(fetched.currency).toEqual("usd"); |
| 142 | expect(fetched.percent_off).toBeNull(); |
| 143 | |
| 144 | const oldGone = yield* waitUntilGone(created.id); |
| 145 | expect(oldGone).toEqual("gone"); |
| 146 | |
| 147 | yield* stack.destroy(); |
| 148 | |
| 149 | const gone = yield* waitUntilGone(replaced.id); |
| 150 | expect(gone).toEqual("gone"); |
| 151 | }).pipe(logLevel), |
| 152 | { |
| 153 | tags: ["provider:stripe", "provider:stripe:coupon", "live"], |
| 154 | timeout: 120_000, |
| 155 | }, |
| 156 | ); |
| 157 | |
| 158 | test.provider( |
| 159 | "list enumerates the deployed coupon", |
| 160 | (stack) => |
| 161 | Effect.gen(function* () { |
| 162 | yield* stack.destroy(); |
| 163 | |
| 164 | const deployed = yield* stack.deploy( |
| 165 | Effect.gen(function* () { |
| 166 | return yield* Stripe.Coupon("ListCoupon", { |
| 167 | percentOff: 15, |
| 168 | duration: "forever", |
| 169 | name: "Alchemy List Coupon", |
| 170 | metadata: { kind: "list" }, |
| 171 | }); |
| 172 | }), |
| 173 | ); |
| 174 | |
| 175 | const provider = yield* Provider.findProvider(Stripe.Coupon); |
| 176 | const all = yield* provider.list(); |
| 177 | const found = all.find((coupon) => coupon.id === deployed.id); |
| 178 | expect(found).toBeDefined(); |
| 179 | expect(found?.name).toEqual(deployed.name); |
| 180 | expect(found?.percentOff).toEqual(15); |
| 181 | expect(found?.metadata).toMatchObject({ kind: "list" }); |
| 182 | |
| 183 | yield* stack.destroy(); |
| 184 | |
| 185 | const gone = yield* waitUntilGone(deployed.id); |
| 186 | expect(gone).toEqual("gone"); |
| 187 | }).pipe(logLevel), |
| 188 | { |
| 189 | tags: ["provider:stripe", "provider:stripe:coupon", "live"], |
| 190 | timeout: 120_000, |
| 191 | }, |
| 192 | ); |