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 { GetRadarValueListItem, GetRadarValueLists } 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 Result from "effect/Result"; |
| 10 | import * as Schedule from "effect/Schedule"; |
| 11 | |
| 12 | const { test } = Test.make({ providers: Stripe.providers() }); |
| 13 | |
| 14 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 15 | |
| 16 | const isMissing = isMissingStripeResource; |
| 17 | |
| 18 | const waitUntilGone = (id: string) => |
| 19 | GetRadarValueListItem({ item: id }).pipe( |
| 20 | Effect.as("found" as const), |
| 21 | Effect.catchIf(isMissing, () => Effect.succeed("gone" as const)), |
| 22 | Effect.repeat({ |
| 23 | schedule: Schedule.spaced("1 second"), |
| 24 | until: (status) => status === "gone", |
| 25 | times: 10, |
| 26 | }), |
| 27 | ); |
| 28 | |
| 29 | test.provider( |
| 30 | "radar value lists entitlement probe", |
| 31 | (stack) => |
| 32 | Effect.gen(function* () { |
| 33 | yield* stack.destroy(); |
| 34 | |
| 35 | const result = yield* GetRadarValueLists({ limit: 1 }).pipe(Effect.result); |
| 36 | |
| 37 | if (Result.isSuccess(result)) { |
| 38 | expect(Array.isArray(result.success.data)).toBe(true); |
| 39 | } else { |
| 40 | expect(["InvalidRequestError", "Forbidden", "Unauthorized"]).toContain(result.failure._tag); |
| 41 | } |
| 42 | |
| 43 | yield* stack.destroy(); |
| 44 | }).pipe(logLevel), |
| 45 | { |
| 46 | tags: [ |
| 47 | "provider:stripe", |
| 48 | "provider:stripe:radarvaluelist", |
| 49 | "provider:stripe:radarvaluelistitem", |
| 50 | "live", |
| 51 | ], |
| 52 | timeout: 60_000, |
| 53 | }, |
| 54 | ); |
| 55 | |
| 56 | test.provider( |
| 57 | "create, update, and delete a radar value list item", |
| 58 | (stack) => |
| 59 | Effect.gen(function* () { |
| 60 | yield* stack.destroy(); |
| 61 | |
| 62 | const created = yield* stack.deploy( |
| 63 | Effect.gen(function* () { |
| 64 | const list = yield* Stripe.RadarValueList("ItemLifecycleList", { |
| 65 | name: "Alchemy Radar Value List Item Lifecycle", |
| 66 | itemType: "email", |
| 67 | }); |
| 68 | const item = yield* Stripe.RadarValueListItem("BlockedEmail", { |
| 69 | valueList: list.id, |
| 70 | value: "blocked@example.com", |
| 71 | }); |
| 72 | return { list, item }; |
| 73 | }), |
| 74 | ); |
| 75 | |
| 76 | expect(created.item.id).toMatch(/^rsli_/); |
| 77 | expect(created.item.valueList).toEqual(created.list.id); |
| 78 | expect(created.item.value).toEqual("blocked@example.com"); |
| 79 | expect(created.item.livemode).toEqual(false); |
| 80 | expect(created.item.created).toEqual(expect.any(Number)); |
| 81 | expect(created.item.createdBy).toEqual(expect.any(String)); |
| 82 | |
| 83 | const fetched = yield* GetRadarValueListItem({ |
| 84 | item: created.item.id, |
| 85 | }); |
| 86 | expect(fetched.id).toEqual(created.item.id); |
| 87 | expect(fetched.value_list).toEqual(created.list.id); |
| 88 | expect(fetched.value).toEqual("blocked@example.com"); |
| 89 | expect(fetched.livemode).toEqual(false); |
| 90 | |
| 91 | const updated = yield* stack.deploy( |
| 92 | Effect.gen(function* () { |
| 93 | const list = yield* Stripe.RadarValueList("ItemLifecycleList", { |
| 94 | name: "Alchemy Radar Value List Item Lifecycle", |
| 95 | itemType: "email", |
| 96 | }); |
| 97 | const item = yield* Stripe.RadarValueListItem("BlockedEmail", { |
| 98 | valueList: list.id, |
| 99 | value: "blocked@example.com", |
| 100 | }); |
| 101 | return { list, item }; |
| 102 | }), |
| 103 | ); |
| 104 | |
| 105 | expect(updated.item.id).toEqual(created.item.id); |
| 106 | expect(updated.item.valueList).toEqual(created.list.id); |
| 107 | expect(updated.item.value).toEqual("blocked@example.com"); |
| 108 | |
| 109 | yield* stack.destroy(); |
| 110 | |
| 111 | const gone = yield* waitUntilGone(created.item.id); |
| 112 | expect(gone).toEqual("gone"); |
| 113 | }).pipe(logLevel), |
| 114 | { |
| 115 | tags: [ |
| 116 | "provider:stripe", |
| 117 | "provider:stripe:radarvaluelist", |
| 118 | "provider:stripe:radarvaluelistitem", |
| 119 | "live", |
| 120 | ], |
| 121 | timeout: 120_000, |
| 122 | }, |
| 123 | ); |
| 124 | |
| 125 | test.provider( |
| 126 | "list enumerates the deployed radar value list item", |
| 127 | (stack) => |
| 128 | Effect.gen(function* () { |
| 129 | yield* stack.destroy(); |
| 130 | |
| 131 | const deployed = yield* stack.deploy( |
| 132 | Effect.gen(function* () { |
| 133 | const list = yield* Stripe.RadarValueList("ItemListList", { |
| 134 | name: "Alchemy Radar Value List Item List", |
| 135 | itemType: "email", |
| 136 | }); |
| 137 | const item = yield* Stripe.RadarValueListItem("ListEmail", { |
| 138 | valueList: list.id, |
| 139 | value: "list@example.com", |
| 140 | }); |
| 141 | return { list, item }; |
| 142 | }), |
| 143 | ); |
| 144 | |
| 145 | const provider = yield* Provider.findProvider(Stripe.RadarValueListItem); |
| 146 | const all = yield* provider.list(); |
| 147 | const found = all.find((item) => item.id === deployed.item.id); |
| 148 | expect(found).toBeDefined(); |
| 149 | expect(found?.valueList).toEqual(deployed.list.id); |
| 150 | expect(found?.value).toEqual("list@example.com"); |
| 151 | |
| 152 | yield* stack.destroy(); |
| 153 | |
| 154 | const gone = yield* waitUntilGone(deployed.item.id); |
| 155 | expect(gone).toEqual("gone"); |
| 156 | |
| 157 | const after = yield* provider.list(); |
| 158 | expect(after.find((item) => item.id === deployed.item.id)).toBeUndefined(); |
| 159 | }).pipe(logLevel), |
| 160 | { |
| 161 | tags: [ |
| 162 | "provider:stripe", |
| 163 | "provider:stripe:radarvaluelist", |
| 164 | "provider:stripe:radarvaluelistitem", |
| 165 | "live", |
| 166 | ], |
| 167 | timeout: 120_000, |
| 168 | }, |
| 169 | ); |
| 170 | |
| 171 | test.provider( |
| 172 | "replace when the item value changes", |
| 173 | (stack) => |
| 174 | Effect.gen(function* () { |
| 175 | yield* stack.destroy(); |
| 176 | |
| 177 | const created = yield* stack.deploy( |
| 178 | Effect.gen(function* () { |
| 179 | const list = yield* Stripe.RadarValueList("ItemReplaceList", { |
| 180 | name: "Alchemy Radar Value List Item Replace", |
| 181 | itemType: "email", |
| 182 | }); |
| 183 | const item = yield* Stripe.RadarValueListItem("ReplaceEmail", { |
| 184 | valueList: list.id, |
| 185 | value: "first@example.com", |
| 186 | }); |
| 187 | return { list, item }; |
| 188 | }), |
| 189 | ); |
| 190 | |
| 191 | expect(created.item.value).toEqual("first@example.com"); |
| 192 | |
| 193 | const replaced = yield* stack.deploy( |
| 194 | Effect.gen(function* () { |
| 195 | const list = yield* Stripe.RadarValueList("ItemReplaceList", { |
| 196 | name: "Alchemy Radar Value List Item Replace", |
| 197 | itemType: "email", |
| 198 | }); |
| 199 | const item = yield* Stripe.RadarValueListItem("ReplaceEmail", { |
| 200 | valueList: list.id, |
| 201 | value: "second@example.com", |
| 202 | }); |
| 203 | return { list, item }; |
| 204 | }), |
| 205 | ); |
| 206 | |
| 207 | expect(replaced.item.id).not.toEqual(created.item.id); |
| 208 | expect(replaced.item.valueList).toEqual(created.list.id); |
| 209 | expect(replaced.item.value).toEqual("second@example.com"); |
| 210 | |
| 211 | const newFetched = yield* GetRadarValueListItem({ |
| 212 | item: replaced.item.id, |
| 213 | }); |
| 214 | expect(newFetched.value).toEqual("second@example.com"); |
| 215 | |
| 216 | const oldGone = yield* waitUntilGone(created.item.id); |
| 217 | expect(oldGone).toEqual("gone"); |
| 218 | |
| 219 | yield* stack.destroy(); |
| 220 | |
| 221 | const gone = yield* waitUntilGone(replaced.item.id); |
| 222 | expect(gone).toEqual("gone"); |
| 223 | }).pipe(logLevel), |
| 224 | { |
| 225 | tags: [ |
| 226 | "provider:stripe", |
| 227 | "provider:stripe:radarvaluelist", |
| 228 | "provider:stripe:radarvaluelistitem", |
| 229 | "live", |
| 230 | ], |
| 231 | timeout: 120_000, |
| 232 | }, |
| 233 | ); |