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 { |
| 6 | GetBillingPortalConfiguration, |
| 7 | GetBillingPortalConfigurations, |
| 8 | } from "@distilled.cloud/stripe/stripe"; |
| 9 | import * as Provider from "alchemy/Provider"; |
| 10 | import * as Effect from "effect/Effect"; |
| 11 | import { MinimumLogLevel } from "effect/References"; |
| 12 | import * as Schedule from "effect/Schedule"; |
| 13 | |
| 14 | const { test } = Test.make({ providers: Stripe.providers() }); |
| 15 | |
| 16 | // Stripe cannot deactivate the account default, and the API cannot choose a |
| 17 | // different default. Require account setup before creating disposable fixtures. |
| 18 | const requireDefaultConfiguration = Effect.gen(function* () { |
| 19 | const configurations = yield* GetBillingPortalConfigurations({ is_default: true, limit: 1 }); |
| 20 | if (configurations.data.length === 0) { |
| 21 | return yield* Effect.fail( |
| 22 | new Error( |
| 23 | "Configure the default customer portal in the Stripe Dashboard before running BillingPortalConfiguration lifecycle tests.", |
| 24 | ), |
| 25 | ); |
| 26 | } |
| 27 | }); |
| 28 | |
| 29 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 30 | |
| 31 | const isMissing = isMissingStripeResource; |
| 32 | |
| 33 | const waitUntilDeactivated = (id: string) => |
| 34 | GetBillingPortalConfiguration({ configuration: id }).pipe( |
| 35 | Effect.map((configuration) => |
| 36 | configuration.active ? ("active" as const) : ("inactive" as const), |
| 37 | ), |
| 38 | Effect.catchIf(isMissing, () => Effect.succeed("inactive" as const)), |
| 39 | Effect.repeat({ |
| 40 | schedule: Schedule.spaced("1 second"), |
| 41 | until: (status) => status === "inactive", |
| 42 | times: 10, |
| 43 | }), |
| 44 | ); |
| 45 | |
| 46 | test.provider( |
| 47 | // Use a separate stack from the legacy test, which could own the account |
| 48 | // default and cannot destroy it. Leave that default and its state intact. |
| 49 | "create, update, and deactivate a non-default billing portal configuration", |
| 50 | (stack) => |
| 51 | Effect.gen(function* () { |
| 52 | yield* requireDefaultConfiguration; |
| 53 | yield* stack.destroy(); |
| 54 | |
| 55 | const created = yield* stack.deploy( |
| 56 | Effect.gen(function* () { |
| 57 | return yield* Stripe.BillingPortalConfiguration("CustomerPortal", { |
| 58 | name: "Alchemy Customer Portal", |
| 59 | defaultReturnUrl: "https://example.com/account", |
| 60 | businessProfile: { |
| 61 | headline: "Manage your billing", |
| 62 | privacyPolicyUrl: "https://example.com/privacy", |
| 63 | }, |
| 64 | features: { |
| 65 | invoiceHistory: { enabled: true }, |
| 66 | customerUpdate: { enabled: true, allowedUpdates: ["email", "address"] }, |
| 67 | }, |
| 68 | metadata: { env: "test" }, |
| 69 | }); |
| 70 | }), |
| 71 | ); |
| 72 | |
| 73 | expect(created.id).toMatch(/^bpc_/); |
| 74 | expect(created.isDefault).toEqual(false); |
| 75 | expect(created.name).toEqual("Alchemy Customer Portal"); |
| 76 | expect(created.active).toEqual(true); |
| 77 | expect(created.defaultReturnUrl).toEqual("https://example.com/account"); |
| 78 | expect(created.businessProfile.headline).toEqual("Manage your billing"); |
| 79 | expect(created.businessProfile.privacyPolicyUrl).toEqual("https://example.com/privacy"); |
| 80 | expect(created.features.invoiceHistory.enabled).toEqual(true); |
| 81 | expect(created.features.customerUpdate.enabled).toEqual(true); |
| 82 | expect(created.features.customerUpdate.allowedUpdates).toEqual(["email", "address"]); |
| 83 | expect(created.metadata).toMatchObject({ env: "test" }); |
| 84 | expect(created.livemode).toEqual(false); |
| 85 | expect(created.created).toEqual(expect.any(Number)); |
| 86 | |
| 87 | const fetched = yield* GetBillingPortalConfiguration({ configuration: created.id }); |
| 88 | expect(fetched.id).toEqual(created.id); |
| 89 | expect(fetched.is_default).toEqual(false); |
| 90 | expect(fetched.name).toEqual("Alchemy Customer Portal"); |
| 91 | expect(fetched.active).toEqual(true); |
| 92 | expect(fetched.default_return_url).toEqual("https://example.com/account"); |
| 93 | expect(fetched.features.invoice_history.enabled).toEqual(true); |
| 94 | expect(fetched.features.customer_update.enabled).toEqual(true); |
| 95 | expect(fetched.metadata?.env).toEqual("test"); |
| 96 | expect(fetched.metadata?.[Stripe.alchemyMetadataKeys.stack]).toBeDefined(); |
| 97 | expect(fetched.metadata?.[Stripe.alchemyMetadataKeys.stage]).toBeDefined(); |
| 98 | expect(fetched.metadata?.[Stripe.alchemyMetadataKeys.id]).toBeDefined(); |
| 99 | |
| 100 | const updated = yield* stack.deploy( |
| 101 | Effect.gen(function* () { |
| 102 | return yield* Stripe.BillingPortalConfiguration("CustomerPortal", { |
| 103 | name: "Alchemy Customer Portal Updated", |
| 104 | defaultReturnUrl: "https://example.com/billing", |
| 105 | businessProfile: { |
| 106 | headline: "Updated headline", |
| 107 | privacyPolicyUrl: "https://example.com/privacy", |
| 108 | termsOfServiceUrl: "https://example.com/terms", |
| 109 | }, |
| 110 | features: { |
| 111 | invoiceHistory: { enabled: true }, |
| 112 | customerUpdate: { enabled: true, allowedUpdates: ["email", "name"] }, |
| 113 | paymentMethodUpdate: { enabled: true }, |
| 114 | }, |
| 115 | metadata: { env: "test", revision: "2" }, |
| 116 | }); |
| 117 | }), |
| 118 | ); |
| 119 | |
| 120 | expect(updated.id).toEqual(created.id); |
| 121 | expect(updated.isDefault).toEqual(false); |
| 122 | expect(updated.name).toEqual("Alchemy Customer Portal Updated"); |
| 123 | expect(updated.active).toEqual(true); |
| 124 | expect(updated.defaultReturnUrl).toEqual("https://example.com/billing"); |
| 125 | expect(updated.businessProfile.headline).toEqual("Updated headline"); |
| 126 | expect(updated.businessProfile.termsOfServiceUrl).toEqual("https://example.com/terms"); |
| 127 | expect(updated.features.customerUpdate.allowedUpdates).toEqual(["email", "name"]); |
| 128 | expect(updated.features.paymentMethodUpdate.enabled).toEqual(true); |
| 129 | expect(updated.metadata).toEqual({ env: "test", revision: "2" }); |
| 130 | |
| 131 | const refetched = yield* GetBillingPortalConfiguration({ configuration: updated.id }); |
| 132 | expect(refetched.name).toEqual("Alchemy Customer Portal Updated"); |
| 133 | expect(refetched.default_return_url).toEqual("https://example.com/billing"); |
| 134 | expect(refetched.features.payment_method_update.enabled).toEqual(true); |
| 135 | expect(refetched.metadata?.env).toEqual("test"); |
| 136 | expect(refetched.metadata?.revision).toEqual("2"); |
| 137 | expect(refetched.metadata?.[Stripe.alchemyMetadataKeys.id]).toBeDefined(); |
| 138 | |
| 139 | yield* stack.destroy(); |
| 140 | |
| 141 | const deactivated = yield* waitUntilDeactivated(created.id); |
| 142 | expect(deactivated).toEqual("inactive"); |
| 143 | }).pipe(logLevel), |
| 144 | { |
| 145 | tags: ["provider:stripe", "provider:stripe:billingportalconfiguration", "live"], |
| 146 | timeout: 120_000, |
| 147 | }, |
| 148 | ); |
| 149 | |
| 150 | test.provider( |
| 151 | "list enumerates the deployed billing portal configuration", |
| 152 | (stack) => |
| 153 | Effect.gen(function* () { |
| 154 | yield* requireDefaultConfiguration; |
| 155 | yield* stack.destroy(); |
| 156 | |
| 157 | const deployed = yield* stack.deploy( |
| 158 | Effect.gen(function* () { |
| 159 | return yield* Stripe.BillingPortalConfiguration("ListPortal", { |
| 160 | name: "Alchemy List Portal", |
| 161 | features: { invoiceHistory: { enabled: true } }, |
| 162 | metadata: { kind: "list" }, |
| 163 | }); |
| 164 | }), |
| 165 | ); |
| 166 | |
| 167 | expect(deployed.isDefault).toEqual(false); |
| 168 | |
| 169 | const provider = yield* Provider.findProvider(Stripe.BillingPortalConfiguration); |
| 170 | const all = yield* provider.list(); |
| 171 | const found = all.find((configuration) => configuration.id === deployed.id); |
| 172 | expect(found).toBeDefined(); |
| 173 | expect(found?.name).toEqual(deployed.name); |
| 174 | expect(found?.features.invoiceHistory.enabled).toEqual(true); |
| 175 | expect(found?.metadata).toMatchObject({ kind: "list" }); |
| 176 | // The account default is a Stripe singleton and is never enumerated. |
| 177 | expect(all.every((configuration) => !configuration.isDefault)).toEqual(true); |
| 178 | |
| 179 | yield* stack.destroy(); |
| 180 | |
| 181 | const deactivated = yield* waitUntilDeactivated(deployed.id); |
| 182 | expect(deactivated).toEqual("inactive"); |
| 183 | |
| 184 | // Deactivated configurations are effectively deleted and must not |
| 185 | // re-enter nuke. |
| 186 | const after = yield* provider.list(); |
| 187 | expect(after.find((configuration) => configuration.id === deployed.id)).toBeUndefined(); |
| 188 | }).pipe(logLevel), |
| 189 | { |
| 190 | tags: ["provider:stripe", "provider:stripe:billingportalconfiguration", "live"], |
| 191 | timeout: 120_000, |
| 192 | }, |
| 193 | ); |