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 { GetShippingRate } 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 | GetShippingRate({ shipping_rate_token: id }).pipe( |
| 19 | Effect.map((rate) => (rate.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 | test.provider( |
| 29 | "create, update, and deactivate a shipping rate", |
| 30 | (stack) => |
| 31 | Effect.gen(function* () { |
| 32 | yield* stack.destroy(); |
| 33 | |
| 34 | const created = yield* stack.deploy( |
| 35 | Effect.gen(function* () { |
| 36 | return yield* Stripe.ShippingRate("GroundRate", { |
| 37 | displayName: "Alchemy Ground", |
| 38 | amount: 500, |
| 39 | currency: "usd", |
| 40 | deliveryEstimate: { |
| 41 | minimum: { unit: "business_day", value: 5 }, |
| 42 | maximum: { unit: "business_day", value: 7 }, |
| 43 | }, |
| 44 | metadata: { region: "us" }, |
| 45 | }); |
| 46 | }), |
| 47 | ); |
| 48 | |
| 49 | expect(created.id).toMatch(/^shr_/); |
| 50 | expect(created.displayName).toEqual("Alchemy Ground"); |
| 51 | expect(created.type).toEqual("fixed_amount"); |
| 52 | expect(created.amount).toEqual(500); |
| 53 | expect(created.currency).toEqual("usd"); |
| 54 | expect(created.active).toEqual(true); |
| 55 | expect(created.deliveryEstimate).toEqual({ |
| 56 | minimum: { unit: "business_day", value: 5 }, |
| 57 | maximum: { unit: "business_day", value: 7 }, |
| 58 | }); |
| 59 | expect(created.metadata).toMatchObject({ region: "us" }); |
| 60 | expect(created.livemode).toEqual(false); |
| 61 | |
| 62 | const fetched = yield* GetShippingRate({ |
| 63 | shipping_rate_token: created.id, |
| 64 | }); |
| 65 | expect(fetched.id).toEqual(created.id); |
| 66 | expect(fetched.display_name).toEqual("Alchemy Ground"); |
| 67 | expect(fetched.fixed_amount?.amount).toEqual(500); |
| 68 | expect(fetched.fixed_amount?.currency).toEqual("usd"); |
| 69 | expect(fetched.active).toEqual(true); |
| 70 | expect(fetched.metadata?.region).toEqual("us"); |
| 71 | expect(fetched.metadata?.[Stripe.alchemyMetadataKeys.stack]).toBeDefined(); |
| 72 | expect(fetched.metadata?.[Stripe.alchemyMetadataKeys.stage]).toBeDefined(); |
| 73 | expect(fetched.metadata?.[Stripe.alchemyMetadataKeys.id]).toBeDefined(); |
| 74 | |
| 75 | const updated = yield* stack.deploy( |
| 76 | Effect.gen(function* () { |
| 77 | return yield* Stripe.ShippingRate("GroundRate", { |
| 78 | displayName: "Alchemy Ground", |
| 79 | amount: 500, |
| 80 | currency: "usd", |
| 81 | deliveryEstimate: { |
| 82 | minimum: { unit: "business_day", value: 5 }, |
| 83 | maximum: { unit: "business_day", value: 7 }, |
| 84 | }, |
| 85 | taxBehavior: "exclusive", |
| 86 | metadata: { region: "us", channel: "web" }, |
| 87 | }); |
| 88 | }), |
| 89 | ); |
| 90 | |
| 91 | expect(updated.id).toEqual(created.id); |
| 92 | expect(updated.active).toEqual(true); |
| 93 | expect(updated.taxBehavior).toEqual("exclusive"); |
| 94 | expect(updated.metadata).toEqual({ region: "us", channel: "web" }); |
| 95 | |
| 96 | const refetched = yield* GetShippingRate({ |
| 97 | shipping_rate_token: updated.id, |
| 98 | }); |
| 99 | expect(refetched.active).toEqual(true); |
| 100 | expect(refetched.tax_behavior).toEqual("exclusive"); |
| 101 | expect(refetched.metadata?.region).toEqual("us"); |
| 102 | expect(refetched.metadata?.channel).toEqual("web"); |
| 103 | expect(refetched.metadata?.[Stripe.alchemyMetadataKeys.id]).toBeDefined(); |
| 104 | |
| 105 | yield* stack.destroy(); |
| 106 | |
| 107 | const deactivated = yield* waitUntilDeactivated(created.id); |
| 108 | expect(deactivated).toEqual("inactive"); |
| 109 | }).pipe(logLevel), |
| 110 | { |
| 111 | tags: ["provider:stripe", "provider:stripe:shippingrate", "live"], |
| 112 | timeout: 120_000, |
| 113 | }, |
| 114 | ); |
| 115 | |
| 116 | test.provider( |
| 117 | "list enumerates the deployed shipping rate", |
| 118 | (stack) => |
| 119 | Effect.gen(function* () { |
| 120 | yield* stack.destroy(); |
| 121 | |
| 122 | const deployed = yield* stack.deploy( |
| 123 | Effect.gen(function* () { |
| 124 | return yield* Stripe.ShippingRate("ListRate", { |
| 125 | displayName: "Alchemy List Rate", |
| 126 | amount: 799, |
| 127 | currency: "usd", |
| 128 | metadata: { kind: "list" }, |
| 129 | }); |
| 130 | }), |
| 131 | ); |
| 132 | |
| 133 | const provider = yield* Provider.findProvider(Stripe.ShippingRate); |
| 134 | const all = yield* provider.list(); |
| 135 | const found = all.find((rate) => rate.id === deployed.id); |
| 136 | expect(found).toBeDefined(); |
| 137 | expect(found?.displayName).toEqual(deployed.displayName); |
| 138 | expect(found?.metadata).toMatchObject({ kind: "list" }); |
| 139 | expect(found?.amount).toEqual(799); |
| 140 | |
| 141 | yield* stack.destroy(); |
| 142 | |
| 143 | const deactivated = yield* waitUntilDeactivated(deployed.id); |
| 144 | expect(deactivated).toEqual("inactive"); |
| 145 | |
| 146 | const after = yield* provider.list(); |
| 147 | expect(after.find((rate) => rate.id === deployed.id)).toBeUndefined(); |
| 148 | }).pipe(logLevel), |
| 149 | { |
| 150 | tags: ["provider:stripe", "provider:stripe:shippingrate", "live"], |
| 151 | timeout: 120_000, |
| 152 | }, |
| 153 | ); |
| 154 | |
| 155 | test.provider( |
| 156 | "replace when amount changes", |
| 157 | (stack) => |
| 158 | Effect.gen(function* () { |
| 159 | yield* stack.destroy(); |
| 160 | |
| 161 | const created = yield* stack.deploy( |
| 162 | Effect.gen(function* () { |
| 163 | return yield* Stripe.ShippingRate("ReplaceRate", { |
| 164 | displayName: "Alchemy Replace Rate", |
| 165 | amount: 1000, |
| 166 | currency: "usd", |
| 167 | metadata: { version: "v1" }, |
| 168 | }); |
| 169 | }), |
| 170 | ); |
| 171 | |
| 172 | const replaced = yield* stack.deploy( |
| 173 | Effect.gen(function* () { |
| 174 | return yield* Stripe.ShippingRate("ReplaceRate", { |
| 175 | displayName: "Alchemy Replace Rate", |
| 176 | amount: 2500, |
| 177 | currency: "usd", |
| 178 | metadata: { version: "v2" }, |
| 179 | }); |
| 180 | }), |
| 181 | ); |
| 182 | |
| 183 | expect(replaced.id).not.toEqual(created.id); |
| 184 | expect(replaced.amount).toEqual(2500); |
| 185 | expect(replaced.metadata).toMatchObject({ version: "v2" }); |
| 186 | |
| 187 | const newFetched = yield* GetShippingRate({ |
| 188 | shipping_rate_token: replaced.id, |
| 189 | }); |
| 190 | expect(newFetched.fixed_amount?.amount).toEqual(2500); |
| 191 | |
| 192 | const oldFetched = yield* GetShippingRate({ |
| 193 | shipping_rate_token: created.id, |
| 194 | }); |
| 195 | expect(oldFetched.active).toEqual(false); |
| 196 | |
| 197 | yield* stack.destroy(); |
| 198 | |
| 199 | const deactivated = yield* waitUntilDeactivated(replaced.id); |
| 200 | expect(deactivated).toEqual("inactive"); |
| 201 | }).pipe(logLevel), |
| 202 | { |
| 203 | tags: ["provider:stripe", "provider:stripe:shippingrate", "live"], |
| 204 | timeout: 120_000, |
| 205 | }, |
| 206 | ); |