@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 144 lines 5.33 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 { GetCustomer } 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 GetCustomer({ customer: id }).pipe(
17 Effect.map((customer) =>
18 "deleted" in customer && customer.deleted ? ("gone" as const) : ("found" as const),
19 ),
20 Effect.catchIf(isMissingStripeResource, () => Effect.succeed("gone" as const)),
21 Effect.repeat({
22 schedule: Schedule.spaced("1 second"),
23 until: (status) => status === "gone",
24 times: 10,
25 }),
26 );
27
28 test.provider(
29 "create, update, and delete a customer",
30 (stack) =>
31 Effect.gen(function* () {
32 yield* stack.destroy();
33
34 const created = yield* stack.deploy(
35 Effect.gen(function* () {
36 return yield* Stripe.Customer("CatalogCustomer", {
37 email: "alchemy.catalog.customer@example.com",
38 name: "Alchemy Catalog Customer",
39 description: "Initial description",
40 phone: "+15555550100",
41 metadata: { plan: "pro" },
42 });
43 }),
44 );
45
46 expect(created.id).toMatch(/^cus_/);
47 expect(created.email).toEqual("alchemy.catalog.customer@example.com");
48 expect(created.name).toEqual("Alchemy Catalog Customer");
49 expect(created.description).toEqual("Initial description");
50 expect(created.phone).toEqual("+15555550100");
51 expect(created.metadata).toMatchObject({ plan: "pro" });
52 expect(created.created).toEqual(expect.any(Number));
53 expect(created.livemode).toEqual(false);
54
55 const fetched = yield* GetCustomer({ customer: created.id });
56 if (!("email" in fetched)) {
57 return;
58 }
59 expect(fetched.id).toEqual(created.id);
60 expect(fetched.email).toEqual(created.email);
61 expect(fetched.name).toEqual(created.name);
62 expect(fetched.description).toEqual("Initial description");
63 expect(fetched.phone).toEqual("+15555550100");
64 expect(fetched.metadata?.plan).toEqual("pro");
65 expect(fetched.metadata?.[Stripe.alchemyMetadataKeys.stack]).toBeDefined();
66 expect(fetched.metadata?.[Stripe.alchemyMetadataKeys.stage]).toBeDefined();
67 expect(fetched.metadata?.[Stripe.alchemyMetadataKeys.id]).toBeDefined();
68
69 const updated = yield* stack.deploy(
70 Effect.gen(function* () {
71 return yield* Stripe.Customer("CatalogCustomer", {
72 email: "alchemy.catalog.customer.updated@example.com",
73 name: "Alchemy Catalog Customer Updated",
74 description: "Updated description",
75 phone: "+15555550199",
76 metadata: { plan: "enterprise", sku: "ent-1" },
77 });
78 }),
79 );
80
81 expect(updated.id).toEqual(created.id);
82 expect(updated.email).toEqual("alchemy.catalog.customer.updated@example.com");
83 expect(updated.name).toEqual("Alchemy Catalog Customer Updated");
84 expect(updated.description).toEqual("Updated description");
85 expect(updated.phone).toEqual("+15555550199");
86 expect(updated.metadata).toEqual({ plan: "enterprise", sku: "ent-1" });
87
88 const refetched = yield* GetCustomer({ customer: updated.id });
89 if (!("email" in refetched)) {
90 return;
91 }
92 expect(refetched.email).toEqual(updated.email);
93 expect(refetched.name).toEqual(updated.name);
94 expect(refetched.description).toEqual("Updated description");
95 expect(refetched.phone).toEqual("+15555550199");
96 expect(refetched.metadata?.plan).toEqual("enterprise");
97 expect(refetched.metadata?.sku).toEqual("ent-1");
98 expect(refetched.metadata?.[Stripe.alchemyMetadataKeys.id]).toBeDefined();
99
100 yield* stack.destroy();
101
102 const gone = yield* waitUntilGone(created.id);
103 expect(gone).toEqual("gone");
104 }).pipe(logLevel),
105 {
106 tags: ["provider:stripe", "provider:stripe:customer", "live"],
107 timeout: 120_000,
108 },
109 );
110
111 test.provider(
112 "list enumerates the deployed customer",
113 (stack) =>
114 Effect.gen(function* () {
115 yield* stack.destroy();
116
117 const deployed = yield* stack.deploy(
118 Effect.gen(function* () {
119 return yield* Stripe.Customer("ListCustomer", {
120 email: "alchemy.list.customer@example.com",
121 name: "Alchemy List Customer",
122 metadata: { kind: "list" },
123 });
124 }),
125 );
126
127 const provider = yield* Provider.findProvider(Stripe.Customer);
128 const all = yield* provider.list();
129 const found = all.find((customer) => customer.id === deployed.id);
130 expect(found).toBeDefined();
131 expect(found?.email).toEqual(deployed.email);
132 expect(found?.name).toEqual(deployed.name);
133 expect(found?.metadata).toMatchObject({ kind: "list" });
134
135 yield* stack.destroy();
136
137 const gone = yield* waitUntilGone(deployed.id);
138 expect(gone).toEqual("gone");
139 }).pipe(logLevel),
140 {
141 tags: ["provider:stripe", "provider:stripe:customer", "live"],
142 timeout: 120_000,
143 },
144 );