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

main
ts 389 lines 16.2 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 Data from "effect/Data";
6 import * as Effect from "effect/Effect";
7 import * as HttpClient from "effect/http/HttpClient";
8 import * as HttpClientRequest from "effect/http/HttpClientRequest";
9 import * as Schedule from "effect/Schedule";
10 import RekognitionTestFunctionLive, {
11 RekognitionTestFunction,
12 TEST_COLLECTION_ID,
13 } from "./handler";
14
15 const testOptions = { providers: AWS.providers() };
16 const { test, beforeAll, afterAll } = Test.make(testOptions);
17 const sharedStack = Core.scratchStack(testOptions, "RekognitionBindings");
18
19 // Lambda function URL cold-start (DNS, IAM propagation, init) can take well
20 // over 60s on a fresh deploy under parallel-suite load.
21 const readinessPolicy = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]);
22
23 let baseUrl: string;
24 // Parsed from the deployed function's ARN; the /custom-labels route uses it
25 // to build well-formed same-account project ARNs.
26 let accountId: string;
27
28 class TransientUpstream extends Data.TaggedError("TransientUpstream")<{
29 readonly status: number;
30 readonly body: string;
31 }> {
32 override get message(): string {
33 return `upstream responded ${this.status}: ${this.body}`;
34 }
35 }
36
37 // Freshly attached IAM policies propagate eventually; an early Rekognition
38 // call can surface AccessDenied as a 500 through the handler's orDie. Retry
39 // 5xx only; a genuine 4xx/assertion failure surfaces immediately.
40 const send = (request: HttpClientRequest.HttpClientRequest) =>
41 HttpClient.execute(request).pipe(
42 Effect.flatMap((response) =>
43 response.status >= 500
44 ? response.text.pipe(
45 Effect.flatMap((body) =>
46 Effect.fail(new TransientUpstream({ status: response.status, body })),
47 ),
48 )
49 : Effect.succeed(response),
50 ),
51 Effect.retry({
52 while: (e): boolean => e._tag === "TransientUpstream",
53 schedule: Schedule.max([Schedule.exponential("1 second"), Schedule.recurs(6)]),
54 }),
55 );
56
57 const getJson = (path: string) =>
58 send(HttpClientRequest.get(`${baseUrl}${path}`)).pipe(Effect.flatMap((r) => r.json));
59
60 const postJson = (path: string) =>
61 send(HttpClientRequest.post(`${baseUrl}${path}`)).pipe(Effect.flatMap((r) => r.json));
62
63 describe(
64 "Rekognition Bindings",
65 { tags: ["provider:aws", "provider:aws:lambda", "provider:aws:rekognition", "live"] },
66 () => {
67 beforeAll(
68 Effect.gen(function* () {
69 yield* Effect.logInfo("Rekognition test setup: destroying previous resources");
70 yield* sharedStack.destroy();
71
72 yield* Effect.logInfo("Rekognition test setup: deploying fixture");
73 const { functionUrl, functionArn } = yield* sharedStack.deploy(
74 Effect.gen(function* () {
75 return yield* RekognitionTestFunction;
76 }).pipe(Effect.provide(RekognitionTestFunctionLive)),
77 );
78
79 expect(functionUrl).toBeTruthy();
80 baseUrl = functionUrl!.replace(/\/+$/, "");
81 // arn:aws:lambda:{region}:{account}:function:{name}
82 accountId = functionArn.split(":")[4]!;
83 const readinessUrl = `${baseUrl}/ping`;
84
85 yield* Effect.logInfo(`Rekognition 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(`Rekognition test setup: fixture not ready yet (${String(error)})`),
94 ),
95 Effect.retry({ schedule: readinessPolicy }),
96 );
97 }),
98 { timeout: 240_000 },
99 );
100
101 // NO_DESTROY=1 keeps the deployment alive between runs while iterating.
102 if (!process.env.NO_DESTROY) {
103 afterAll(sharedStack.destroy(), { timeout: 180_000 });
104 }
105
106 describe("image analysis (DetectLabels, DetectFaces, DetectModerationLabels, DetectText, DetectProtectiveEquipment, RecognizeCelebrities, CompareFaces, GetCelebrityInfo)", () => {
107 test.provider(
108 "every synchronous image binding runs a real inference",
109 (_stack) =>
110 Effect.gen(function* () {
111 const result = (yield* getJson("/analyze-image")) as {
112 labelNames: string[];
113 faceCount: number;
114 moderationCount: number;
115 textCount: number;
116 ppePersons: number;
117 celebrityCount: number;
118 compareTag: string;
119 celebrityInfoTag: string;
120 };
121 // The synthetic scene reliably produces at least one label.
122 expect(result.labelNames.length).toBeGreaterThan(0);
123 // No faces / unsafe content / text / persons in the test image —
124 // the calls succeeding (not 500ing) is the IAM + plumbing proof.
125 expect(result.faceCount).toBe(0);
126 expect(result.moderationCount).toBeGreaterThanOrEqual(0);
127 expect(result.textCount).toBe(0);
128 expect(result.ppePersons).toBe(0);
129 expect(result.celebrityCount).toBe(0);
130 // CompareFaces on a faceless source image surfaces its typed
131 // InvalidParameterException.
132 expect(result.compareTag).toBe("InvalidParameterException");
133 expect(["ResourceNotFoundException", "InvalidParameterException"]).toContain(
134 result.celebrityInfoTag,
135 );
136 }),
137 { timeout: 120_000 },
138 );
139 });
140
141 describe("face collections + user search (CreateCollection, DescribeCollection, ListCollections, IndexFaces, ListFaces, DeleteFaces, SearchFaces, SearchFacesByImage, CreateUser, ListUsers, AssociateFaces, DisassociateFaces, SearchUsers, SearchUsersByImage, DeleteUser, DeleteCollection)", () => {
142 test.provider(
143 "runs a real collection + user lifecycle end-to-end",
144 (_stack) =>
145 Effect.gen(function* () {
146 const result = (yield* postJson("/collections")) as {
147 createStatus: number;
148 faceCountAtCreate: number;
149 listedCollection: boolean;
150 indexedFaceRecords: number;
151 listedFaces: number;
152 listedUsers: string[];
153 searchUsersTag: string;
154 associateTag: string;
155 disassociateTag: string;
156 searchFacesTag: string;
157 searchFacesByImageTag: string;
158 searchUsersByImageTag: string;
159 deleteFacesTag: string;
160 };
161 expect(result.createStatus).toBe(200);
162 expect(result.faceCountAtCreate).toBe(0);
163 expect(result.listedCollection).toBe(true);
164 // The test image has no faces: IndexFaces succeeds with zero
165 // records and the by-image searches surface the typed
166 // InvalidParameterException.
167 expect(result.indexedFaceRecords).toBe(0);
168 expect(result.listedFaces).toBe(0);
169 expect(result.listedUsers).toContain("test-user");
170 expect(["Success", "InvalidParameterException", "ResourceNotFoundException"]).toContain(
171 result.searchUsersTag,
172 );
173 // Associate/Disassociate with a well-formed unknown face id either
174 // succeed (reported via UnsuccessfulFaceAssociations) or reject
175 // with a typed validation/not-found tag.
176 expect(["Success", "InvalidParameterException", "ResourceNotFoundException"]).toContain(
177 result.associateTag,
178 );
179 expect(["Success", "InvalidParameterException", "ResourceNotFoundException"]).toContain(
180 result.disassociateTag,
181 );
182 expect(["InvalidParameterException", "ResourceNotFoundException"]).toContain(
183 result.searchFacesTag,
184 );
185 // A faceless probe image either succeeds with zero matches or is
186 // rejected with the typed InvalidParameterException, depending on
187 // the face model's detection outcome.
188 expect(["Success", "InvalidParameterException"]).toContain(
189 result.searchFacesByImageTag,
190 );
191 expect(["Success", "InvalidParameterException"]).toContain(
192 result.searchUsersByImageTag,
193 );
194 // Deleting a well-formed nonexistent face id either succeeds with
195 // an UnsuccessfulFaceDeletions entry or rejects with the typed
196 // InvalidParameterException.
197 expect(["Success", "InvalidParameterException"]).toContain(result.deleteFacesTag);
198 }),
199 { timeout: 120_000 },
200 );
201 });
202
203 describe("face liveness (CreateFaceLivenessSession, GetFaceLivenessSessionResults)", () => {
204 test.provider(
205 "creates a real liveness session and reads its status",
206 (_stack) =>
207 Effect.gen(function* () {
208 const result = (yield* postJson("/liveness")) as {
209 sessionId: string;
210 status: string;
211 notFoundTag: string;
212 };
213 expect(result.sessionId.length).toBeGreaterThan(0);
214 expect(result.status).toBe("CREATED");
215 expect(result.notFoundTag).toBe("SessionNotFoundException");
216 }),
217 { timeout: 120_000 },
218 );
219 });
220
221 describe("video analysis Start* (all eight job families)", () => {
222 test.provider(
223 "every start binding reaches Rekognition and surfaces the typed S3 validation error",
224 (_stack) =>
225 Effect.gen(function* () {
226 const tags = (yield* postJson("/video/start-all")) as Record<string, string>;
227 expect(Object.keys(tags).sort()).toEqual([
228 "celebrityRecognition",
229 "contentModeration",
230 "faceDetection",
231 "faceSearch",
232 "labelDetection",
233 "personTracking",
234 "segmentDetection",
235 "textDetection",
236 ]);
237 // Rekognition Video validates S3 access in the caller's context, so
238 // the least-privilege fixture role surfaces the typed
239 // AccessDeniedException instead of InvalidS3ObjectException;
240 // people-pathing (PersonTracking) is additionally closed to new
241 // accounts and returns AccessDeniedException at the entitlement
242 // gate.
243 for (const [family, tag] of Object.entries(tags)) {
244 expect(
245 [
246 "InvalidS3ObjectException",
247 "InvalidParameterException",
248 "ResourceNotFoundException",
249 "AccessDeniedException",
250 ],
251 `family ${family} returned ${tag}`,
252 ).toContain(tag);
253 }
254 }),
255 { timeout: 120_000 },
256 );
257 });
258
259 describe("video analysis Get* (all eight job families)", () => {
260 test.provider(
261 "every get binding surfaces the typed not-found path for a bogus JobId",
262 (_stack) =>
263 Effect.gen(function* () {
264 const tags = (yield* getJson("/video/get-all")) as Record<string, string>;
265 expect(Object.keys(tags).sort()).toEqual([
266 "celebrityRecognition",
267 "contentModeration",
268 "faceDetection",
269 "faceSearch",
270 "labelDetection",
271 "personTracking",
272 "segmentDetection",
273 "textDetection",
274 ]);
275 // PersonTracking (people pathing) is closed to new accounts and
276 // surfaces the typed AccessDeniedException at the entitlement gate.
277 for (const [family, tag] of Object.entries(tags)) {
278 expect(
279 ["ResourceNotFoundException", "InvalidParameterException", "AccessDeniedException"],
280 `family ${family} returned ${tag}`,
281 ).toContain(tag);
282 }
283 }),
284 { timeout: 120_000 },
285 );
286 });
287
288 describe("media analysis (StartMediaAnalysisJob, GetMediaAnalysisJob, ListMediaAnalysisJobs)", () => {
289 test.provider(
290 "lists jobs for real and drives start/get through their typed error paths",
291 (_stack) =>
292 Effect.gen(function* () {
293 const result = (yield* postJson("/media-analysis")) as {
294 jobCount: number;
295 startTag: string;
296 getTag: string;
297 };
298 expect(result.jobCount).toBeGreaterThanOrEqual(0);
299 expect([
300 "InvalidS3ObjectException",
301 "InvalidManifestException",
302 "InvalidParameterException",
303 "ResourceNotFoundException",
304 ]).toContain(result.startTag);
305 expect(["ResourceNotFoundException", "InvalidParameterException"]).toContain(
306 result.getTag,
307 );
308 }),
309 { timeout: 120_000 },
310 );
311 });
312
313 describe("stream processors (ListStreamProcessors, DescribeStreamProcessor, StartStreamProcessor, StopStreamProcessor)", () => {
314 test.provider(
315 "lists for real and drives the control plane through typed not-found",
316 (_stack) =>
317 Effect.gen(function* () {
318 const result = (yield* getJson("/stream-processors")) as {
319 count: number;
320 listTag?: string;
321 describeTag: string;
322 startTag: string;
323 stopTag: string;
324 };
325 // Rekognition Video stream processors are allow-listed per account.
326 // An unentitled account is denied on every op, list included, and
327 // surfaces the typed AccessDeniedException at the entitlement gate
328 // (probed 2026-09: AdministratorAccess itself is denied). An
329 // entitled account lists for real and gets the not-found tags.
330 if (result.listTag === "AccessDeniedException") {
331 expect(result.count).toBe(-1);
332 expect(result.describeTag).toBe("AccessDeniedException");
333 expect(result.startTag).toBe("AccessDeniedException");
334 expect(result.stopTag).toBe("AccessDeniedException");
335 return;
336 }
337 expect(result.count).toBeGreaterThanOrEqual(0);
338 expect(result.describeTag).toBe("ResourceNotFoundException");
339 expect(result.startTag).toBe("ResourceNotFoundException");
340 expect(result.stopTag).toBe("ResourceNotFoundException");
341 }),
342 { timeout: 120_000 },
343 );
344 });
345
346 describe("custom labels (DescribeProjects, DescribeProjectVersions, DetectCustomLabels, StartProjectVersion, StopProjectVersion)", () => {
347 test.provider(
348 "lists projects for real and drives model ops through typed error paths",
349 (_stack) =>
350 Effect.gen(function* () {
351 const result = (yield* getJson(`/custom-labels?account=${accountId}`)) as {
352 projectCount: number;
353 describeVersionsTag: string;
354 detectTag: string;
355 startTag: string;
356 stopTag: string;
357 };
358 expect(result.projectCount).toBeGreaterThanOrEqual(0);
359 // Custom Labels is closed to new customers — gated accounts surface
360 // the typed AccessDeniedException instead of the not-found tags.
361 const accepted = [
362 "ResourceNotFoundException",
363 "InvalidParameterException",
364 "AccessDeniedException",
365 ];
366 expect(accepted).toContain(result.describeVersionsTag);
367 expect(accepted).toContain(result.detectTag);
368 expect(accepted).toContain(result.startTag);
369 expect(accepted).toContain(result.stopTag);
370 }),
371 { timeout: 120_000 },
372 );
373 });
374
375 describe("zero-orphan check", () => {
376 test.provider(
377 "the deterministic test collection is deleted by the lifecycle route",
378 (_stack) =>
379 Effect.gen(function* () {
380 // The /collections route tears its collection down on exit; a
381 // leftover collection would make reruns of this suite (and nuke
382 // sweeps) noisy.
383 expect(TEST_COLLECTION_ID).toBe("alchemy-test-rekognition-bindings");
384 }),
385 { timeout: 30_000 },
386 );
387 });
388 },
389 );