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 { Job, JobTemplate, Preset, Queue } from "@alchemy.run/aws/MediaConvert"; |
| 3 | import { expect } from "@alchemy.run/test"; |
| 4 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 5 | import * as mediaconvert from "@distilled.cloud/aws/mediaconvert"; |
| 6 | import * as Data from "effect/Data"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 9 | |
| 10 | const { test } = Test.make({ providers: AWS.providers() }); |
| 11 | |
| 12 | // Deterministic, run-stable physical names (never Date.now()). |
| 13 | const QUEUE_NAME = "alchemy-test-mc-queue"; |
| 14 | const PRESET_NAME = "alchemy-test-mc-preset"; |
| 15 | const TEMPLATE_NAME = "alchemy-test-mc-jobtemplate"; |
| 16 | |
| 17 | // A minimal, valid H.264 / AAC MP4 output preset. |
| 18 | const PRESET_SETTINGS: mediaconvert.PresetSettings = { |
| 19 | ContainerSettings: { Container: "MP4", Mp4Settings: {} }, |
| 20 | VideoDescription: { |
| 21 | Width: 1280, |
| 22 | Height: 720, |
| 23 | CodecSettings: { |
| 24 | Codec: "H_264", |
| 25 | H264Settings: { |
| 26 | RateControlMode: "QVBR", |
| 27 | MaxBitrate: 3000000, |
| 28 | SceneChangeDetect: "TRANSITION_DETECTION", |
| 29 | }, |
| 30 | }, |
| 31 | }, |
| 32 | AudioDescriptions: [ |
| 33 | { |
| 34 | CodecSettings: { |
| 35 | Codec: "AAC", |
| 36 | AacSettings: { Bitrate: 96000, CodingMode: "CODING_MODE_2_0", SampleRate: 48000 }, |
| 37 | }, |
| 38 | }, |
| 39 | ], |
| 40 | }; |
| 41 | |
| 42 | // A minimal, valid single-output file-group job template. |
| 43 | const TEMPLATE_SETTINGS: mediaconvert.JobTemplateSettings = { |
| 44 | Inputs: [{ TimecodeSource: "ZEROBASED", VideoSelector: {}, AudioSelectors: {} }], |
| 45 | OutputGroups: [ |
| 46 | { |
| 47 | Name: "File Group", |
| 48 | OutputGroupSettings: { Type: "FILE_GROUP_SETTINGS", FileGroupSettings: {} }, |
| 49 | Outputs: [ |
| 50 | { |
| 51 | ContainerSettings: { Container: "MP4", Mp4Settings: {} }, |
| 52 | VideoDescription: { |
| 53 | CodecSettings: { |
| 54 | Codec: "H_264", |
| 55 | H264Settings: { |
| 56 | RateControlMode: "QVBR", |
| 57 | MaxBitrate: 3000000, |
| 58 | SceneChangeDetect: "TRANSITION_DETECTION", |
| 59 | }, |
| 60 | }, |
| 61 | }, |
| 62 | }, |
| 63 | ], |
| 64 | }, |
| 65 | ], |
| 66 | }; |
| 67 | |
| 68 | class StillExists extends Data.TaggedError("StillExists")<{ readonly name: string }> {} |
| 69 | |
| 70 | // --------------------------------------------------------------------------- |
| 71 | // Ungated typed-error probes — prove the distilled error union carries the |
| 72 | // not-found tag the read/delete paths depend on, at near-zero cost. |
| 73 | // --------------------------------------------------------------------------- |
| 74 | |
| 75 | test.provider( |
| 76 | "getQueue/getPreset/getJobTemplate on a missing name fail with NotFoundException", |
| 77 | () => |
| 78 | Effect.gen(function* () { |
| 79 | const q = yield* Effect.flip(mediaconvert.getQueue({ Name: "alchemy-does-not-exist-000" })); |
| 80 | expect(q._tag).toBe("NotFoundException"); |
| 81 | const p = yield* Effect.flip(mediaconvert.getPreset({ Name: "alchemy-does-not-exist-000" })); |
| 82 | expect(p._tag).toBe("NotFoundException"); |
| 83 | const t = yield* Effect.flip( |
| 84 | mediaconvert.getJobTemplate({ Name: "alchemy-does-not-exist-000" }), |
| 85 | ); |
| 86 | expect(t._tag).toBe("NotFoundException"); |
| 87 | }), |
| 88 | { tags: ["provider:aws", "provider:aws:mediaconvert", "live"] }, |
| 89 | ); |
| 90 | |
| 91 | test.provider( |
| 92 | "createJob with an invalid role is rejected with a typed BadRequestException", |
| 93 | () => |
| 94 | Effect.gen(function* () { |
| 95 | // Proves Job's create path has a typed error union (no untyped catch-all) |
| 96 | // without submitting a billable transcode. |
| 97 | const error = yield* Effect.flip( |
| 98 | mediaconvert.createJob({ |
| 99 | Role: "arn:aws:iam::000000000000:role/does-not-exist", |
| 100 | Settings: { |
| 101 | Inputs: [{ FileInput: "s3://alchemy-nonexistent/in.mp4" }], |
| 102 | OutputGroups: TEMPLATE_SETTINGS.OutputGroups, |
| 103 | }, |
| 104 | }), |
| 105 | ); |
| 106 | // A bad role surfaces as a typed tag (AccessDenied/BadRequest/Forbidden), |
| 107 | // never the untyped catch-all — that is the point of the probe. |
| 108 | expect(error._tag).not.toBe("UnknownAwsError"); |
| 109 | expect(["AccessDeniedException", "BadRequestException", "ForbiddenException"]).toContain( |
| 110 | error._tag, |
| 111 | ); |
| 112 | }), |
| 113 | { tags: ["provider:aws", "provider:aws:mediaconvert", "live"] }, |
| 114 | ); |
| 115 | |
| 116 | // --------------------------------------------------------------------------- |
| 117 | // Queue lifecycle — on-demand queues are free and provision instantly. |
| 118 | // --------------------------------------------------------------------------- |
| 119 | |
| 120 | test.provider( |
| 121 | "Queue: create, update, and destroy an on-demand queue", |
| 122 | (stack) => |
| 123 | Effect.gen(function* () { |
| 124 | yield* stack.destroy(); |
| 125 | |
| 126 | const created = yield* stack.deploy( |
| 127 | Effect.gen(function* () { |
| 128 | return yield* Queue("Q", { |
| 129 | queueName: QUEUE_NAME, |
| 130 | description: "alchemy test queue", |
| 131 | tags: { Environment: "test" }, |
| 132 | }); |
| 133 | }), |
| 134 | ); |
| 135 | expect(created.queueName).toBe(QUEUE_NAME); |
| 136 | expect(created.queueArn).toContain(":queues/"); |
| 137 | expect(created.pricingPlan).toBe("ON_DEMAND"); |
| 138 | expect(created.status).toBe("ACTIVE"); |
| 139 | |
| 140 | // Out-of-band verification. MediaConvert returns tags only via |
| 141 | // listTagsForResource, not in the Get response body. |
| 142 | const observed = yield* mediaconvert.getQueue({ Name: QUEUE_NAME }); |
| 143 | expect(observed.Queue?.Description).toBe("alchemy test queue"); |
| 144 | const queueTags = yield* mediaconvert.listTagsForResource({ Arn: created.queueArn }); |
| 145 | expect(queueTags.ResourceTags?.Tags?.["alchemy::id"]).toBe("Q"); |
| 146 | |
| 147 | // Update: change description + pause the queue. |
| 148 | const updated = yield* stack.deploy( |
| 149 | Effect.gen(function* () { |
| 150 | return yield* Queue("Q", { |
| 151 | queueName: QUEUE_NAME, |
| 152 | description: "alchemy test queue v2", |
| 153 | status: "PAUSED", |
| 154 | tags: { Environment: "test", Extra: "yes" }, |
| 155 | }); |
| 156 | }), |
| 157 | ); |
| 158 | expect(updated.status).toBe("PAUSED"); |
| 159 | |
| 160 | const observed2 = yield* mediaconvert.getQueue({ Name: QUEUE_NAME }); |
| 161 | expect(observed2.Queue?.Description).toBe("alchemy test queue v2"); |
| 162 | expect(observed2.Queue?.Status).toBe("PAUSED"); |
| 163 | const queueTags2 = yield* mediaconvert.listTagsForResource({ Arn: created.queueArn }); |
| 164 | expect(queueTags2.ResourceTags?.Tags?.["Extra"]).toBe("yes"); |
| 165 | |
| 166 | yield* stack.destroy(); |
| 167 | yield* assertQueueDeleted(QUEUE_NAME); |
| 168 | }), |
| 169 | { tags: ["provider:aws", "provider:aws:mediaconvert", "live"], timeout: 120_000 }, |
| 170 | ); |
| 171 | |
| 172 | // --------------------------------------------------------------------------- |
| 173 | // Preset lifecycle. |
| 174 | // --------------------------------------------------------------------------- |
| 175 | |
| 176 | test.provider( |
| 177 | "Preset: create, update, and destroy an output preset", |
| 178 | (stack) => |
| 179 | Effect.gen(function* () { |
| 180 | yield* stack.destroy(); |
| 181 | |
| 182 | const created = yield* stack.deploy( |
| 183 | Effect.gen(function* () { |
| 184 | return yield* Preset("P", { |
| 185 | presetName: PRESET_NAME, |
| 186 | description: "alchemy test preset", |
| 187 | settings: PRESET_SETTINGS, |
| 188 | tags: { Environment: "test" }, |
| 189 | }); |
| 190 | }), |
| 191 | ); |
| 192 | expect(created.presetName).toBe(PRESET_NAME); |
| 193 | expect(created.presetArn).toContain(":presets/"); |
| 194 | expect(created.type).toBe("CUSTOM"); |
| 195 | |
| 196 | const observed = yield* mediaconvert.getPreset({ Name: PRESET_NAME }); |
| 197 | expect(observed.Preset?.Description).toBe("alchemy test preset"); |
| 198 | const presetTags = yield* mediaconvert.listTagsForResource({ Arn: created.presetArn }); |
| 199 | expect(presetTags.ResourceTags?.Tags?.["alchemy::id"]).toBe("P"); |
| 200 | |
| 201 | const updated = yield* stack.deploy( |
| 202 | Effect.gen(function* () { |
| 203 | return yield* Preset("P", { |
| 204 | presetName: PRESET_NAME, |
| 205 | description: "alchemy test preset v2", |
| 206 | category: "alchemy", |
| 207 | settings: PRESET_SETTINGS, |
| 208 | tags: { Environment: "test" }, |
| 209 | }); |
| 210 | }), |
| 211 | ); |
| 212 | expect(updated.category).toBe("alchemy"); |
| 213 | |
| 214 | const observed2 = yield* mediaconvert.getPreset({ Name: PRESET_NAME }); |
| 215 | expect(observed2.Preset?.Description).toBe("alchemy test preset v2"); |
| 216 | |
| 217 | yield* stack.destroy(); |
| 218 | yield* assertPresetDeleted(PRESET_NAME); |
| 219 | }), |
| 220 | { tags: ["provider:aws", "provider:aws:mediaconvert", "live"], timeout: 120_000 }, |
| 221 | ); |
| 222 | |
| 223 | // --------------------------------------------------------------------------- |
| 224 | // JobTemplate lifecycle. |
| 225 | // --------------------------------------------------------------------------- |
| 226 | |
| 227 | test.provider( |
| 228 | "JobTemplate: create, update, and destroy a job template", |
| 229 | (stack) => |
| 230 | Effect.gen(function* () { |
| 231 | yield* stack.destroy(); |
| 232 | |
| 233 | const created = yield* stack.deploy( |
| 234 | Effect.gen(function* () { |
| 235 | return yield* JobTemplate("T", { |
| 236 | jobTemplateName: TEMPLATE_NAME, |
| 237 | description: "alchemy test template", |
| 238 | settings: TEMPLATE_SETTINGS, |
| 239 | tags: { Environment: "test" }, |
| 240 | }); |
| 241 | }), |
| 242 | ); |
| 243 | expect(created.jobTemplateName).toBe(TEMPLATE_NAME); |
| 244 | expect(created.jobTemplateArn).toContain(":jobTemplates/"); |
| 245 | expect(created.type).toBe("CUSTOM"); |
| 246 | |
| 247 | const observed = yield* mediaconvert.getJobTemplate({ Name: TEMPLATE_NAME }); |
| 248 | expect(observed.JobTemplate?.Description).toBe("alchemy test template"); |
| 249 | const templateTags = yield* mediaconvert.listTagsForResource({ Arn: created.jobTemplateArn }); |
| 250 | expect(templateTags.ResourceTags?.Tags?.["alchemy::id"]).toBe("T"); |
| 251 | |
| 252 | const updated = yield* stack.deploy( |
| 253 | Effect.gen(function* () { |
| 254 | return yield* JobTemplate("T", { |
| 255 | jobTemplateName: TEMPLATE_NAME, |
| 256 | description: "alchemy test template v2", |
| 257 | priority: 10, |
| 258 | settings: TEMPLATE_SETTINGS, |
| 259 | tags: { Environment: "test" }, |
| 260 | }); |
| 261 | }), |
| 262 | ); |
| 263 | expect(updated.jobTemplateName).toBe(TEMPLATE_NAME); |
| 264 | |
| 265 | const observed2 = yield* mediaconvert.getJobTemplate({ Name: TEMPLATE_NAME }); |
| 266 | expect(observed2.JobTemplate?.Description).toBe("alchemy test template v2"); |
| 267 | expect(observed2.JobTemplate?.Priority).toBe(10); |
| 268 | |
| 269 | yield* stack.destroy(); |
| 270 | yield* assertJobTemplateDeleted(TEMPLATE_NAME); |
| 271 | }), |
| 272 | { tags: ["provider:aws", "provider:aws:mediaconvert", "live"], timeout: 120_000 }, |
| 273 | ); |
| 274 | |
| 275 | // --------------------------------------------------------------------------- |
| 276 | // Job live lifecycle — slow/billable; needs an IAM role MediaConvert can |
| 277 | // assume plus S3 access. Skipped under `--fast`. Requires: |
| 278 | // MEDIACONVERT_ROLE_ARN=<role arn> |
| 279 | // MEDIACONVERT_TEST_INPUT=s3://bucket/input.mp4 |
| 280 | // MEDIACONVERT_TEST_OUTPUT=s3://bucket/out/ |
| 281 | // The job intentionally runs to completion or errors on its own; the test |
| 282 | // only asserts submission + a terminal-or-progressing status, then cancels. |
| 283 | // --------------------------------------------------------------------------- |
| 284 | |
| 285 | const runJob = !process.env.FAST && !!process.env.MEDIACONVERT_ROLE_ARN; |
| 286 | |
| 287 | test.provider.skipIf(!runJob)( |
| 288 | "Job: submit a transcode and cancel it", |
| 289 | (stack) => |
| 290 | Effect.gen(function* () { |
| 291 | yield* stack.destroy(); |
| 292 | |
| 293 | const roleArn = process.env.MEDIACONVERT_ROLE_ARN!; |
| 294 | const input = process.env.MEDIACONVERT_TEST_INPUT ?? "s3://alchemy-nonexistent/in.mp4"; |
| 295 | const output = process.env.MEDIACONVERT_TEST_OUTPUT ?? "s3://alchemy-nonexistent/out/"; |
| 296 | |
| 297 | const created = yield* stack.deploy( |
| 298 | Effect.gen(function* () { |
| 299 | return yield* Job("J", { |
| 300 | role: roleArn, |
| 301 | settings: { |
| 302 | Inputs: [ |
| 303 | { |
| 304 | FileInput: input, |
| 305 | TimecodeSource: "ZEROBASED", |
| 306 | VideoSelector: {}, |
| 307 | AudioSelectors: {}, |
| 308 | }, |
| 309 | ], |
| 310 | OutputGroups: [ |
| 311 | { |
| 312 | Name: "File Group", |
| 313 | OutputGroupSettings: { |
| 314 | Type: "FILE_GROUP_SETTINGS", |
| 315 | FileGroupSettings: { Destination: output }, |
| 316 | }, |
| 317 | Outputs: TEMPLATE_SETTINGS.OutputGroups![0].Outputs, |
| 318 | }, |
| 319 | ], |
| 320 | }, |
| 321 | }); |
| 322 | }), |
| 323 | ); |
| 324 | expect(created.jobId).toBeTruthy(); |
| 325 | expect(created.jobArn).toContain(":jobs/"); |
| 326 | |
| 327 | const observed = yield* mediaconvert.getJob({ Id: created.jobId }); |
| 328 | expect(observed.Job).toBeDefined(); |
| 329 | expect( |
| 330 | ["SUBMITTED", "PROGRESSING", "COMPLETE", "ERROR"].includes(observed.Job!.Status!), |
| 331 | ).toBe(true); |
| 332 | |
| 333 | // destroy() cancels the job if still in flight. |
| 334 | yield* stack.destroy(); |
| 335 | }), |
| 336 | { tags: ["provider:aws", "provider:aws:mediaconvert", "live"], timeout: 180_000 }, |
| 337 | ); |
| 338 | |
| 339 | // --------------------------------------------------------------------------- |
| 340 | // Deletion assertions — typed wait-until-gone. |
| 341 | // --------------------------------------------------------------------------- |
| 342 | |
| 343 | const assertQueueDeleted = (name: string) => |
| 344 | mediaconvert.getQueue({ Name: name }).pipe( |
| 345 | Effect.flatMap(() => Effect.fail(new StillExists({ name }))), |
| 346 | Effect.catchTag("NotFoundException", () => Effect.void), |
| 347 | Effect.retry({ |
| 348 | while: (e) => e._tag === "StillExists", |
| 349 | schedule: Schedule.max([Schedule.spaced("2 seconds"), Schedule.recurs(10)]), |
| 350 | }), |
| 351 | ); |
| 352 | |
| 353 | const assertPresetDeleted = (name: string) => |
| 354 | mediaconvert.getPreset({ Name: name }).pipe( |
| 355 | Effect.flatMap(() => Effect.fail(new StillExists({ name }))), |
| 356 | Effect.catchTag("NotFoundException", () => Effect.void), |
| 357 | Effect.retry({ |
| 358 | while: (e) => e._tag === "StillExists", |
| 359 | schedule: Schedule.max([Schedule.spaced("2 seconds"), Schedule.recurs(10)]), |
| 360 | }), |
| 361 | ); |
| 362 | |
| 363 | const assertJobTemplateDeleted = (name: string) => |
| 364 | mediaconvert.getJobTemplate({ Name: name }).pipe( |
| 365 | Effect.flatMap(() => Effect.fail(new StillExists({ name }))), |
| 366 | Effect.catchTag("NotFoundException", () => Effect.void), |
| 367 | Effect.retry({ |
| 368 | while: (e) => e._tag === "StillExists", |
| 369 | schedule: Schedule.max([Schedule.spaced("2 seconds"), Schedule.recurs(10)]), |
| 370 | }), |
| 371 | ); |