The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 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 { GetPrice, CreateProduct, UpdateProduct } 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 isMissing = isMissingStripeResource; |
| 16 | |
| 17 | const waitUntilDeactivated = (id: string) => |
| 18 | GetPrice({ price: id }).pipe( |
| 19 | Effect.map((price) => (price.active ? ("active" as const) : ("inactive" as const))), |
| 20 | Effect.catchIf(isMissing, () => Effect.succeed("inactive" as const)), |
| 21 | Effect.repeat({ |
| 22 | schedule: Schedule.spaced("1 second"), |
| 23 | until: (status) => status === "inactive", |
| 24 | times: 10, |
| 25 | }), |
| 26 | ); |
| 27 | |
| 28 | const archiveProduct = (id: string) => |
| 29 | UpdateProduct({ id, active: false }).pipe(Effect.catchIf(isMissing, () => Effect.void)); |
| 30 | |
| 31 | test.provider( |
| 32 | "create, update, and deactivate a one-time price", |
| 33 | (stack) => |
| 34 | Effect.gen(function* () { |
| 35 | yield* stack.destroy(); |
| 36 | |
| 37 | const product = yield* CreateProduct({ |
| 38 | name: "Alchemy One-Time Price Product", |
| 39 | }); |
| 40 | |
| 41 | const created = yield* stack.deploy( |
| 42 | Effect.gen(function* () { |
| 43 | return yield* Stripe.Price("OneTimePrice", { |
| 44 | product: product.id, |
| 45 | currency: "usd", |
| 46 | unitAmount: 2000, |
| 47 | nickname: "Launch price", |
| 48 | metadata: { tier: "pro" }, |
| 49 | }); |
| 50 | }), |
| 51 | ); |
| 52 | |
| 53 | expect(created.id).toMatch(/^price_/); |
| 54 | expect(created.product).toEqual(product.id); |
| 55 | expect(created.currency).toEqual("usd"); |
| 56 | expect(created.unitAmount).toEqual(2000); |
| 57 | expect(created.active).toEqual(true); |
| 58 | expect(created.nickname).toEqual("Launch price"); |
| 59 | expect(created.type).toEqual("one_time"); |
| 60 | expect(created.recurring).toBeUndefined(); |
| 61 | expect(created.metadata).toMatchObject({ tier: "pro" }); |
| 62 | expect(created.livemode).toEqual(false); |
| 63 | |
| 64 | const fetched = yield* GetPrice({ price: created.id }); |
| 65 | expect(fetched.id).toEqual(created.id); |
| 66 | expect(fetched.unit_amount).toEqual(2000); |
| 67 | expect(fetched.nickname).toEqual("Launch price"); |
| 68 | expect(fetched.active).toEqual(true); |
| 69 | expect(fetched.metadata?.tier).toEqual("pro"); |
| 70 | expect(fetched.metadata?.[Stripe.alchemyMetadataKeys.stack]).toBeDefined(); |
| 71 | expect(fetched.metadata?.[Stripe.alchemyMetadataKeys.stage]).toBeDefined(); |
| 72 | expect(fetched.metadata?.[Stripe.alchemyMetadataKeys.id]).toBeDefined(); |
| 73 | |
| 74 | const updated = yield* stack.deploy( |
| 75 | Effect.gen(function* () { |
| 76 | return yield* Stripe.Price("OneTimePrice", { |
| 77 | product: product.id, |
| 78 | currency: "usd", |
| 79 | unitAmount: 2000, |
| 80 | nickname: "Launch price (paused)", |
| 81 | active: false, |
| 82 | metadata: { tier: "enterprise", sku: "ent-1" }, |
| 83 | }); |
| 84 | }), |
| 85 | ); |
| 86 | |
| 87 | expect(updated.id).toEqual(created.id); |
| 88 | expect(updated.nickname).toEqual("Launch price (paused)"); |
| 89 | expect(updated.active).toEqual(false); |
| 90 | expect(updated.metadata).toEqual({ tier: "enterprise", sku: "ent-1" }); |
| 91 | |
| 92 | const refetched = yield* GetPrice({ price: updated.id }); |
| 93 | expect(refetched.nickname).toEqual(updated.nickname); |
| 94 | expect(refetched.active).toEqual(false); |
| 95 | expect(refetched.metadata?.tier).toEqual("enterprise"); |
| 96 | expect(refetched.metadata?.sku).toEqual("ent-1"); |
| 97 | expect(refetched.metadata?.[Stripe.alchemyMetadataKeys.id]).toBeDefined(); |
| 98 | |
| 99 | yield* stack.destroy(); |
| 100 | |
| 101 | const deactivated = yield* waitUntilDeactivated(created.id); |
| 102 | expect(deactivated).toEqual("inactive"); |
| 103 | |
| 104 | yield* archiveProduct(product.id); |
| 105 | }).pipe(logLevel), |
| 106 | { |
| 107 | tags: ["provider:stripe", "provider:stripe:price", "live"], |
| 108 | timeout: 120_000, |
| 109 | }, |
| 110 | ); |
| 111 | |
| 112 | test.provider( |
| 113 | "create a recurring price and list it", |
| 114 | (stack) => |
| 115 | Effect.gen(function* () { |
| 116 | yield* stack.destroy(); |
| 117 | |
| 118 | const product = yield* CreateProduct({ |
| 119 | name: "Alchemy Recurring Price Product", |
| 120 | }); |
| 121 | |
| 122 | const deployed = yield* stack.deploy( |
| 123 | Effect.gen(function* () { |
| 124 | return yield* Stripe.Price("MonthlyPrice", { |
| 125 | product: product.id, |
| 126 | currency: "usd", |
| 127 | unitAmount: 1500, |
| 128 | recurring: { interval: "month" }, |
| 129 | nickname: "Pro monthly", |
| 130 | metadata: { kind: "list" }, |
| 131 | }); |
| 132 | }), |
| 133 | ); |
| 134 | |
| 135 | expect(deployed.type).toEqual("recurring"); |
| 136 | expect(deployed.recurring?.interval).toEqual("month"); |
| 137 | expect(deployed.unitAmount).toEqual(1500); |
| 138 | |
| 139 | const fetched = yield* GetPrice({ price: deployed.id }); |
| 140 | expect(fetched.recurring?.interval).toEqual("month"); |
| 141 | expect(fetched.unit_amount).toEqual(1500); |
| 142 | |
| 143 | const provider = yield* Provider.findProvider(Stripe.Price); |
| 144 | const all = yield* provider.list(); |
| 145 | const found = all.find((price) => price.id === deployed.id); |
| 146 | expect(found).toBeDefined(); |
| 147 | expect(found?.nickname).toEqual("Pro monthly"); |
| 148 | expect(found?.metadata).toMatchObject({ kind: "list" }); |
| 149 | expect(found?.recurring?.interval).toEqual("month"); |
| 150 | |
| 151 | yield* stack.destroy(); |
| 152 | |
| 153 | const deactivated = yield* waitUntilDeactivated(deployed.id); |
| 154 | expect(deactivated).toEqual("inactive"); |
| 155 | |
| 156 | const after = yield* provider.list(); |
| 157 | expect(after.find((price) => price.id === deployed.id)).toBeUndefined(); |
| 158 | |
| 159 | yield* archiveProduct(product.id); |
| 160 | }).pipe(logLevel), |
| 161 | { |
| 162 | tags: ["provider:stripe", "provider:stripe:price", "live"], |
| 163 | timeout: 120_000, |
| 164 | }, |
| 165 | ); |
| 166 | |
| 167 | test.provider( |
| 168 | "replace when unit amount changes", |
| 169 | (stack) => |
| 170 | Effect.gen(function* () { |
| 171 | yield* stack.destroy(); |
| 172 | |
| 173 | const product = yield* CreateProduct({ |
| 174 | name: "Alchemy Replace Price Product", |
| 175 | }); |
| 176 | |
| 177 | const created = yield* stack.deploy( |
| 178 | Effect.gen(function* () { |
| 179 | return yield* Stripe.Price("ReplacePrice", { |
| 180 | product: product.id, |
| 181 | currency: "usd", |
| 182 | unitAmount: 1000, |
| 183 | nickname: "v1", |
| 184 | }); |
| 185 | }), |
| 186 | ); |
| 187 | |
| 188 | const replaced = yield* stack.deploy( |
| 189 | Effect.gen(function* () { |
| 190 | return yield* Stripe.Price("ReplacePrice", { |
| 191 | product: product.id, |
| 192 | currency: "usd", |
| 193 | unitAmount: 2500, |
| 194 | nickname: "v2", |
| 195 | }); |
| 196 | }), |
| 197 | ); |
| 198 | |
| 199 | expect(replaced.id).not.toEqual(created.id); |
| 200 | expect(replaced.unitAmount).toEqual(2500); |
| 201 | expect(replaced.nickname).toEqual("v2"); |
| 202 | expect(replaced.product).toEqual(product.id); |
| 203 | |
| 204 | const newFetched = yield* GetPrice({ price: replaced.id }); |
| 205 | expect(newFetched.unit_amount).toEqual(2500); |
| 206 | |
| 207 | const oldFetched = yield* GetPrice({ price: created.id }); |
| 208 | expect(oldFetched.active).toEqual(false); |
| 209 | |
| 210 | yield* stack.destroy(); |
| 211 | |
| 212 | const deactivated = yield* waitUntilDeactivated(replaced.id); |
| 213 | expect(deactivated).toEqual("inactive"); |
| 214 | |
| 215 | yield* archiveProduct(product.id); |
| 216 | }).pipe(logLevel), |
| 217 | { |
| 218 | tags: ["provider:stripe", "provider:stripe:price", "live"], |
| 219 | timeout: 120_000, |
| 220 | }, |
| 221 | ); |