@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 213 lines 9.25 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { Channel, ChannelGroup, OriginEndpoint } from "@alchemy.run/aws/MediaPackageV2";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as mediapackagev2 from "@distilled.cloud/aws/mediapackagev2";
6 import type * as Duration from "effect/Duration";
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 // Ungated typed-error probe: prove the distilled error union carries the
13 // not-found tag this provider's read/reconcile paths depend on.
14 test.provider(
15 "getChannelGroup on a bogus name fails with ResourceNotFoundException",
16 () =>
17 Effect.gen(function* () {
18 const error = yield* Effect.flip(
19 mediapackagev2.getChannelGroup({
20 ChannelGroupName: "alchemy-nonexistent-channel-group-probe",
21 }),
22 );
23 expect(error._tag).toBe("ResourceNotFoundException");
24 }),
25 { tags: ["provider:aws", "provider:aws:mediapackagev2", "live"] },
26 );
27
28 const assertGroupGone = (channelGroupName: string) =>
29 Effect.gen(function* () {
30 const status = yield* mediapackagev2
31 .getChannelGroup({ ChannelGroupName: channelGroupName })
32 .pipe(
33 Effect.map(() => "exists" as const),
34 Effect.catchTag("ResourceNotFoundException", () => Effect.succeed("gone" as const)),
35 );
36 if (status !== "gone") {
37 return yield* Effect.fail(new Error(`channel group '${channelGroupName}' still exists`));
38 }
39 }).pipe(
40 Effect.retry({ schedule: Schedule.max([Schedule.fixed("3 seconds"), Schedule.recurs(10)]) }),
41 );
42
43 test.provider(
44 "channel group → channel → origin endpoint lifecycle (create, update, replace, destroy)",
45 (stack) =>
46 Effect.gen(function* () {
47 yield* stack.destroy();
48
49 const deployStack = (props: {
50 endpointDescription?: string;
51 startoverWindow?: Duration.Input;
52 containerType: mediapackagev2.ContainerType;
53 channelPolicy?: string;
54 endpointPolicy?: string;
55 }) =>
56 stack.deploy(
57 Effect.gen(function* () {
58 const group = yield* ChannelGroup("Group", {
59 description: "alchemy mediapackagev2 test group",
60 tags: { fixture: "mediapackagev2" },
61 });
62 const channel = yield* Channel("Feed", {
63 channelGroupName: group.channelGroupName,
64 inputType: "HLS",
65 description: "alchemy mediapackagev2 test channel",
66 policy: props.channelPolicy,
67 tags: { fixture: "mediapackagev2" },
68 });
69 const endpoint = yield* OriginEndpoint("Playback", {
70 channelGroupName: group.channelGroupName,
71 channelName: channel.channelName,
72 containerType: props.containerType,
73 segment: { SegmentDurationSeconds: 6 },
74 description: props.endpointDescription,
75 startoverWindow: props.startoverWindow,
76 hlsManifests: [{ ManifestName: "index" }],
77 policy: props.endpointPolicy,
78 tags: { fixture: "mediapackagev2" },
79 });
80 return { group, channel, endpoint };
81 }),
82 );
83
84 // 1. Greenfield create.
85 const first = yield* deployStack({ endpointDescription: "v1", containerType: "TS" });
86
87 expect(first.group.channelGroupArn).toContain(":channelGroup/");
88 expect(first.group.egressDomain).toContain(".mediapackagev2.");
89 expect(first.channel.channelArn).toContain("/channel/");
90 expect(first.channel.ingestEndpoints.length).toBeGreaterThan(0);
91 expect(first.channel.inputType).toBe("HLS");
92 expect(first.endpoint.originEndpointArn).toContain("/originEndpoint/");
93 expect(first.endpoint.containerType).toBe("TS");
94 expect(first.endpoint.hlsManifests).toHaveLength(1);
95 expect(first.endpoint.hlsManifests[0]?.manifestName).toBe("index");
96 expect(first.endpoint.hlsManifests[0]?.url).toContain("index.m3u8");
97
98 // Out-of-band verification via distilled.
99 const observedGroup = yield* mediapackagev2.getChannelGroup({
100 ChannelGroupName: first.group.channelGroupName,
101 });
102 expect(observedGroup.EgressDomain).toBe(first.group.egressDomain);
103 expect(observedGroup.Description).toBe("alchemy mediapackagev2 test group");
104 const observedChannel = yield* mediapackagev2.getChannel({
105 ChannelGroupName: first.group.channelGroupName,
106 ChannelName: first.channel.channelName,
107 });
108 expect(observedChannel.Arn).toBe(first.channel.channelArn);
109 const observedEndpoint = yield* mediapackagev2.getOriginEndpoint({
110 ChannelGroupName: first.group.channelGroupName,
111 ChannelName: first.channel.channelName,
112 OriginEndpointName: first.endpoint.originEndpointName,
113 });
114 expect(observedEndpoint.Description).toBe("v1");
115 expect(observedEndpoint.Tags?.fixture).toBe("mediapackagev2");
116
117 // 2. In-place update: endpoint description + startover window change,
118 // and resource policies are attached to the channel and endpoint;
119 // every ARN must survive.
120 const accountId = first.channel.channelArn.split(":")[4]!;
121 const channelPolicy = JSON.stringify({
122 Version: "2012-10-17",
123 Statement: [
124 {
125 Sid: "AllowIngest",
126 Effect: "Allow",
127 Principal: { AWS: `arn:aws:iam::${accountId}:root` },
128 Action: "mediapackagev2:PutObject",
129 Resource: first.channel.channelArn,
130 },
131 ],
132 });
133 const endpointPolicy = JSON.stringify({
134 Version: "2012-10-17",
135 Statement: [
136 {
137 Sid: "AllowPlayback",
138 Effect: "Allow",
139 Principal: { AWS: `arn:aws:iam::${accountId}:root` },
140 Action: ["mediapackagev2:GetObject", "mediapackagev2:GetHeadObject"],
141 Resource: first.endpoint.originEndpointArn,
142 },
143 ],
144 });
145 const second = yield* deployStack({
146 endpointDescription: "v2",
147 startoverWindow: "5 minutes",
148 containerType: "TS",
149 channelPolicy,
150 endpointPolicy,
151 });
152 expect(second.group.channelGroupArn).toBe(first.group.channelGroupArn);
153 expect(second.channel.channelArn).toBe(first.channel.channelArn);
154 expect(second.endpoint.originEndpointArn).toBe(first.endpoint.originEndpointArn);
155 const updatedEndpoint = yield* mediapackagev2.getOriginEndpoint({
156 ChannelGroupName: second.group.channelGroupName,
157 ChannelName: second.channel.channelName,
158 OriginEndpointName: second.endpoint.originEndpointName,
159 });
160 expect(updatedEndpoint.Description).toBe("v2");
161 expect(updatedEndpoint.StartoverWindowSeconds).toBe(300);
162 // Policies landed — verify out-of-band.
163 const observedChannelPolicy = yield* mediapackagev2.getChannelPolicy({
164 ChannelGroupName: second.group.channelGroupName,
165 ChannelName: second.channel.channelName,
166 });
167 expect(JSON.parse(observedChannelPolicy.Policy).Statement[0].Sid).toBe("AllowIngest");
168 const observedEndpointPolicy = yield* mediapackagev2.getOriginEndpointPolicy({
169 ChannelGroupName: second.group.channelGroupName,
170 ChannelName: second.channel.channelName,
171 OriginEndpointName: second.endpoint.originEndpointName,
172 });
173 expect(JSON.parse(observedEndpointPolicy.Policy).Statement[0].Sid).toBe("AllowPlayback");
174
175 // 3. Replacement: the container type is immutable, so TS → CMAF must
176 // replace the endpoint (new physical name) while the channel and
177 // group stay in place. Policies are dropped, exercising the
178 // delete-policy sync path on the surviving channel.
179 const third = yield* deployStack({
180 endpointDescription: "v2",
181 startoverWindow: "5 minutes",
182 containerType: "CMAF",
183 });
184 expect(third.group.channelGroupArn).toBe(first.group.channelGroupArn);
185 expect(third.channel.channelArn).toBe(first.channel.channelArn);
186 expect(third.endpoint.originEndpointArn).not.toBe(first.endpoint.originEndpointArn);
187 expect(third.endpoint.originEndpointName).not.toBe(first.endpoint.originEndpointName);
188 expect(third.endpoint.containerType).toBe("CMAF");
189 // The replaced endpoint is deleted.
190 const oldEndpointError = yield* Effect.flip(
191 mediapackagev2.getOriginEndpoint({
192 ChannelGroupName: first.group.channelGroupName,
193 ChannelName: first.channel.channelName,
194 OriginEndpointName: first.endpoint.originEndpointName,
195 }),
196 );
197 expect(oldEndpointError._tag).toBe("ResourceNotFoundException");
198 // The channel policy was removed (policy prop dropped).
199 const removedPolicyError = yield* Effect.flip(
200 mediapackagev2.getChannelPolicy({
201 ChannelGroupName: third.group.channelGroupName,
202 ChannelName: third.channel.channelName,
203 }),
204 );
205 expect(removedPolicyError._tag).toBe("ResourceNotFoundException");
206
207 // 4. Destroy everything (endpoint → channel → group) and verify the
208 // group is gone out-of-band.
209 yield* stack.destroy();
210 yield* assertGroupGone(first.group.channelGroupName);
211 }),
212 { tags: ["provider:aws", "provider:aws:mediapackagev2", "live"], timeout: 300_000 },
213 );