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

main
ts 251 lines 9.24 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 { GetBillingCreditGrant } 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 EXPIRES_AT = 4_102_444_800;
16
17 const waitUntilVoided = (id: string) =>
18 GetBillingCreditGrant({ id }).pipe(
19 Effect.map((grant) => (grant.voided_at != null ? ("voided" as const) : ("active" as const))),
20 Effect.catchIf(isMissingStripeResource, () => Effect.succeed("gone" as const)),
21 Effect.repeat({
22 schedule: Schedule.spaced("1 second"),
23 until: (status) => status === "voided" || status === "gone",
24 times: 10,
25 }),
26 );
27
28 test.provider(
29 "create, update, and void a credit grant",
30 (stack) =>
31 Effect.gen(function* () {
32 yield* stack.destroy();
33
34 const created = yield* stack.deploy(
35 Effect.gen(function* () {
36 const customer = yield* Stripe.Customer("GrantCustomer", {
37 email: "alchemy.credit.grant@example.com",
38 name: "Alchemy Credit Grant Customer",
39 });
40 return yield* Stripe.CreditGrant("WelcomeCredits", {
41 customer: customer.id,
42 amount: {
43 type: "monetary",
44 monetary: { currency: "usd", value: 1000 },
45 },
46 applicabilityConfig: { scope: { priceType: "metered" } },
47 category: "promotional",
48 name: "Alchemy Welcome Credits",
49 metadata: { campaign: "welcome" },
50 });
51 }),
52 );
53
54 expect(created.id).toMatch(/^credgr_/);
55 expect(created.customer).toMatch(/^cus_/);
56 expect(created.amount).toEqual({
57 type: "monetary",
58 monetary: { currency: "usd", value: 1000 },
59 });
60 expect(created.applicabilityConfig).toEqual({
61 scope: { priceType: "metered" },
62 });
63 expect(created.category).toEqual("promotional");
64 expect(created.name).toEqual("Alchemy Welcome Credits");
65 expect(created.voidedAt).toBeUndefined();
66 expect(created.livemode).toEqual(false);
67 expect(created.metadata).toMatchObject({ campaign: "welcome" });
68 expect(created.created).toEqual(expect.any(Number));
69
70 const fetched = yield* GetBillingCreditGrant({ id: created.id });
71 expect(fetched.id).toEqual(created.id);
72 expect(fetched.amount.monetary?.currency).toEqual("usd");
73 expect(fetched.amount.monetary?.value).toEqual(1000);
74 expect(fetched.applicability_config.scope.price_type).toEqual("metered");
75 expect(fetched.category).toEqual("promotional");
76 expect(fetched.name).toEqual("Alchemy Welcome Credits");
77 expect(fetched.voided_at).toBeNull();
78 expect(fetched.metadata?.campaign).toEqual("welcome");
79 expect(fetched.metadata?.[Stripe.alchemyMetadataKeys.stack]).toBeDefined();
80 expect(fetched.metadata?.[Stripe.alchemyMetadataKeys.stage]).toBeDefined();
81 expect(fetched.metadata?.[Stripe.alchemyMetadataKeys.id]).toBeDefined();
82
83 const updated = yield* stack.deploy(
84 Effect.gen(function* () {
85 const customer = yield* Stripe.Customer("GrantCustomer", {
86 email: "alchemy.credit.grant@example.com",
87 name: "Alchemy Credit Grant Customer",
88 });
89 return yield* Stripe.CreditGrant("WelcomeCredits", {
90 customer: customer.id,
91 amount: {
92 type: "monetary",
93 monetary: { currency: "usd", value: 1000 },
94 },
95 applicabilityConfig: { scope: { priceType: "metered" } },
96 category: "promotional",
97 name: "Alchemy Welcome Credits",
98 expiresAt: EXPIRES_AT,
99 metadata: { campaign: "spring", sku: "credits-2" },
100 });
101 }),
102 );
103
104 expect(updated.id).toEqual(created.id);
105 expect(updated.expiresAt).toEqual(EXPIRES_AT);
106 expect(updated.metadata).toEqual({
107 campaign: "spring",
108 sku: "credits-2",
109 });
110 expect(updated.amount.monetary?.value).toEqual(1000);
111
112 const refetched = yield* GetBillingCreditGrant({ id: updated.id });
113 expect(refetched.id).toEqual(updated.id);
114 expect(refetched.expires_at).toEqual(EXPIRES_AT);
115 expect(refetched.metadata?.campaign).toEqual("spring");
116 expect(refetched.metadata?.sku).toEqual("credits-2");
117 expect(refetched.metadata?.[Stripe.alchemyMetadataKeys.id]).toBeDefined();
118 expect(refetched.voided_at).toBeNull();
119
120 yield* stack.destroy();
121
122 const voided = yield* waitUntilVoided(created.id);
123 expect(voided === "voided" || voided === "gone").toEqual(true);
124 if (voided === "voided") {
125 const deactivated = yield* GetBillingCreditGrant({
126 id: created.id,
127 });
128 expect(deactivated.voided_at).toEqual(expect.any(Number));
129 }
130 }).pipe(logLevel),
131 {
132 tags: ["provider:stripe", "provider:stripe:creditgrant", "provider:stripe:customer", "live"],
133 timeout: 120_000,
134 },
135 );
136
137 test.provider(
138 "list enumerates the deployed credit grant",
139 (stack) =>
140 Effect.gen(function* () {
141 yield* stack.destroy();
142
143 const deployed = yield* stack.deploy(
144 Effect.gen(function* () {
145 const customer = yield* Stripe.Customer("ListGrantCustomer", {
146 email: "alchemy.credit.grant.list@example.com",
147 name: "Alchemy Credit Grant List Customer",
148 });
149 return yield* Stripe.CreditGrant("ListCredits", {
150 customer: customer.id,
151 amount: {
152 type: "monetary",
153 monetary: { currency: "usd", value: 2500 },
154 },
155 applicabilityConfig: { scope: { priceType: "metered" } },
156 category: "paid",
157 name: "Alchemy List Credits",
158 metadata: { kind: "list" },
159 });
160 }),
161 );
162
163 const provider = yield* Provider.findProvider(Stripe.CreditGrant);
164 const all = yield* provider.list();
165 const found = all.find((grant) => grant.id === deployed.id);
166 expect(found).toBeDefined();
167 expect(found?.name).toEqual(deployed.name);
168 expect(found?.amount.monetary?.value).toEqual(2500);
169 expect(found?.metadata).toMatchObject({ kind: "list" });
170 expect(found?.voidedAt).toBeUndefined();
171
172 yield* stack.destroy();
173
174 const voided = yield* waitUntilVoided(deployed.id);
175 expect(voided === "voided" || voided === "gone").toEqual(true);
176
177 const after = yield* provider.list();
178 expect(after.find((grant) => grant.id === deployed.id)).toBeUndefined();
179 }).pipe(logLevel),
180 {
181 tags: ["provider:stripe", "provider:stripe:creditgrant", "provider:stripe:customer", "live"],
182 timeout: 120_000,
183 },
184 );
185
186 test.provider(
187 "replace when the amount changes",
188 (stack) =>
189 Effect.gen(function* () {
190 yield* stack.destroy();
191
192 const created = yield* stack.deploy(
193 Effect.gen(function* () {
194 const customer = yield* Stripe.Customer("ReplaceGrantCustomer", {
195 email: "alchemy.credit.grant.replace@example.com",
196 name: "Alchemy Credit Grant Replace Customer",
197 });
198 return yield* Stripe.CreditGrant("ReplaceCredits", {
199 customer: customer.id,
200 amount: {
201 type: "monetary",
202 monetary: { currency: "usd", value: 1000 },
203 },
204 applicabilityConfig: { scope: { priceType: "metered" } },
205 category: "promotional",
206 name: "Alchemy Replace Credits",
207 });
208 }),
209 );
210
211 expect(created.amount.monetary?.value).toEqual(1000);
212
213 const replaced = yield* stack.deploy(
214 Effect.gen(function* () {
215 const customer = yield* Stripe.Customer("ReplaceGrantCustomer", {
216 email: "alchemy.credit.grant.replace@example.com",
217 name: "Alchemy Credit Grant Replace Customer",
218 });
219 return yield* Stripe.CreditGrant("ReplaceCredits", {
220 customer: customer.id,
221 amount: {
222 type: "monetary",
223 monetary: { currency: "usd", value: 2000 },
224 },
225 applicabilityConfig: { scope: { priceType: "metered" } },
226 category: "promotional",
227 name: "Alchemy Replace Credits",
228 });
229 }),
230 );
231
232 expect(replaced.id).not.toEqual(created.id);
233 expect(replaced.amount.monetary?.value).toEqual(2000);
234
235 const fetched = yield* GetBillingCreditGrant({ id: replaced.id });
236 expect(fetched.id).toEqual(replaced.id);
237 expect(fetched.amount.monetary?.value).toEqual(2000);
238
239 const oldVoided = yield* waitUntilVoided(created.id);
240 expect(oldVoided === "voided" || oldVoided === "gone").toEqual(true);
241
242 yield* stack.destroy();
243
244 const gone = yield* waitUntilVoided(replaced.id);
245 expect(gone === "voided" || gone === "gone").toEqual(true);
246 }).pipe(logLevel),
247 {
248 tags: ["provider:stripe", "provider:stripe:creditgrant", "provider:stripe:customer", "live"],
249 timeout: 120_000,
250 },
251 );