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
| 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 { GetAppsSecretsFind } 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 ACCOUNT_SCOPE = { type: "account" as const }; |
| 16 | |
| 17 | const LIFECYCLE_NAME = "alchemy-apps-secret-lifecycle"; |
| 18 | const LIST_NAME = "alchemy-apps-secret-list"; |
| 19 | const REPLACE_FROM_NAME = "alchemy-apps-secret-replace-a"; |
| 20 | const REPLACE_TO_NAME = "alchemy-apps-secret-replace-b"; |
| 21 | |
| 22 | const LIFECYCLE_PAYLOAD = "alchemy-apps-secret-payload"; |
| 23 | const LIFECYCLE_PAYLOAD_ROTATED = "alchemy-apps-secret-payload-rotated"; |
| 24 | const LIST_PAYLOAD = "alchemy-apps-secret-list-payload"; |
| 25 | const REPLACE_PAYLOAD = "alchemy-apps-secret-replace-payload"; |
| 26 | |
| 27 | const EXPIRES_AT = 2_000_000_000; |
| 28 | const EXPIRES_AT_UPDATED = 2_100_000_000; |
| 29 | |
| 30 | const waitUntilGone = (name: string) => |
| 31 | GetAppsSecretsFind({ name, scope: ACCOUNT_SCOPE }).pipe( |
| 32 | Effect.map((secret) => (secret.deleted === true ? ("gone" as const) : ("found" as const))), |
| 33 | Effect.catchIf(isMissingStripeResource, () => Effect.succeed("gone" as const)), |
| 34 | Effect.repeat({ |
| 35 | schedule: Schedule.spaced("1 second"), |
| 36 | until: (status) => status === "gone", |
| 37 | times: 10, |
| 38 | }), |
| 39 | ); |
| 40 | |
| 41 | test.provider( |
| 42 | "create, update, and delete an apps secret", |
| 43 | (stack) => |
| 44 | Effect.gen(function* () { |
| 45 | yield* stack.destroy(); |
| 46 | |
| 47 | const created = yield* stack.deploy( |
| 48 | Effect.gen(function* () { |
| 49 | return yield* Stripe.AppsSecret("LifecycleSecret", { |
| 50 | name: LIFECYCLE_NAME, |
| 51 | payload: LIFECYCLE_PAYLOAD, |
| 52 | scope: ACCOUNT_SCOPE, |
| 53 | expiresAt: EXPIRES_AT, |
| 54 | }); |
| 55 | }), |
| 56 | ); |
| 57 | |
| 58 | expect(created.id).toMatch(/^(appsecret_|secret_)/); |
| 59 | expect(created.name).toEqual(LIFECYCLE_NAME); |
| 60 | expect(created.scope).toEqual(ACCOUNT_SCOPE); |
| 61 | expect(created.expiresAt).toEqual(EXPIRES_AT); |
| 62 | expect(created.livemode).toEqual(false); |
| 63 | expect(created.created).toEqual(expect.any(Number)); |
| 64 | |
| 65 | const fetched = yield* GetAppsSecretsFind({ |
| 66 | name: LIFECYCLE_NAME, |
| 67 | scope: ACCOUNT_SCOPE, |
| 68 | expand: ["payload"], |
| 69 | }); |
| 70 | expect(fetched.id).toEqual(created.id); |
| 71 | expect(fetched.name).toEqual(LIFECYCLE_NAME); |
| 72 | expect(fetched.scope.type).toEqual("account"); |
| 73 | expect(fetched.expires_at).toEqual(EXPIRES_AT); |
| 74 | if (fetched.payload != null) { |
| 75 | expect(fetched.payload).toEqual(LIFECYCLE_PAYLOAD); |
| 76 | } |
| 77 | |
| 78 | const updated = yield* stack.deploy( |
| 79 | Effect.gen(function* () { |
| 80 | return yield* Stripe.AppsSecret("LifecycleSecret", { |
| 81 | name: LIFECYCLE_NAME, |
| 82 | payload: LIFECYCLE_PAYLOAD_ROTATED, |
| 83 | scope: ACCOUNT_SCOPE, |
| 84 | expiresAt: EXPIRES_AT_UPDATED, |
| 85 | }); |
| 86 | }), |
| 87 | ); |
| 88 | |
| 89 | expect(updated.name).toEqual(LIFECYCLE_NAME); |
| 90 | expect(updated.scope).toEqual(ACCOUNT_SCOPE); |
| 91 | expect(updated.expiresAt).toEqual(EXPIRES_AT_UPDATED); |
| 92 | |
| 93 | const refetched = yield* GetAppsSecretsFind({ |
| 94 | name: LIFECYCLE_NAME, |
| 95 | scope: ACCOUNT_SCOPE, |
| 96 | expand: ["payload"], |
| 97 | }); |
| 98 | expect(refetched.name).toEqual(LIFECYCLE_NAME); |
| 99 | expect(refetched.expires_at).toEqual(EXPIRES_AT_UPDATED); |
| 100 | if (refetched.payload != null) { |
| 101 | expect(refetched.payload).toEqual(LIFECYCLE_PAYLOAD_ROTATED); |
| 102 | } |
| 103 | |
| 104 | yield* stack.destroy(); |
| 105 | |
| 106 | const gone = yield* waitUntilGone(LIFECYCLE_NAME); |
| 107 | expect(gone).toEqual("gone"); |
| 108 | }).pipe(logLevel), |
| 109 | { |
| 110 | tags: ["provider:stripe", "provider:stripe:appssecret", "live"], |
| 111 | timeout: 120_000, |
| 112 | }, |
| 113 | ); |
| 114 | |
| 115 | test.provider( |
| 116 | "replace when the name changes", |
| 117 | (stack) => |
| 118 | Effect.gen(function* () { |
| 119 | yield* stack.destroy(); |
| 120 | |
| 121 | const created = yield* stack.deploy( |
| 122 | Effect.gen(function* () { |
| 123 | return yield* Stripe.AppsSecret("ReplaceSecret", { |
| 124 | name: REPLACE_FROM_NAME, |
| 125 | payload: REPLACE_PAYLOAD, |
| 126 | scope: ACCOUNT_SCOPE, |
| 127 | }); |
| 128 | }), |
| 129 | ); |
| 130 | |
| 131 | expect(created.name).toEqual(REPLACE_FROM_NAME); |
| 132 | |
| 133 | const replaced = yield* stack.deploy( |
| 134 | Effect.gen(function* () { |
| 135 | return yield* Stripe.AppsSecret("ReplaceSecret", { |
| 136 | name: REPLACE_TO_NAME, |
| 137 | payload: REPLACE_PAYLOAD, |
| 138 | scope: ACCOUNT_SCOPE, |
| 139 | }); |
| 140 | }), |
| 141 | ); |
| 142 | |
| 143 | expect(replaced.name).toEqual(REPLACE_TO_NAME); |
| 144 | expect(replaced.id).not.toEqual(created.id); |
| 145 | |
| 146 | const fetched = yield* GetAppsSecretsFind({ |
| 147 | name: REPLACE_TO_NAME, |
| 148 | scope: ACCOUNT_SCOPE, |
| 149 | }); |
| 150 | expect(fetched.id).toEqual(replaced.id); |
| 151 | expect(fetched.name).toEqual(REPLACE_TO_NAME); |
| 152 | |
| 153 | const oldGone = yield* waitUntilGone(REPLACE_FROM_NAME); |
| 154 | expect(oldGone).toEqual("gone"); |
| 155 | |
| 156 | yield* stack.destroy(); |
| 157 | |
| 158 | const gone = yield* waitUntilGone(REPLACE_TO_NAME); |
| 159 | expect(gone).toEqual("gone"); |
| 160 | }).pipe(logLevel), |
| 161 | { |
| 162 | tags: ["provider:stripe", "provider:stripe:appssecret", "live"], |
| 163 | timeout: 120_000, |
| 164 | }, |
| 165 | ); |
| 166 | |
| 167 | test.provider( |
| 168 | "list enumerates the deployed apps secret", |
| 169 | (stack) => |
| 170 | Effect.gen(function* () { |
| 171 | yield* stack.destroy(); |
| 172 | |
| 173 | const deployed = yield* stack.deploy( |
| 174 | Effect.gen(function* () { |
| 175 | return yield* Stripe.AppsSecret("ListSecret", { |
| 176 | name: LIST_NAME, |
| 177 | payload: LIST_PAYLOAD, |
| 178 | scope: ACCOUNT_SCOPE, |
| 179 | }); |
| 180 | }), |
| 181 | ); |
| 182 | |
| 183 | const provider = yield* Provider.findProvider(Stripe.AppsSecret); |
| 184 | const all = yield* provider.list(); |
| 185 | const found = all.find((secret) => secret.id === deployed.id); |
| 186 | expect(found).toBeDefined(); |
| 187 | expect(found?.name).toEqual(LIST_NAME); |
| 188 | expect(found?.scope).toEqual(ACCOUNT_SCOPE); |
| 189 | |
| 190 | yield* stack.destroy(); |
| 191 | |
| 192 | const gone = yield* waitUntilGone(LIST_NAME); |
| 193 | expect(gone).toEqual("gone"); |
| 194 | }).pipe(logLevel), |
| 195 | { |
| 196 | tags: ["provider:stripe", "provider:stripe:appssecret", "live"], |
| 197 | timeout: 120_000, |
| 198 | }, |
| 199 | ); |