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

main
ts 91 lines 3.49 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { Channel, StreamKey } from "@alchemy.run/aws/IVS";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as ivs from "@distilled.cloud/aws/ivs";
6 import * as Effect from "effect/Effect";
7 import * as Redacted from "effect/Redacted";
8 import * as Schedule from "effect/Schedule";
9
10 const { test } = Test.make({ providers: AWS.providers() });
11
12 const assertStreamKeyGone = (arn: string) =>
13 Effect.gen(function* () {
14 const streamKey = yield* ivs.getStreamKey({ arn }).pipe(
15 Effect.map((r) => r.streamKey),
16 Effect.catchTag("ResourceNotFoundException", () => Effect.succeed(undefined)),
17 );
18 if (streamKey !== undefined) {
19 return yield* Effect.fail(new Error(`stream key '${arn}' still exists`));
20 }
21 }).pipe(
22 Effect.retry({
23 schedule: Schedule.max([Schedule.fixed("3 seconds"), Schedule.recurs(8)]),
24 }),
25 );
26
27 // CreateChannel auto-provisions a channel's single allowed stream key, so
28 // this also covers the adopt-the-default-key path of the provider.
29 test.provider(
30 "manage the stream key of a channel",
31 (stack) =>
32 Effect.gen(function* () {
33 yield* stack.destroy();
34
35 const deployed = yield* stack.deploy(
36 Effect.gen(function* () {
37 const channel = yield* Channel("KeyedChannel", {
38 channelName: "alchemy-test-ivs-streamkey-channel",
39 type: "BASIC",
40 });
41 const streamKey = yield* StreamKey("Key", {
42 channelArn: channel.channelArn,
43 tags: { fixture: "ivs-stream-key" },
44 });
45 return { channel, streamKey };
46 }),
47 );
48
49 expect(deployed.streamKey.streamKeyArn).toContain(":stream-key/");
50 expect(deployed.streamKey.channelArn).toBe(deployed.channel.channelArn);
51 expect(deployed.streamKey.value).toBeDefined();
52 expect(Redacted.value(deployed.streamKey.value!)).toMatch(/^sk_/);
53
54 // Out-of-band verification via distilled: the key is tagged ours
55 // (the provider adopted the channel's auto-created key or created
56 // one — either way tags must have converged).
57 const observed = yield* ivs.getStreamKey({
58 arn: deployed.streamKey.streamKeyArn,
59 });
60 expect(observed.streamKey?.channelArn).toBe(deployed.channel.channelArn);
61 expect(observed.streamKey?.tags?.fixture).toBe("ivs-stream-key");
62 expect(observed.streamKey?.tags?.["alchemy::id"]).toBe("Key");
63
64 // The channel must have exactly one key (we adopted, not duplicated).
65 const listed = yield* ivs.listStreamKeys({
66 channelArn: deployed.channel.channelArn,
67 });
68 expect(listed.streamKeys.length).toBe(1);
69
70 // No-op redeploy keeps the same key.
71 const redeployed = yield* stack.deploy(
72 Effect.gen(function* () {
73 const channel = yield* Channel("KeyedChannel", {
74 channelName: "alchemy-test-ivs-streamkey-channel",
75 type: "BASIC",
76 });
77 const streamKey = yield* StreamKey("Key", {
78 channelArn: channel.channelArn,
79 tags: { fixture: "ivs-stream-key" },
80 });
81 return { channel, streamKey };
82 }),
83 );
84 expect(redeployed.streamKey.streamKeyArn).toBe(deployed.streamKey.streamKeyArn);
85
86 // Destroy and verify out-of-band with a typed wait-until-gone.
87 yield* stack.destroy();
88 yield* assertStreamKeyGone(deployed.streamKey.streamKeyArn);
89 }),
90 { tags: ["provider:aws", "provider:aws:ivs", "live"], timeout: 240_000 },
91 );