The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as AWS from "@alchemy.run/aws"; |
| 2 | import { describe, expect } from "@alchemy.run/test"; |
| 3 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 4 | import * as Core from "@alchemy.run/test/Core"; |
| 5 | import * as smsvoice from "@distilled.cloud/aws/pinpoint-sms-voice-v2"; |
| 6 | import * as Data from "effect/Data"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as HttpClient from "effect/http/HttpClient"; |
| 9 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 10 | import * as Schedule from "effect/Schedule"; |
| 11 | import SmsVoiceOptOutTestFunctionLive, { |
| 12 | SmsVoiceOptOutTestFunction, |
| 13 | TEST_DESTINATION, |
| 14 | } from "./fixtures/optout-handler.ts"; |
| 15 | |
| 16 | const testOptions = { providers: AWS.providers() }; |
| 17 | const { test, beforeAll, afterAll } = Test.make(testOptions); |
| 18 | const sharedStack = Core.scratchStack(testOptions, "SmsVoiceBindings"); |
| 19 | |
| 20 | const readinessPolicy = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(60)]); |
| 21 | |
| 22 | let baseUrl: string; |
| 23 | |
| 24 | class TransientUpstream extends Data.TaggedError("TransientUpstream")<{ |
| 25 | readonly status: number; |
| 26 | readonly body: string; |
| 27 | }> {} |
| 28 | |
| 29 | const post = (path: string) => |
| 30 | HttpClient.execute(HttpClientRequest.post(`${baseUrl}${path}`)).pipe( |
| 31 | Effect.flatMap((response) => |
| 32 | response.status >= 500 |
| 33 | ? response.text.pipe( |
| 34 | Effect.flatMap((body) => |
| 35 | Effect.fail(new TransientUpstream({ status: response.status, body })), |
| 36 | ), |
| 37 | ) |
| 38 | : Effect.succeed(response), |
| 39 | ), |
| 40 | Effect.retry({ |
| 41 | while: (e) => e._tag === "TransientUpstream", |
| 42 | schedule: Schedule.max([Schedule.exponential("1 second"), Schedule.recurs(5)]), |
| 43 | }), |
| 44 | ); |
| 45 | |
| 46 | // Ungated typed-error probes: prove the distilled error unions carry the |
| 47 | // tags the bindings and tests depend on. |
| 48 | test.provider( |
| 49 | "describeKeywords on a nonexistent origination identity fails with ResourceNotFoundException", |
| 50 | () => |
| 51 | Effect.gen(function* () { |
| 52 | const error = yield* Effect.flip( |
| 53 | smsvoice.describeKeywords({ |
| 54 | OriginationIdentity: "phone-ffffffffffffffffffffffffffffffff", |
| 55 | }), |
| 56 | ); |
| 57 | expect(error._tag).toBe("ResourceNotFoundException"); |
| 58 | }), |
| 59 | { tags: ["provider:aws", "provider:aws:pinpointsmsvoicev2", "live"], timeout: 60_000 }, |
| 60 | ); |
| 61 | |
| 62 | test.provider( |
| 63 | "putOptedOutNumber on a nonexistent opt-out list fails with ResourceNotFoundException", |
| 64 | () => |
| 65 | Effect.gen(function* () { |
| 66 | const error = yield* Effect.flip( |
| 67 | smsvoice.putOptedOutNumber({ |
| 68 | OptOutListName: "alchemy-nonexistent-opt-out-list", |
| 69 | OptedOutNumber: TEST_DESTINATION, |
| 70 | }), |
| 71 | ); |
| 72 | expect(error._tag).toBe("ResourceNotFoundException"); |
| 73 | }), |
| 74 | { |
| 75 | tags: ["provider:aws", "provider:aws:lambda", "provider:aws:pinpointsmsvoicev2", "live"], |
| 76 | timeout: 60_000, |
| 77 | }, |
| 78 | ); |
| 79 | |
| 80 | describe( |
| 81 | "PinpointSMSVoiceV2 Bindings", |
| 82 | { tags: ["provider:aws", "provider:aws:lambda", "provider:aws:pinpointsmsvoicev2", "live"] }, |
| 83 | () => { |
| 84 | beforeAll( |
| 85 | Effect.gen(function* () { |
| 86 | yield* Effect.logInfo("SmsVoice bindings setup: destroying previous resources"); |
| 87 | yield* sharedStack.destroy(); |
| 88 | |
| 89 | yield* Effect.logInfo("SmsVoice bindings setup: deploying fixture"); |
| 90 | const { functionUrl } = yield* sharedStack.deploy( |
| 91 | Effect.gen(function* () { |
| 92 | return yield* SmsVoiceOptOutTestFunction; |
| 93 | }).pipe(Effect.provide(SmsVoiceOptOutTestFunctionLive)), |
| 94 | ); |
| 95 | |
| 96 | expect(functionUrl).toBeTruthy(); |
| 97 | baseUrl = functionUrl!.replace(/\/+$/, ""); |
| 98 | const readinessUrl = `${baseUrl}/ping`; |
| 99 | |
| 100 | yield* Effect.logInfo(`SmsVoice bindings setup: probing readiness at ${readinessUrl}`); |
| 101 | yield* HttpClient.get(readinessUrl).pipe( |
| 102 | Effect.flatMap((response) => |
| 103 | response.status === 200 |
| 104 | ? Effect.succeed(response) |
| 105 | : Effect.fail(new Error(`Function not ready: ${response.status}`)), |
| 106 | ), |
| 107 | Effect.tapError((error) => |
| 108 | Effect.logWarning(`SmsVoice bindings setup: fixture not ready yet (${String(error)})`), |
| 109 | ), |
| 110 | Effect.retry({ schedule: readinessPolicy }), |
| 111 | ); |
| 112 | |
| 113 | // Freshly attached IAM role policies take a few seconds to |
| 114 | // propagate — hold the tests until the account-level grant works |
| 115 | // (the probe returns AccessDeniedException until then). |
| 116 | yield* post("/feedback-probe").pipe( |
| 117 | Effect.flatMap((r) => r.json), |
| 118 | Effect.repeat({ |
| 119 | schedule: Schedule.spaced("3 seconds"), |
| 120 | until: (body): boolean => (body as { tag?: string }).tag !== "AccessDeniedException", |
| 121 | times: 20, |
| 122 | }), |
| 123 | ); |
| 124 | }), |
| 125 | { timeout: 240_000 }, |
| 126 | ); |
| 127 | |
| 128 | afterAll.skipIf(!!process.env.NO_DESTROY)(sharedStack.destroy(), { timeout: 120_000 }); |
| 129 | |
| 130 | describe("PinpointSMSVoiceV2.PutOptedOutNumber", () => { |
| 131 | test.provider( |
| 132 | "opts a destination number out", |
| 133 | (_stack) => |
| 134 | Effect.gen(function* () { |
| 135 | const response = (yield* post("/opt-out").pipe(Effect.flatMap((r) => r.json))) as { |
| 136 | optedOutNumber?: string; |
| 137 | endUserOptedOut?: boolean; |
| 138 | }; |
| 139 | |
| 140 | expect(response.optedOutNumber).toBe(TEST_DESTINATION); |
| 141 | // Manually opted out (by the API), not by the end user. |
| 142 | expect(response.endUserOptedOut).toBe(false); |
| 143 | }), |
| 144 | { timeout: 120_000 }, |
| 145 | ); |
| 146 | }); |
| 147 | |
| 148 | describe("PinpointSMSVoiceV2.DescribeOptedOutNumbers", () => { |
| 149 | test.provider( |
| 150 | "finds the opted-out number", |
| 151 | (_stack) => |
| 152 | Effect.gen(function* () { |
| 153 | // /opt-out is idempotent for a manually-opted-out number's |
| 154 | // presence — ensure it exists, then check. |
| 155 | yield* post("/opt-out"); |
| 156 | const response = (yield* post("/opt-out-check").pipe( |
| 157 | Effect.flatMap((r) => r.json), |
| 158 | )) as { count: number; numbers: string[] }; |
| 159 | |
| 160 | expect(response.count).toBe(1); |
| 161 | expect(response.numbers).toContain(TEST_DESTINATION); |
| 162 | }), |
| 163 | { timeout: 120_000 }, |
| 164 | ); |
| 165 | }); |
| 166 | |
| 167 | describe("PinpointSMSVoiceV2.DeleteOptedOutNumber", () => { |
| 168 | test.provider( |
| 169 | "opts the number back in", |
| 170 | (_stack) => |
| 171 | Effect.gen(function* () { |
| 172 | yield* post("/opt-out"); |
| 173 | const deleted = (yield* post("/opt-out-delete").pipe( |
| 174 | Effect.flatMap((r) => r.json), |
| 175 | )) as { ok: boolean; deleted?: string; tag?: string; message?: string }; |
| 176 | expect(deleted.ok, JSON.stringify(deleted)).toBe(true); |
| 177 | expect(deleted.deleted).toBe(TEST_DESTINATION); |
| 178 | |
| 179 | const after = (yield* post("/opt-out-check").pipe(Effect.flatMap((r) => r.json))) as { |
| 180 | count: number; |
| 181 | }; |
| 182 | expect(after.count).toBe(0); |
| 183 | }), |
| 184 | { timeout: 120_000 }, |
| 185 | ); |
| 186 | }); |
| 187 | |
| 188 | describe("PinpointSMSVoiceV2.CarrierLookup", () => { |
| 189 | test.provider( |
| 190 | "the grant allows the lookup", |
| 191 | (_stack) => |
| 192 | Effect.gen(function* () { |
| 193 | const response = (yield* post("/carrier-lookup").pipe( |
| 194 | Effect.flatMap((r) => r.json), |
| 195 | )) as { |
| 196 | ok: boolean; |
| 197 | e164PhoneNumber?: string; |
| 198 | phoneNumberType?: string; |
| 199 | tag?: string; |
| 200 | message?: string; |
| 201 | }; |
| 202 | |
| 203 | // The simulator number may be rejected as unsupported by the |
| 204 | // lookup provider — the binding is proven as long as IAM let |
| 205 | // the call through. |
| 206 | expect(response.tag, JSON.stringify(response)).not.toBe("AccessDeniedException"); |
| 207 | if (response.ok) { |
| 208 | expect(response.e164PhoneNumber).toBe(TEST_DESTINATION); |
| 209 | expect(response.phoneNumberType).toBeTruthy(); |
| 210 | } |
| 211 | }), |
| 212 | { timeout: 120_000 }, |
| 213 | ); |
| 214 | }); |
| 215 | |
| 216 | describe("PinpointSMSVoiceV2.PutMessageFeedback", () => { |
| 217 | test.provider( |
| 218 | "surfaces the typed ResourceNotFoundException for an unknown message", |
| 219 | (_stack) => |
| 220 | Effect.gen(function* () { |
| 221 | const response = (yield* post("/feedback-probe").pipe( |
| 222 | Effect.flatMap((r) => r.json), |
| 223 | )) as { ok: boolean; tag?: string; message?: string }; |
| 224 | |
| 225 | // The grant is on `*`; an unknown MessageId must surface the |
| 226 | // typed not-found tag (AccessDenied would mean a broken grant). |
| 227 | expect(response.ok).toBe(false); |
| 228 | expect(response.tag, JSON.stringify(response)).toBe("ResourceNotFoundException"); |
| 229 | }), |
| 230 | { timeout: 120_000 }, |
| 231 | ); |
| 232 | }); |
| 233 | }, |
| 234 | ); |