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 { AWSEnvironment } from "@alchemy.run/aws/Environment"; |
| 3 | import { describe, expect } from "@alchemy.run/test"; |
| 4 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 5 | import * as Core from "@alchemy.run/test/Core"; |
| 6 | import * as medicalimaging from "@distilled.cloud/aws/medical-imaging"; |
| 7 | import * as s3 from "@distilled.cloud/aws/s3"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import * as HttpClient from "effect/http/HttpClient"; |
| 10 | import * as Schedule from "effect/Schedule"; |
| 11 | import MedicalImagingTestFunctionLive, { |
| 12 | IMPORT_PREFIX, |
| 13 | MedicalImagingTestFunction, |
| 14 | } from "./handler"; |
| 15 | |
| 16 | const testOptions = { providers: AWS.providers() }; |
| 17 | const { test } = Test.make(testOptions); |
| 18 | |
| 19 | // Well-formed-but-nonexistent ids (32 hex chars) the ungated probes are |
| 20 | // driven against. |
| 21 | const NONEXISTENT_DATASTORE = "0123456789abcdef0123456789abcdef"; |
| 22 | const NONEXISTENT_IMAGE_SET = "0123456789abcdef0123456789abcdef"; |
| 23 | const NONEXISTENT_JOB = "0123456789abcdef0123456789abcdef"; |
| 24 | |
| 25 | // --------------------------------------------------------------------------- |
| 26 | // Ungated typed-error probes: every data-plane operation the eleven bindings |
| 27 | // wrap is exercised directly through distilled against a nonexistent data |
| 28 | // store, and must answer with its typed tag. Import-job routes consistently |
| 29 | // answer ResourceNotFoundException; image-set routes flip-flop between |
| 30 | // AccessDeniedException and ResourceNotFoundException (observed answering |
| 31 | // each within hours of the other in mid-2026), so those probes accept |
| 32 | // either typed tag. These prove the distilled error unions (and request |
| 33 | // serialization) at near-zero cost on every CI pass, while the full runtime |
| 34 | // fixture below is gated behind the multi-minute data store provisioning. |
| 35 | // --------------------------------------------------------------------------- |
| 36 | |
| 37 | // AWS answers image-set routes on a nonexistent datastore with either tag |
| 38 | // depending on which backend handles the request; both are typed. |
| 39 | const expectNotFoundOrDenied = (error: { _tag: string }) => |
| 40 | expect(["ResourceNotFoundException", "AccessDeniedException"]).toContain(error._tag); |
| 41 | |
| 42 | describe( |
| 43 | "MedicalImaging data-plane operations (typed-error probes)", |
| 44 | { tags: ["provider:aws", "provider:aws:medicalimaging", "live"] }, |
| 45 | () => { |
| 46 | test.provider( |
| 47 | "getImageSet on a nonexistent datastore fails with a typed not-found/denied tag", |
| 48 | () => |
| 49 | Effect.gen(function* () { |
| 50 | const error = yield* Effect.flip( |
| 51 | medicalimaging.getImageSet({ |
| 52 | datastoreId: NONEXISTENT_DATASTORE, |
| 53 | imageSetId: NONEXISTENT_IMAGE_SET, |
| 54 | }), |
| 55 | ); |
| 56 | expectNotFoundOrDenied(error); |
| 57 | }), |
| 58 | ); |
| 59 | |
| 60 | test.provider( |
| 61 | "getImageSetMetadata on a nonexistent datastore fails with a typed not-found/denied tag", |
| 62 | () => |
| 63 | Effect.gen(function* () { |
| 64 | const error = yield* Effect.flip( |
| 65 | medicalimaging.getImageSetMetadata({ |
| 66 | datastoreId: NONEXISTENT_DATASTORE, |
| 67 | imageSetId: NONEXISTENT_IMAGE_SET, |
| 68 | }), |
| 69 | ); |
| 70 | expectNotFoundOrDenied(error); |
| 71 | }), |
| 72 | ); |
| 73 | |
| 74 | test.provider( |
| 75 | "getImageFrame on a nonexistent datastore fails with a typed not-found/denied tag", |
| 76 | () => |
| 77 | Effect.gen(function* () { |
| 78 | const error = yield* Effect.flip( |
| 79 | medicalimaging.getImageFrame({ |
| 80 | datastoreId: NONEXISTENT_DATASTORE, |
| 81 | imageSetId: NONEXISTENT_IMAGE_SET, |
| 82 | imageFrameInformation: { imageFrameId: NONEXISTENT_IMAGE_SET }, |
| 83 | }), |
| 84 | ); |
| 85 | expectNotFoundOrDenied(error); |
| 86 | }), |
| 87 | ); |
| 88 | |
| 89 | test.provider( |
| 90 | "searchImageSets on a nonexistent datastore fails with a typed not-found/denied tag", |
| 91 | () => |
| 92 | Effect.gen(function* () { |
| 93 | const error = yield* Effect.flip( |
| 94 | medicalimaging.searchImageSets({ datastoreId: NONEXISTENT_DATASTORE }), |
| 95 | ); |
| 96 | expectNotFoundOrDenied(error); |
| 97 | }), |
| 98 | ); |
| 99 | |
| 100 | test.provider( |
| 101 | "listImageSetVersions on a nonexistent datastore fails with a typed not-found/denied tag", |
| 102 | () => |
| 103 | Effect.gen(function* () { |
| 104 | const error = yield* Effect.flip( |
| 105 | medicalimaging.listImageSetVersions({ |
| 106 | datastoreId: NONEXISTENT_DATASTORE, |
| 107 | imageSetId: NONEXISTENT_IMAGE_SET, |
| 108 | }), |
| 109 | ); |
| 110 | expectNotFoundOrDenied(error); |
| 111 | }), |
| 112 | ); |
| 113 | |
| 114 | test.provider( |
| 115 | "updateImageSetMetadata on a nonexistent datastore fails with a typed not-found/denied tag", |
| 116 | () => |
| 117 | Effect.gen(function* () { |
| 118 | const error = yield* Effect.flip( |
| 119 | medicalimaging.updateImageSetMetadata({ |
| 120 | datastoreId: NONEXISTENT_DATASTORE, |
| 121 | imageSetId: NONEXISTENT_IMAGE_SET, |
| 122 | latestVersionId: "1", |
| 123 | updateImageSetMetadataUpdates: { revertToVersionId: "1" }, |
| 124 | }), |
| 125 | ); |
| 126 | expectNotFoundOrDenied(error); |
| 127 | }), |
| 128 | ); |
| 129 | |
| 130 | test.provider( |
| 131 | "copyImageSet on a nonexistent datastore fails with a typed not-found/denied tag", |
| 132 | () => |
| 133 | Effect.gen(function* () { |
| 134 | const error = yield* Effect.flip( |
| 135 | medicalimaging.copyImageSet({ |
| 136 | datastoreId: NONEXISTENT_DATASTORE, |
| 137 | sourceImageSetId: NONEXISTENT_IMAGE_SET, |
| 138 | copyImageSetInformation: { sourceImageSet: { latestVersionId: "1" } }, |
| 139 | }), |
| 140 | ); |
| 141 | expectNotFoundOrDenied(error); |
| 142 | }), |
| 143 | ); |
| 144 | |
| 145 | test.provider( |
| 146 | "deleteImageSet on a nonexistent datastore fails with a typed not-found/denied tag", |
| 147 | () => |
| 148 | Effect.gen(function* () { |
| 149 | const error = yield* Effect.flip( |
| 150 | medicalimaging.deleteImageSet({ |
| 151 | datastoreId: NONEXISTENT_DATASTORE, |
| 152 | imageSetId: NONEXISTENT_IMAGE_SET, |
| 153 | }), |
| 154 | ); |
| 155 | expectNotFoundOrDenied(error); |
| 156 | }), |
| 157 | ); |
| 158 | |
| 159 | test.provider( |
| 160 | "getDICOMImportJob on a nonexistent datastore fails with ResourceNotFoundException", |
| 161 | () => |
| 162 | Effect.gen(function* () { |
| 163 | const error = yield* Effect.flip( |
| 164 | medicalimaging.getDICOMImportJob({ |
| 165 | datastoreId: NONEXISTENT_DATASTORE, |
| 166 | jobId: NONEXISTENT_JOB, |
| 167 | }), |
| 168 | ); |
| 169 | expect(error._tag).toBe("ResourceNotFoundException"); |
| 170 | }), |
| 171 | ); |
| 172 | |
| 173 | test.provider( |
| 174 | "listDICOMImportJobs on a nonexistent datastore fails with ResourceNotFoundException", |
| 175 | () => |
| 176 | Effect.gen(function* () { |
| 177 | const error = yield* Effect.flip( |
| 178 | medicalimaging.listDICOMImportJobs({ datastoreId: NONEXISTENT_DATASTORE }), |
| 179 | ); |
| 180 | expect(error._tag).toBe("ResourceNotFoundException"); |
| 181 | }), |
| 182 | ); |
| 183 | |
| 184 | test.provider("startDICOMImportJob on a nonexistent datastore fails with a typed tag", () => |
| 185 | Effect.gen(function* () { |
| 186 | const { accountId } = yield* AWSEnvironment.current; |
| 187 | const error = yield* Effect.flip( |
| 188 | medicalimaging.startDICOMImportJob({ |
| 189 | datastoreId: NONEXISTENT_DATASTORE, |
| 190 | clientToken: "alchemy-medicalimaging-probe", |
| 191 | dataAccessRoleArn: `arn:aws:iam::${accountId}:role/alchemy-probe-nonexistent`, |
| 192 | inputS3Uri: "s3://alchemy-probe-nonexistent/in/", |
| 193 | outputS3Uri: "s3://alchemy-probe-nonexistent/out/", |
| 194 | }), |
| 195 | ); |
| 196 | expect([ |
| 197 | "ResourceNotFoundException", |
| 198 | "ValidationException", |
| 199 | "AccessDeniedException", |
| 200 | ]).toContain(error._tag); |
| 201 | }), |
| 202 | ); |
| 203 | }, |
| 204 | ); |
| 205 | |
| 206 | // --------------------------------------------------------------------------- |
| 207 | // Full runtime fixture: a Lambda bound to all eleven data-plane bindings |
| 208 | // against a live HealthImaging data store. A data store takes a few minutes |
| 209 | // to provision (and must be empty to delete), so this is gated behind |
| 210 | // AWS_TEST_MEDICAL_IMAGING=1 and always destroys what it created. Running it |
| 211 | // alongside the (default-on) Datastore lifecycle test has been observed to |
| 212 | // fail createDatastore with an untyped `UnknownAwsError: Too Many Requests` |
| 213 | // (2026-10-09, not reproducible with 4 concurrent CLI creates) — the wire |
| 214 | // error code is unknown, so it is not yet patched into distilled. |
| 215 | // --------------------------------------------------------------------------- |
| 216 | |
| 217 | const sharedStack = Core.scratchStack(testOptions, "MedicalImagingBindings"); |
| 218 | |
| 219 | test.provider.skipIf(!process.env.AWS_TEST_MEDICAL_IMAGING)( |
| 220 | "image set + import job bindings against a live data store", |
| 221 | () => |
| 222 | Effect.gen(function* () { |
| 223 | yield* sharedStack.destroy(); |
| 224 | |
| 225 | yield* Effect.gen(function* () { |
| 226 | const { functionUrl } = yield* sharedStack.deploy( |
| 227 | Effect.gen(function* () { |
| 228 | return yield* MedicalImagingTestFunction; |
| 229 | }).pipe(Effect.provide(MedicalImagingTestFunctionLive)), |
| 230 | ); |
| 231 | expect(functionUrl).toBeTruthy(); |
| 232 | const baseUrl = functionUrl!.replace(/\/+$/, ""); |
| 233 | |
| 234 | const getJson = (path: string) => |
| 235 | HttpClient.get(`${baseUrl}${path}`).pipe( |
| 236 | Effect.flatMap((response) => |
| 237 | response.status >= 500 |
| 238 | ? Effect.fail(new Error(`transient upstream ${response.status}`)) |
| 239 | : Effect.succeed(response), |
| 240 | ), |
| 241 | Effect.retry({ |
| 242 | schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(10)]), |
| 243 | }), |
| 244 | Effect.flatMap((r) => r.json), |
| 245 | ); |
| 246 | |
| 247 | // All eleven capabilities initialized in the runtime. |
| 248 | const bindings = (yield* getJson("/bindings")) as { bound: string[] }; |
| 249 | expect(bindings.bound).toHaveLength(11); |
| 250 | |
| 251 | // Upload a (deliberately invalid) DICOM input out-of-band — the |
| 252 | // import job still submits and runs; per-object failures land in |
| 253 | // the output manifests. |
| 254 | const config = (yield* getJson("/config")) as { bucketName: string }; |
| 255 | yield* s3.putObject({ |
| 256 | Bucket: config.bucketName, |
| 257 | Key: `${IMPORT_PREFIX}not-a-dicom.dcm`, |
| 258 | Body: new TextEncoder().encode("alchemy medical-imaging fixture"), |
| 259 | ContentType: "application/dicom", |
| 260 | }); |
| 261 | |
| 262 | // StartDICOMImportJob — proves datastoreId + dataAccessRoleArn |
| 263 | // injection and the iam:PassRole grant. |
| 264 | const importJob = (yield* getJson("/import")) as { |
| 265 | jobId?: string; |
| 266 | status?: string; |
| 267 | errorTag?: string; |
| 268 | }; |
| 269 | expect(importJob.errorTag).toBeUndefined(); |
| 270 | expect(importJob.jobId).toBeTruthy(); |
| 271 | expect(importJob.status).toBe("SUBMITTED"); |
| 272 | |
| 273 | // GetDICOMImportJob — the job is observable immediately. |
| 274 | const described = (yield* getJson(`/describe-import?jobId=${importJob.jobId}`)) as { |
| 275 | status?: string; |
| 276 | errorTag?: string; |
| 277 | }; |
| 278 | expect(described.errorTag).toBeUndefined(); |
| 279 | expect(described.status).toBeTruthy(); |
| 280 | |
| 281 | // ListDICOMImportJobs sees it. |
| 282 | const imports = (yield* getJson("/list-imports")) as { count: number }; |
| 283 | expect(imports.count).toBeGreaterThanOrEqual(1); |
| 284 | |
| 285 | // SearchImageSets — the store has no image sets (the import input |
| 286 | // is invalid), so an empty result proves the grant + wiring. |
| 287 | const search = (yield* getJson("/search")) as { count?: number; errorTag?: string }; |
| 288 | expect(search.errorTag).toBeUndefined(); |
| 289 | expect(search.count).toBe(0); |
| 290 | |
| 291 | // Image-set-scoped bindings against a nonexistent image set: the |
| 292 | // typed not-found tag (not AccessDeniedException) proves the IAM |
| 293 | // grant on `{datastoreArn}/imageset/*` and the id injection. |
| 294 | for (const route of [ |
| 295 | "/get-image-set", |
| 296 | "/get-metadata", |
| 297 | "/get-frame", |
| 298 | "/versions", |
| 299 | "/update-metadata", |
| 300 | "/copy", |
| 301 | "/delete-image-set", |
| 302 | ]) { |
| 303 | const result = (yield* getJson(route)) as { errorTag?: string }; |
| 304 | expect(result.errorTag).toBe("ResourceNotFoundException"); |
| 305 | } |
| 306 | |
| 307 | // Wait (bounded) for the import job to finish so the data store is |
| 308 | // quiescent (and guaranteed image-set-free) before destroy. |
| 309 | yield* getJson(`/describe-import?jobId=${importJob.jobId}`).pipe( |
| 310 | Effect.map((r) => (r as { status: string }).status), |
| 311 | Effect.repeat({ |
| 312 | schedule: Schedule.spaced("10 seconds"), |
| 313 | until: (status): boolean => status !== "SUBMITTED" && status !== "IN_PROGRESS", |
| 314 | times: 30, |
| 315 | }), |
| 316 | ); |
| 317 | }).pipe(Effect.ensuring(sharedStack.destroy().pipe(Effect.orDie))); |
| 318 | }), |
| 319 | // data store create (a few minutes) + import job + async delete, one test. |
| 320 | { |
| 321 | tags: [ |
| 322 | "provider:aws", |
| 323 | "provider:aws:iam", |
| 324 | "provider:aws:lambda", |
| 325 | "provider:aws:medicalimaging", |
| 326 | "provider:aws:s3", |
| 327 | "live", |
| 328 | ], |
| 329 | timeout: 1_800_000, |
| 330 | }, |
| 331 | ); |