| 1 | import * as Provider from "@/Provider"; |
| 2 | import * as Stripe from "@/Stripe"; |
| 3 | import * as Test from "@/Test/Alchemy"; |
| 4 | import { GetProduct } from "@distilled.cloud/stripe/stripe"; |
| 5 | import { expect } from "alchemy-test"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import { MinimumLogLevel } from "effect/References"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 9 | import { isMissingStripeResource } from "@/Stripe/missing.ts"; |
| 10 | |
| 11 | const { test } = Test.make({ providers: Stripe.providers() }); |
| 12 | |
| 13 | const logLevel = Effect.provideService( |
| 14 | MinimumLogLevel, |
| 15 | process.env.DEBUG ? "Debug" : "Info", |
| 16 | ); |
| 17 | |
| 18 | const waitUntilGone = (id: string) => |
| 19 | GetProduct({ id }).pipe( |
| 20 | Effect.as("found" as const), |
| 21 | Effect.catchIf(isMissingStripeResource, () => |
| 22 | Effect.succeed("gone" as const), |
| 23 | ), |
| 24 | Effect.repeat({ |
| 25 | schedule: Schedule.spaced("1 second"), |
| 26 | until: (status) => status === "gone", |
| 27 | times: 10, |
| 28 | }), |
| 29 | ); |
| 30 | |
| 31 | test.provider( |
| 32 | "create, update, and delete a product", |
| 33 | (stack) => |
| 34 | Effect.gen(function* () { |
| 35 | yield* stack.destroy(); |
| 36 | |
| 37 | const created = yield* stack.deploy( |
| 38 | Effect.gen(function* () { |
| 39 | return yield* Stripe.Product("CatalogProduct", { |
| 40 | name: "Alchemy Catalog Product", |
| 41 | description: "Initial description", |
| 42 | images: ["https://example.com/product.png"], |
| 43 | metadata: { tier: "pro" }, |
| 44 | }); |
| 45 | }), |
| 46 | ); |
| 47 | |
| 48 | expect(created.id).toMatch(/^prod_/); |
| 49 | expect(created.name).toEqual("Alchemy Catalog Product"); |
| 50 | expect(created.description).toEqual("Initial description"); |
| 51 | expect(created.active).toEqual(true); |
| 52 | expect(created.images).toEqual(["https://example.com/product.png"]); |
| 53 | expect(created.metadata).toMatchObject({ tier: "pro" }); |
| 54 | expect(created.created).toEqual(expect.any(Number)); |
| 55 | expect(created.livemode).toEqual(false); |
| 56 | |
| 57 | const fetched = yield* GetProduct({ id: created.id }); |
| 58 | expect(fetched.id).toEqual(created.id); |
| 59 | expect(fetched.name).toEqual(created.name); |
| 60 | expect(fetched.description).toEqual("Initial description"); |
| 61 | expect(fetched.active).toEqual(true); |
| 62 | expect(fetched.images).toEqual(["https://example.com/product.png"]); |
| 63 | expect(fetched.metadata?.tier).toEqual("pro"); |
| 64 | expect( |
| 65 | fetched.metadata?.[Stripe.alchemyMetadataKeys.stack], |
| 66 | ).toBeDefined(); |
| 67 | expect( |
| 68 | fetched.metadata?.[Stripe.alchemyMetadataKeys.stage], |
| 69 | ).toBeDefined(); |
| 70 | expect(fetched.metadata?.[Stripe.alchemyMetadataKeys.id]).toBeDefined(); |
| 71 | |
| 72 | const updated = yield* stack.deploy( |
| 73 | Effect.gen(function* () { |
| 74 | return yield* Stripe.Product("CatalogProduct", { |
| 75 | name: "Alchemy Catalog Product Updated", |
| 76 | description: "Updated description", |
| 77 | active: false, |
| 78 | images: ["https://example.com/product-updated.png"], |
| 79 | metadata: { tier: "enterprise", sku: "ent-1" }, |
| 80 | }); |
| 81 | }), |
| 82 | ); |
| 83 | |
| 84 | expect(updated.id).toEqual(created.id); |
| 85 | expect(updated.name).toEqual("Alchemy Catalog Product Updated"); |
| 86 | expect(updated.description).toEqual("Updated description"); |
| 87 | expect(updated.active).toEqual(false); |
| 88 | expect(updated.images).toEqual([ |
| 89 | "https://example.com/product-updated.png", |
| 90 | ]); |
| 91 | expect(updated.metadata).toEqual({ tier: "enterprise", sku: "ent-1" }); |
| 92 | |
| 93 | const refetched = yield* GetProduct({ id: updated.id }); |
| 94 | expect(refetched.name).toEqual(updated.name); |
| 95 | expect(refetched.description).toEqual("Updated description"); |
| 96 | expect(refetched.active).toEqual(false); |
| 97 | expect(refetched.images).toEqual(updated.images); |
| 98 | expect(refetched.metadata?.tier).toEqual("enterprise"); |
| 99 | expect(refetched.metadata?.sku).toEqual("ent-1"); |
| 100 | expect(refetched.metadata?.[Stripe.alchemyMetadataKeys.id]).toBeDefined(); |
| 101 | |
| 102 | yield* stack.destroy(); |
| 103 | |
| 104 | const gone = yield* waitUntilGone(created.id); |
| 105 | expect(gone).toEqual("gone"); |
| 106 | }).pipe(logLevel), |
| 107 | { |
| 108 | tags: ["provider:stripe", "provider:stripe:product", "live"], |
| 109 | timeout: 120_000, |
| 110 | }, |
| 111 | ); |
| 112 | |
| 113 | test.provider( |
| 114 | "list enumerates the deployed product", |
| 115 | (stack) => |
| 116 | Effect.gen(function* () { |
| 117 | yield* stack.destroy(); |
| 118 | |
| 119 | const deployed = yield* stack.deploy( |
| 120 | Effect.gen(function* () { |
| 121 | return yield* Stripe.Product("ListProduct", { |
| 122 | name: "Alchemy List Product", |
| 123 | metadata: { kind: "list" }, |
| 124 | }); |
| 125 | }), |
| 126 | ); |
| 127 | |
| 128 | const provider = yield* Provider.findProvider(Stripe.Product); |
| 129 | const all = yield* provider.list(); |
| 130 | const found = all.find((product) => product.id === deployed.id); |
| 131 | expect(found).toBeDefined(); |
| 132 | expect(found?.name).toEqual(deployed.name); |
| 133 | expect(found?.metadata).toMatchObject({ kind: "list" }); |
| 134 | |
| 135 | yield* stack.destroy(); |
| 136 | |
| 137 | const gone = yield* waitUntilGone(deployed.id); |
| 138 | expect(gone).toEqual("gone"); |
| 139 | }).pipe(logLevel), |
| 140 | { |
| 141 | tags: ["provider:stripe", "provider:stripe:product", "live"], |
| 142 | timeout: 120_000, |
| 143 | }, |
| 144 | ); |
| 145 | |
| 146 | test.provider( |
| 147 | "destroy archives a product that still has prices", |
| 148 | (stack) => |
| 149 | Effect.gen(function* () { |
| 150 | yield* stack.destroy(); |
| 151 | |
| 152 | const created = yield* stack.deploy( |
| 153 | Effect.gen(function* () { |
| 154 | const product = yield* Stripe.Product("PricedProduct", { |
| 155 | name: "Alchemy Priced Product", |
| 156 | }); |
| 157 | const price = yield* Stripe.Price("AttachedPrice", { |
| 158 | product: product.id, |
| 159 | currency: "usd", |
| 160 | unitAmount: 1500, |
| 161 | }); |
| 162 | return { product, price }; |
| 163 | }), |
| 164 | ); |
| 165 | |
| 166 | expect(created.product.id).toMatch(/^prod_/); |
| 167 | expect(created.price.id).toMatch(/^price_/); |
| 168 | expect(created.price.product).toEqual(created.product.id); |
| 169 | |
| 170 | yield* stack.destroy(); |
| 171 | |
| 172 | const archived = yield* GetProduct({ id: created.product.id }); |
| 173 | expect(archived.id).toEqual(created.product.id); |
| 174 | expect(archived.active).toEqual(false); |
| 175 | }).pipe(logLevel), |
| 176 | { |
| 177 | tags: [ |
| 178 | "provider:stripe", |
| 179 | "provider:stripe:price", |
| 180 | "provider:stripe:product", |
| 181 | "live", |
| 182 | ], |
| 183 | timeout: 120_000, |
| 184 | }, |
| 185 | ); |