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 { GetPlan } 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 | GetPlan({ plan: 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 plan", |
| 28 | (stack) => |
| 29 | Effect.gen(function* () { |
| 30 | yield* stack.destroy(); |
| 31 | |
| 32 | const created = yield* stack.deploy( |
| 33 | Effect.gen(function* () { |
| 34 | const product = yield* Stripe.Product("MonthlyPlanProduct", { |
| 35 | name: "Alchemy Monthly Plan Product", |
| 36 | }); |
| 37 | return yield* Stripe.Plan("MonthlyPlan", { |
| 38 | product: product.id, |
| 39 | currency: "usd", |
| 40 | interval: "month", |
| 41 | amount: 1500, |
| 42 | nickname: "Alchemy monthly", |
| 43 | metadata: { tier: "pro" }, |
| 44 | }); |
| 45 | }), |
| 46 | ); |
| 47 | |
| 48 | expect(created.id).toEqual(expect.any(String)); |
| 49 | expect(created.product).toEqual(expect.any(String)); |
| 50 | expect(created.currency).toEqual("usd"); |
| 51 | expect(created.interval).toEqual("month"); |
| 52 | expect(created.intervalCount).toEqual(1); |
| 53 | expect(created.amount).toEqual(1500); |
| 54 | expect(created.active).toEqual(true); |
| 55 | expect(created.nickname).toEqual("Alchemy monthly"); |
| 56 | expect(created.usageType).toEqual("licensed"); |
| 57 | expect(created.billingScheme).toEqual("per_unit"); |
| 58 | expect(created.livemode).toEqual(false); |
| 59 | expect(created.metadata).toMatchObject({ tier: "pro" }); |
| 60 | expect(created.created).toEqual(expect.any(Number)); |
| 61 | |
| 62 | const fetched = yield* GetPlan({ plan: created.id }); |
| 63 | expect(fetched.id).toEqual(created.id); |
| 64 | expect(fetched.amount).toEqual(1500); |
| 65 | expect(fetched.interval).toEqual("month"); |
| 66 | expect(fetched.nickname).toEqual("Alchemy monthly"); |
| 67 | expect(fetched.active).toEqual(true); |
| 68 | expect(fetched.metadata?.tier).toEqual("pro"); |
| 69 | expect(fetched.metadata?.[Stripe.alchemyMetadataKeys.stack]).toBeDefined(); |
| 70 | expect(fetched.metadata?.[Stripe.alchemyMetadataKeys.stage]).toBeDefined(); |
| 71 | expect(fetched.metadata?.[Stripe.alchemyMetadataKeys.id]).toBeDefined(); |
| 72 | |
| 73 | const updated = yield* stack.deploy( |
| 74 | Effect.gen(function* () { |
| 75 | const product = yield* Stripe.Product("MonthlyPlanProduct", { |
| 76 | name: "Alchemy Monthly Plan Product", |
| 77 | }); |
| 78 | return yield* Stripe.Plan("MonthlyPlan", { |
| 79 | product: product.id, |
| 80 | currency: "usd", |
| 81 | interval: "month", |
| 82 | amount: 1500, |
| 83 | nickname: "Alchemy monthly (trial)", |
| 84 | trialPeriodDays: 14, |
| 85 | metadata: { tier: "enterprise", sku: "ent-1" }, |
| 86 | }); |
| 87 | }), |
| 88 | ); |
| 89 | |
| 90 | expect(updated.id).toEqual(created.id); |
| 91 | expect(updated.nickname).toEqual("Alchemy monthly (trial)"); |
| 92 | expect(updated.trialPeriodDays).toEqual(14); |
| 93 | expect(updated.amount).toEqual(1500); |
| 94 | expect(updated.metadata).toEqual({ |
| 95 | tier: "enterprise", |
| 96 | sku: "ent-1", |
| 97 | }); |
| 98 | |
| 99 | const refetched = yield* GetPlan({ plan: updated.id }); |
| 100 | expect(refetched.id).toEqual(updated.id); |
| 101 | expect(refetched.nickname).toEqual("Alchemy monthly (trial)"); |
| 102 | expect(refetched.trial_period_days).toEqual(14); |
| 103 | expect(refetched.amount).toEqual(1500); |
| 104 | expect(refetched.metadata?.tier).toEqual("enterprise"); |
| 105 | expect(refetched.metadata?.sku).toEqual("ent-1"); |
| 106 | expect(refetched.metadata?.[Stripe.alchemyMetadataKeys.id]).toBeDefined(); |
| 107 | |
| 108 | yield* stack.destroy(); |
| 109 | |
| 110 | const gone = yield* waitUntilGone(created.id); |
| 111 | expect(gone).toEqual("gone"); |
| 112 | }).pipe(logLevel), |
| 113 | { |
| 114 | tags: ["provider:stripe", "provider:stripe:plan", "provider:stripe:product", "live"], |
| 115 | timeout: 120_000, |
| 116 | }, |
| 117 | ); |
| 118 | |
| 119 | test.provider( |
| 120 | "replace when the amount changes", |
| 121 | (stack) => |
| 122 | Effect.gen(function* () { |
| 123 | yield* stack.destroy(); |
| 124 | |
| 125 | const created = yield* stack.deploy( |
| 126 | Effect.gen(function* () { |
| 127 | const product = yield* Stripe.Product("ReplacePlanProduct", { |
| 128 | name: "Alchemy Replace Plan Product", |
| 129 | }); |
| 130 | return yield* Stripe.Plan("ReplacePlan", { |
| 131 | product: product.id, |
| 132 | currency: "usd", |
| 133 | interval: "month", |
| 134 | amount: 1000, |
| 135 | nickname: "v1", |
| 136 | }); |
| 137 | }), |
| 138 | ); |
| 139 | |
| 140 | expect(created.amount).toEqual(1000); |
| 141 | |
| 142 | const replaced = yield* stack.deploy( |
| 143 | Effect.gen(function* () { |
| 144 | const product = yield* Stripe.Product("ReplacePlanProduct", { |
| 145 | name: "Alchemy Replace Plan Product", |
| 146 | }); |
| 147 | return yield* Stripe.Plan("ReplacePlan", { |
| 148 | product: product.id, |
| 149 | currency: "usd", |
| 150 | interval: "month", |
| 151 | amount: 2500, |
| 152 | nickname: "v2", |
| 153 | }); |
| 154 | }), |
| 155 | ); |
| 156 | |
| 157 | expect(replaced.id).not.toEqual(created.id); |
| 158 | expect(replaced.amount).toEqual(2500); |
| 159 | expect(replaced.nickname).toEqual("v2"); |
| 160 | expect(replaced.interval).toEqual("month"); |
| 161 | |
| 162 | const fetched = yield* GetPlan({ plan: replaced.id }); |
| 163 | expect(fetched.id).toEqual(replaced.id); |
| 164 | expect(fetched.amount).toEqual(2500); |
| 165 | expect(fetched.nickname).toEqual("v2"); |
| 166 | |
| 167 | const oldGone = yield* waitUntilGone(created.id); |
| 168 | expect(oldGone).toEqual("gone"); |
| 169 | |
| 170 | yield* stack.destroy(); |
| 171 | |
| 172 | const gone = yield* waitUntilGone(replaced.id); |
| 173 | expect(gone).toEqual("gone"); |
| 174 | }).pipe(logLevel), |
| 175 | { |
| 176 | tags: ["provider:stripe", "provider:stripe:plan", "provider:stripe:product", "live"], |
| 177 | timeout: 120_000, |
| 178 | }, |
| 179 | ); |
| 180 | |
| 181 | test.provider( |
| 182 | "list enumerates the deployed plan", |
| 183 | (stack) => |
| 184 | Effect.gen(function* () { |
| 185 | yield* stack.destroy(); |
| 186 | |
| 187 | const deployed = yield* stack.deploy( |
| 188 | Effect.gen(function* () { |
| 189 | const product = yield* Stripe.Product("ListPlanProduct", { |
| 190 | name: "Alchemy List Plan Product", |
| 191 | }); |
| 192 | return yield* Stripe.Plan("ListPlan", { |
| 193 | product: product.id, |
| 194 | currency: "usd", |
| 195 | interval: "year", |
| 196 | amount: 12000, |
| 197 | nickname: "Alchemy yearly", |
| 198 | metadata: { kind: "list" }, |
| 199 | }); |
| 200 | }), |
| 201 | ); |
| 202 | |
| 203 | expect(deployed.interval).toEqual("year"); |
| 204 | expect(deployed.amount).toEqual(12000); |
| 205 | |
| 206 | const provider = yield* Provider.findProvider(Stripe.Plan); |
| 207 | const all = yield* provider.list(); |
| 208 | const found = all.find((plan) => plan.id === deployed.id); |
| 209 | expect(found).toBeDefined(); |
| 210 | expect(found?.nickname).toEqual("Alchemy yearly"); |
| 211 | expect(found?.interval).toEqual("year"); |
| 212 | expect(found?.metadata).toMatchObject({ kind: "list" }); |
| 213 | |
| 214 | yield* stack.destroy(); |
| 215 | |
| 216 | const gone = yield* waitUntilGone(deployed.id); |
| 217 | expect(gone).toEqual("gone"); |
| 218 | }).pipe(logLevel), |
| 219 | { |
| 220 | tags: ["provider:stripe", "provider:stripe:plan", "provider:stripe:product", "live"], |
| 221 | timeout: 120_000, |
| 222 | }, |
| 223 | ); |