@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 187 lines 7.01 KB
Raw
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 MediaPackageV2TestFunctionLive, { MediaPackageV2TestFunction } from "./fixtures/handler";
12
13 const testOptions = { providers: AWS.providers() };
14 const { test, beforeAll, afterAll } = Test.make(testOptions);
15 const sharedStack = Core.scratchStack(testOptions, "MediaPackageV2Bindings");
16
17 // Lambda function URL cold-start (DNS, IAM propagation, init) can take well
18 // over 60s on a fresh deploy.
19 const readinessPolicy = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]);
20
21 let baseUrl: string;
22 let functionArn: string;
23
24 class TransientUpstream extends Data.TaggedError("TransientUpstream")<{
25 readonly status: number;
26 readonly body: string;
27 }> {}
28
29 // The shared Lambda fixture occasionally answers a transient 5xx under load
30 // (cold re-init, IAM propagation on the freshly attached policy that the
31 // handler's `Effect.orDie` surfaces as a 500). Retry only 5xx; a genuine
32 // 4xx/assertion failure surfaces 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 const getJson = (path: string) =>
51 send(HttpClientRequest.get(`${baseUrl}${path}`)).pipe(Effect.flatMap((r) => r.json));
52
53 const postJson = (path: string) =>
54 send(HttpClientRequest.post(`${baseUrl}${path}`)).pipe(Effect.flatMap((r) => r.json));
55
56 describe.sequential(
57 "MediaPackageV2 Bindings",
58 {
59 tags: [
60 "provider:aws",
61 "provider:aws:lambda",
62 "provider:aws:mediapackagev2",
63 "provider:aws:s3",
64 "live",
65 ],
66 },
67 () => {
68 beforeAll(
69 Effect.gen(function* () {
70 yield* Effect.logInfo("MediaPackageV2 test setup: destroying previous resources");
71 yield* sharedStack.destroy();
72
73 yield* Effect.logInfo("MediaPackageV2 test setup: deploying fixture");
74 const attrs = yield* sharedStack.deploy(
75 Effect.gen(function* () {
76 return yield* MediaPackageV2TestFunction;
77 }).pipe(Effect.provide(MediaPackageV2TestFunctionLive)),
78 );
79
80 expect(attrs.functionUrl).toBeTruthy();
81 baseUrl = attrs.functionUrl!.replace(/\/+$/, "");
82 functionArn = attrs.functionArn;
83
84 const readinessUrl = `${baseUrl}/bindings`;
85 yield* Effect.logInfo(`MediaPackageV2 test setup: probing readiness at ${readinessUrl}`);
86 yield* HttpClient.get(readinessUrl).pipe(
87 Effect.flatMap((response) =>
88 response.status === 200
89 ? Effect.succeed(response)
90 : Effect.fail(new Error(`Function not ready: ${response.status}`)),
91 ),
92 Effect.tapError((error) =>
93 Effect.logWarning(
94 `MediaPackageV2 test setup: fixture not ready yet (${String(error)})`,
95 ),
96 ),
97 Effect.retry({ schedule: readinessPolicy }),
98 );
99 }),
100 { timeout: 300_000 },
101 );
102
103 afterAll.skipIf(!!process.env.NO_DESTROY)(sharedStack.destroy(), { timeout: 240_000 });
104
105 describe("binding registration", () => {
106 test.provider("all six capabilities initialize in the runtime", () =>
107 Effect.gen(function* () {
108 const response = (yield* getJson("/bindings")) as { bound: string[] };
109 expect(response.bound).toHaveLength(6);
110 }),
111 );
112 });
113
114 describe("ResetChannelState", () => {
115 test.provider("resetting the idle channel's ingest state succeeds", () =>
116 Effect.gen(function* () {
117 const response = (yield* postJson("/reset-channel")) as { arn?: string; tag?: string };
118 // A retained stack (NO_DESTROY) can hit MediaPackage's reset rate
119 // limit; the typed tag is then the observable outcome.
120 if (response.tag === undefined) {
121 expect(response.arn).toContain("/channel/");
122 } else {
123 expect(["ConflictException", "ThrottlingException"]).toContain(response.tag);
124 }
125 }),
126 );
127 });
128
129 describe("ResetOriginEndpointState", () => {
130 test.provider("resetting the idle endpoint's state succeeds", () =>
131 Effect.gen(function* () {
132 const response = (yield* postJson("/reset-endpoint")) as { arn?: string; tag?: string };
133 if (response.tag === undefined) {
134 expect(response.arn).toContain("/originEndpoint/");
135 } else {
136 expect(["ConflictException", "ThrottlingException"]).toContain(response.tag);
137 }
138 }),
139 );
140 });
141
142 describe("ListHarvestJobs", () => {
143 test.provider("enumerating the group's harvest jobs succeeds", () =>
144 Effect.gen(function* () {
145 const response = (yield* getJson("/harvest-jobs")) as { count: number };
146 expect(response.count).toBeGreaterThanOrEqual(0);
147 }),
148 );
149 });
150
151 describe("CreateHarvestJob + GetHarvestJob + CancelHarvestJob", () => {
152 test.provider("create → get → cancel roundtrips (or typed rejection)", () =>
153 Effect.gen(function* () {
154 const response = (yield* postJson("/harvest-cycle")) as {
155 created: boolean;
156 status?: string;
157 cancelled: boolean;
158 tag?: string;
159 };
160 if (response.created) {
161 expect(["QUEUED", "IN_PROGRESS", "CANCELLED", "COMPLETED", "FAILED"]).toContain(
162 response.status,
163 );
164 } else {
165 // The idle channel has no harvestable content; MediaPackage may
166 // reject the job upfront with the typed ValidationException.
167 expect(["ValidationException", "ConflictException"]).toContain(response.tag);
168 }
169 }),
170 );
171 });
172
173 describe("consumeHarvestJobEvents", { tags: ["provider:aws:eventbridge"] }, () => {
174 test.provider("the deploy created an EventBridge rule targeting the function", () =>
175 Effect.gen(function* () {
176 // Out-of-band via distilled: the fixture's consumeHarvestJobEvents
177 // must have materialized as a rule on the default bus with the
178 // Lambda as target.
179 const { RuleNames } = yield* eventbridge.listRuleNamesByTarget({
180 TargetArn: functionArn,
181 });
182 expect((RuleNames ?? []).length).toBeGreaterThanOrEqual(1);
183 }),
184 );
185 });
186 },
187 );