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 { GetEntitlementsFeature } 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 | GetEntitlementsFeature({ id }).pipe( |
| 19 | Effect.map((feature) => (feature.active ? ("active" as const) : ("inactive" as const))), |
| 20 | Effect.catchIf(isMissing, () => Effect.succeed("inactive" as const)), |
| 21 | Effect.repeat({ |
| 22 | schedule: Schedule.spaced("1 second"), |
| 23 | until: (status) => status === "inactive", |
| 24 | times: 10, |
| 25 | }), |
| 26 | ); |
| 27 | |
| 28 | test.provider( |
| 29 | "create, update, and deactivate an entitlements feature", |
| 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.EntitlementsFeature("Seats", { |
| 37 | lookupKey: "alchemy-ent-feat-lifecycle", |
| 38 | name: "Alchemy Seats Feature", |
| 39 | metadata: { plan: "pro" }, |
| 40 | }); |
| 41 | }), |
| 42 | ); |
| 43 | |
| 44 | expect(created.id).toMatch(/^feat_/); |
| 45 | expect(created.lookupKey).toEqual("alchemy-ent-feat-lifecycle"); |
| 46 | expect(created.name).toEqual("Alchemy Seats Feature"); |
| 47 | expect(created.active).toEqual(true); |
| 48 | expect(created.metadata).toMatchObject({ plan: "pro" }); |
| 49 | expect(created.livemode).toEqual(false); |
| 50 | |
| 51 | const fetched = yield* GetEntitlementsFeature({ id: created.id }); |
| 52 | expect(fetched.id).toEqual(created.id); |
| 53 | expect(fetched.lookup_key).toEqual("alchemy-ent-feat-lifecycle"); |
| 54 | expect(fetched.name).toEqual("Alchemy Seats Feature"); |
| 55 | expect(fetched.active).toEqual(true); |
| 56 | expect(fetched.metadata?.plan).toEqual("pro"); |
| 57 | expect(fetched.metadata?.[Stripe.alchemyMetadataKeys.stack]).toBeDefined(); |
| 58 | expect(fetched.metadata?.[Stripe.alchemyMetadataKeys.stage]).toBeDefined(); |
| 59 | expect(fetched.metadata?.[Stripe.alchemyMetadataKeys.id]).toBeDefined(); |
| 60 | |
| 61 | const updated = yield* stack.deploy( |
| 62 | Effect.gen(function* () { |
| 63 | return yield* Stripe.EntitlementsFeature("Seats", { |
| 64 | lookupKey: "alchemy-ent-feat-lifecycle", |
| 65 | name: "Alchemy Seats Feature Updated", |
| 66 | metadata: { plan: "enterprise", sku: "ent-1" }, |
| 67 | }); |
| 68 | }), |
| 69 | ); |
| 70 | |
| 71 | expect(updated.id).toEqual(created.id); |
| 72 | expect(updated.lookupKey).toEqual(created.lookupKey); |
| 73 | expect(updated.name).toEqual("Alchemy Seats Feature Updated"); |
| 74 | expect(updated.active).toEqual(true); |
| 75 | expect(updated.metadata).toEqual({ plan: "enterprise", sku: "ent-1" }); |
| 76 | |
| 77 | const refetched = yield* GetEntitlementsFeature({ id: updated.id }); |
| 78 | expect(refetched.id).toEqual(updated.id); |
| 79 | expect(refetched.name).toEqual("Alchemy Seats Feature Updated"); |
| 80 | expect(refetched.lookup_key).toEqual("alchemy-ent-feat-lifecycle"); |
| 81 | expect(refetched.metadata?.plan).toEqual("enterprise"); |
| 82 | expect(refetched.metadata?.sku).toEqual("ent-1"); |
| 83 | expect(refetched.metadata?.[Stripe.alchemyMetadataKeys.id]).toBeDefined(); |
| 84 | |
| 85 | yield* stack.destroy(); |
| 86 | |
| 87 | const inactive = yield* waitUntilInactive(created.id); |
| 88 | expect(inactive).toEqual("inactive"); |
| 89 | const deactivated = yield* GetEntitlementsFeature({ id: created.id }); |
| 90 | expect(deactivated.active).toEqual(false); |
| 91 | }).pipe(logLevel), |
| 92 | { |
| 93 | tags: ["provider:stripe", "provider:stripe:entitlementsfeature", "live"], |
| 94 | timeout: 120_000, |
| 95 | }, |
| 96 | ); |
| 97 | |
| 98 | test.provider( |
| 99 | "list enumerates the deployed entitlements feature", |
| 100 | (stack) => |
| 101 | Effect.gen(function* () { |
| 102 | yield* stack.destroy(); |
| 103 | |
| 104 | const deployed = yield* stack.deploy( |
| 105 | Effect.gen(function* () { |
| 106 | return yield* Stripe.EntitlementsFeature("ListFeature", { |
| 107 | lookupKey: "alchemy-ent-feat-list", |
| 108 | name: "Alchemy List Feature", |
| 109 | metadata: { kind: "list" }, |
| 110 | }); |
| 111 | }), |
| 112 | ); |
| 113 | |
| 114 | const provider = yield* Provider.findProvider(Stripe.EntitlementsFeature); |
| 115 | const all = yield* provider.list(); |
| 116 | const found = all.find((feature) => feature.id === deployed.id); |
| 117 | expect(found).toBeDefined(); |
| 118 | expect(found?.name).toEqual(deployed.name); |
| 119 | expect(found?.lookupKey).toEqual("alchemy-ent-feat-list"); |
| 120 | expect(found?.metadata).toMatchObject({ kind: "list" }); |
| 121 | |
| 122 | yield* stack.destroy(); |
| 123 | |
| 124 | const inactive = yield* waitUntilInactive(deployed.id); |
| 125 | expect(inactive).toEqual("inactive"); |
| 126 | |
| 127 | const after = yield* provider.list(); |
| 128 | expect(after.find((feature) => feature.id === deployed.id)).toBeUndefined(); |
| 129 | }).pipe(logLevel), |
| 130 | { |
| 131 | tags: ["provider:stripe", "provider:stripe:entitlementsfeature", "live"], |
| 132 | timeout: 120_000, |
| 133 | }, |
| 134 | ); |
| 135 | |
| 136 | test.provider( |
| 137 | "replace when lookup key changes", |
| 138 | (stack) => |
| 139 | Effect.gen(function* () { |
| 140 | yield* stack.destroy(); |
| 141 | |
| 142 | const created = yield* stack.deploy( |
| 143 | Effect.gen(function* () { |
| 144 | return yield* Stripe.EntitlementsFeature("ReplaceFeature", { |
| 145 | lookupKey: "alchemy-ent-feat-replace-a", |
| 146 | name: "Alchemy Replace Feature", |
| 147 | }); |
| 148 | }), |
| 149 | ); |
| 150 | |
| 151 | expect(created.lookupKey).toEqual("alchemy-ent-feat-replace-a"); |
| 152 | |
| 153 | const replaced = yield* stack.deploy( |
| 154 | Effect.gen(function* () { |
| 155 | return yield* Stripe.EntitlementsFeature("ReplaceFeature", { |
| 156 | lookupKey: "alchemy-ent-feat-replace-b", |
| 157 | name: "Alchemy Replace Feature", |
| 158 | }); |
| 159 | }), |
| 160 | ); |
| 161 | |
| 162 | expect(replaced.id).not.toEqual(created.id); |
| 163 | expect(replaced.lookupKey).toEqual("alchemy-ent-feat-replace-b"); |
| 164 | expect(replaced.name).toEqual("Alchemy Replace Feature"); |
| 165 | |
| 166 | const fetched = yield* GetEntitlementsFeature({ id: replaced.id }); |
| 167 | expect(fetched.id).toEqual(replaced.id); |
| 168 | expect(fetched.lookup_key).toEqual("alchemy-ent-feat-replace-b"); |
| 169 | |
| 170 | const oldInactive = yield* waitUntilInactive(created.id); |
| 171 | expect(oldInactive).toEqual("inactive"); |
| 172 | |
| 173 | yield* stack.destroy(); |
| 174 | |
| 175 | const gone = yield* waitUntilInactive(replaced.id); |
| 176 | expect(gone).toEqual("inactive"); |
| 177 | }).pipe(logLevel), |
| 178 | { |
| 179 | tags: ["provider:stripe", "provider:stripe:entitlementsfeature", "live"], |
| 180 | timeout: 120_000, |
| 181 | }, |
| 182 | ); |