The best way to control Claude Code, Codex and more from any computer or phone

main
ts 464 lines 16 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { Role } from "@alchemy.run/aws/IAM/Role";
3 import { Channel, Input, InputSecurityGroup } from "@alchemy.run/aws/MediaLive";
4 import { expect } from "@alchemy.run/test";
5 import * as Test from "@alchemy.run/test/Alchemy";
6 import * as medialive from "@distilled.cloud/aws/medialive";
7 import * as Data from "effect/Data";
8 import * as Effect from "effect/Effect";
9 import * as Schedule from "effect/Schedule";
10
11 const { test } = Test.make({ providers: AWS.providers() });
12
13 // Deterministic, run-stable physical names (never Date.now()).
14 const PULL_INPUT_NAME = "alchemy-test-ml-pull-input";
15 const PUSH_INPUT_NAME = "alchemy-test-ml-push-input";
16 const CHANNEL_NAME = "alchemy-test-ml-channel";
17
18 class StillExists extends Data.TaggedError("StillExists")<{
19 readonly id: string;
20 }> {}
21
22 // ---------------------------------------------------------------------------
23 // Ungated typed-error probes — prove the distilled error union carries the
24 // not-found tag the read/delete paths depend on, at near-zero cost.
25 // ---------------------------------------------------------------------------
26
27 test.provider(
28 "describeChannel/describeInput/describeInputSecurityGroup on a bogus id fail with NotFoundException",
29 () =>
30 Effect.gen(function* () {
31 const c = yield* Effect.flip(medialive.describeChannel({ ChannelId: "9999999" }));
32 expect(c._tag).toBe("NotFoundException");
33 const i = yield* Effect.flip(medialive.describeInput({ InputId: "9999999" }));
34 expect(i._tag).toBe("NotFoundException");
35 const g = yield* Effect.flip(
36 medialive.describeInputSecurityGroup({
37 InputSecurityGroupId: "9999999",
38 }),
39 );
40 expect(g._tag).toBe("NotFoundException");
41 }),
42 { tags: ["provider:aws", "provider:aws:medialive", "live"] },
43 );
44
45 // ---------------------------------------------------------------------------
46 // InputSecurityGroup lifecycle — free and fast.
47 // ---------------------------------------------------------------------------
48
49 test.provider(
50 "InputSecurityGroup: create, update whitelist rules, and destroy",
51 (stack) =>
52 Effect.gen(function* () {
53 yield* stack.destroy();
54
55 const created = yield* stack.deploy(
56 Effect.gen(function* () {
57 return yield* InputSecurityGroup("Allowlist", {
58 whitelistRules: ["10.0.0.0/16"],
59 tags: { Environment: "test" },
60 });
61 }),
62 );
63 expect(created.inputSecurityGroupId).toBeTruthy();
64 expect(created.inputSecurityGroupArn).toContain(":inputSecurityGroup:");
65 expect(created.whitelistRules).toEqual(["10.0.0.0/16"]);
66
67 // Out-of-band verification via distilled, including ownership tags.
68 const observed = yield* medialive.describeInputSecurityGroup({
69 InputSecurityGroupId: created.inputSecurityGroupId,
70 });
71 expect(observed.WhitelistRules?.map((r) => r.Cidr)).toEqual(["10.0.0.0/16"]);
72 expect(observed.Tags?.["alchemy::id"]).toBe("Allowlist");
73 expect(observed.Tags?.["Environment"]).toBe("test");
74
75 // Update: change the whitelist + tags in place (same id).
76 const updated = yield* stack.deploy(
77 Effect.gen(function* () {
78 return yield* InputSecurityGroup("Allowlist", {
79 whitelistRules: ["10.1.0.0/16", "192.168.0.0/24"],
80 tags: { Environment: "test", Extra: "yes" },
81 });
82 }),
83 );
84 expect(updated.inputSecurityGroupId).toBe(created.inputSecurityGroupId);
85 expect([...updated.whitelistRules].sort()).toEqual(["10.1.0.0/16", "192.168.0.0/24"]);
86
87 const observed2 = yield* medialive.describeInputSecurityGroup({
88 InputSecurityGroupId: created.inputSecurityGroupId,
89 });
90 expect(observed2.WhitelistRules?.map((r) => r.Cidr)?.sort()).toEqual([
91 "10.1.0.0/16",
92 "192.168.0.0/24",
93 ]);
94 expect(observed2.Tags?.["Extra"]).toBe("yes");
95
96 yield* stack.destroy();
97 yield* assertInputSecurityGroupDeleted(created.inputSecurityGroupId);
98 }),
99 {
100 tags: ["provider:aws", "provider:aws:medialive", "live"],
101 timeout: 120_000,
102 },
103 );
104
105 // ---------------------------------------------------------------------------
106 // Input lifecycle — a URL_PULL input needs no security group; covers update
107 // (sources) plus replacement (type is immutable).
108 // ---------------------------------------------------------------------------
109
110 test.provider(
111 "Input: create URL_PULL, update sources, replace on type change, destroy",
112 (stack) =>
113 Effect.gen(function* () {
114 yield* stack.destroy();
115
116 const created = yield* stack.deploy(
117 Effect.gen(function* () {
118 return yield* Input("Pull", {
119 name: PULL_INPUT_NAME,
120 type: "URL_PULL",
121 sources: [{ Url: "https://example.com/stream/index.m3u8" }],
122 tags: { Environment: "test" },
123 });
124 }),
125 );
126 expect(created.inputId).toBeTruthy();
127 expect(created.inputArn).toContain(":input:");
128 expect(created.inputName).toBe(PULL_INPUT_NAME);
129 expect(created.type).toBe("URL_PULL");
130 expect(created.state === "DETACHED" || created.state === "ATTACHED").toBe(true);
131
132 const observed = yield* medialive.describeInput({
133 InputId: created.inputId,
134 });
135 expect(observed.Sources?.map((s) => s.Url)).toEqual([
136 "https://example.com/stream/index.m3u8",
137 ]);
138 expect(observed.Tags?.["alchemy::id"]).toBe("Pull");
139
140 // Update: swap the pull URL in place (same id).
141 const updated = yield* stack.deploy(
142 Effect.gen(function* () {
143 return yield* Input("Pull", {
144 name: PULL_INPUT_NAME,
145 type: "URL_PULL",
146 sources: [{ Url: "https://example.com/stream/other.m3u8" }],
147 tags: { Environment: "test" },
148 });
149 }),
150 );
151 expect(updated.inputId).toBe(created.inputId);
152
153 const observed2 = yield* medialive.describeInput({
154 InputId: created.inputId,
155 });
156 expect(observed2.Sources?.map((s) => s.Url)).toEqual([
157 "https://example.com/stream/other.m3u8",
158 ]);
159
160 // Replace: the input type is immutable, so switching to MP4_FILE must
161 // produce a NEW input id.
162 const replaced = yield* stack.deploy(
163 Effect.gen(function* () {
164 return yield* Input("Pull", {
165 name: PULL_INPUT_NAME,
166 type: "MP4_FILE",
167 sources: [{ Url: "s3ssl://alchemy-nonexistent/in.mp4" }],
168 tags: { Environment: "test" },
169 });
170 }),
171 );
172 expect(replaced.type).toBe("MP4_FILE");
173 expect(replaced.inputId).not.toBe(created.inputId);
174 yield* assertInputDeleted(created.inputId);
175
176 yield* stack.destroy();
177 yield* assertInputDeleted(replaced.inputId);
178 }),
179 {
180 tags: ["provider:aws", "provider:aws:medialive", "live"],
181 timeout: 180_000,
182 },
183 );
184
185 // ---------------------------------------------------------------------------
186 // Composition: an RTMP_PUSH input gated by an input security group. Also
187 // exercises the delete ordering (the group is IN_USE until the input is
188 // fully deleted).
189 // ---------------------------------------------------------------------------
190
191 test.provider(
192 "Input + InputSecurityGroup: RTMP_PUSH input behind an allowlist",
193 (stack) =>
194 Effect.gen(function* () {
195 yield* stack.destroy();
196
197 const { isg, input } = yield* stack.deploy(
198 Effect.gen(function* () {
199 const isg = yield* InputSecurityGroup("PushAllowlist", {
200 whitelistRules: ["0.0.0.0/0"],
201 });
202 const input = yield* Input("Push", {
203 name: PUSH_INPUT_NAME,
204 type: "RTMP_PUSH",
205 inputSecurityGroups: [isg.inputSecurityGroupId],
206 destinations: [{ StreamName: "live/primary" }, { StreamName: "live/secondary" }],
207 });
208 return { isg, input };
209 }),
210 );
211 expect(input.securityGroups).toEqual([isg.inputSecurityGroupId]);
212 // A STANDARD-class push input exposes two ingest endpoints.
213 expect(input.destinations.length).toBe(2);
214 expect(input.destinations[0].Url).toContain("rtmp");
215
216 const observedIsg = yield* medialive.describeInputSecurityGroup({
217 InputSecurityGroupId: isg.inputSecurityGroupId,
218 });
219 expect(observedIsg.Inputs).toContain(input.inputId);
220
221 yield* stack.destroy();
222 yield* assertInputDeleted(input.inputId);
223 yield* assertInputSecurityGroupDeleted(isg.inputSecurityGroupId);
224 }),
225 {
226 tags: ["provider:aws", "provider:aws:medialive", "live"],
227 timeout: 180_000,
228 },
229 );
230
231 // ---------------------------------------------------------------------------
232 // Channel lifecycle — channels are heavy (slow create, billed per RUNNING
233 // hour). Gated behind AWS_TEST_MEDIALIVE=1; the channel is provisioned IDLE
234 // and never started.
235 // ---------------------------------------------------------------------------
236
237 const ENCODER_SETTINGS: medialive.EncoderSettings = {
238 TimecodeConfig: { Source: "EMBEDDED" },
239 AudioDescriptions: [
240 {
241 Name: "audio_1",
242 AudioSelectorName: "default",
243 CodecSettings: {
244 AacSettings: { Bitrate: 96000, SampleRate: 48000 },
245 },
246 },
247 ],
248 VideoDescriptions: [
249 {
250 Name: "video_1",
251 Width: 640,
252 Height: 360,
253 CodecSettings: {
254 H264Settings: {
255 Bitrate: 1000000,
256 FramerateControl: "SPECIFIED",
257 FramerateNumerator: 30,
258 FramerateDenominator: 1,
259 GopSize: 60,
260 },
261 },
262 },
263 ],
264 OutputGroups: [
265 {
266 Name: "udp",
267 OutputGroupSettings: { UdpGroupSettings: {} },
268 Outputs: [
269 {
270 OutputName: "output_1",
271 VideoDescriptionName: "video_1",
272 AudioDescriptionNames: ["audio_1"],
273 OutputSettings: {
274 UdpOutputSettings: {
275 Destination: { DestinationRefId: "dest1" },
276 ContainerSettings: { M2tsSettings: {} },
277 },
278 },
279 },
280 ],
281 },
282 ],
283 };
284
285 test.provider.skipIf(!process.env.AWS_TEST_MEDIALIVE)(
286 "Channel: create IDLE single-pipeline channel, update log level, destroy (gated AWS_TEST_MEDIALIVE=1)",
287 (stack) =>
288 Effect.gen(function* () {
289 yield* stack.destroy();
290
291 const { channel, input } = yield* stack.deploy(
292 Effect.gen(function* () {
293 const role = yield* Role("MediaLiveRole", {
294 assumeRolePolicyDocument: {
295 Version: "2012-10-17",
296 Statement: [
297 {
298 Effect: "Allow",
299 Principal: { Service: "medialive.amazonaws.com" },
300 Action: ["sts:AssumeRole"],
301 },
302 ],
303 },
304 });
305 const input = yield* Input("ChannelPull", {
306 type: "URL_PULL",
307 sources: [{ Url: "https://example.com/stream/index.m3u8" }],
308 });
309 const channel = yield* Channel("Live", {
310 name: CHANNEL_NAME,
311 channelClass: "SINGLE_PIPELINE",
312 roleArn: role.roleArn,
313 inputAttachments: [
314 {
315 InputId: input.inputId,
316 InputAttachmentName: "primary",
317 InputSettings: {
318 AudioSelectors: [{ Name: "default" }],
319 },
320 },
321 ],
322 inputSpecification: {
323 Codec: "AVC",
324 Resolution: "SD",
325 MaximumBitrate: "MAX_10_MBPS",
326 },
327 destinations: [
328 {
329 Id: "dest1",
330 Settings: [{ Url: "udp://10.220.171.28:5000" }],
331 },
332 ],
333 encoderSettings: ENCODER_SETTINGS,
334 tags: { Environment: "test" },
335 });
336 return { channel, input };
337 }),
338 );
339 expect(channel.channelId).toBeTruthy();
340 expect(channel.channelArn).toContain(":channel:");
341 expect(channel.channelName).toBe(CHANNEL_NAME);
342 expect(channel.channelClass).toBe("SINGLE_PIPELINE");
343 // NEVER started — provisioned and left IDLE.
344 expect(channel.state).toBe("IDLE");
345
346 const observed = yield* medialive.describeChannel({
347 ChannelId: channel.channelId,
348 });
349 expect(observed.State).toBe("IDLE");
350 expect(observed.Tags?.["alchemy::id"]).toBe("Live");
351
352 // Update: bump the log level in place (same id), channel stays IDLE.
353 const updated = yield* stack.deploy(
354 Effect.gen(function* () {
355 const role = yield* Role("MediaLiveRole", {
356 assumeRolePolicyDocument: {
357 Version: "2012-10-17",
358 Statement: [
359 {
360 Effect: "Allow",
361 Principal: { Service: "medialive.amazonaws.com" },
362 Action: ["sts:AssumeRole"],
363 },
364 ],
365 },
366 });
367 const input = yield* Input("ChannelPull", {
368 type: "URL_PULL",
369 sources: [{ Url: "https://example.com/stream/index.m3u8" }],
370 });
371 return {
372 channel: yield* Channel("Live", {
373 name: CHANNEL_NAME,
374 channelClass: "SINGLE_PIPELINE",
375 roleArn: role.roleArn,
376 logLevel: "ERROR",
377 inputAttachments: [
378 {
379 InputId: input.inputId,
380 InputAttachmentName: "primary",
381 InputSettings: {
382 AudioSelectors: [{ Name: "default" }],
383 },
384 },
385 ],
386 inputSpecification: {
387 Codec: "AVC",
388 Resolution: "SD",
389 MaximumBitrate: "MAX_10_MBPS",
390 },
391 destinations: [
392 {
393 Id: "dest1",
394 Settings: [{ Url: "udp://10.220.171.28:5000" }],
395 },
396 ],
397 encoderSettings: ENCODER_SETTINGS,
398 tags: { Environment: "test" },
399 }),
400 input,
401 };
402 }),
403 );
404 expect(updated.channel.channelId).toBe(channel.channelId);
405
406 const observed2 = yield* medialive.describeChannel({
407 ChannelId: channel.channelId,
408 });
409 expect(observed2.LogLevel).toBe("ERROR");
410 expect(observed2.State).toBe("IDLE");
411
412 yield* stack.destroy();
413 yield* assertChannelDeleted(channel.channelId);
414 yield* assertInputDeleted(input.inputId);
415 }),
416 {
417 tags: ["provider:aws", "provider:aws:iam", "provider:aws:medialive", "live"],
418 timeout: 600_000,
419 },
420 );
421
422 // ---------------------------------------------------------------------------
423 // Deletion assertions — typed wait-until-gone.
424 // ---------------------------------------------------------------------------
425
426 const assertInputDeleted = (inputId: string) =>
427 medialive.describeInput({ InputId: inputId }).pipe(
428 Effect.flatMap((input) =>
429 input.State === "DELETED" || input.State === "DELETING"
430 ? Effect.void
431 : Effect.fail(new StillExists({ id: inputId })),
432 ),
433 Effect.catchTag("NotFoundException", () => Effect.void),
434 Effect.retry({
435 while: (e) => e._tag === "StillExists",
436 schedule: Schedule.max([Schedule.spaced("3 seconds"), Schedule.recurs(10)]),
437 }),
438 );
439
440 const assertInputSecurityGroupDeleted = (id: string) =>
441 medialive.describeInputSecurityGroup({ InputSecurityGroupId: id }).pipe(
442 Effect.flatMap((isg) =>
443 isg.State === "DELETED" ? Effect.void : Effect.fail(new StillExists({ id })),
444 ),
445 Effect.catchTag("NotFoundException", () => Effect.void),
446 Effect.retry({
447 while: (e) => e._tag === "StillExists",
448 schedule: Schedule.max([Schedule.spaced("3 seconds"), Schedule.recurs(10)]),
449 }),
450 );
451
452 const assertChannelDeleted = (channelId: string) =>
453 medialive.describeChannel({ ChannelId: channelId }).pipe(
454 Effect.flatMap((channel) =>
455 channel.State === "DELETED" || channel.State === "DELETING"
456 ? Effect.void
457 : Effect.fail(new StillExists({ id: channelId })),
458 ),
459 Effect.catchTag("NotFoundException", () => Effect.void),
460 Effect.retry({
461 while: (e) => e._tag === "StillExists",
462 schedule: Schedule.max([Schedule.spaced("5 seconds"), Schedule.recurs(12)]),
463 }),
464 );