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

main
ts 178 lines 6.59 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 { GetProduct } 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 waitUntilGone = (id: string) =>
16 GetProduct({ id }).pipe(
17 Effect.as("found" as const),
18 Effect.catchIf(isMissingStripeResource, () => Effect.succeed("gone" as const)),
19 Effect.repeat({
20 schedule: Schedule.spaced("1 second"),
21 until: (status) => status === "gone",
22 times: 10,
23 }),
24 );
25
26 test.provider(
27 "create, update, and delete a product",
28 (stack) =>
29 Effect.gen(function* () {
30 yield* stack.destroy();
31
32 const created = yield* stack.deploy(
33 Effect.gen(function* () {
34 return yield* Stripe.Product("CatalogProduct", {
35 name: "Alchemy Catalog Product",
36 description: "Initial description",
37 images: ["https://example.com/product.png"],
38 metadata: { tier: "pro" },
39 });
40 }),
41 );
42
43 expect(created.id).toMatch(/^prod_/);
44 expect(created.name).toEqual("Alchemy Catalog Product");
45 expect(created.description).toEqual("Initial description");
46 expect(created.active).toEqual(true);
47 expect(created.images).toEqual(["https://example.com/product.png"]);
48 expect(created.metadata).toMatchObject({ tier: "pro" });
49 expect(created.created).toEqual(expect.any(Number));
50 expect(created.livemode).toEqual(false);
51
52 const fetched = yield* GetProduct({ id: created.id });
53 expect(fetched.id).toEqual(created.id);
54 expect(fetched.name).toEqual(created.name);
55 expect(fetched.description).toEqual("Initial description");
56 expect(fetched.active).toEqual(true);
57 expect(fetched.images).toEqual(["https://example.com/product.png"]);
58 expect(fetched.metadata?.tier).toEqual("pro");
59 expect(fetched.metadata?.[Stripe.alchemyMetadataKeys.stack]).toBeDefined();
60 expect(fetched.metadata?.[Stripe.alchemyMetadataKeys.stage]).toBeDefined();
61 expect(fetched.metadata?.[Stripe.alchemyMetadataKeys.id]).toBeDefined();
62
63 const updated = yield* stack.deploy(
64 Effect.gen(function* () {
65 return yield* Stripe.Product("CatalogProduct", {
66 name: "Alchemy Catalog Product Updated",
67 description: "Updated description",
68 active: false,
69 images: ["https://example.com/product-updated.png"],
70 metadata: { tier: "enterprise", sku: "ent-1" },
71 });
72 }),
73 );
74
75 expect(updated.id).toEqual(created.id);
76 expect(updated.name).toEqual("Alchemy Catalog Product Updated");
77 expect(updated.description).toEqual("Updated description");
78 expect(updated.active).toEqual(false);
79 expect(updated.images).toEqual(["https://example.com/product-updated.png"]);
80 expect(updated.metadata).toEqual({ tier: "enterprise", sku: "ent-1" });
81
82 const refetched = yield* GetProduct({ id: updated.id });
83 expect(refetched.name).toEqual(updated.name);
84 expect(refetched.description).toEqual("Updated description");
85 expect(refetched.active).toEqual(false);
86 expect(refetched.images).toEqual(updated.images);
87 expect(refetched.metadata?.tier).toEqual("enterprise");
88 expect(refetched.metadata?.sku).toEqual("ent-1");
89 expect(refetched.metadata?.[Stripe.alchemyMetadataKeys.id]).toBeDefined();
90
91 yield* stack.destroy();
92
93 const gone = yield* waitUntilGone(created.id);
94 expect(gone).toEqual("gone");
95 }).pipe(logLevel),
96 {
97 tags: ["provider:stripe", "provider:stripe:product", "live"],
98 timeout: 120_000,
99 },
100 );
101
102 test.provider(
103 "list enumerates the deployed product",
104 (stack) =>
105 Effect.gen(function* () {
106 yield* stack.destroy();
107
108 const deployed = yield* stack.deploy(
109 Effect.gen(function* () {
110 return yield* Stripe.Product("ListProduct", {
111 name: "Alchemy List Product",
112 metadata: { kind: "list" },
113 });
114 }),
115 );
116
117 const provider = yield* Provider.findProvider(Stripe.Product);
118 const all = yield* provider.list();
119 const found = all.find((product) => product.id === deployed.id);
120 expect(found).toBeDefined();
121 expect(found?.name).toEqual(deployed.name);
122 expect(found?.metadata).toMatchObject({ kind: "list" });
123
124 yield* stack.destroy();
125
126 const gone = yield* waitUntilGone(deployed.id);
127 expect(gone).toEqual("gone");
128 }).pipe(logLevel),
129 {
130 tags: ["provider:stripe", "provider:stripe:product", "live"],
131 timeout: 120_000,
132 },
133 );
134
135 test.provider(
136 "destroy archives a product that still has prices",
137 (stack) =>
138 Effect.gen(function* () {
139 yield* stack.destroy();
140
141 const created = yield* stack.deploy(
142 Effect.gen(function* () {
143 const product = yield* Stripe.Product("PricedProduct", {
144 name: "Alchemy Priced Product",
145 });
146 const price = yield* Stripe.Price("AttachedPrice", {
147 product: product.id,
148 currency: "usd",
149 unitAmount: 1500,
150 });
151 return { product, price };
152 }),
153 );
154
155 expect(created.product.id).toMatch(/^prod_/);
156 expect(created.price.id).toMatch(/^price_/);
157 expect(created.price.product).toEqual(created.product.id);
158
159 yield* stack.destroy();
160
161 const archived = yield* GetProduct({ id: created.product.id });
162 expect(archived.id).toEqual(created.product.id);
163 expect(archived.active).toEqual(false);
164
165 // Archived products (and their archived prices) are effectively
166 // deleted and must not re-enter nuke.
167 const products = yield* (yield* Provider.findProvider(Stripe.Product)).list();
168 expect(products.find((product) => product.id === created.product.id)).toBeUndefined();
169 const prices = yield* (yield* Provider.findProvider(Stripe.Price)).list();
170 expect(prices.find((price) => price.id === created.price.id)).toBeUndefined();
171 const plans = yield* (yield* Provider.findProvider(Stripe.Plan)).list();
172 expect(plans.find((plan) => plan.id === created.price.id)).toBeUndefined();
173 }).pipe(logLevel),
174 {
175 tags: ["provider:stripe", "provider:stripe:price", "provider:stripe:product", "live"],
176 timeout: 120_000,
177 },
178 );