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 { GetTaxRate } 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 waitUntilInactive = (id: string) => |
| 18 | GetTaxRate({ tax_rate: 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 tax 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.TaxRate("SalesTax", { |
| 37 | displayName: "Alchemy Sales Tax", |
| 38 | percentage: 8.25, |
| 39 | inclusive: false, |
| 40 | description: "Initial sales tax", |
| 41 | taxType: "sales_tax", |
| 42 | metadata: { region: "us" }, |
| 43 | }); |
| 44 | }), |
| 45 | ); |
| 46 | |
| 47 | expect(created.id).toMatch(/^txr_/); |
| 48 | expect(created.displayName).toEqual("Alchemy Sales Tax"); |
| 49 | expect(created.percentage).toEqual(8.25); |
| 50 | expect(created.inclusive).toEqual(false); |
| 51 | expect(created.active).toEqual(true); |
| 52 | expect(created.description).toEqual("Initial sales tax"); |
| 53 | expect(created.taxType).toEqual("sales_tax"); |
| 54 | expect(created.metadata).toMatchObject({ region: "us" }); |
| 55 | expect(created.created).toEqual(expect.any(Number)); |
| 56 | expect(created.livemode).toEqual(false); |
| 57 | |
| 58 | const fetched = yield* GetTaxRate({ tax_rate: created.id }); |
| 59 | expect(fetched.id).toEqual(created.id); |
| 60 | expect(fetched.display_name).toEqual("Alchemy Sales Tax"); |
| 61 | expect(fetched.percentage).toEqual(8.25); |
| 62 | expect(fetched.inclusive).toEqual(false); |
| 63 | expect(fetched.active).toEqual(true); |
| 64 | expect(fetched.description).toEqual("Initial sales tax"); |
| 65 | expect(fetched.tax_type).toEqual("sales_tax"); |
| 66 | expect(fetched.metadata?.region).toEqual("us"); |
| 67 | expect(fetched.metadata?.[Stripe.alchemyMetadataKeys.stack]).toBeDefined(); |
| 68 | expect(fetched.metadata?.[Stripe.alchemyMetadataKeys.stage]).toBeDefined(); |
| 69 | expect(fetched.metadata?.[Stripe.alchemyMetadataKeys.id]).toBeDefined(); |
| 70 | |
| 71 | const updated = yield* stack.deploy( |
| 72 | Effect.gen(function* () { |
| 73 | return yield* Stripe.TaxRate("SalesTax", { |
| 74 | displayName: "Alchemy Sales Tax Updated", |
| 75 | percentage: 8.25, |
| 76 | inclusive: false, |
| 77 | description: "Updated sales tax", |
| 78 | taxType: "sales_tax", |
| 79 | metadata: { region: "ca", sku: "tax-2" }, |
| 80 | }); |
| 81 | }), |
| 82 | ); |
| 83 | |
| 84 | expect(updated.id).toEqual(created.id); |
| 85 | expect(updated.displayName).toEqual("Alchemy Sales Tax Updated"); |
| 86 | expect(updated.percentage).toEqual(8.25); |
| 87 | expect(updated.inclusive).toEqual(false); |
| 88 | expect(updated.active).toEqual(true); |
| 89 | expect(updated.description).toEqual("Updated sales tax"); |
| 90 | expect(updated.metadata).toEqual({ region: "ca", sku: "tax-2" }); |
| 91 | |
| 92 | const refetched = yield* GetTaxRate({ tax_rate: updated.id }); |
| 93 | expect(refetched.id).toEqual(updated.id); |
| 94 | expect(refetched.display_name).toEqual("Alchemy Sales Tax Updated"); |
| 95 | expect(refetched.percentage).toEqual(8.25); |
| 96 | expect(refetched.description).toEqual("Updated sales tax"); |
| 97 | expect(refetched.metadata?.region).toEqual("ca"); |
| 98 | expect(refetched.metadata?.sku).toEqual("tax-2"); |
| 99 | expect(refetched.metadata?.[Stripe.alchemyMetadataKeys.id]).toBeDefined(); |
| 100 | |
| 101 | yield* stack.destroy(); |
| 102 | |
| 103 | const inactive = yield* waitUntilInactive(created.id); |
| 104 | expect(inactive).toEqual("inactive"); |
| 105 | const deactivated = yield* GetTaxRate({ tax_rate: created.id }); |
| 106 | expect(deactivated.active).toEqual(false); |
| 107 | }).pipe(logLevel), |
| 108 | { |
| 109 | tags: ["provider:stripe", "provider:stripe:taxrate", "live"], |
| 110 | timeout: 120_000, |
| 111 | }, |
| 112 | ); |
| 113 | |
| 114 | test.provider( |
| 115 | "replace when percentage or inclusive changes", |
| 116 | (stack) => |
| 117 | Effect.gen(function* () { |
| 118 | yield* stack.destroy(); |
| 119 | |
| 120 | const created = yield* stack.deploy( |
| 121 | Effect.gen(function* () { |
| 122 | return yield* Stripe.TaxRate("ReplaceTax", { |
| 123 | displayName: "Alchemy Replace Tax", |
| 124 | percentage: 5, |
| 125 | inclusive: false, |
| 126 | }); |
| 127 | }), |
| 128 | ); |
| 129 | |
| 130 | expect(created.percentage).toEqual(5); |
| 131 | expect(created.inclusive).toEqual(false); |
| 132 | |
| 133 | const replaced = yield* stack.deploy( |
| 134 | Effect.gen(function* () { |
| 135 | return yield* Stripe.TaxRate("ReplaceTax", { |
| 136 | displayName: "Alchemy Replace Tax", |
| 137 | percentage: 10, |
| 138 | inclusive: true, |
| 139 | }); |
| 140 | }), |
| 141 | ); |
| 142 | |
| 143 | expect(replaced.id).not.toEqual(created.id); |
| 144 | expect(replaced.percentage).toEqual(10); |
| 145 | expect(replaced.inclusive).toEqual(true); |
| 146 | |
| 147 | const fetched = yield* GetTaxRate({ tax_rate: replaced.id }); |
| 148 | expect(fetched.id).toEqual(replaced.id); |
| 149 | expect(fetched.percentage).toEqual(10); |
| 150 | expect(fetched.inclusive).toEqual(true); |
| 151 | |
| 152 | const oldInactive = yield* waitUntilInactive(created.id); |
| 153 | expect(oldInactive).toEqual("inactive"); |
| 154 | |
| 155 | yield* stack.destroy(); |
| 156 | |
| 157 | const gone = yield* waitUntilInactive(replaced.id); |
| 158 | expect(gone).toEqual("inactive"); |
| 159 | }).pipe(logLevel), |
| 160 | { |
| 161 | tags: ["provider:stripe", "provider:stripe:taxrate", "live"], |
| 162 | timeout: 120_000, |
| 163 | }, |
| 164 | ); |
| 165 | |
| 166 | test.provider( |
| 167 | "list enumerates the deployed tax rate", |
| 168 | (stack) => |
| 169 | Effect.gen(function* () { |
| 170 | yield* stack.destroy(); |
| 171 | |
| 172 | const deployed = yield* stack.deploy( |
| 173 | Effect.gen(function* () { |
| 174 | return yield* Stripe.TaxRate("ListTax", { |
| 175 | displayName: "Alchemy List Tax", |
| 176 | percentage: 7, |
| 177 | inclusive: false, |
| 178 | metadata: { kind: "list" }, |
| 179 | }); |
| 180 | }), |
| 181 | ); |
| 182 | |
| 183 | const provider = yield* Provider.findProvider(Stripe.TaxRate); |
| 184 | const all = yield* provider.list(); |
| 185 | const found = all.find((rate) => rate.id === deployed.id); |
| 186 | expect(found).toBeDefined(); |
| 187 | expect(found?.displayName).toEqual(deployed.displayName); |
| 188 | expect(found?.percentage).toEqual(7); |
| 189 | expect(found?.metadata).toMatchObject({ kind: "list" }); |
| 190 | |
| 191 | yield* stack.destroy(); |
| 192 | |
| 193 | const inactive = yield* waitUntilInactive(deployed.id); |
| 194 | expect(inactive).toEqual("inactive"); |
| 195 | }).pipe(logLevel), |
| 196 | { |
| 197 | tags: ["provider:stripe", "provider:stripe:taxrate", "live"], |
| 198 | timeout: 120_000, |
| 199 | }, |
| 200 | ); |