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

main
ts 300 lines 9.73 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 GetEntitlementsFeatures,
7 GetProductFeatures,
8 GetProductFeature,
9 CreateEntitlementsFeature,
10 UpdateEntitlementsFeature,
11 type EntitlementsFeature as StripeEntitlementsFeature,
12 } from "@distilled.cloud/stripe/stripe";
13 import * as Provider from "alchemy/Provider";
14 import * as Effect from "effect/Effect";
15 import { MinimumLogLevel } from "effect/References";
16 import * as Schedule from "effect/Schedule";
17
18 const { test } = Test.make({ providers: Stripe.providers() });
19
20 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
21
22 const isMissing = isMissingStripeResource;
23
24 const waitUntilGone = (product: string, id: string) =>
25 GetProductFeatures({ product, limit: 100 }).pipe(
26 Effect.map((response) =>
27 response.data.some((feature) => feature.id === id) ? ("found" as const) : ("gone" as const),
28 ),
29 Effect.catchIf(isMissing, () => Effect.succeed("gone" as const)),
30 Effect.repeat({
31 schedule: Schedule.spaced("1 second"),
32 until: (status) => status === "gone",
33 times: 10,
34 }),
35 );
36
37 /**
38 * Entitlements Features cannot be hard-deleted and cannot be updated once
39 * inactive. Create (or reuse an active row) via distilled so this suite is
40 * not blocked by the sibling resource's deactivate-only lifecycle.
41 */
42 const ensureFeature = (lookupKey: string, name: string) =>
43 Effect.gen(function* () {
44 const active = yield* GetEntitlementsFeatures({
45 lookup_key: lookupKey,
46 archived: false,
47 limit: 1,
48 });
49 if (active.data[0] !== undefined) return active.data[0];
50 return yield* CreateEntitlementsFeature({
51 lookup_key: lookupKey,
52 name,
53 metadata: { [Stripe.alchemyMetadataKeys.stack]: "ProductFeatureTest" },
54 }).pipe(
55 Effect.catchIf(
56 (e) => e._tag === "InvalidRequestError",
57 (e) =>
58 GetEntitlementsFeatures({
59 lookup_key: lookupKey,
60 archived: false,
61 limit: 1,
62 }).pipe(
63 Effect.flatMap((res) =>
64 res.data[0] !== undefined ? Effect.succeed(res.data[0]) : Effect.fail(e),
65 ),
66 ),
67 ),
68 );
69 });
70
71 const archiveFeature = (id: string) =>
72 UpdateEntitlementsFeature({ id, active: false }).pipe(
73 Effect.catchIf(isMissing, () => Effect.void),
74 Effect.catchIf(
75 (e) => e._tag === "InvalidRequestError",
76 () => Effect.void,
77 ),
78 );
79
80 const withFeatures = <A, E, R>(
81 features: ReadonlyArray<StripeEntitlementsFeature>,
82 body: Effect.Effect<A, E, R>,
83 ) =>
84 body.pipe(
85 Effect.ensuring(
86 Effect.forEach(features, (feature) => archiveFeature(feature.id).pipe(Effect.ignore), {
87 concurrency: "unbounded",
88 discard: true,
89 }),
90 ),
91 );
92
93 test.provider(
94 "create, update, and delete a product feature",
95 (stack) =>
96 Effect.gen(function* () {
97 yield* stack.destroy();
98
99 const feature = yield* ensureFeature(
100 "alchemy-pf-lifecycle-feat",
101 "Alchemy Product Feature Lifecycle",
102 );
103
104 yield* withFeatures(
105 [feature],
106 Effect.gen(function* () {
107 const created = yield* stack.deploy(
108 Effect.gen(function* () {
109 const product = yield* Stripe.Product("PFProduct", {
110 name: "Alchemy Product Feature Product",
111 });
112 const attachment = yield* Stripe.ProductFeature("Analytics", {
113 product: product.id,
114 entitlementFeature: feature.id,
115 });
116 return { product, attachment };
117 }),
118 );
119
120 expect(created.attachment.id).toMatch(/^prodft_/);
121 expect(created.attachment.product).toEqual(created.product.id);
122 expect(created.attachment.entitlementFeature).toEqual(feature.id);
123 expect(created.attachment.livemode).toEqual(false);
124
125 const fetched = yield* GetProductFeature({
126 product: created.attachment.product,
127 id: created.attachment.id,
128 });
129 expect(fetched.id).toEqual(created.attachment.id);
130 expect(fetched.entitlement_feature.id).toEqual(feature.id);
131 expect(fetched.livemode).toEqual(false);
132
133 const updated = yield* stack.deploy(
134 Effect.gen(function* () {
135 const product = yield* Stripe.Product("PFProduct", {
136 name: "Alchemy Product Feature Product",
137 });
138 const attachment = yield* Stripe.ProductFeature("Analytics", {
139 product: product.id,
140 entitlementFeature: feature.id,
141 });
142 return { product, attachment };
143 }),
144 );
145
146 expect(updated.attachment.id).toEqual(created.attachment.id);
147 expect(updated.attachment.product).toEqual(created.product.id);
148 expect(updated.attachment.entitlementFeature).toEqual(feature.id);
149
150 yield* stack.destroy();
151
152 const gone = yield* waitUntilGone(created.attachment.product, created.attachment.id);
153 expect(gone).toEqual("gone");
154 }),
155 );
156 }).pipe(logLevel),
157 {
158 tags: [
159 "provider:stripe",
160 "provider:stripe:entitlementsfeature",
161 "provider:stripe:product",
162 "provider:stripe:productfeature",
163 "live",
164 ],
165 timeout: 120_000,
166 },
167 );
168
169 test.provider(
170 "list enumerates the deployed product feature",
171 (stack) =>
172 Effect.gen(function* () {
173 yield* stack.destroy();
174
175 const feature = yield* ensureFeature("alchemy-pf-list-feat", "Alchemy Product Feature List");
176
177 yield* withFeatures(
178 [feature],
179 Effect.gen(function* () {
180 const deployed = yield* stack.deploy(
181 Effect.gen(function* () {
182 const product = yield* Stripe.Product("PFListProduct", {
183 name: "Alchemy Product Feature List Product",
184 });
185 const attachment = yield* Stripe.ProductFeature("ListAttach", {
186 product: product.id,
187 entitlementFeature: feature.id,
188 });
189 return { product, attachment };
190 }),
191 );
192
193 const provider = yield* Provider.findProvider(Stripe.ProductFeature);
194 const all = yield* provider.list();
195 const found = all.find((attachment) => attachment.id === deployed.attachment.id);
196 expect(found).toBeDefined();
197 expect(found?.product).toEqual(deployed.product.id);
198 expect(found?.entitlementFeature).toEqual(feature.id);
199
200 yield* stack.destroy();
201
202 const gone = yield* waitUntilGone(deployed.attachment.product, deployed.attachment.id);
203 expect(gone).toEqual("gone");
204
205 const after = yield* provider.list();
206 expect(
207 after.find((attachment) => attachment.id === deployed.attachment.id),
208 ).toBeUndefined();
209 }),
210 );
211 }).pipe(logLevel),
212 {
213 tags: [
214 "provider:stripe",
215 "provider:stripe:entitlementsfeature",
216 "provider:stripe:product",
217 "provider:stripe:productfeature",
218 "live",
219 ],
220 timeout: 120_000,
221 },
222 );
223
224 test.provider(
225 "replace when entitlement feature changes",
226 (stack) =>
227 Effect.gen(function* () {
228 yield* stack.destroy();
229
230 const featureA = yield* ensureFeature(
231 "alchemy-pf-replace-feat-a",
232 "Alchemy Product Feature Replace A",
233 );
234 const featureB = yield* ensureFeature(
235 "alchemy-pf-replace-feat-b",
236 "Alchemy Product Feature Replace B",
237 );
238
239 yield* withFeatures(
240 [featureA, featureB],
241 Effect.gen(function* () {
242 const created = yield* stack.deploy(
243 Effect.gen(function* () {
244 const product = yield* Stripe.Product("PFReplaceProduct", {
245 name: "Alchemy Product Feature Replace Product",
246 });
247 const attachment = yield* Stripe.ProductFeature("ReplaceAttach", {
248 product: product.id,
249 entitlementFeature: featureA.id,
250 });
251 return { product, attachment };
252 }),
253 );
254
255 expect(created.attachment.entitlementFeature).toEqual(featureA.id);
256
257 const replaced = yield* stack.deploy(
258 Effect.gen(function* () {
259 const product = yield* Stripe.Product("PFReplaceProduct", {
260 name: "Alchemy Product Feature Replace Product",
261 });
262 const attachment = yield* Stripe.ProductFeature("ReplaceAttach", {
263 product: product.id,
264 entitlementFeature: featureB.id,
265 });
266 return { product, attachment };
267 }),
268 );
269
270 expect(replaced.attachment.id).not.toEqual(created.attachment.id);
271 expect(replaced.attachment.product).toEqual(created.product.id);
272 expect(replaced.attachment.entitlementFeature).toEqual(featureB.id);
273
274 const newFetched = yield* GetProductFeature({
275 product: replaced.attachment.product,
276 id: replaced.attachment.id,
277 });
278 expect(newFetched.entitlement_feature.id).toEqual(featureB.id);
279
280 const oldGone = yield* waitUntilGone(created.attachment.product, created.attachment.id);
281 expect(oldGone).toEqual("gone");
282
283 yield* stack.destroy();
284
285 const gone = yield* waitUntilGone(replaced.attachment.product, replaced.attachment.id);
286 expect(gone).toEqual("gone");
287 }),
288 );
289 }).pipe(logLevel),
290 {
291 tags: [
292 "provider:stripe",
293 "provider:stripe:entitlementsfeature",
294 "provider:stripe:product",
295 "provider:stripe:productfeature",
296 "live",
297 ],
298 timeout: 120_000,
299 },
300 );