The best way to control Claude Code, Codex and more from any computer or phone
| 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 { GetBillingAlert } 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 waitUntilArchived = (id: string) => |
| 18 | GetBillingAlert({ id }).pipe( |
| 19 | Effect.map((alert) => |
| 20 | alert.status === "archived" ? ("archived" as const) : ("present" as const), |
| 21 | ), |
| 22 | Effect.catchIf(isMissing, () => Effect.succeed("archived" as const)), |
| 23 | Effect.repeat({ |
| 24 | schedule: Schedule.spaced("1 second"), |
| 25 | until: (status) => status === "archived", |
| 26 | times: 10, |
| 27 | }), |
| 28 | ); |
| 29 | |
| 30 | test.provider( |
| 31 | "create, deactivate, and archive a billing alert", |
| 32 | (stack) => |
| 33 | Effect.gen(function* () { |
| 34 | yield* stack.destroy(); |
| 35 | |
| 36 | const created = yield* stack.deploy( |
| 37 | Effect.gen(function* () { |
| 38 | const meter = yield* Stripe.BillingMeter("AlertMeter", { |
| 39 | displayName: "Alchemy Alert Meter", |
| 40 | eventName: "alchemy_alrt_lifecycle", |
| 41 | defaultAggregation: { formula: "sum" }, |
| 42 | }); |
| 43 | const alert = yield* Stripe.Alert("UsageAlert", { |
| 44 | title: "Alchemy Usage Alert", |
| 45 | usageThreshold: { |
| 46 | gte: 100, |
| 47 | meter: meter.id, |
| 48 | recurrence: "one_time", |
| 49 | }, |
| 50 | }); |
| 51 | return { meter, alert }; |
| 52 | }), |
| 53 | ); |
| 54 | |
| 55 | expect(created.alert.id).toMatch(/^alrt_/); |
| 56 | expect(created.alert.title).toEqual("Alchemy Usage Alert"); |
| 57 | expect(created.alert.alertType).toEqual("usage_threshold"); |
| 58 | expect(created.alert.status).toEqual("active"); |
| 59 | expect(created.alert.usageThreshold).toEqual({ |
| 60 | gte: 100, |
| 61 | meter: created.meter.id, |
| 62 | recurrence: "one_time", |
| 63 | }); |
| 64 | expect(created.alert.livemode).toEqual(false); |
| 65 | |
| 66 | const fetched = yield* GetBillingAlert({ id: created.alert.id }); |
| 67 | expect(fetched.id).toEqual(created.alert.id); |
| 68 | expect(fetched.title).toEqual("Alchemy Usage Alert"); |
| 69 | expect(fetched.alert_type).toEqual("usage_threshold"); |
| 70 | expect(fetched.status).toEqual("active"); |
| 71 | expect(fetched.usage_threshold?.gte).toEqual(100); |
| 72 | expect( |
| 73 | typeof fetched.usage_threshold?.meter === "string" |
| 74 | ? fetched.usage_threshold.meter |
| 75 | : fetched.usage_threshold?.meter.id, |
| 76 | ).toEqual(created.meter.id); |
| 77 | expect(fetched.usage_threshold?.recurrence).toEqual("one_time"); |
| 78 | |
| 79 | const updated = yield* stack.deploy( |
| 80 | Effect.gen(function* () { |
| 81 | const meter = yield* Stripe.BillingMeter("AlertMeter", { |
| 82 | displayName: "Alchemy Alert Meter", |
| 83 | eventName: "alchemy_alrt_lifecycle", |
| 84 | defaultAggregation: { formula: "sum" }, |
| 85 | }); |
| 86 | const alert = yield* Stripe.Alert("UsageAlert", { |
| 87 | title: "Alchemy Usage Alert", |
| 88 | usageThreshold: { |
| 89 | gte: 100, |
| 90 | meter: meter.id, |
| 91 | recurrence: "one_time", |
| 92 | }, |
| 93 | status: "inactive", |
| 94 | }); |
| 95 | return { meter, alert }; |
| 96 | }), |
| 97 | ); |
| 98 | |
| 99 | expect(updated.alert.id).toEqual(created.alert.id); |
| 100 | expect(updated.alert.title).toEqual("Alchemy Usage Alert"); |
| 101 | expect(updated.alert.status).toEqual("inactive"); |
| 102 | expect(updated.alert.usageThreshold?.gte).toEqual(100); |
| 103 | |
| 104 | const refetched = yield* GetBillingAlert({ id: updated.alert.id }); |
| 105 | expect(refetched.id).toEqual(updated.alert.id); |
| 106 | expect(refetched.title).toEqual("Alchemy Usage Alert"); |
| 107 | expect(refetched.status).toEqual("inactive"); |
| 108 | |
| 109 | yield* stack.destroy(); |
| 110 | |
| 111 | const archived = yield* waitUntilArchived(created.alert.id); |
| 112 | expect(archived).toEqual("archived"); |
| 113 | }).pipe(logLevel), |
| 114 | { |
| 115 | tags: ["provider:stripe", "provider:stripe:alert", "provider:stripe:billingmeter", "live"], |
| 116 | timeout: 120_000, |
| 117 | }, |
| 118 | ); |
| 119 | |
| 120 | test.provider( |
| 121 | "replace when usage threshold changes", |
| 122 | (stack) => |
| 123 | Effect.gen(function* () { |
| 124 | yield* stack.destroy(); |
| 125 | |
| 126 | const created = yield* stack.deploy( |
| 127 | Effect.gen(function* () { |
| 128 | const meter = yield* Stripe.BillingMeter("ReplaceAlertMeter", { |
| 129 | displayName: "Alchemy Replace Alert Meter", |
| 130 | eventName: "alchemy_alrt_replace", |
| 131 | defaultAggregation: { formula: "sum" }, |
| 132 | }); |
| 133 | const alert = yield* Stripe.Alert("ReplaceAlert", { |
| 134 | title: "Alchemy Replace Alert", |
| 135 | usageThreshold: { |
| 136 | gte: 50, |
| 137 | meter: meter.id, |
| 138 | recurrence: "one_time", |
| 139 | }, |
| 140 | }); |
| 141 | return { meter, alert }; |
| 142 | }), |
| 143 | ); |
| 144 | |
| 145 | expect(created.alert.usageThreshold?.gte).toEqual(50); |
| 146 | expect(created.alert.status).toEqual("active"); |
| 147 | |
| 148 | const replaced = yield* stack.deploy( |
| 149 | Effect.gen(function* () { |
| 150 | const meter = yield* Stripe.BillingMeter("ReplaceAlertMeter", { |
| 151 | displayName: "Alchemy Replace Alert Meter", |
| 152 | eventName: "alchemy_alrt_replace", |
| 153 | defaultAggregation: { formula: "sum" }, |
| 154 | }); |
| 155 | const alert = yield* Stripe.Alert("ReplaceAlert", { |
| 156 | title: "Alchemy Replace Alert", |
| 157 | usageThreshold: { |
| 158 | gte: 500, |
| 159 | meter: meter.id, |
| 160 | recurrence: "one_time", |
| 161 | }, |
| 162 | }); |
| 163 | return { meter, alert }; |
| 164 | }), |
| 165 | ); |
| 166 | |
| 167 | expect(replaced.alert.id).not.toEqual(created.alert.id); |
| 168 | expect(replaced.alert.usageThreshold?.gte).toEqual(500); |
| 169 | expect(replaced.alert.status).toEqual("active"); |
| 170 | |
| 171 | const fetched = yield* GetBillingAlert({ id: replaced.alert.id }); |
| 172 | expect(fetched.id).toEqual(replaced.alert.id); |
| 173 | expect(fetched.usage_threshold?.gte).toEqual(500); |
| 174 | expect(fetched.status).toEqual("active"); |
| 175 | |
| 176 | const oldArchived = yield* waitUntilArchived(created.alert.id); |
| 177 | expect(oldArchived).toEqual("archived"); |
| 178 | |
| 179 | yield* stack.destroy(); |
| 180 | |
| 181 | const gone = yield* waitUntilArchived(replaced.alert.id); |
| 182 | expect(gone).toEqual("archived"); |
| 183 | }).pipe(logLevel), |
| 184 | { |
| 185 | tags: ["provider:stripe", "provider:stripe:alert", "provider:stripe:billingmeter", "live"], |
| 186 | timeout: 120_000, |
| 187 | }, |
| 188 | ); |
| 189 | |
| 190 | test.provider( |
| 191 | "list enumerates the deployed billing alert", |
| 192 | (stack) => |
| 193 | Effect.gen(function* () { |
| 194 | yield* stack.destroy(); |
| 195 | |
| 196 | const deployed = yield* stack.deploy( |
| 197 | Effect.gen(function* () { |
| 198 | const meter = yield* Stripe.BillingMeter("ListAlertMeter", { |
| 199 | displayName: "Alchemy List Alert Meter", |
| 200 | eventName: "alchemy_alrt_list", |
| 201 | defaultAggregation: { formula: "count" }, |
| 202 | }); |
| 203 | const alert = yield* Stripe.Alert("ListAlert", { |
| 204 | title: "Alchemy List Alert", |
| 205 | usageThreshold: { |
| 206 | gte: 10, |
| 207 | meter: meter.id, |
| 208 | recurrence: "one_time", |
| 209 | }, |
| 210 | }); |
| 211 | return { meter, alert }; |
| 212 | }), |
| 213 | ); |
| 214 | |
| 215 | const provider = yield* Provider.findProvider(Stripe.Alert); |
| 216 | const all = yield* provider.list(); |
| 217 | const found = all.find((alert) => alert.id === deployed.alert.id); |
| 218 | expect(found).toBeDefined(); |
| 219 | expect(found?.title).toEqual(deployed.alert.title); |
| 220 | expect(found?.usageThreshold?.gte).toEqual(10); |
| 221 | expect(found?.usageThreshold?.meter).toEqual(deployed.meter.id); |
| 222 | |
| 223 | yield* stack.destroy(); |
| 224 | |
| 225 | const archived = yield* waitUntilArchived(deployed.alert.id); |
| 226 | expect(archived).toEqual("archived"); |
| 227 | |
| 228 | const after = yield* provider.list(); |
| 229 | expect(after.find((alert) => alert.id === deployed.alert.id)).toBeUndefined(); |
| 230 | }).pipe(logLevel), |
| 231 | { |
| 232 | tags: ["provider:stripe", "provider:stripe:alert", "provider:stripe:billingmeter", "live"], |
| 233 | timeout: 120_000, |
| 234 | }, |
| 235 | ); |