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 Output from "alchemy/Output"; |
| 7 | import * as Data from "effect/Data"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import * as HttpClient from "effect/http/HttpClient"; |
| 10 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 11 | import * as Schedule from "effect/Schedule"; |
| 12 | import MediaConvertTestFunctionLive, { MediaConvertTestFunction } from "./handler"; |
| 13 | |
| 14 | const testOptions = { providers: AWS.providers() }; |
| 15 | const { test, beforeAll, afterAll } = Test.make(testOptions); |
| 16 | const sharedStack = Core.scratchStack(testOptions, "MediaConvertBindings"); |
| 17 | |
| 18 | // A well-formed-but-nonexistent job id (MediaConvert ids look like |
| 19 | // `1582077664817-n4h2ck`) — drives the typed NotFoundException path. |
| 20 | const NONEXISTENT_JOB_ID = "0000000000000-aaaaaa"; |
| 21 | |
| 22 | let baseUrl: 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 (cold |
| 30 | // re-init, IAM propagation on the freshly attached policy that the handler's |
| 31 | // `Effect.orDie` surfaces as a 500). Retry only 5xx; a genuine 4xx surfaces |
| 32 | // 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("1 second"), Schedule.recurs(8)]), |
| 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, body: object) => |
| 54 | send( |
| 55 | HttpClientRequest.post(`${baseUrl}${path}`).pipe(HttpClientRequest.bodyJsonUnsafe(body)), |
| 56 | ).pipe(Effect.flatMap((r) => r.json)); |
| 57 | |
| 58 | describe.sequential( |
| 59 | "MediaConvert Bindings", |
| 60 | { tags: ["provider:aws", "provider:aws:lambda", "provider:aws:mediaconvert", "live"] }, |
| 61 | () => { |
| 62 | beforeAll( |
| 63 | Effect.gen(function* () { |
| 64 | yield* Effect.logInfo("MediaConvert test setup: destroying previous resources"); |
| 65 | yield* sharedStack.destroy(); |
| 66 | |
| 67 | yield* Effect.logInfo("MediaConvert test setup: deploying fixture"); |
| 68 | const { functionUrl } = yield* sharedStack.deploy( |
| 69 | Effect.gen(function* () { |
| 70 | return yield* MediaConvertTestFunction; |
| 71 | }).pipe(Effect.provide(MediaConvertTestFunctionLive)), |
| 72 | ); |
| 73 | |
| 74 | expect(functionUrl).toBeTruthy(); |
| 75 | baseUrl = functionUrl!.replace(/\/+$/, ""); |
| 76 | |
| 77 | // Readiness probe — fresh function URLs take seconds (sometimes over a |
| 78 | // minute) to serve 200s. |
| 79 | yield* HttpClient.get(`${baseUrl}/jobs`).pipe( |
| 80 | Effect.flatMap((response) => |
| 81 | response.status === 200 |
| 82 | ? Effect.succeed(response) |
| 83 | : Effect.fail(new Error(`Function not ready: ${response.status}`)), |
| 84 | ), |
| 85 | Effect.retry({ |
| 86 | schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]), |
| 87 | }), |
| 88 | ); |
| 89 | }), |
| 90 | { timeout: 300_000 }, |
| 91 | ); |
| 92 | afterAll(sharedStack.destroy(), { timeout: 300_000 }); |
| 93 | |
| 94 | describe("ListJobs", () => { |
| 95 | test.provider( |
| 96 | "lists recent jobs from the runtime", |
| 97 | () => |
| 98 | Effect.gen(function* () { |
| 99 | const body = (yield* getJson("/jobs")) as { count: number }; |
| 100 | expect(typeof body.count).toBe("number"); |
| 101 | }), |
| 102 | { timeout: 60_000 }, |
| 103 | ); |
| 104 | }); |
| 105 | |
| 106 | describe("SearchJobs", () => { |
| 107 | test.provider( |
| 108 | "searches completed jobs from the runtime", |
| 109 | () => |
| 110 | Effect.gen(function* () { |
| 111 | const body = (yield* getJson("/search")) as { count: number }; |
| 112 | expect(typeof body.count).toBe("number"); |
| 113 | }), |
| 114 | { timeout: 60_000 }, |
| 115 | ); |
| 116 | }); |
| 117 | |
| 118 | describe("GetJob", () => { |
| 119 | test.provider( |
| 120 | "returns the typed NotFoundException for a missing job", |
| 121 | () => |
| 122 | Effect.gen(function* () { |
| 123 | const body = (yield* getJson(`/job?id=${NONEXISTENT_JOB_ID}`)) as { |
| 124 | status?: string; |
| 125 | error?: string; |
| 126 | }; |
| 127 | // NotFound (never Forbidden) proves the mediaconvert:GetJob grant |
| 128 | // reached the API and the typed tag surfaced in the runtime. |
| 129 | expect(body.error).toBe("NotFoundException"); |
| 130 | }), |
| 131 | { timeout: 60_000 }, |
| 132 | ); |
| 133 | }); |
| 134 | |
| 135 | describe("CancelJob", () => { |
| 136 | test.provider( |
| 137 | "returns the typed NotFoundException for a missing job", |
| 138 | () => |
| 139 | Effect.gen(function* () { |
| 140 | const body = (yield* postJson("/cancel", { id: NONEXISTENT_JOB_ID })) as { |
| 141 | cancelled: boolean; |
| 142 | error?: string; |
| 143 | }; |
| 144 | expect(body.cancelled).toBe(false); |
| 145 | expect(body.error).toBe("NotFoundException"); |
| 146 | }), |
| 147 | { timeout: 60_000 }, |
| 148 | ); |
| 149 | }); |
| 150 | |
| 151 | describe("Probe", () => { |
| 152 | test.provider( |
| 153 | "probe of a missing input fails with a typed NotFoundException", |
| 154 | () => |
| 155 | Effect.gen(function* () { |
| 156 | const body = (yield* postJson("/probe", { |
| 157 | fileUrl: "s3://alchemy-nonexistent/in.mp4", |
| 158 | })) as { probed: boolean; error?: string }; |
| 159 | expect(body.probed).toBe(false); |
| 160 | // NotFound (never Forbidden/AccessDenied) proves the |
| 161 | // mediaconvert:Probe grant reached the API; the input simply does |
| 162 | // not exist. |
| 163 | expect(body.error).toBe("NotFoundException"); |
| 164 | }), |
| 165 | { timeout: 60_000 }, |
| 166 | ); |
| 167 | }); |
| 168 | |
| 169 | describe("CreateJob", () => { |
| 170 | test.provider( |
| 171 | "submit with an unassumable role fails with a typed tag (never Forbidden)", |
| 172 | () => |
| 173 | Effect.gen(function* () { |
| 174 | // An in-account-format role that does not exist: the request passes |
| 175 | // IAM authorization (mediaconvert:CreateJob granted + iam:PassRole |
| 176 | // conditioned to the service) and fails service-side validation with |
| 177 | // a typed BadRequestException — no billable transcode is started. |
| 178 | const body = (yield* postJson("/submit", { |
| 179 | role: "arn:aws:iam::000000000000:role/alchemy-does-not-exist", |
| 180 | })) as { jobId?: string; error?: string }; |
| 181 | expect(body.jobId).toBeUndefined(); |
| 182 | expect(["BadRequestException", "AccessDeniedException"]).toContain(body.error); |
| 183 | }), |
| 184 | { timeout: 60_000 }, |
| 185 | ); |
| 186 | }); |
| 187 | |
| 188 | describe("StartJobsQuery + GetJobsQueryResults", () => { |
| 189 | test.provider( |
| 190 | "starts an async jobs query and fetches its results", |
| 191 | () => |
| 192 | Effect.gen(function* () { |
| 193 | const started = (yield* postJson("/jobsQuery", {})) as { |
| 194 | queryId?: string; |
| 195 | error?: string; |
| 196 | }; |
| 197 | expect(started.error).toBeUndefined(); |
| 198 | expect(started.queryId).toBeTruthy(); |
| 199 | |
| 200 | const result = yield* getJson(`/jobsQueryResults?id=${started.queryId}`).pipe( |
| 201 | Effect.map((r) => r as { status?: string; count: number; error?: string }), |
| 202 | Effect.repeat({ |
| 203 | schedule: Schedule.spaced("3 seconds"), |
| 204 | until: (r): boolean => |
| 205 | r.status === "COMPLETE" || r.status === "ERROR" || r.error !== undefined, |
| 206 | times: 20, |
| 207 | }), |
| 208 | ); |
| 209 | expect(result.error).toBeUndefined(); |
| 210 | expect(result.status).toBe("COMPLETE"); |
| 211 | expect(typeof result.count).toBe("number"); |
| 212 | }), |
| 213 | { timeout: 120_000 }, |
| 214 | ); |
| 215 | }); |
| 216 | |
| 217 | describe("consumeJobEvents", { tags: ["provider:aws:eventbridge"] }, () => { |
| 218 | test.provider( |
| 219 | "created the EventBridge rule for MediaConvert job state changes", |
| 220 | () => |
| 221 | Effect.gen(function* () { |
| 222 | const ref = yield* AWS.EventBridge.Rule.ref("MediaConvertJobEvents", { |
| 223 | stack: sharedStack.name, |
| 224 | stage: sharedStack.stage, |
| 225 | }); |
| 226 | const { Name, EventBusName } = yield* Effect.all({ |
| 227 | Name: Output.evaluate(ref.ruleName, {}), |
| 228 | EventBusName: Output.evaluate(ref.eventBusName, {}), |
| 229 | }).pipe(Effect.provide(sharedStack.state)); |
| 230 | const rule = yield* eventbridge.describeRule({ Name, EventBusName }); |
| 231 | expect(rule?.EventPattern).toContain("aws.mediaconvert"); |
| 232 | expect(rule?.EventPattern).toContain("MediaConvert Job State Change"); |
| 233 | }), |
| 234 | { timeout: 60_000 }, |
| 235 | ); |
| 236 | }); |
| 237 | }, |
| 238 | ); |