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
| 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 eventbridge from "@distilled.cloud/aws/eventbridge"; |
| 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 MediaLiveChannelTestFunctionLive, { |
| 12 | MediaLiveChannelTestFunction, |
| 13 | } from "./fixtures/channel-handler"; |
| 14 | import MediaLiveTestFunctionLive, { MediaLiveTestFunction } from "./fixtures/handler"; |
| 15 | |
| 16 | const testOptions = { providers: AWS.providers() }; |
| 17 | const { test, beforeAll, afterAll } = Test.make(testOptions); |
| 18 | const sharedStack = Core.scratchStack(testOptions, "MediaLiveBindings"); |
| 19 | const channelStack = Core.scratchStack(testOptions, "MediaLiveChannelBindings"); |
| 20 | |
| 21 | // Lambda function URL cold-start (DNS, IAM propagation, init) can take well |
| 22 | // over 60s on a fresh deploy. |
| 23 | const readinessPolicy = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]); |
| 24 | |
| 25 | class TransientUpstream extends Data.TaggedError("TransientUpstream")<{ |
| 26 | readonly status: number; |
| 27 | readonly body: string; |
| 28 | }> {} |
| 29 | |
| 30 | // The shared Lambda fixture occasionally answers a transient 5xx under load |
| 31 | // (cold re-init, IAM propagation on the freshly attached policy that the |
| 32 | // handler's `Effect.orDie` surfaces as a 500). Retry only 5xx; a genuine |
| 33 | // 4xx/assertion failure surfaces immediately. |
| 34 | const send = (request: HttpClientRequest.HttpClientRequest) => |
| 35 | HttpClient.execute(request).pipe( |
| 36 | Effect.flatMap((response) => |
| 37 | response.status >= 500 |
| 38 | ? response.text.pipe( |
| 39 | Effect.flatMap((body) => |
| 40 | Effect.fail(new TransientUpstream({ status: response.status, body })), |
| 41 | ), |
| 42 | ) |
| 43 | : Effect.succeed(response), |
| 44 | ), |
| 45 | Effect.retry({ |
| 46 | while: (e) => e._tag === "TransientUpstream", |
| 47 | schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(6)]), |
| 48 | }), |
| 49 | ); |
| 50 | |
| 51 | const getJson = (base: string, path: string) => |
| 52 | send(HttpClientRequest.get(`${base}${path}`)).pipe(Effect.flatMap((r) => r.json)); |
| 53 | |
| 54 | const postJson = (base: string, path: string) => |
| 55 | send(HttpClientRequest.post(`${base}${path}`)).pipe(Effect.flatMap((r) => r.json)); |
| 56 | |
| 57 | const awaitReady = (readinessUrl: string) => |
| 58 | HttpClient.get(readinessUrl).pipe( |
| 59 | Effect.flatMap((response) => |
| 60 | response.status === 200 |
| 61 | ? Effect.succeed(response) |
| 62 | : Effect.fail(new Error(`Function not ready: ${response.status}`)), |
| 63 | ), |
| 64 | Effect.tapError((error) => |
| 65 | Effect.logWarning(`MediaLive test setup: fixture not ready yet (${String(error)})`), |
| 66 | ), |
| 67 | Effect.retry({ schedule: readinessPolicy }), |
| 68 | ); |
| 69 | |
| 70 | let baseUrl: string; |
| 71 | let functionArn: string; |
| 72 | |
| 73 | describe.sequential( |
| 74 | "MediaLive Bindings", |
| 75 | { tags: ["provider:aws", "provider:aws:lambda", "provider:aws:medialive", "live"] }, |
| 76 | () => { |
| 77 | beforeAll( |
| 78 | Effect.gen(function* () { |
| 79 | yield* Effect.logInfo("MediaLive test setup: destroying previous resources"); |
| 80 | yield* sharedStack.destroy(); |
| 81 | |
| 82 | yield* Effect.logInfo("MediaLive test setup: deploying fixture"); |
| 83 | const attrs = yield* sharedStack.deploy( |
| 84 | Effect.gen(function* () { |
| 85 | return yield* MediaLiveTestFunction; |
| 86 | }).pipe(Effect.provide(MediaLiveTestFunctionLive)), |
| 87 | ); |
| 88 | |
| 89 | expect(attrs.functionUrl).toBeTruthy(); |
| 90 | baseUrl = attrs.functionUrl!.replace(/\/+$/, ""); |
| 91 | functionArn = attrs.functionArn; |
| 92 | |
| 93 | yield* awaitReady(`${baseUrl}/bindings`); |
| 94 | }), |
| 95 | { timeout: 300_000 }, |
| 96 | ); |
| 97 | |
| 98 | afterAll(sharedStack.destroy(), { timeout: 240_000 }); |
| 99 | |
| 100 | describe("binding registration", () => { |
| 101 | test.provider("all three capabilities initialize in the runtime", () => |
| 102 | Effect.gen(function* () { |
| 103 | const response = (yield* getJson(baseUrl, "/bindings")) as { bound: string[] }; |
| 104 | expect(response.bound).toHaveLength(3); |
| 105 | }), |
| 106 | ); |
| 107 | }); |
| 108 | |
| 109 | describe("DescribeInput", () => { |
| 110 | test.provider("the fixture input idles DETACHED with its pull URL", () => |
| 111 | Effect.gen(function* () { |
| 112 | const response = (yield* getJson(baseUrl, "/input")) as { |
| 113 | state?: string; |
| 114 | type?: string; |
| 115 | sourceUrls?: string[]; |
| 116 | }; |
| 117 | expect(["DETACHED", "ATTACHED"]).toContain(response.state); |
| 118 | expect(response.type).toBe("URL_PULL"); |
| 119 | expect(response.sourceUrls).toEqual(["https://example.com/stream/index.m3u8"]); |
| 120 | }), |
| 121 | ); |
| 122 | }); |
| 123 | |
| 124 | describe("ListChannels", () => { |
| 125 | test.provider("account-level channel enumeration succeeds", () => |
| 126 | Effect.gen(function* () { |
| 127 | const response = (yield* getJson(baseUrl, "/channels")) as { count: number }; |
| 128 | expect(response.count).toBeGreaterThanOrEqual(0); |
| 129 | }), |
| 130 | ); |
| 131 | }); |
| 132 | |
| 133 | describe("ListInputs", () => { |
| 134 | test.provider("account-level input enumeration sees the fixture input", () => |
| 135 | Effect.gen(function* () { |
| 136 | const response = (yield* getJson(baseUrl, "/inputs")) as { count: number }; |
| 137 | expect(response.count).toBeGreaterThanOrEqual(1); |
| 138 | }), |
| 139 | ); |
| 140 | }); |
| 141 | |
| 142 | describe("consumeChannelEvents", { tags: ["provider:aws:eventbridge"] }, () => { |
| 143 | test.provider("the deploy created an EventBridge rule targeting the function", () => |
| 144 | Effect.gen(function* () { |
| 145 | // Out-of-band via distilled: the fixture's consumeChannelEvents |
| 146 | // must have materialized as a rule on the default bus with the |
| 147 | // Lambda as target. |
| 148 | const { RuleNames } = yield* eventbridge.listRuleNamesByTarget({ |
| 149 | TargetArn: functionArn, |
| 150 | }); |
| 151 | expect((RuleNames ?? []).length).toBeGreaterThanOrEqual(1); |
| 152 | }), |
| 153 | ); |
| 154 | }); |
| 155 | }, |
| 156 | ); |
| 157 | |
| 158 | // --------------------------------------------------------------------------- |
| 159 | // Channel-scoped bindings — the fixture provisions a real (IDLE, unbilled) |
| 160 | // channel, which takes minutes to create/delete, so the block is gated the |
| 161 | // same way as the Channel lifecycle test. |
| 162 | // --------------------------------------------------------------------------- |
| 163 | |
| 164 | let channelBaseUrl: string; |
| 165 | |
| 166 | describe.skipIf(!process.env.AWS_TEST_MEDIALIVE)( |
| 167 | "MediaLive Channel Bindings (gated AWS_TEST_MEDIALIVE=1)", |
| 168 | { |
| 169 | tags: [ |
| 170 | "provider:aws", |
| 171 | "provider:aws:batch", |
| 172 | "provider:aws:iam", |
| 173 | "provider:aws:lambda", |
| 174 | "provider:aws:medialive", |
| 175 | "live", |
| 176 | ], |
| 177 | }, |
| 178 | () => { |
| 179 | beforeAll( |
| 180 | Effect.gen(function* () { |
| 181 | yield* Effect.logInfo("MediaLive channel test setup: destroying previous resources"); |
| 182 | yield* channelStack.destroy(); |
| 183 | |
| 184 | yield* Effect.logInfo("MediaLive channel test setup: deploying channel fixture"); |
| 185 | const attrs = yield* channelStack.deploy( |
| 186 | Effect.gen(function* () { |
| 187 | return yield* MediaLiveChannelTestFunction; |
| 188 | }).pipe(Effect.provide(MediaLiveChannelTestFunctionLive)), |
| 189 | ); |
| 190 | |
| 191 | expect(attrs.functionUrl).toBeTruthy(); |
| 192 | channelBaseUrl = attrs.functionUrl!.replace(/\/+$/, ""); |
| 193 | |
| 194 | yield* awaitReady(`${channelBaseUrl}/bindings`); |
| 195 | }), |
| 196 | { timeout: 600_000 }, |
| 197 | ); |
| 198 | |
| 199 | afterAll(channelStack.destroy(), { timeout: 600_000 }); |
| 200 | |
| 201 | describe("binding registration", () => { |
| 202 | test.provider("all nine capabilities initialize in the runtime", () => |
| 203 | Effect.gen(function* () { |
| 204 | const response = (yield* getJson(channelBaseUrl, "/bindings")) as { bound: string[] }; |
| 205 | expect(response.bound).toHaveLength(9); |
| 206 | }), |
| 207 | ); |
| 208 | }); |
| 209 | |
| 210 | describe("DescribeChannel", () => { |
| 211 | test.provider("the fixture channel idles IDLE and never runs", () => |
| 212 | Effect.gen(function* () { |
| 213 | const response = (yield* getJson(channelBaseUrl, "/channel")) as { |
| 214 | state?: string; |
| 215 | channelClass?: string; |
| 216 | pipelinesRunning?: number; |
| 217 | }; |
| 218 | expect(response.state).toBe("IDLE"); |
| 219 | expect(response.channelClass).toBe("SINGLE_PIPELINE"); |
| 220 | }), |
| 221 | ); |
| 222 | }); |
| 223 | |
| 224 | describe("DescribeSchedule", () => { |
| 225 | test.provider("a fresh channel has an empty schedule", () => |
| 226 | Effect.gen(function* () { |
| 227 | const response = (yield* getJson(channelBaseUrl, "/schedule")) as { actions: number }; |
| 228 | expect(response.actions).toBe(0); |
| 229 | }), |
| 230 | ); |
| 231 | }); |
| 232 | |
| 233 | describe("BatchUpdateSchedule + DeleteSchedule", () => { |
| 234 | test.provider("programming and clearing a schedule action roundtrips", () => |
| 235 | Effect.gen(function* () { |
| 236 | const response = (yield* postJson(channelBaseUrl, "/schedule-cycle")) as { |
| 237 | created: number; |
| 238 | cleared: boolean; |
| 239 | tag?: string; |
| 240 | }; |
| 241 | // An IDLE channel accepts fixed-time actions on most accounts; |
| 242 | // where MediaLive rejects them, the typed tag is the outcome. |
| 243 | if (response.tag === undefined) { |
| 244 | expect(response.created).toBe(1); |
| 245 | expect(response.cleared).toBe(true); |
| 246 | } else { |
| 247 | expect(["BadRequestException", "UnprocessableEntityException"]).toContain(response.tag); |
| 248 | } |
| 249 | }), |
| 250 | ); |
| 251 | }); |
| 252 | |
| 253 | describe("ListAlerts", () => { |
| 254 | test.provider("a never-started channel lists its (empty) alerts", () => |
| 255 | Effect.gen(function* () { |
| 256 | const response = (yield* getJson(channelBaseUrl, "/alerts")) as { alerts: number }; |
| 257 | expect(response.alerts).toBeGreaterThanOrEqual(0); |
| 258 | }), |
| 259 | ); |
| 260 | }); |
| 261 | |
| 262 | describe("DescribeThumbnails", () => { |
| 263 | test.provider( |
| 264 | "an IDLE channel has no thumbnails — empty details or the typed BadRequestException", |
| 265 | () => |
| 266 | Effect.gen(function* () { |
| 267 | const response = (yield* getJson(channelBaseUrl, "/thumbnails")) as { |
| 268 | details: number; |
| 269 | tag?: string; |
| 270 | }; |
| 271 | expect(response.details).toBe(0); |
| 272 | if (response.tag !== undefined) { |
| 273 | expect(response.tag).toBe("BadRequestException"); |
| 274 | } |
| 275 | }), |
| 276 | ); |
| 277 | }); |
| 278 | }, |
| 279 | ); |