The best way to control Claude Code, Codex and more from any computer or phone

main
ts 233 lines 7.48 KB
Raw
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 GetPaymentLink,
7 CreatePrice,
8 CreateProduct,
9 UpdateProduct,
10 } from "@distilled.cloud/stripe/stripe";
11 import * as Provider from "alchemy/Provider";
12 import * as Effect from "effect/Effect";
13 import { MinimumLogLevel } from "effect/References";
14 import * as Schedule from "effect/Schedule";
15
16 const { test } = Test.make({ providers: Stripe.providers() });
17
18 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
19
20 const isMissing = isMissingStripeResource;
21
22 const waitUntilDeactivated = (id: string) =>
23 GetPaymentLink({ payment_link: id }).pipe(
24 Effect.map((link) => (link.active ? ("active" as const) : ("inactive" as const))),
25 Effect.catchIf(isMissing, () => Effect.succeed("inactive" as const)),
26 Effect.repeat({
27 schedule: Schedule.spaced("1 second"),
28 until: (status) => status === "inactive",
29 times: 10,
30 }),
31 );
32
33 const archiveProduct = (id: string) =>
34 UpdateProduct({ id, active: false }).pipe(Effect.catchIf(isMissing, () => Effect.void));
35
36 test.provider(
37 "create, update, and deactivate a payment link",
38 (stack) =>
39 Effect.gen(function* () {
40 yield* stack.destroy();
41
42 const product = yield* CreateProduct({
43 name: "Alchemy Payment Link Product",
44 });
45 const price = yield* CreatePrice({
46 product: product.id,
47 currency: "usd",
48 unit_amount: 2000,
49 });
50
51 const created = yield* stack.deploy(
52 Effect.gen(function* () {
53 return yield* Stripe.PaymentLink("BuyLink", {
54 lineItems: [{ price: price.id, quantity: 1 }],
55 metadata: { campaign: "launch" },
56 });
57 }),
58 );
59
60 expect(created.id).toMatch(/^plink_/);
61 expect(created.url).toEqual(expect.any(String));
62 expect(created.active).toEqual(true);
63 expect(created.lineItems).toEqual([
64 expect.objectContaining({ price: price.id, quantity: 1 }),
65 ]);
66 expect(created.allowPromotionCodes).toEqual(false);
67 expect(created.billingAddressCollection).toEqual("auto");
68 expect(created.metadata).toMatchObject({ campaign: "launch" });
69 expect(created.livemode).toEqual(false);
70
71 const fetched = yield* GetPaymentLink({
72 payment_link: created.id,
73 });
74 expect(fetched.id).toEqual(created.id);
75 expect(fetched.active).toEqual(true);
76 expect(fetched.metadata?.campaign).toEqual("launch");
77 expect(fetched.metadata?.[Stripe.alchemyMetadataKeys.stack]).toBeDefined();
78 expect(fetched.metadata?.[Stripe.alchemyMetadataKeys.stage]).toBeDefined();
79 expect(fetched.metadata?.[Stripe.alchemyMetadataKeys.id]).toBeDefined();
80
81 const updated = yield* stack.deploy(
82 Effect.gen(function* () {
83 return yield* Stripe.PaymentLink("BuyLink", {
84 lineItems: [{ price: price.id, quantity: 2 }],
85 allowPromotionCodes: true,
86 inactiveMessage: "This link is no longer available.",
87 metadata: { campaign: "spring", sku: "pro" },
88 });
89 }),
90 );
91
92 expect(updated.id).toEqual(created.id);
93 expect(updated.lineItems).toEqual([
94 expect.objectContaining({ price: price.id, quantity: 2 }),
95 ]);
96 expect(updated.allowPromotionCodes).toEqual(true);
97 expect(updated.inactiveMessage).toEqual("This link is no longer available.");
98 expect(updated.metadata).toEqual({ campaign: "spring", sku: "pro" });
99 expect(updated.active).toEqual(true);
100
101 const refetched = yield* GetPaymentLink({
102 payment_link: updated.id,
103 });
104 expect(refetched.allow_promotion_codes).toEqual(true);
105 expect(refetched.inactive_message).toEqual("This link is no longer available.");
106 expect(refetched.metadata?.campaign).toEqual("spring");
107 expect(refetched.metadata?.sku).toEqual("pro");
108 expect(refetched.metadata?.[Stripe.alchemyMetadataKeys.id]).toBeDefined();
109
110 yield* stack.destroy();
111
112 const deactivated = yield* waitUntilDeactivated(created.id);
113 expect(deactivated).toEqual("inactive");
114
115 yield* archiveProduct(product.id);
116 }).pipe(logLevel),
117 {
118 tags: ["provider:stripe", "provider:stripe:paymentlink", "live"],
119 timeout: 120_000,
120 },
121 );
122
123 test.provider(
124 "list enumerates the deployed payment link",
125 (stack) =>
126 Effect.gen(function* () {
127 yield* stack.destroy();
128
129 const product = yield* CreateProduct({
130 name: "Alchemy Payment Link List Product",
131 });
132 const price = yield* CreatePrice({
133 product: product.id,
134 currency: "usd",
135 unit_amount: 1500,
136 });
137
138 const deployed = yield* stack.deploy(
139 Effect.gen(function* () {
140 return yield* Stripe.PaymentLink("ListLink", {
141 lineItems: [{ price: price.id, quantity: 1 }],
142 metadata: { kind: "list" },
143 });
144 }),
145 );
146
147 const provider = yield* Provider.findProvider(Stripe.PaymentLink);
148 const all = yield* provider.list();
149 const found = all.find((link) => link.id === deployed.id);
150 expect(found).toBeDefined();
151 expect(found?.url).toEqual(deployed.url);
152 expect(found?.metadata).toMatchObject({ kind: "list" });
153 expect(found?.active).toEqual(true);
154
155 yield* stack.destroy();
156
157 const deactivated = yield* waitUntilDeactivated(deployed.id);
158 expect(deactivated).toEqual("inactive");
159
160 const after = yield* provider.list();
161 expect(after.find((link) => link.id === deployed.id)).toBeUndefined();
162
163 yield* archiveProduct(product.id);
164 }).pipe(logLevel),
165 {
166 tags: ["provider:stripe", "provider:stripe:paymentlink", "live"],
167 timeout: 120_000,
168 },
169 );
170
171 test.provider(
172 "replace when line item price changes",
173 (stack) =>
174 Effect.gen(function* () {
175 yield* stack.destroy();
176
177 const product = yield* CreateProduct({
178 name: "Alchemy Payment Link Replace Product",
179 });
180 const priceV1 = yield* CreatePrice({
181 product: product.id,
182 currency: "usd",
183 unit_amount: 1000,
184 });
185 const priceV2 = yield* CreatePrice({
186 product: product.id,
187 currency: "usd",
188 unit_amount: 2500,
189 });
190
191 const created = yield* stack.deploy(
192 Effect.gen(function* () {
193 return yield* Stripe.PaymentLink("ReplaceLink", {
194 lineItems: [{ price: priceV1.id, quantity: 1 }],
195 });
196 }),
197 );
198
199 const replaced = yield* stack.deploy(
200 Effect.gen(function* () {
201 return yield* Stripe.PaymentLink("ReplaceLink", {
202 lineItems: [{ price: priceV2.id, quantity: 1 }],
203 });
204 }),
205 );
206
207 expect(replaced.id).not.toEqual(created.id);
208 expect(replaced.lineItems).toEqual([
209 expect.objectContaining({ price: priceV2.id, quantity: 1 }),
210 ]);
211
212 const oldFetched = yield* GetPaymentLink({
213 payment_link: created.id,
214 });
215 expect(oldFetched.active).toEqual(false);
216
217 const newFetched = yield* GetPaymentLink({
218 payment_link: replaced.id,
219 });
220 expect(newFetched.active).toEqual(true);
221
222 yield* stack.destroy();
223
224 const deactivated = yield* waitUntilDeactivated(replaced.id);
225 expect(deactivated).toEqual("inactive");
226
227 yield* archiveProduct(product.id);
228 }).pipe(logLevel),
229 {
230 tags: ["provider:stripe", "provider:stripe:paymentlink", "live"],
231 timeout: 120_000,
232 },
233 );