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 sts from "@distilled.cloud/aws/sts"; |
| 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 ComprehendMedicalTestFunctionLive, { ComprehendMedicalTestFunction } from "./handler"; |
| 12 | |
| 13 | const testOptions = { providers: AWS.providers() }; |
| 14 | const { test, beforeAll, afterAll } = Test.make(testOptions); |
| 15 | const sharedStack = Core.scratchStack(testOptions, "ComprehendMedicalBindings"); |
| 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. Budget ~150s of |
| 19 | // readiness polling. |
| 20 | const readinessPolicy = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]); |
| 21 | |
| 22 | let baseUrl: 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 |
| 30 | // parallel load (cold re-init, IAM propagation, throttling surfaced as a |
| 31 | // defect). Retry 5xx only; a genuine 4xx/assertion failure surfaces |
| 32 | // 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("1 second"), Schedule.recurs(5)]), |
| 47 | }), |
| 48 | ); |
| 49 | |
| 50 | describe( |
| 51 | "ComprehendMedical Bindings", |
| 52 | { tags: ["provider:aws", "provider:aws:comprehendmedical", "provider:aws:lambda", "live"] }, |
| 53 | () => { |
| 54 | beforeAll( |
| 55 | Effect.gen(function* () { |
| 56 | yield* Effect.logInfo("ComprehendMedical test setup: destroying previous resources"); |
| 57 | yield* sharedStack.destroy(); |
| 58 | |
| 59 | yield* Effect.logInfo("ComprehendMedical test setup: deploying fixture"); |
| 60 | const { functionUrl } = yield* sharedStack.deploy( |
| 61 | Effect.gen(function* () { |
| 62 | return yield* ComprehendMedicalTestFunction; |
| 63 | }).pipe(Effect.provide(ComprehendMedicalTestFunctionLive)), |
| 64 | ); |
| 65 | |
| 66 | expect(functionUrl).toBeTruthy(); |
| 67 | baseUrl = functionUrl!.replace(/\/+$/, ""); |
| 68 | const readinessUrl = `${baseUrl}/ping`; |
| 69 | |
| 70 | yield* Effect.logInfo(`ComprehendMedical test setup: probing readiness at ${readinessUrl}`); |
| 71 | yield* HttpClient.get(readinessUrl).pipe( |
| 72 | Effect.flatMap((response) => |
| 73 | response.status === 200 |
| 74 | ? Effect.succeed(response) |
| 75 | : Effect.fail(new Error(`Function not ready: ${response.status}`)), |
| 76 | ), |
| 77 | Effect.tapError((error) => |
| 78 | Effect.logWarning( |
| 79 | `ComprehendMedical test setup: fixture not ready yet (${String(error)})`, |
| 80 | ), |
| 81 | ), |
| 82 | Effect.retry({ schedule: readinessPolicy }), |
| 83 | ); |
| 84 | }), |
| 85 | { timeout: 240_000 }, |
| 86 | ); |
| 87 | |
| 88 | afterAll(sharedStack.destroy(), { timeout: 120_000 }); |
| 89 | |
| 90 | describe("DetectEntitiesV2", () => { |
| 91 | test.provider( |
| 92 | "extracts medical entities from a clinical note", |
| 93 | (_stack) => |
| 94 | Effect.gen(function* () { |
| 95 | const response = (yield* send(HttpClientRequest.get(`${baseUrl}/entities`)).pipe( |
| 96 | Effect.flatMap((r) => r.json), |
| 97 | )) as { |
| 98 | entities: Array<{ text: string; category: string; type: string }>; |
| 99 | modelVersion: string; |
| 100 | }; |
| 101 | |
| 102 | expect(response.entities.length).toBeGreaterThan(0); |
| 103 | // atenolol is a medication; hypertension is a medical condition. |
| 104 | const categories = response.entities.map((e) => e.category); |
| 105 | expect(categories).toContain("MEDICATION"); |
| 106 | expect(categories).toContain("MEDICAL_CONDITION"); |
| 107 | expect(response.modelVersion.length).toBeGreaterThan(0); |
| 108 | }), |
| 109 | { timeout: 120_000 }, |
| 110 | ); |
| 111 | }); |
| 112 | |
| 113 | describe("DetectPHI", () => { |
| 114 | test.provider( |
| 115 | "detects protected health information", |
| 116 | (_stack) => |
| 117 | Effect.gen(function* () { |
| 118 | const response = (yield* send(HttpClientRequest.get(`${baseUrl}/phi`)).pipe( |
| 119 | Effect.flatMap((r) => r.json), |
| 120 | )) as { entities: Array<{ text: string; type: string }> }; |
| 121 | |
| 122 | expect(response.entities.length).toBeGreaterThan(0); |
| 123 | const types = response.entities.map((e) => e.type); |
| 124 | // The note contains a name, an age, and a date. |
| 125 | expect(types).toContain("NAME"); |
| 126 | }), |
| 127 | { timeout: 120_000 }, |
| 128 | ); |
| 129 | }); |
| 130 | |
| 131 | describe("InferICD10CM", () => { |
| 132 | test.provider( |
| 133 | "links conditions to ICD-10-CM codes", |
| 134 | (_stack) => |
| 135 | Effect.gen(function* () { |
| 136 | const response = (yield* send(HttpClientRequest.get(`${baseUrl}/icd10`)).pipe( |
| 137 | Effect.flatMap((r) => r.json), |
| 138 | )) as { |
| 139 | entities: Array<{ |
| 140 | text: string; |
| 141 | codes: Array<{ code: string; description: string }>; |
| 142 | }>; |
| 143 | }; |
| 144 | |
| 145 | expect(response.entities.length).toBeGreaterThan(0); |
| 146 | const withCodes = response.entities.filter((e) => e.codes.length > 0); |
| 147 | expect(withCodes.length).toBeGreaterThan(0); |
| 148 | // ICD-10-CM codes look like "E11.9", "I10", etc. |
| 149 | expect(withCodes[0].codes[0].code.length).toBeGreaterThan(0); |
| 150 | }), |
| 151 | { timeout: 120_000 }, |
| 152 | ); |
| 153 | }); |
| 154 | |
| 155 | describe("InferRxNorm", () => { |
| 156 | test.provider( |
| 157 | "links medications to RxNorm concepts", |
| 158 | (_stack) => |
| 159 | Effect.gen(function* () { |
| 160 | const response = (yield* send(HttpClientRequest.get(`${baseUrl}/rxnorm`)).pipe( |
| 161 | Effect.flatMap((r) => r.json), |
| 162 | )) as { |
| 163 | entities: Array<{ |
| 164 | text: string; |
| 165 | concepts: Array<{ code: string; description: string }>; |
| 166 | }>; |
| 167 | }; |
| 168 | |
| 169 | expect(response.entities.length).toBeGreaterThan(0); |
| 170 | const withConcepts = response.entities.filter((e) => e.concepts.length > 0); |
| 171 | expect(withConcepts.length).toBeGreaterThan(0); |
| 172 | expect(withConcepts[0].concepts[0].code.length).toBeGreaterThan(0); |
| 173 | }), |
| 174 | { timeout: 120_000 }, |
| 175 | ); |
| 176 | }); |
| 177 | |
| 178 | describe("InferSNOMEDCT", () => { |
| 179 | test.provider( |
| 180 | "links concepts to SNOMED CT codes", |
| 181 | (_stack) => |
| 182 | Effect.gen(function* () { |
| 183 | const response = (yield* send(HttpClientRequest.get(`${baseUrl}/snomed`)).pipe( |
| 184 | Effect.flatMap((r) => r.json), |
| 185 | )) as { |
| 186 | entities: Array<{ |
| 187 | text: string; |
| 188 | concepts: Array<{ code: string; description: string }>; |
| 189 | }>; |
| 190 | }; |
| 191 | |
| 192 | expect(response.entities.length).toBeGreaterThan(0); |
| 193 | const withConcepts = response.entities.filter((e) => e.concepts.length > 0); |
| 194 | expect(withConcepts.length).toBeGreaterThan(0); |
| 195 | expect(withConcepts[0].concepts[0].code.length).toBeGreaterThan(0); |
| 196 | }), |
| 197 | { timeout: 120_000 }, |
| 198 | ); |
| 199 | }); |
| 200 | |
| 201 | describe("List*Jobs", () => { |
| 202 | test.provider( |
| 203 | "lists batch jobs across all five job families", |
| 204 | (_stack) => |
| 205 | Effect.gen(function* () { |
| 206 | const response = (yield* send(HttpClientRequest.get(`${baseUrl}/jobs`)).pipe( |
| 207 | Effect.flatMap((r) => r.json), |
| 208 | )) as { |
| 209 | entities: number; |
| 210 | icd10cm: number; |
| 211 | phi: number; |
| 212 | rxnorm: number; |
| 213 | snomedct: number; |
| 214 | }; |
| 215 | |
| 216 | // Every list call authorized and answered — counts are >= 0. |
| 217 | expect(response.entities).toBeGreaterThanOrEqual(0); |
| 218 | expect(response.icd10cm).toBeGreaterThanOrEqual(0); |
| 219 | expect(response.phi).toBeGreaterThanOrEqual(0); |
| 220 | expect(response.rxnorm).toBeGreaterThanOrEqual(0); |
| 221 | expect(response.snomedct).toBeGreaterThanOrEqual(0); |
| 222 | }), |
| 223 | { timeout: 120_000 }, |
| 224 | ); |
| 225 | }); |
| 226 | |
| 227 | describe("Describe*/Stop*Job", () => { |
| 228 | test.provider( |
| 229 | "answers with the typed ResourceNotFoundException for unknown jobs", |
| 230 | (_stack) => |
| 231 | Effect.gen(function* () { |
| 232 | const response = (yield* send(HttpClientRequest.get(`${baseUrl}/job-checks`)).pipe( |
| 233 | Effect.flatMap((r) => r.json), |
| 234 | )) as { describes: string[]; stops: string[] }; |
| 235 | |
| 236 | // IAM authorizes before the job lookup, so the typed |
| 237 | // ResourceNotFoundException proves both wiring and permissions |
| 238 | // for every describe/stop binding. |
| 239 | expect(response.describes).toEqual(Array(5).fill("ResourceNotFoundException")); |
| 240 | expect(response.stops).toEqual(Array(5).fill("ResourceNotFoundException")); |
| 241 | }), |
| 242 | { timeout: 120_000 }, |
| 243 | ); |
| 244 | }); |
| 245 | |
| 246 | describe("StartPHIDetectionJob", () => { |
| 247 | test.provider( |
| 248 | "passes IAM authorization and fails typed on the bogus data-access role", |
| 249 | (_stack) => |
| 250 | Effect.gen(function* () { |
| 251 | // A same-account-but-nonexistent role is rejected AFTER the |
| 252 | // comprehendmedical:StartPHIDetectionJob authorization with the |
| 253 | // typed InvalidRequestException (DATA_ACCESS_ROLE_ARN_INVALID); a |
| 254 | // caller missing the Start permission gets AccessDeniedException |
| 255 | // instead — so this proves the binding granted the action. |
| 256 | const { Account } = yield* sts.getCallerIdentity({}); |
| 257 | const roleArn = `arn:aws:iam::${Account}:role/alchemy-nonexistent-comprehendmedical-role`; |
| 258 | |
| 259 | const response = (yield* send( |
| 260 | HttpClientRequest.get(`${baseUrl}/job-start?roleArn=${encodeURIComponent(roleArn)}`), |
| 261 | ).pipe(Effect.flatMap((r) => r.json))) as { tag: string }; |
| 262 | |
| 263 | expect(response.tag).toBe("InvalidRequestException"); |
| 264 | }), |
| 265 | { timeout: 120_000 }, |
| 266 | ); |
| 267 | }); |
| 268 | }, |
| 269 | ); |