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 eventbridge from "@distilled.cloud/aws/eventbridge"; |
| 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 MediaConnectTestFunctionLive, { MediaConnectTestFunction } from "./fixtures/handler"; |
| 12 | |
| 13 | const testOptions = { providers: AWS.providers() }; |
| 14 | const { test, beforeAll, afterAll } = Test.make(testOptions); |
| 15 | const sharedStack = Core.scratchStack(testOptions, "MediaConnectBindings"); |
| 16 | |
| 17 | // Lambda function URL cold-start (DNS, IAM propagation, init) can take well |
| 18 | // over 60s on a fresh deploy. |
| 19 | const readinessPolicy = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]); |
| 20 | |
| 21 | let baseUrl: string; |
| 22 | let functionArn: string; |
| 23 | |
| 24 | class TransientUpstream extends Data.TaggedError("TransientUpstream")<{ |
| 25 | readonly status: number; |
| 26 | readonly body: string; |
| 27 | }> {} |
| 28 | |
| 29 | // The shared Lambda fixture occasionally answers a transient 5xx under load |
| 30 | // (cold re-init, IAM propagation on the freshly attached policy that the |
| 31 | // handler's `Effect.orDie` surfaces as a 500). Retry only 5xx; a genuine |
| 32 | // 4xx/assertion failure surfaces immediately. |
| 33 | const send = (request: HttpClientRequest.HttpClientRequest) => |
| 34 | HttpClient.execute(request).pipe( |
| 35 | Effect.flatMap((response) => |
| 36 | response.status >= 500 |
| 37 | ? response.text.pipe( |
| 38 | Effect.flatMap((body) => |
| 39 | Effect.fail(new TransientUpstream({ status: response.status, body })), |
| 40 | ), |
| 41 | ) |
| 42 | : Effect.succeed(response), |
| 43 | ), |
| 44 | Effect.retry({ |
| 45 | while: (e) => e._tag === "TransientUpstream", |
| 46 | schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(6)]), |
| 47 | }), |
| 48 | ); |
| 49 | |
| 50 | const getJson = (path: string) => |
| 51 | send(HttpClientRequest.get(`${baseUrl}${path}`)).pipe(Effect.flatMap((r) => r.json)); |
| 52 | |
| 53 | const postJson = (path: string) => |
| 54 | send(HttpClientRequest.post(`${baseUrl}${path}`)).pipe(Effect.flatMap((r) => r.json)); |
| 55 | |
| 56 | describe.sequential( |
| 57 | "MediaConnect Bindings", |
| 58 | { tags: ["provider:aws", "provider:aws:lambda", "provider:aws:mediaconnect", "live"] }, |
| 59 | () => { |
| 60 | beforeAll( |
| 61 | Effect.gen(function* () { |
| 62 | yield* Effect.logInfo("MediaConnect test setup: destroying previous resources"); |
| 63 | yield* sharedStack.destroy(); |
| 64 | |
| 65 | yield* Effect.logInfo("MediaConnect test setup: deploying fixture"); |
| 66 | const attrs = yield* sharedStack.deploy( |
| 67 | Effect.gen(function* () { |
| 68 | return yield* MediaConnectTestFunction; |
| 69 | }).pipe(Effect.provide(MediaConnectTestFunctionLive)), |
| 70 | ); |
| 71 | |
| 72 | expect(attrs.functionUrl).toBeTruthy(); |
| 73 | baseUrl = attrs.functionUrl!.replace(/\/+$/, ""); |
| 74 | functionArn = attrs.functionArn; |
| 75 | |
| 76 | const readinessUrl = `${baseUrl}/bindings`; |
| 77 | yield* Effect.logInfo(`MediaConnect test setup: probing readiness at ${readinessUrl}`); |
| 78 | yield* HttpClient.get(readinessUrl).pipe( |
| 79 | Effect.flatMap((response) => |
| 80 | response.status === 200 |
| 81 | ? Effect.succeed(response) |
| 82 | : Effect.fail(new Error(`Function not ready: ${response.status}`)), |
| 83 | ), |
| 84 | Effect.tapError((error) => |
| 85 | Effect.logWarning(`MediaConnect test setup: fixture not ready yet (${String(error)})`), |
| 86 | ), |
| 87 | Effect.retry({ schedule: readinessPolicy }), |
| 88 | ); |
| 89 | }), |
| 90 | { timeout: 300_000 }, |
| 91 | ); |
| 92 | |
| 93 | afterAll(sharedStack.destroy(), { timeout: 240_000 }); |
| 94 | |
| 95 | describe("binding registration", () => { |
| 96 | test.provider("all nine capabilities initialize in the runtime", () => |
| 97 | Effect.gen(function* () { |
| 98 | const response = (yield* getJson("/bindings")) as { bound: string[] }; |
| 99 | expect(response.bound).toHaveLength(9); |
| 100 | }), |
| 101 | ); |
| 102 | }); |
| 103 | |
| 104 | describe("DescribeFlow", () => { |
| 105 | test.provider("the fixture flow idles in STANDBY", () => |
| 106 | Effect.gen(function* () { |
| 107 | const response = (yield* getJson("/flow")) as { status?: string; sourceName?: string }; |
| 108 | expect(response.status).toBe("STANDBY"); |
| 109 | expect(response.sourceName).toBe("primary"); |
| 110 | }), |
| 111 | ); |
| 112 | }); |
| 113 | |
| 114 | describe("DescribeFlowSourceMetadata", () => { |
| 115 | test.provider( |
| 116 | "an idle flow has no programs — only messages or the typed BadRequestException", |
| 117 | () => |
| 118 | Effect.gen(function* () { |
| 119 | const response = (yield* getJson("/source-metadata")) as { |
| 120 | programs: number; |
| 121 | messages: number; |
| 122 | tag?: string; |
| 123 | }; |
| 124 | expect(response.programs).toBe(0); |
| 125 | if (response.tag !== undefined) { |
| 126 | expect(response.tag).toBe("BadRequestException"); |
| 127 | } |
| 128 | }), |
| 129 | ); |
| 130 | }); |
| 131 | |
| 132 | describe("DescribeFlowSourceThumbnail", () => { |
| 133 | test.provider( |
| 134 | "an idle flow has no thumbnail image — only messages or the typed BadRequestException", |
| 135 | () => |
| 136 | Effect.gen(function* () { |
| 137 | const response = (yield* getJson("/thumbnail")) as { |
| 138 | hasImage: boolean; |
| 139 | messages: number; |
| 140 | tag?: string; |
| 141 | }; |
| 142 | expect(response.hasImage).toBe(false); |
| 143 | if (response.tag !== undefined) { |
| 144 | expect(response.tag).toBe("BadRequestException"); |
| 145 | } |
| 146 | }), |
| 147 | ); |
| 148 | }); |
| 149 | |
| 150 | describe("ListFlows", () => { |
| 151 | test.provider("account-level flow enumeration sees the fixture flow", () => |
| 152 | Effect.gen(function* () { |
| 153 | const response = (yield* getJson("/flows")) as { count: number }; |
| 154 | expect(response.count).toBeGreaterThanOrEqual(1); |
| 155 | }), |
| 156 | ); |
| 157 | }); |
| 158 | |
| 159 | describe("ListEntitlements", () => { |
| 160 | test.provider("account-level entitlement enumeration succeeds", () => |
| 161 | Effect.gen(function* () { |
| 162 | const response = (yield* getJson("/entitlements")) as { count: number }; |
| 163 | expect(response.count).toBeGreaterThanOrEqual(0); |
| 164 | }), |
| 165 | ); |
| 166 | }); |
| 167 | |
| 168 | describe("StopFlow", () => { |
| 169 | test.provider("stopping a STANDBY flow is the typed BadRequestException", () => |
| 170 | Effect.gen(function* () { |
| 171 | const response = (yield* postJson("/stop")) as { status?: string; tag?: string }; |
| 172 | expect(response.tag).toBe("BadRequestException"); |
| 173 | expect(response.status).toBeUndefined(); |
| 174 | }), |
| 175 | ); |
| 176 | }); |
| 177 | |
| 178 | describe("GrantFlowEntitlements + RevokeFlowEntitlement", () => { |
| 179 | test.provider("granting and revoking an entitlement roundtrips", () => |
| 180 | Effect.gen(function* () { |
| 181 | const response = (yield* postJson("/entitlement-cycle")) as { |
| 182 | granted: boolean; |
| 183 | revoked: boolean; |
| 184 | tag?: string; |
| 185 | }; |
| 186 | // A failure surfaces its typed tag in the response for diagnosis. |
| 187 | expect(response.tag).toBeUndefined(); |
| 188 | expect(response.granted).toBe(true); |
| 189 | expect(response.revoked).toBe(true); |
| 190 | }), |
| 191 | ); |
| 192 | }); |
| 193 | |
| 194 | describe("StartFlow", () => { |
| 195 | // An ACTIVE flow bills for transport by the hour, so the start/stop |
| 196 | // roundtrip only runs when explicitly enabled. |
| 197 | test.provider.skipIf(!process.env.AWS_TEST_MEDIACONNECT)( |
| 198 | "start/stop roundtrip returns the flow to rest", |
| 199 | () => |
| 200 | Effect.gen(function* () { |
| 201 | const response = (yield* postJson("/start-stop")) as { |
| 202 | startStatus?: string; |
| 203 | stopStatus?: string; |
| 204 | }; |
| 205 | expect(["STARTING", "ACTIVE"]).toContain(response.startStatus); |
| 206 | expect(["STOPPING", "STANDBY"]).toContain(response.stopStatus); |
| 207 | }), |
| 208 | { timeout: 150_000 }, |
| 209 | ); |
| 210 | }); |
| 211 | |
| 212 | describe("consumeFlowEvents", { tags: ["provider:aws:eventbridge"] }, () => { |
| 213 | test.provider("the deploy created an EventBridge rule targeting the function", () => |
| 214 | Effect.gen(function* () { |
| 215 | // Out-of-band via distilled: the fixture's consumeFlowEvents |
| 216 | // must have materialized as a rule on the default bus with the |
| 217 | // Lambda as target. |
| 218 | const { RuleNames } = yield* eventbridge.listRuleNamesByTarget({ |
| 219 | TargetArn: functionArn, |
| 220 | }); |
| 221 | expect((RuleNames ?? []).length).toBeGreaterThanOrEqual(1); |
| 222 | }), |
| 223 | ); |
| 224 | }); |
| 225 | }, |
| 226 | ); |