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 kv from "@distilled.cloud/aws/kinesis-video"; |
| 6 | import * as Data from "effect/Data"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as HttpClient from "effect/http/HttpClient"; |
| 9 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 10 | import * as Schedule from "effect/Schedule"; |
| 11 | import KinesisVideoTestFunctionLive, { KinesisVideoTestFunction } from "./handler"; |
| 12 | |
| 13 | const testOptions = { providers: AWS.providers() }; |
| 14 | const { test, beforeAll, afterAll } = Test.make(testOptions); |
| 15 | const sharedStack = Core.scratchStack(testOptions, "KinesisVideoBindings"); |
| 16 | |
| 17 | const readinessPolicy = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]); |
| 18 | |
| 19 | let baseUrl: string; |
| 20 | |
| 21 | class TransientUpstream extends Data.TaggedError("TransientUpstream")<{ |
| 22 | readonly status: number; |
| 23 | readonly body: string; |
| 24 | }> {} |
| 25 | |
| 26 | class FixtureStillExists extends Data.TaggedError("FixtureStillExists")<{ |
| 27 | readonly streams: string[]; |
| 28 | readonly channels: string[]; |
| 29 | }> {} |
| 30 | |
| 31 | // Retry transient 5xx from the shared fixture under parallel-suite load; |
| 32 | // genuine 4xx failures surface 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("500 millis"), Schedule.recurs(6)]), |
| 47 | }), |
| 48 | ); |
| 49 | |
| 50 | // The fixture's role policies are freshly created on every deploy, and IAM |
| 51 | // is eventually consistent — the first data-plane calls can transiently |
| 52 | // return AccessDeniedException. Fetch the route's JSON outcome and repeat |
| 53 | // (bounded) until propagation settles on the terminal typed tag. |
| 54 | const fetchOutcome = (url: string) => |
| 55 | send(HttpClientRequest.get(url)).pipe( |
| 56 | Effect.flatMap((response) => |
| 57 | response.status === 200 |
| 58 | ? (response.json as Effect.Effect<unknown>) |
| 59 | : Effect.fail(new TransientUpstream({ status: response.status, body: "" })), |
| 60 | ), |
| 61 | Effect.map( |
| 62 | (body) => |
| 63 | body as { |
| 64 | ok?: boolean; |
| 65 | count?: number; |
| 66 | url?: string; |
| 67 | contentType?: string; |
| 68 | timedOut?: boolean; |
| 69 | answered?: boolean; |
| 70 | errorTag?: string; |
| 71 | errorMessage?: string; |
| 72 | }, |
| 73 | ), |
| 74 | Effect.repeat({ |
| 75 | schedule: Schedule.spaced("3 seconds"), |
| 76 | until: (body): boolean => body.errorTag !== "AccessDeniedException", |
| 77 | times: 20, |
| 78 | }), |
| 79 | ); |
| 80 | |
| 81 | describe( |
| 82 | "KinesisVideo Bindings", |
| 83 | { tags: ["provider:aws", "provider:aws:kinesisvideo", "provider:aws:lambda", "live"] }, |
| 84 | () => { |
| 85 | beforeAll( |
| 86 | Effect.gen(function* () { |
| 87 | yield* Effect.logInfo("KinesisVideo test setup: destroying previous resources"); |
| 88 | yield* sharedStack.destroy(); |
| 89 | |
| 90 | yield* Effect.logInfo("KinesisVideo test setup: deploying fixture"); |
| 91 | const { functionUrl } = yield* sharedStack.deploy( |
| 92 | Effect.gen(function* () { |
| 93 | return yield* KinesisVideoTestFunction; |
| 94 | }).pipe(Effect.provide(KinesisVideoTestFunctionLive)), |
| 95 | ); |
| 96 | |
| 97 | expect(functionUrl).toBeTruthy(); |
| 98 | baseUrl = functionUrl!.replace(/\/+$/, ""); |
| 99 | |
| 100 | yield* Effect.logInfo(`KinesisVideo test setup: probing readiness at ${baseUrl}/info`); |
| 101 | yield* HttpClient.get(`${baseUrl}/info`).pipe( |
| 102 | Effect.flatMap((response) => |
| 103 | response.status === 200 |
| 104 | ? Effect.void |
| 105 | : Effect.fail(new Error(`Function not ready: ${response.status}`)), |
| 106 | ), |
| 107 | Effect.retry({ schedule: readinessPolicy }), |
| 108 | ); |
| 109 | }), |
| 110 | { timeout: 300_000 }, |
| 111 | ); |
| 112 | |
| 113 | afterAll.skipIf(!!process.env.NO_DESTROY)( |
| 114 | Effect.gen(function* () { |
| 115 | yield* sharedStack.destroy(); |
| 116 | // assert the fixture stream + channel are gone out-of-band. Physical |
| 117 | // names are deterministic: `${stackName}-${logicalId}-${stage}-…`, so a |
| 118 | // BEGINS_WITH listing pinpoints exactly the resources this suite owns. |
| 119 | // Deletion is asynchronous — DELETING counts as gone. |
| 120 | yield* Effect.gen(function* () { |
| 121 | const streams = yield* kv.listStreams({ |
| 122 | StreamNameCondition: { |
| 123 | ComparisonOperator: "BEGINS_WITH", |
| 124 | ComparisonValue: "KinesisVideoBindings-FixtureStream-test-", |
| 125 | }, |
| 126 | }); |
| 127 | const liveStreams = (streams.StreamInfoList ?? []).filter((s) => s.Status !== "DELETING"); |
| 128 | const channels = yield* kv.listSignalingChannels({ |
| 129 | ChannelNameCondition: { |
| 130 | ComparisonOperator: "BEGINS_WITH", |
| 131 | ComparisonValue: "KinesisVideoBindings-FixtureChannel-test-", |
| 132 | }, |
| 133 | }); |
| 134 | const liveChannels = (channels.ChannelInfoList ?? []).filter( |
| 135 | (c) => c.ChannelStatus !== "DELETING", |
| 136 | ); |
| 137 | if (liveStreams.length > 0 || liveChannels.length > 0) { |
| 138 | return yield* Effect.fail( |
| 139 | new FixtureStillExists({ |
| 140 | streams: liveStreams.map((s) => s.StreamName ?? "?"), |
| 141 | channels: liveChannels.map((c) => c.ChannelName ?? "?"), |
| 142 | }), |
| 143 | ); |
| 144 | } |
| 145 | }).pipe( |
| 146 | Effect.retry({ |
| 147 | while: (e): boolean => e._tag === "FixtureStillExists", |
| 148 | schedule: Schedule.max([Schedule.spaced("3 seconds"), Schedule.recurs(10)]), |
| 149 | }), |
| 150 | (effect) => Core.withProviders(effect, testOptions, "KinesisVideoBindings"), |
| 151 | ); |
| 152 | }), |
| 153 | { timeout: 240_000 }, |
| 154 | ); |
| 155 | |
| 156 | describe("GetHLSStreamingSessionURL", () => { |
| 157 | test.provider( |
| 158 | "lambda reaches the archived-media data plane through the binding", |
| 159 | (_stack) => |
| 160 | Effect.gen(function* () { |
| 161 | const body = yield* fetchOutcome(`${baseUrl}/hls`); |
| 162 | // The fixture stream has never ingested media, so LIVE playback |
| 163 | // deterministically returns the archived-media data plane's typed |
| 164 | // no-fragments error. Getting THIS tag (and not NotAuthorized / |
| 165 | // an unknown error) proves GetDataEndpoint resolution, IAM, and |
| 166 | // the signed data-plane call all work. |
| 167 | expect(body.errorTag).toBe("ResourceNotFoundException"); |
| 168 | }), |
| 169 | { timeout: 120_000 }, |
| 170 | ); |
| 171 | }); |
| 172 | |
| 173 | describe("GetIceServerConfig", () => { |
| 174 | test.provider( |
| 175 | "lambda fetches TURN servers through the binding", |
| 176 | (_stack) => |
| 177 | Effect.gen(function* () { |
| 178 | const response = yield* send(HttpClientRequest.get(`${baseUrl}/ice`)); |
| 179 | expect(response.status).toBe(200); |
| 180 | const body = (yield* response.json) as { |
| 181 | servers?: { uris: string[]; hasCredentials: boolean; ttl?: number }[]; |
| 182 | errorTag?: string; |
| 183 | }; |
| 184 | expect(body.errorTag).toBeUndefined(); |
| 185 | expect(body.servers!.length).toBeGreaterThan(0); |
| 186 | const turn = body.servers!.find((s) => s.uris.some((uri) => uri.startsWith("turn"))); |
| 187 | expect(turn).toBeDefined(); |
| 188 | expect(turn?.hasCredentials).toBe(true); |
| 189 | }), |
| 190 | { timeout: 120_000 }, |
| 191 | ); |
| 192 | }); |
| 193 | |
| 194 | describe("GetDASHStreamingSessionURL", () => { |
| 195 | test.provider( |
| 196 | "lambda reaches the archived-media data plane through the binding", |
| 197 | (_stack) => |
| 198 | Effect.gen(function* () { |
| 199 | const body = yield* fetchOutcome(`${baseUrl}/dash`); |
| 200 | // Same rationale as /hls: LIVE playback on the never-ingested |
| 201 | // stream returns the typed no-fragments error. |
| 202 | expect(body.errorTag).toBe("ResourceNotFoundException"); |
| 203 | }), |
| 204 | { timeout: 120_000 }, |
| 205 | ); |
| 206 | }); |
| 207 | |
| 208 | describe("ListFragments", () => { |
| 209 | test.provider( |
| 210 | "lambda lists fragments of the empty stream (empty page)", |
| 211 | (_stack) => |
| 212 | Effect.gen(function* () { |
| 213 | const body = yield* fetchOutcome(`${baseUrl}/fragments`); |
| 214 | // toMatchObject so a failure diff surfaces the errorTag |
| 215 | expect(body).toMatchObject({ ok: true, count: 0 }); |
| 216 | }), |
| 217 | { timeout: 120_000 }, |
| 218 | ); |
| 219 | }); |
| 220 | |
| 221 | describe("GetClip", () => { |
| 222 | test.provider( |
| 223 | "lambda requests a clip and receives the typed no-fragments error", |
| 224 | (_stack) => |
| 225 | Effect.gen(function* () { |
| 226 | const body = yield* fetchOutcome(`${baseUrl}/clip`); |
| 227 | expect(body.ok).toBe(false); |
| 228 | expect(body.errorTag).toBe("ResourceNotFoundException"); |
| 229 | }), |
| 230 | { timeout: 120_000 }, |
| 231 | ); |
| 232 | }); |
| 233 | |
| 234 | describe("GetImages", () => { |
| 235 | test.provider( |
| 236 | "lambda extracts images from the empty stream (empty page or typed error)", |
| 237 | (_stack) => |
| 238 | Effect.gen(function* () { |
| 239 | const body = yield* fetchOutcome(`${baseUrl}/images`); |
| 240 | // The empty stream always rejects the request with a typed error; |
| 241 | // WHICH tag is a function of the seconds-old stream's age: while |
| 242 | // the trailing-60s window still predates the stream's creation the |
| 243 | // service answers InvalidArgumentException ("End timestamp … is |
| 244 | // outside of the stream retention period"), and once the window |
| 245 | // overlaps its lifetime it answers ResourceNotFoundException |
| 246 | // ("No fragments found"). Both are data-plane responses, proving |
| 247 | // endpoint discovery, IAM, and the signed call. |
| 248 | expect(body.ok).toBe(false); |
| 249 | expect(["ResourceNotFoundException", "InvalidArgumentException"]).toContain( |
| 250 | body.errorTag, |
| 251 | ); |
| 252 | }), |
| 253 | { timeout: 120_000 }, |
| 254 | ); |
| 255 | }); |
| 256 | |
| 257 | describe("GetMediaForFragmentList", () => { |
| 258 | test.provider( |
| 259 | "lambda requests fragment media and receives the typed no-fragment error", |
| 260 | (_stack) => |
| 261 | Effect.gen(function* () { |
| 262 | const body = yield* fetchOutcome(`${baseUrl}/fragment-media`); |
| 263 | // The nonexistent fragment number is deterministically rejected by |
| 264 | // the media data plane: "Fragment numbers are invalid". |
| 265 | expect(body.ok).toBe(false); |
| 266 | expect(body.errorTag).toBe("InvalidArgumentException"); |
| 267 | expect(body.errorMessage).toContain("Fragment numbers are invalid"); |
| 268 | }), |
| 269 | { timeout: 120_000 }, |
| 270 | ); |
| 271 | }); |
| 272 | |
| 273 | describe("JoinStorageSession", () => { |
| 274 | test.provider( |
| 275 | "lambda reaches the WebRTC storage plane; unconfigured storage is a typed error", |
| 276 | (_stack) => |
| 277 | Effect.gen(function* () { |
| 278 | const body = yield* fetchOutcome(`${baseUrl}/join-storage`); |
| 279 | // The fixture channel has no MediaStorageConfiguration, so WEBRTC |
| 280 | // endpoint discovery deterministically rejects the channel: |
| 281 | // "MediaStorageConfiguration is required for WEBRTC protocol". |
| 282 | expect(body.ok).toBe(false); |
| 283 | expect(body.errorTag).toBe("InvalidArgumentException"); |
| 284 | expect(body.errorMessage).toContain("MediaStorageConfiguration"); |
| 285 | }), |
| 286 | { timeout: 120_000 }, |
| 287 | ); |
| 288 | }); |
| 289 | |
| 290 | describe("JoinStorageSessionAsViewer", () => { |
| 291 | test.provider( |
| 292 | "lambda reaches the WebRTC storage plane as viewer; typed error without storage", |
| 293 | (_stack) => |
| 294 | Effect.gen(function* () { |
| 295 | const body = yield* fetchOutcome(`${baseUrl}/join-storage-viewer`); |
| 296 | // Same deterministic rejection as /join-storage. |
| 297 | expect(body.ok).toBe(false); |
| 298 | expect(body.errorTag).toBe("InvalidArgumentException"); |
| 299 | expect(body.errorMessage).toContain("MediaStorageConfiguration"); |
| 300 | }), |
| 301 | { timeout: 120_000 }, |
| 302 | ); |
| 303 | }); |
| 304 | |
| 305 | describe("SendAlexaOfferToMaster", () => { |
| 306 | test.provider( |
| 307 | "lambda delivers an SDP offer through the signaling plane", |
| 308 | (_stack) => |
| 309 | Effect.gen(function* () { |
| 310 | const body = yield* fetchOutcome(`${baseUrl}/alexa-offer`); |
| 311 | // The signaling data plane accepts the SDP stub and, with no |
| 312 | // master connected to answer, deterministically holds the offer |
| 313 | // until the fixture's bounded 5s timeout. |
| 314 | expect(body).toMatchObject({ ok: true, timedOut: true }); |
| 315 | }), |
| 316 | { timeout: 120_000 }, |
| 317 | ); |
| 318 | }); |
| 319 | |
| 320 | describe("GetMedia", () => { |
| 321 | test.provider( |
| 322 | "lambda opens a media connection through the binding", |
| 323 | (_stack) => |
| 324 | Effect.gen(function* () { |
| 325 | const body = yield* fetchOutcome(`${baseUrl}/media`); |
| 326 | // The media data plane deterministically accepts the connection to |
| 327 | // the empty stream and answers with matroska headers before any |
| 328 | // fragment exists. |
| 329 | expect(body).toMatchObject({ ok: true, contentType: "video/webm" }); |
| 330 | }), |
| 331 | { timeout: 120_000 }, |
| 332 | ); |
| 333 | }); |
| 334 | }, |
| 335 | ); |