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 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 { HELLO_PNG_TEXT } from "./constants.ts"; |
| 11 | import TextractTestFunctionLive, { TextractTestFunction } from "./handler.ts"; |
| 12 | |
| 13 | const testOptions = { providers: AWS.providers() }; |
| 14 | const { test, beforeAll, afterAll } = Test.make(testOptions); |
| 15 | const sharedStack = Core.scratchStack(testOptions, "TextractBindings"); |
| 16 | |
| 17 | // Lambda function URL cold-start (DNS, IAM propagation, init) can take well |
| 18 | // over 60s on a fresh deploy under parallel-suite load. |
| 19 | const readinessPolicy = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]); |
| 20 | |
| 21 | let baseUrl: string; |
| 22 | |
| 23 | class TransientUpstream extends Data.TaggedError("TransientUpstream")<{ |
| 24 | readonly status: number; |
| 25 | readonly body: string; |
| 26 | }> {} |
| 27 | |
| 28 | // The shared Lambda fixture occasionally answers a transient 5xx under load |
| 29 | // (cold re-init, IAM propagation, upstream throttling surfaced as a defect). |
| 30 | // Retry 5xx only; a genuine 4xx/assertion failure surfaces immediately. |
| 31 | const send = (request: HttpClientRequest.HttpClientRequest) => |
| 32 | HttpClient.execute(request).pipe( |
| 33 | Effect.flatMap((response) => |
| 34 | response.status >= 500 |
| 35 | ? response.text.pipe( |
| 36 | Effect.flatMap((body) => |
| 37 | Effect.fail(new TransientUpstream({ status: response.status, body })), |
| 38 | ), |
| 39 | ) |
| 40 | : Effect.succeed(response), |
| 41 | ), |
| 42 | Effect.retry({ |
| 43 | while: (e) => e._tag === "TransientUpstream", |
| 44 | schedule: Schedule.max([Schedule.exponential("1 second"), Schedule.recurs(5)]), |
| 45 | }), |
| 46 | ); |
| 47 | |
| 48 | const getJson = <T>(path: string) => |
| 49 | send(HttpClientRequest.get(`${baseUrl}${path}`)).pipe( |
| 50 | Effect.flatMap((r) => r.json), |
| 51 | Effect.map((body) => body as T), |
| 52 | ); |
| 53 | |
| 54 | const postJson = <T>(path: string) => |
| 55 | send(HttpClientRequest.post(`${baseUrl}${path}`)).pipe( |
| 56 | Effect.flatMap((r) => r.json), |
| 57 | Effect.map((body) => body as T), |
| 58 | ); |
| 59 | |
| 60 | /** Poll an async job's get route until it leaves IN_PROGRESS (bounded). */ |
| 61 | const pollJob = <T extends { jobStatus?: string }>(path: string, jobId: string) => |
| 62 | getJson<T>(`${path}?jobId=${jobId}`).pipe( |
| 63 | Effect.repeat({ |
| 64 | schedule: Schedule.spaced("3 seconds"), |
| 65 | until: (r): boolean => r.jobStatus !== undefined && r.jobStatus !== "IN_PROGRESS", |
| 66 | times: 40, |
| 67 | }), |
| 68 | ); |
| 69 | |
| 70 | describe( |
| 71 | "Textract Bindings", |
| 72 | { |
| 73 | tags: [ |
| 74 | "provider:aws", |
| 75 | "provider:aws:lambda", |
| 76 | "provider:aws:s3", |
| 77 | "provider:aws:textract", |
| 78 | "live", |
| 79 | ], |
| 80 | }, |
| 81 | () => { |
| 82 | beforeAll( |
| 83 | Effect.gen(function* () { |
| 84 | yield* Effect.logInfo("Textract test setup: destroying previous resources"); |
| 85 | yield* sharedStack.destroy(); |
| 86 | |
| 87 | yield* Effect.logInfo("Textract test setup: deploying fixture"); |
| 88 | const { functionUrl } = yield* sharedStack.deploy( |
| 89 | Effect.gen(function* () { |
| 90 | return yield* TextractTestFunction; |
| 91 | }).pipe(Effect.provide(TextractTestFunctionLive)), |
| 92 | ); |
| 93 | |
| 94 | expect(functionUrl).toBeTruthy(); |
| 95 | baseUrl = functionUrl!.replace(/\/+$/, ""); |
| 96 | |
| 97 | yield* Effect.logInfo("Textract test setup: probing readiness"); |
| 98 | yield* HttpClient.get(`${baseUrl}/ping`).pipe( |
| 99 | Effect.flatMap((response) => |
| 100 | response.status === 200 |
| 101 | ? Effect.succeed(response) |
| 102 | : Effect.fail(new Error(`Function not ready: ${response.status}`)), |
| 103 | ), |
| 104 | Effect.retry({ schedule: readinessPolicy }), |
| 105 | ); |
| 106 | |
| 107 | // Seed the S3 input object the async Start* routes analyze. |
| 108 | const seeded = yield* postJson<{ seeded: boolean }>("/seed"); |
| 109 | expect(seeded.seeded).toBe(true); |
| 110 | }), |
| 111 | { timeout: 240_000 }, |
| 112 | ); |
| 113 | |
| 114 | afterAll.skipIf(!!process.env.NO_DESTROY)(sharedStack.destroy(), { timeout: 180_000 }); |
| 115 | |
| 116 | describe("synchronous analysis", () => { |
| 117 | test.provider( |
| 118 | "AnalyzeDocument detects the rendered text", |
| 119 | (_stack) => |
| 120 | Effect.gen(function* () { |
| 121 | const response = yield* getJson<{ pages?: number; lines: string[] }>( |
| 122 | "/analyze-document", |
| 123 | ); |
| 124 | expect(response.pages).toBe(1); |
| 125 | expect(response.lines).toContain(HELLO_PNG_TEXT); |
| 126 | }), |
| 127 | { timeout: 120_000 }, |
| 128 | ); |
| 129 | |
| 130 | test.provider( |
| 131 | "AnalyzeExpense analyzes the document as a receipt", |
| 132 | (_stack) => |
| 133 | Effect.gen(function* () { |
| 134 | const response = yield* getJson<{ pages?: number; expenseDocuments: number }>( |
| 135 | "/analyze-expense", |
| 136 | ); |
| 137 | expect(response.pages).toBe(1); |
| 138 | expect(response.expenseDocuments).toBeGreaterThanOrEqual(0); |
| 139 | }), |
| 140 | { timeout: 120_000 }, |
| 141 | ); |
| 142 | |
| 143 | test.provider( |
| 144 | "AnalyzeID analyzes the document as an identity document", |
| 145 | (_stack) => |
| 146 | Effect.gen(function* () { |
| 147 | const response = yield* getJson<{ identityDocuments: number }>("/analyze-id"); |
| 148 | expect(response.identityDocuments).toBeGreaterThanOrEqual(1); |
| 149 | }), |
| 150 | { timeout: 120_000 }, |
| 151 | ); |
| 152 | }); |
| 153 | |
| 154 | describe("asynchronous jobs", () => { |
| 155 | test.provider( |
| 156 | "StartDocumentTextDetection + GetDocumentTextDetection run to completion", |
| 157 | (_stack) => |
| 158 | Effect.gen(function* () { |
| 159 | const { jobId } = yield* postJson<{ jobId: string }>("/start-text-detection"); |
| 160 | expect(jobId).toBeTruthy(); |
| 161 | |
| 162 | const result = yield* pollJob<{ jobStatus?: string; lines: string[] }>( |
| 163 | "/get-text-detection", |
| 164 | jobId, |
| 165 | ); |
| 166 | expect(result.jobStatus).toBe("SUCCEEDED"); |
| 167 | expect(result.lines).toContain(HELLO_PNG_TEXT); |
| 168 | }), |
| 169 | { timeout: 180_000 }, |
| 170 | ); |
| 171 | |
| 172 | test.provider( |
| 173 | "analysis, expense, and lending jobs run to completion", |
| 174 | (_stack) => |
| 175 | Effect.gen(function* () { |
| 176 | const [analysis, expense, lending] = yield* Effect.all( |
| 177 | [ |
| 178 | postJson<{ jobId: string }>("/start-analysis"), |
| 179 | postJson<{ jobId: string }>("/start-expense"), |
| 180 | postJson<{ jobId: string }>("/start-lending"), |
| 181 | ], |
| 182 | { concurrency: 3 }, |
| 183 | ); |
| 184 | expect(analysis.jobId).toBeTruthy(); |
| 185 | expect(expense.jobId).toBeTruthy(); |
| 186 | expect(lending.jobId).toBeTruthy(); |
| 187 | |
| 188 | const [analysisResult, expenseResult, lendingResult] = yield* Effect.all( |
| 189 | [ |
| 190 | pollJob<{ jobStatus?: string; blocks: number }>("/get-analysis", analysis.jobId), |
| 191 | pollJob<{ jobStatus?: string; expenseDocuments: number }>( |
| 192 | "/get-expense", |
| 193 | expense.jobId, |
| 194 | ), |
| 195 | pollJob<{ jobStatus?: string; results: number }>("/get-lending", lending.jobId), |
| 196 | ], |
| 197 | { concurrency: 3 }, |
| 198 | ); |
| 199 | expect(analysisResult.jobStatus).toBe("SUCCEEDED"); |
| 200 | expect(analysisResult.blocks).toBeGreaterThan(0); |
| 201 | expect(expenseResult.jobStatus).toBe("SUCCEEDED"); |
| 202 | expect(lendingResult.jobStatus).toBe("SUCCEEDED"); |
| 203 | |
| 204 | // Lending summary is available once the job completed. |
| 205 | const summary = yield* getJson<{ jobStatus?: string; documentGroups: number }>( |
| 206 | `/get-lending-summary?jobId=${lending.jobId}`, |
| 207 | ); |
| 208 | expect(summary.jobStatus).toBe("SUCCEEDED"); |
| 209 | expect(summary.documentGroups).toBeGreaterThanOrEqual(0); |
| 210 | }), |
| 211 | { timeout: 240_000 }, |
| 212 | ); |
| 213 | }); |
| 214 | |
| 215 | describe("adapter management", () => { |
| 216 | test.provider( |
| 217 | "GetAdapter, ListAdapters, and ListAdapterVersions read the deployed adapter", |
| 218 | (_stack) => |
| 219 | Effect.gen(function* () { |
| 220 | const response = yield* getJson<{ |
| 221 | adapterName?: string; |
| 222 | featureTypes?: string[]; |
| 223 | listed: (string | undefined)[]; |
| 224 | versionsCount: number; |
| 225 | }>("/adapters"); |
| 226 | expect(response.adapterName).toBeTruthy(); |
| 227 | expect(response.featureTypes).toEqual(["QUERIES"]); |
| 228 | expect(response.listed).toContain(response.adapterName); |
| 229 | expect(response.versionsCount).toBe(0); |
| 230 | }), |
| 231 | { timeout: 120_000 }, |
| 232 | ); |
| 233 | |
| 234 | test.provider( |
| 235 | "adapter-version bindings surface typed errors (IAM grants proven)", |
| 236 | (_stack) => |
| 237 | Effect.gen(function* () { |
| 238 | const probes = yield* getJson<{ |
| 239 | getVersionProbe: string; |
| 240 | deleteVersionProbe: string; |
| 241 | createVersionProbe: string; |
| 242 | }>("/adapter-version-probes"); |
| 243 | expect(["ResourceNotFoundException", "ValidationException"]).toContain( |
| 244 | probes.getVersionProbe, |
| 245 | ); |
| 246 | // DeleteAdapterVersion is idempotent — deleting a nonexistent |
| 247 | // version succeeds (verified live), which proves the grant. |
| 248 | expect([ |
| 249 | "success", |
| 250 | "ResourceNotFoundException", |
| 251 | "ValidationException", |
| 252 | "ConflictException", |
| 253 | ]).toContain(probes.deleteVersionProbe); |
| 254 | expect([ |
| 255 | "InvalidS3ObjectException", |
| 256 | "InvalidParameterException", |
| 257 | "ValidationException", |
| 258 | "LimitExceededException", |
| 259 | ]).toContain(probes.createVersionProbe); |
| 260 | }), |
| 261 | { timeout: 120_000 }, |
| 262 | ); |
| 263 | }); |
| 264 | }, |
| 265 | ); |