| 1 | import * as Provider from "@/Provider"; |
| 2 | import * as Stripe from "@/Stripe"; |
| 3 | import * as Test from "@/Test/Alchemy"; |
| 4 | import { GetApplePayDomain } 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 LIFECYCLE_DOMAIN = "alchemy-apple-pay-lifecycle.example.com"; |
| 19 | const REPLACE_FROM_DOMAIN = "alchemy-apple-pay-replace-a.example.com"; |
| 20 | const REPLACE_TO_DOMAIN = "alchemy-apple-pay-replace-b.example.com"; |
| 21 | const LIST_DOMAIN = "alchemy-apple-pay-list.example.com"; |
| 22 | |
| 23 | const waitUntilGone = (id: string) => |
| 24 | GetApplePayDomain({ domain: id }).pipe( |
| 25 | Effect.as("found" as const), |
| 26 | Effect.catchIf(isMissingStripeResource, () => |
| 27 | Effect.succeed("gone" as const), |
| 28 | ), |
| 29 | Effect.repeat({ |
| 30 | schedule: Schedule.spaced("1 second"), |
| 31 | until: (status) => status === "gone", |
| 32 | times: 10, |
| 33 | }), |
| 34 | ); |
| 35 | |
| 36 | test.provider( |
| 37 | "create, update, and delete an apple pay domain", |
| 38 | (stack) => |
| 39 | Effect.gen(function* () { |
| 40 | yield* stack.destroy(); |
| 41 | |
| 42 | const created = yield* stack.deploy( |
| 43 | Effect.gen(function* () { |
| 44 | return yield* Stripe.ApplePayDomain("Checkout", { |
| 45 | domainName: LIFECYCLE_DOMAIN, |
| 46 | }); |
| 47 | }), |
| 48 | ); |
| 49 | |
| 50 | expect(created.id).toMatch(/^apwc_/); |
| 51 | expect(created.domainName).toEqual(LIFECYCLE_DOMAIN); |
| 52 | expect(created.created).toEqual(expect.any(Number)); |
| 53 | expect(created.livemode).toEqual(false); |
| 54 | |
| 55 | const fetched = yield* GetApplePayDomain({ |
| 56 | domain: created.id, |
| 57 | }); |
| 58 | expect(fetched.id).toEqual(created.id); |
| 59 | expect(fetched.domain_name).toEqual(LIFECYCLE_DOMAIN); |
| 60 | expect(fetched.livemode).toEqual(false); |
| 61 | |
| 62 | const updated = yield* stack.deploy( |
| 63 | Effect.gen(function* () { |
| 64 | return yield* Stripe.ApplePayDomain("Checkout", { |
| 65 | domainName: LIFECYCLE_DOMAIN, |
| 66 | }); |
| 67 | }), |
| 68 | ); |
| 69 | |
| 70 | expect(updated.id).toEqual(created.id); |
| 71 | expect(updated.domainName).toEqual(LIFECYCLE_DOMAIN); |
| 72 | |
| 73 | const refetched = yield* GetApplePayDomain({ |
| 74 | domain: updated.id, |
| 75 | }); |
| 76 | expect(refetched.id).toEqual(updated.id); |
| 77 | expect(refetched.domain_name).toEqual(LIFECYCLE_DOMAIN); |
| 78 | |
| 79 | yield* stack.destroy(); |
| 80 | |
| 81 | const gone = yield* waitUntilGone(created.id); |
| 82 | expect(gone).toEqual("gone"); |
| 83 | }).pipe(logLevel), |
| 84 | { |
| 85 | tags: ["provider:stripe", "provider:stripe:applepaydomain", "live"], |
| 86 | timeout: 120_000, |
| 87 | }, |
| 88 | ); |
| 89 | |
| 90 | test.provider( |
| 91 | "replace when domainName changes", |
| 92 | (stack) => |
| 93 | Effect.gen(function* () { |
| 94 | yield* stack.destroy(); |
| 95 | |
| 96 | const created = yield* stack.deploy( |
| 97 | Effect.gen(function* () { |
| 98 | return yield* Stripe.ApplePayDomain("ReplaceDomain", { |
| 99 | domainName: REPLACE_FROM_DOMAIN, |
| 100 | }); |
| 101 | }), |
| 102 | ); |
| 103 | |
| 104 | expect(created.domainName).toEqual(REPLACE_FROM_DOMAIN); |
| 105 | |
| 106 | const replaced = yield* stack.deploy( |
| 107 | Effect.gen(function* () { |
| 108 | return yield* Stripe.ApplePayDomain("ReplaceDomain", { |
| 109 | domainName: REPLACE_TO_DOMAIN, |
| 110 | }); |
| 111 | }), |
| 112 | ); |
| 113 | |
| 114 | expect(replaced.id).not.toEqual(created.id); |
| 115 | expect(replaced.domainName).toEqual(REPLACE_TO_DOMAIN); |
| 116 | |
| 117 | const fetched = yield* GetApplePayDomain({ |
| 118 | domain: replaced.id, |
| 119 | }); |
| 120 | expect(fetched.id).toEqual(replaced.id); |
| 121 | expect(fetched.domain_name).toEqual(REPLACE_TO_DOMAIN); |
| 122 | |
| 123 | const oldGone = yield* waitUntilGone(created.id); |
| 124 | expect(oldGone).toEqual("gone"); |
| 125 | |
| 126 | yield* stack.destroy(); |
| 127 | |
| 128 | const gone = yield* waitUntilGone(replaced.id); |
| 129 | expect(gone).toEqual("gone"); |
| 130 | }).pipe(logLevel), |
| 131 | { |
| 132 | tags: ["provider:stripe", "provider:stripe:applepaydomain", "live"], |
| 133 | timeout: 120_000, |
| 134 | }, |
| 135 | ); |
| 136 | |
| 137 | test.provider( |
| 138 | "list enumerates the deployed apple pay domain", |
| 139 | (stack) => |
| 140 | Effect.gen(function* () { |
| 141 | yield* stack.destroy(); |
| 142 | |
| 143 | const deployed = yield* stack.deploy( |
| 144 | Effect.gen(function* () { |
| 145 | return yield* Stripe.ApplePayDomain("ListDomain", { |
| 146 | domainName: LIST_DOMAIN, |
| 147 | }); |
| 148 | }), |
| 149 | ); |
| 150 | |
| 151 | const provider = yield* Provider.findProvider(Stripe.ApplePayDomain); |
| 152 | const all = yield* provider.list(); |
| 153 | const found = all.find((domain) => domain.id === deployed.id); |
| 154 | expect(found).toBeDefined(); |
| 155 | expect(found?.domainName).toEqual(LIST_DOMAIN); |
| 156 | |
| 157 | yield* stack.destroy(); |
| 158 | |
| 159 | const gone = yield* waitUntilGone(deployed.id); |
| 160 | expect(gone).toEqual("gone"); |
| 161 | }).pipe(logLevel), |
| 162 | { |
| 163 | tags: ["provider:stripe", "provider:stripe:applepaydomain", "live"], |
| 164 | timeout: 120_000, |
| 165 | }, |
| 166 | ); |