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 { Region } from "@alchemy.run/aws/Region"; |
| 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 mi from "@distilled.cloud/aws/iot-managed-integrations"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as HttpClient from "effect/http/HttpClient"; |
| 9 | import * as Schedule from "effect/Schedule"; |
| 10 | import IoTMITestFunctionLive, { IoTMITestFunction } from "./bindings-handler"; |
| 11 | |
| 12 | const testOptions = { providers: AWS.providers() }; |
| 13 | const { test } = Test.make(testOptions); |
| 14 | |
| 15 | // IoT Managed Integrations is only deployed in a few regions (eu-west-1, |
| 16 | // ca-central-1, ...). The testing profile's default region (us-west-2) has no |
| 17 | // endpoint, so the ungated probes pin a supported region explicitly. The |
| 18 | // gated fixture below requires an AWS profile whose region IS a supported |
| 19 | // one, e.g. a profile with region = eu-west-1. |
| 20 | const MI_REGION = "eu-west-1"; |
| 21 | const pin = <A, E, R>(effect: Effect.Effect<A, E, R>) => |
| 22 | effect.pipe(Effect.provideService(Region, Effect.succeed(MI_REGION))); |
| 23 | |
| 24 | // --------------------------------------------------------------------------- |
| 25 | // Ungated typed-error probes: the operations the runtime bindings wrap are |
| 26 | // exercised directly through distilled and must answer with typed tags (or |
| 27 | // succeed, for the public schema catalog). These prove the distilled error |
| 28 | // unions and request serialization at near-zero cost on every CI pass; the |
| 29 | // full Lambda fixture is gated on a supported-region profile. |
| 30 | // --------------------------------------------------------------------------- |
| 31 | |
| 32 | describe( |
| 33 | "IoTManagedIntegrations binding operations (typed probes)", |
| 34 | { tags: ["provider:aws", "provider:aws:iotmanagedintegrations", "live"] }, |
| 35 | () => { |
| 36 | test.provider("getManagedThingState on a nonexistent thing fails with a typed tag", () => |
| 37 | Effect.gen(function* () { |
| 38 | const error = yield* Effect.flip( |
| 39 | pin(mi.getManagedThingState({ ManagedThingId: "alchemynonexistentthingprobe" })), |
| 40 | ); |
| 41 | expect(["ResourceNotFoundException", "ValidationException"]).toContain(error._tag); |
| 42 | }), |
| 43 | ); |
| 44 | |
| 45 | test.provider("getDeviceDiscovery on a nonexistent discovery fails with a typed tag", () => |
| 46 | Effect.gen(function* () { |
| 47 | const error = yield* Effect.flip( |
| 48 | pin(mi.getDeviceDiscovery({ Identifier: "alchemynonexistentdiscoveryprobe" })), |
| 49 | ); |
| 50 | expect(["ResourceNotFoundException", "ValidationException"]).toContain(error._tag); |
| 51 | }), |
| 52 | ); |
| 53 | |
| 54 | test.provider("sendConnectorEvent to a nonexistent connector fails with a typed tag", () => |
| 55 | Effect.gen(function* () { |
| 56 | const error = yield* Effect.flip( |
| 57 | pin( |
| 58 | mi.sendConnectorEvent({ |
| 59 | ConnectorId: "alchemynonexistentconnectorprobe", |
| 60 | Operation: "DEVICE_EVENT", |
| 61 | }), |
| 62 | ), |
| 63 | ); |
| 64 | expect(["ResourceNotFoundException", "ValidationException"]).toContain(error._tag); |
| 65 | }), |
| 66 | ); |
| 67 | |
| 68 | test.provider("listSchemaVersions reads the public capability schema catalog", () => |
| 69 | Effect.gen(function* () { |
| 70 | const result = yield* pin(mi.listSchemaVersions({ Type: "capability", MaxResults: 3 })); |
| 71 | expect(result.Items).toBeDefined(); |
| 72 | expect(result.Items!.length).toBeGreaterThan(0); |
| 73 | }), |
| 74 | ); |
| 75 | }, |
| 76 | ); |
| 77 | |
| 78 | // --------------------------------------------------------------------------- |
| 79 | // Full runtime fixture: a Lambda bound to all 14 runtime bindings against a |
| 80 | // live credential locker + (unprovisioned) managed thing. Gated behind |
| 81 | // AWS_TEST_IOT_MI=1 with an AWS profile configured in a supported region |
| 82 | // (same gate as the resource lifecycle tests). |
| 83 | // --------------------------------------------------------------------------- |
| 84 | |
| 85 | const sharedStack = Core.scratchStack(testOptions, "IoTMIBindings"); |
| 86 | |
| 87 | test.provider.skipIf(!process.env.AWS_TEST_IOT_MI)( |
| 88 | "runtime bindings against a live managed thing", |
| 89 | () => |
| 90 | Effect.gen(function* () { |
| 91 | yield* sharedStack.destroy(); |
| 92 | |
| 93 | yield* Effect.gen(function* () { |
| 94 | const { functionUrl } = yield* sharedStack.deploy( |
| 95 | Effect.gen(function* () { |
| 96 | return yield* IoTMITestFunction; |
| 97 | }).pipe(Effect.provide(IoTMITestFunctionLive)), |
| 98 | ); |
| 99 | expect(functionUrl).toBeTruthy(); |
| 100 | const baseUrl = functionUrl!.replace(/\/+$/, ""); |
| 101 | |
| 102 | const getJson = (path: string) => |
| 103 | HttpClient.get(`${baseUrl}${path}`).pipe( |
| 104 | Effect.flatMap((response) => |
| 105 | response.status >= 500 |
| 106 | ? Effect.fail(new Error(`transient upstream ${response.status}`)) |
| 107 | : Effect.succeed(response), |
| 108 | ), |
| 109 | Effect.retry({ |
| 110 | schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(10)]), |
| 111 | }), |
| 112 | Effect.flatMap((r) => r.json), |
| 113 | ); |
| 114 | |
| 115 | // All 15 capabilities initialized in the runtime. |
| 116 | const bindings = (yield* getJson("/bindings")) as { bound: string[] }; |
| 117 | expect(bindings.bound).toHaveLength(15); |
| 118 | |
| 119 | // Account-level ops must be authorized (Resource "*") and succeed. |
| 120 | const schemas = (yield* getJson("/schema-versions")) as { |
| 121 | count?: number; |
| 122 | errorTag?: string; |
| 123 | }; |
| 124 | expect(schemas.errorTag).toBeUndefined(); |
| 125 | expect(schemas.count).toBeGreaterThan(0); |
| 126 | |
| 127 | // Thing-scoped reads must be authorized on the thing ARN. Metadata |
| 128 | // and capabilities succeed even for an unprovisioned device. |
| 129 | const metadata = (yield* getJson("/metadata")) as { |
| 130 | managedThingId?: string; |
| 131 | errorTag?: string; |
| 132 | }; |
| 133 | expect(metadata.errorTag).toBeUndefined(); |
| 134 | expect(metadata.managedThingId).toBeTruthy(); |
| 135 | |
| 136 | const capabilities = (yield* getJson("/capabilities")) as { errorTag?: string }; |
| 137 | expect(capabilities.errorTag).toBeUndefined(); |
| 138 | |
| 139 | // The remaining routes exercise ops that cannot fully succeed |
| 140 | // against an unprovisioned device / absent connector: each must |
| 141 | // answer with data or a TYPED tag that is not an IAM denial — |
| 142 | // proving the wiring, identifier injection, and grants. |
| 143 | for (const path of [ |
| 144 | "/state", |
| 145 | "/certificate", |
| 146 | "/connectivity", |
| 147 | "/thing-schemas", |
| 148 | "/command", |
| 149 | "/discovery", |
| 150 | "/discoveries", |
| 151 | "/get-discovery", |
| 152 | "/discovered", |
| 153 | "/connector-event", |
| 154 | "/custom-endpoint", |
| 155 | ]) { |
| 156 | const result = (yield* getJson(path)) as { errorTag?: string }; |
| 157 | expect(result.errorTag).not.toBe("AccessDeniedException"); |
| 158 | } |
| 159 | }).pipe(Effect.ensuring(sharedStack.destroy().pipe(Effect.orDie))); |
| 160 | }), |
| 161 | { |
| 162 | tags: ["provider:aws", "provider:aws:iotmanagedintegrations", "provider:aws:lambda", "live"], |
| 163 | timeout: 600_000, |
| 164 | }, |
| 165 | ); |