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 { |
| 6 | GetTerminalReaders, |
| 7 | GetTerminalReader, |
| 8 | type DeletedTerminalReader, |
| 9 | type TerminalReader as StripeTerminalReader, |
| 10 | } from "@distilled.cloud/stripe/stripe"; |
| 11 | import * as Provider from "alchemy/Provider"; |
| 12 | import * as Effect from "effect/Effect"; |
| 13 | import { MinimumLogLevel } from "effect/References"; |
| 14 | import * as Result from "effect/Result"; |
| 15 | import * as Schedule from "effect/Schedule"; |
| 16 | |
| 17 | const { test } = Test.make({ providers: Stripe.providers() }); |
| 18 | |
| 19 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 20 | |
| 21 | const SIMULATED_REGISTRATION_CODE = "simulated-wpe"; |
| 22 | |
| 23 | const usAddress = { |
| 24 | line1: "185 Berry Street", |
| 25 | city: "San Francisco", |
| 26 | state: "CA", |
| 27 | postalCode: "94107", |
| 28 | country: "US", |
| 29 | } as const; |
| 30 | |
| 31 | const isDeletedReader = ( |
| 32 | value: StripeTerminalReader | DeletedTerminalReader, |
| 33 | ): value is DeletedTerminalReader => "deleted" in value && value.deleted === true; |
| 34 | |
| 35 | const waitUntilGone = (id: string) => |
| 36 | GetTerminalReader({ reader: id }).pipe( |
| 37 | Effect.map((reader) => (isDeletedReader(reader) ? ("gone" as const) : ("found" as const))), |
| 38 | Effect.catchIf(isMissingStripeResource, () => Effect.succeed("gone" as const)), |
| 39 | Effect.repeat({ |
| 40 | schedule: Schedule.spaced("1 second"), |
| 41 | until: (status) => status === "gone", |
| 42 | times: 10, |
| 43 | }), |
| 44 | ); |
| 45 | |
| 46 | test.provider( |
| 47 | "terminal readers entitlement probe", |
| 48 | (stack) => |
| 49 | Effect.gen(function* () { |
| 50 | yield* stack.destroy(); |
| 51 | |
| 52 | const result = yield* GetTerminalReaders({ limit: 1 }).pipe(Effect.result); |
| 53 | |
| 54 | if (Result.isSuccess(result)) { |
| 55 | expect(Array.isArray(result.success.data)).toBe(true); |
| 56 | } else { |
| 57 | expect(result.failure._tag).not.toEqual("UnknownStripeError"); |
| 58 | expect(["InvalidRequestError", "Forbidden", "Unauthorized"]).toContain(result.failure._tag); |
| 59 | } |
| 60 | |
| 61 | yield* stack.destroy(); |
| 62 | }).pipe(logLevel), |
| 63 | { |
| 64 | tags: ["provider:stripe", "provider:stripe:terminalreader", "live"], |
| 65 | timeout: 60_000, |
| 66 | }, |
| 67 | ); |
| 68 | |
| 69 | test.provider( |
| 70 | "create, update, and delete a terminal reader", |
| 71 | (stack) => |
| 72 | Effect.gen(function* () { |
| 73 | yield* stack.destroy(); |
| 74 | |
| 75 | const created = yield* stack.deploy( |
| 76 | Effect.gen(function* () { |
| 77 | const location = yield* Stripe.TerminalLocation("ReaderLifecycleLocation", { |
| 78 | displayName: "Alchemy Terminal Reader Lifecycle", |
| 79 | address: { ...usAddress }, |
| 80 | }); |
| 81 | const reader = yield* Stripe.TerminalReader("FrontCounter", { |
| 82 | registrationCode: SIMULATED_REGISTRATION_CODE, |
| 83 | label: "Alchemy Front Counter", |
| 84 | location: location.id, |
| 85 | metadata: { station: "1" }, |
| 86 | }); |
| 87 | return { location, reader }; |
| 88 | }), |
| 89 | ); |
| 90 | |
| 91 | expect(created.reader.id).toMatch(/^tmr_/); |
| 92 | expect(created.reader.label).toEqual("Alchemy Front Counter"); |
| 93 | expect(created.reader.location).toEqual(created.location.id); |
| 94 | expect(created.reader.deviceType).toEqual("simulated_wisepos_e"); |
| 95 | expect(created.reader.serialNumber.length).toBeGreaterThan(0); |
| 96 | expect(created.reader.metadata).toMatchObject({ station: "1" }); |
| 97 | expect(created.reader.livemode).toEqual(false); |
| 98 | |
| 99 | const fetched = yield* GetTerminalReader({ |
| 100 | reader: created.reader.id, |
| 101 | }); |
| 102 | expect(isDeletedReader(fetched)).toEqual(false); |
| 103 | if (isDeletedReader(fetched)) return; |
| 104 | expect(fetched.id).toEqual(created.reader.id); |
| 105 | expect(fetched.label).toEqual("Alchemy Front Counter"); |
| 106 | expect(fetched.device_type).toEqual("simulated_wisepos_e"); |
| 107 | expect(fetched.metadata?.station).toEqual("1"); |
| 108 | expect(fetched.metadata?.[Stripe.alchemyMetadataKeys.stack]).toBeDefined(); |
| 109 | expect(fetched.metadata?.[Stripe.alchemyMetadataKeys.stage]).toBeDefined(); |
| 110 | expect(fetched.metadata?.[Stripe.alchemyMetadataKeys.id]).toBeDefined(); |
| 111 | |
| 112 | const updated = yield* stack.deploy( |
| 113 | Effect.gen(function* () { |
| 114 | const location = yield* Stripe.TerminalLocation("ReaderLifecycleLocation", { |
| 115 | displayName: "Alchemy Terminal Reader Lifecycle", |
| 116 | address: { ...usAddress }, |
| 117 | }); |
| 118 | const reader = yield* Stripe.TerminalReader("FrontCounter", { |
| 119 | registrationCode: SIMULATED_REGISTRATION_CODE, |
| 120 | label: "Alchemy Front Counter Updated", |
| 121 | location: location.id, |
| 122 | metadata: { station: "1", floor: "lobby" }, |
| 123 | }); |
| 124 | return { location, reader }; |
| 125 | }), |
| 126 | ); |
| 127 | |
| 128 | expect(updated.reader.id).toEqual(created.reader.id); |
| 129 | expect(updated.reader.label).toEqual("Alchemy Front Counter Updated"); |
| 130 | expect(updated.reader.location).toEqual(created.location.id); |
| 131 | expect(updated.reader.metadata).toEqual({ |
| 132 | station: "1", |
| 133 | floor: "lobby", |
| 134 | }); |
| 135 | |
| 136 | const refetched = yield* GetTerminalReader({ |
| 137 | reader: updated.reader.id, |
| 138 | }); |
| 139 | expect(isDeletedReader(refetched)).toEqual(false); |
| 140 | if (isDeletedReader(refetched)) return; |
| 141 | expect(refetched.label).toEqual("Alchemy Front Counter Updated"); |
| 142 | expect(refetched.metadata?.station).toEqual("1"); |
| 143 | expect(refetched.metadata?.floor).toEqual("lobby"); |
| 144 | expect(refetched.metadata?.[Stripe.alchemyMetadataKeys.id]).toBeDefined(); |
| 145 | |
| 146 | yield* stack.destroy(); |
| 147 | |
| 148 | const gone = yield* waitUntilGone(created.reader.id); |
| 149 | expect(gone).toEqual("gone"); |
| 150 | }).pipe(logLevel), |
| 151 | { |
| 152 | tags: [ |
| 153 | "provider:stripe", |
| 154 | "provider:stripe:terminallocation", |
| 155 | "provider:stripe:terminalreader", |
| 156 | "live", |
| 157 | ], |
| 158 | timeout: 120_000, |
| 159 | }, |
| 160 | ); |
| 161 | |
| 162 | test.provider( |
| 163 | "list enumerates the deployed terminal reader", |
| 164 | (stack) => |
| 165 | Effect.gen(function* () { |
| 166 | yield* stack.destroy(); |
| 167 | |
| 168 | const deployed = yield* stack.deploy( |
| 169 | Effect.gen(function* () { |
| 170 | const location = yield* Stripe.TerminalLocation("ReaderListLocation", { |
| 171 | displayName: "Alchemy Terminal Reader List", |
| 172 | address: { ...usAddress }, |
| 173 | }); |
| 174 | const reader = yield* Stripe.TerminalReader("ListReader", { |
| 175 | registrationCode: SIMULATED_REGISTRATION_CODE, |
| 176 | label: "Alchemy List Reader", |
| 177 | location: location.id, |
| 178 | metadata: { kind: "list" }, |
| 179 | }); |
| 180 | return { location, reader }; |
| 181 | }), |
| 182 | ); |
| 183 | |
| 184 | const provider = yield* Provider.findProvider(Stripe.TerminalReader); |
| 185 | const all = yield* provider.list(); |
| 186 | const found = all.find((reader) => reader.id === deployed.reader.id); |
| 187 | expect(found).toBeDefined(); |
| 188 | expect(found?.label).toEqual("Alchemy List Reader"); |
| 189 | expect(found?.metadata).toMatchObject({ kind: "list" }); |
| 190 | |
| 191 | yield* stack.destroy(); |
| 192 | |
| 193 | const gone = yield* waitUntilGone(deployed.reader.id); |
| 194 | expect(gone).toEqual("gone"); |
| 195 | }).pipe(logLevel), |
| 196 | { |
| 197 | tags: [ |
| 198 | "provider:stripe", |
| 199 | "provider:stripe:terminallocation", |
| 200 | "provider:stripe:terminalreader", |
| 201 | "live", |
| 202 | ], |
| 203 | timeout: 120_000, |
| 204 | }, |
| 205 | ); |