@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 202 lines 7.14 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 { GetBillingMeter } 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 isMissing = isMissingStripeResource;
16
17 const waitUntilInactive = (id: string) =>
18 GetBillingMeter({ id }).pipe(
19 Effect.map((meter) =>
20 meter.status === "inactive" ? ("inactive" as const) : ("active" as const),
21 ),
22 Effect.catchIf(isMissing, () => Effect.succeed("inactive" as const)),
23 Effect.repeat({
24 schedule: Schedule.spaced("1 second"),
25 until: (status) => status === "inactive",
26 times: 10,
27 }),
28 );
29
30 test.provider(
31 "create, update, and deactivate a billing meter",
32 (stack) =>
33 Effect.gen(function* () {
34 yield* stack.destroy();
35
36 const created = yield* stack.deploy(
37 Effect.gen(function* () {
38 return yield* Stripe.BillingMeter("UsageMeter", {
39 displayName: "Alchemy API Calls",
40 eventName: "alchemy_bm_lifecycle",
41 defaultAggregation: { formula: "sum" },
42 valueSettings: { eventPayloadKey: "value" },
43 customerMapping: {
44 type: "by_id",
45 eventPayloadKey: "stripe_customer_id",
46 },
47 });
48 }),
49 );
50
51 expect(created.id).toMatch(/^mtr_/);
52 expect(created.displayName).toEqual("Alchemy API Calls");
53 expect(created.eventName).toEqual("alchemy_bm_lifecycle");
54 expect(created.defaultAggregation).toEqual({ formula: "sum" });
55 expect(created.valueSettings).toEqual({ eventPayloadKey: "value" });
56 expect(created.customerMapping).toEqual({
57 type: "by_id",
58 eventPayloadKey: "stripe_customer_id",
59 });
60 expect(created.status).toEqual("active");
61 expect(created.created).toEqual(expect.any(Number));
62 expect(created.updated).toEqual(expect.any(Number));
63 expect(created.livemode).toEqual(false);
64
65 const fetched = yield* GetBillingMeter({ id: created.id });
66 expect(fetched.id).toEqual(created.id);
67 expect(fetched.display_name).toEqual("Alchemy API Calls");
68 expect(fetched.event_name).toEqual("alchemy_bm_lifecycle");
69 expect(fetched.default_aggregation.formula).toEqual("sum");
70 expect(fetched.value_settings.event_payload_key).toEqual("value");
71 expect(fetched.customer_mapping.type).toEqual("by_id");
72 expect(fetched.customer_mapping.event_payload_key).toEqual("stripe_customer_id");
73 expect(fetched.status).toEqual("active");
74
75 const updated = yield* stack.deploy(
76 Effect.gen(function* () {
77 return yield* Stripe.BillingMeter("UsageMeter", {
78 displayName: "Alchemy API Calls Updated",
79 eventName: "alchemy_bm_lifecycle",
80 defaultAggregation: { formula: "sum" },
81 valueSettings: { eventPayloadKey: "value" },
82 customerMapping: {
83 type: "by_id",
84 eventPayloadKey: "stripe_customer_id",
85 },
86 });
87 }),
88 );
89
90 expect(updated.id).toEqual(created.id);
91 expect(updated.displayName).toEqual("Alchemy API Calls Updated");
92 expect(updated.eventName).toEqual("alchemy_bm_lifecycle");
93 expect(updated.status).toEqual("active");
94
95 const refetched = yield* GetBillingMeter({ id: updated.id });
96 expect(refetched.id).toEqual(updated.id);
97 expect(refetched.display_name).toEqual("Alchemy API Calls Updated");
98 expect(refetched.event_name).toEqual("alchemy_bm_lifecycle");
99 expect(refetched.status).toEqual("active");
100
101 yield* stack.destroy();
102
103 const inactive = yield* waitUntilInactive(created.id);
104 expect(inactive).toEqual("inactive");
105 const deactivated = yield* GetBillingMeter({ id: created.id });
106 expect(deactivated.status).toEqual("inactive");
107 }).pipe(logLevel),
108 {
109 tags: ["provider:stripe", "provider:stripe:billingmeter", "live"],
110 timeout: 120_000,
111 },
112 );
113
114 test.provider(
115 "replace when event name changes",
116 (stack) =>
117 Effect.gen(function* () {
118 yield* stack.destroy();
119
120 const created = yield* stack.deploy(
121 Effect.gen(function* () {
122 return yield* Stripe.BillingMeter("ReplaceMeter", {
123 displayName: "Alchemy Replace Meter",
124 eventName: "alchemy_bm_replace_a",
125 defaultAggregation: { formula: "sum" },
126 });
127 }),
128 );
129
130 expect(created.eventName).toEqual("alchemy_bm_replace_a");
131 expect(created.status).toEqual("active");
132
133 const replaced = yield* stack.deploy(
134 Effect.gen(function* () {
135 return yield* Stripe.BillingMeter("ReplaceMeter", {
136 displayName: "Alchemy Replace Meter",
137 eventName: "alchemy_bm_replace_b",
138 defaultAggregation: { formula: "sum" },
139 });
140 }),
141 );
142
143 expect(replaced.id).not.toEqual(created.id);
144 expect(replaced.eventName).toEqual("alchemy_bm_replace_b");
145 expect(replaced.status).toEqual("active");
146
147 const fetched = yield* GetBillingMeter({ id: replaced.id });
148 expect(fetched.id).toEqual(replaced.id);
149 expect(fetched.event_name).toEqual("alchemy_bm_replace_b");
150 expect(fetched.status).toEqual("active");
151
152 const oldInactive = yield* waitUntilInactive(created.id);
153 expect(oldInactive).toEqual("inactive");
154
155 yield* stack.destroy();
156
157 const gone = yield* waitUntilInactive(replaced.id);
158 expect(gone).toEqual("inactive");
159 }).pipe(logLevel),
160 {
161 tags: ["provider:stripe", "provider:stripe:billingmeter", "live"],
162 timeout: 120_000,
163 },
164 );
165
166 test.provider(
167 "list enumerates the deployed billing meter",
168 (stack) =>
169 Effect.gen(function* () {
170 yield* stack.destroy();
171
172 const deployed = yield* stack.deploy(
173 Effect.gen(function* () {
174 return yield* Stripe.BillingMeter("ListMeter", {
175 displayName: "Alchemy List Meter",
176 eventName: "alchemy_bm_list",
177 defaultAggregation: { formula: "count" },
178 });
179 }),
180 );
181
182 const provider = yield* Provider.findProvider(Stripe.BillingMeter);
183 const all = yield* provider.list();
184 const found = all.find((meter) => meter.id === deployed.id);
185 expect(found).toBeDefined();
186 expect(found?.displayName).toEqual(deployed.displayName);
187 expect(found?.eventName).toEqual("alchemy_bm_list");
188 expect(found?.defaultAggregation).toEqual({ formula: "count" });
189
190 yield* stack.destroy();
191
192 const inactive = yield* waitUntilInactive(deployed.id);
193 expect(inactive).toEqual("inactive");
194
195 const after = yield* provider.list();
196 expect(after.find((meter) => meter.id === deployed.id)).toBeUndefined();
197 }).pipe(logLevel),
198 {
199 tags: ["provider:stripe", "provider:stripe:billingmeter", "live"],
200 timeout: 120_000,
201 },
202 );