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 { Role } from "@alchemy.run/aws/IAM/Role"; |
| 3 | import { |
| 4 | Destination, |
| 5 | DeviceProfile, |
| 6 | ServiceProfile, |
| 7 | WirelessDevice, |
| 8 | WirelessGateway, |
| 9 | } from "@alchemy.run/aws/IoTWireless"; |
| 10 | import { expect } from "@alchemy.run/test"; |
| 11 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 12 | import * as iotw from "@distilled.cloud/aws/iot-wireless"; |
| 13 | import * as Effect from "effect/Effect"; |
| 14 | import * as Redacted from "effect/Redacted"; |
| 15 | |
| 16 | const { test } = Test.make({ providers: AWS.providers() }); |
| 17 | |
| 18 | // Ungated typed-error probe: prove the distilled error union carries the |
| 19 | // not-found tag every IoTWireless provider's read/delete path depends on. |
| 20 | // Runs in every CI pass at near-zero cost. |
| 21 | test.provider( |
| 22 | "getServiceProfile on a bogus id fails with ResourceNotFoundException", |
| 23 | () => |
| 24 | Effect.gen(function* () { |
| 25 | const error = yield* Effect.flip( |
| 26 | iotw.getServiceProfile({ Id: "00000000-0000-0000-0000-000000000000" }), |
| 27 | ); |
| 28 | expect(error._tag).toBe("ResourceNotFoundException"); |
| 29 | }), |
| 30 | { tags: ["provider:aws", "provider:aws:iotwireless", "live"] }, |
| 31 | ); |
| 32 | |
| 33 | // The IAM role IoT Wireless assumes to deliver uplinks to the rule/topic. |
| 34 | const deliveryRole = Role("IotWirelessDelivery", { |
| 35 | assumeRolePolicyDocument: { |
| 36 | Version: "2012-10-17", |
| 37 | Statement: [ |
| 38 | { |
| 39 | Effect: "Allow", |
| 40 | Principal: { Service: "iotwireless.amazonaws.com" }, |
| 41 | Action: ["sts:AssumeRole"], |
| 42 | }, |
| 43 | ], |
| 44 | }, |
| 45 | inlinePolicies: { |
| 46 | deliver: { |
| 47 | Version: "2012-10-17", |
| 48 | Statement: [ |
| 49 | { Effect: "Allow", Action: ["iot:DescribeEndpoint", "iot:Publish"], Resource: ["*"] }, |
| 50 | ], |
| 51 | }, |
| 52 | }, |
| 53 | }); |
| 54 | |
| 55 | // Service profiles, device profiles, and destinations are free, provision |
| 56 | // instantly, and need no radio hardware — full lifecycle runs ungated. |
| 57 | test.provider( |
| 58 | "lifecycle: service profile + device profile + destination", |
| 59 | (stack) => |
| 60 | Effect.gen(function* () { |
| 61 | yield* stack.destroy(); |
| 62 | |
| 63 | // Create. |
| 64 | const deployed = yield* stack.deploy( |
| 65 | Effect.gen(function* () { |
| 66 | const role = yield* deliveryRole; |
| 67 | const serviceProfile = yield* ServiceProfile("Fleet", { |
| 68 | loRaWAN: { AddGwMetadata: true }, |
| 69 | tags: { fixture: "iot-wireless" }, |
| 70 | }); |
| 71 | const deviceProfile = yield* DeviceProfile("SensorModel", { |
| 72 | loRaWAN: { |
| 73 | MacVersion: "1.0.3", |
| 74 | RegParamsRevision: "RP002-1.0.1", |
| 75 | RfRegion: "US915", |
| 76 | MaxEirp: 10, |
| 77 | SupportsJoin: true, |
| 78 | }, |
| 79 | tags: { fixture: "iot-wireless" }, |
| 80 | }); |
| 81 | const destination = yield* Destination("Uplinks", { |
| 82 | expressionType: "RuleName", |
| 83 | expression: "alchemy_iot_wireless_test_rule", |
| 84 | description: "alchemy iot-wireless test destination", |
| 85 | roleArn: role.roleArn, |
| 86 | tags: { fixture: "iot-wireless" }, |
| 87 | }); |
| 88 | return { serviceProfile, deviceProfile, destination }; |
| 89 | }), |
| 90 | ); |
| 91 | |
| 92 | expect(deployed.serviceProfile.serviceProfileId).toBeDefined(); |
| 93 | expect(deployed.serviceProfile.serviceProfileArn).toContain("iotwireless"); |
| 94 | expect(deployed.deviceProfile.deviceProfileId).toBeDefined(); |
| 95 | expect(deployed.destination.destinationArn).toContain("iotwireless"); |
| 96 | expect(deployed.destination.expression).toBe("alchemy_iot_wireless_test_rule"); |
| 97 | |
| 98 | // Out-of-band verification via distilled. |
| 99 | const liveServiceProfile = yield* iotw.getServiceProfile({ |
| 100 | Id: deployed.serviceProfile.serviceProfileId, |
| 101 | }); |
| 102 | expect(liveServiceProfile.Name).toBe(deployed.serviceProfile.serviceProfileName); |
| 103 | expect(liveServiceProfile.LoRaWAN?.AddGwMetadata).toBe(true); |
| 104 | |
| 105 | const liveDeviceProfile = yield* iotw.getDeviceProfile({ |
| 106 | Id: deployed.deviceProfile.deviceProfileId, |
| 107 | }); |
| 108 | expect(liveDeviceProfile.LoRaWAN?.RfRegion).toBe("US915"); |
| 109 | expect(liveDeviceProfile.LoRaWAN?.MacVersion).toBe("1.0.3"); |
| 110 | |
| 111 | const liveDestination = yield* iotw.getDestination({ |
| 112 | Name: deployed.destination.destinationName, |
| 113 | }); |
| 114 | expect(liveDestination.ExpressionType).toBe("RuleName"); |
| 115 | expect(liveDestination.Expression).toBe("alchemy_iot_wireless_test_rule"); |
| 116 | |
| 117 | const destinationTags = yield* iotw.listTagsForResource({ |
| 118 | ResourceArn: deployed.destination.destinationArn, |
| 119 | }); |
| 120 | expect( |
| 121 | destinationTags.Tags?.some((t) => t.Key === "alchemy::id" && t.Value === "Uplinks"), |
| 122 | ).toBe(true); |
| 123 | expect( |
| 124 | destinationTags.Tags?.some((t) => t.Key === "fixture" && t.Value === "iot-wireless"), |
| 125 | ).toBe(true); |
| 126 | |
| 127 | // Update — the destination's expression and the service profile's |
| 128 | // tags change in place; the service profile's LoRaWAN config is |
| 129 | // immutable so changing it REPLACES the profile (new id). |
| 130 | const updated = yield* stack.deploy( |
| 131 | Effect.gen(function* () { |
| 132 | const role = yield* deliveryRole; |
| 133 | const serviceProfile = yield* ServiceProfile("Fleet", { |
| 134 | loRaWAN: { AddGwMetadata: false }, |
| 135 | tags: { fixture: "iot-wireless", env: "test" }, |
| 136 | }); |
| 137 | const deviceProfile = yield* DeviceProfile("SensorModel", { |
| 138 | loRaWAN: { |
| 139 | MacVersion: "1.0.3", |
| 140 | RegParamsRevision: "RP002-1.0.1", |
| 141 | RfRegion: "US915", |
| 142 | MaxEirp: 10, |
| 143 | SupportsJoin: true, |
| 144 | }, |
| 145 | tags: { fixture: "iot-wireless" }, |
| 146 | }); |
| 147 | const destination = yield* Destination("Uplinks", { |
| 148 | expressionType: "RuleName", |
| 149 | expression: "alchemy_iot_wireless_test_rule_v2", |
| 150 | description: "alchemy iot-wireless test destination", |
| 151 | roleArn: role.roleArn, |
| 152 | tags: { fixture: "iot-wireless" }, |
| 153 | }); |
| 154 | return { serviceProfile, deviceProfile, destination }; |
| 155 | }), |
| 156 | ); |
| 157 | |
| 158 | // Destination updated in place (same identity). |
| 159 | expect(updated.destination.destinationName).toBe(deployed.destination.destinationName); |
| 160 | const updatedDestination = yield* iotw.getDestination({ |
| 161 | Name: updated.destination.destinationName, |
| 162 | }); |
| 163 | expect(updatedDestination.Expression).toBe("alchemy_iot_wireless_test_rule_v2"); |
| 164 | |
| 165 | // Device profile untouched (same id). |
| 166 | expect(updated.deviceProfile.deviceProfileId).toBe(deployed.deviceProfile.deviceProfileId); |
| 167 | |
| 168 | // Service profile replaced (new id) and the old one deleted. |
| 169 | expect(updated.serviceProfile.serviceProfileId).not.toBe( |
| 170 | deployed.serviceProfile.serviceProfileId, |
| 171 | ); |
| 172 | const replacedServiceProfile = yield* iotw.getServiceProfile({ |
| 173 | Id: updated.serviceProfile.serviceProfileId, |
| 174 | }); |
| 175 | expect(replacedServiceProfile.LoRaWAN?.AddGwMetadata).toBe(false); |
| 176 | const oldServiceProfileError = yield* Effect.flip( |
| 177 | iotw.getServiceProfile({ Id: deployed.serviceProfile.serviceProfileId }), |
| 178 | ); |
| 179 | expect(oldServiceProfileError._tag).toBe("ResourceNotFoundException"); |
| 180 | const serviceProfileTags = yield* iotw.listTagsForResource({ |
| 181 | ResourceArn: updated.serviceProfile.serviceProfileArn, |
| 182 | }); |
| 183 | expect(serviceProfileTags.Tags?.some((t) => t.Key === "env" && t.Value === "test")).toBe( |
| 184 | true, |
| 185 | ); |
| 186 | |
| 187 | // Destroy — everything gone, typed. |
| 188 | yield* stack.destroy(); |
| 189 | const destinationError = yield* Effect.flip( |
| 190 | iotw.getDestination({ Name: updated.destination.destinationName }), |
| 191 | ); |
| 192 | expect(destinationError._tag).toBe("ResourceNotFoundException"); |
| 193 | const serviceProfileError = yield* Effect.flip( |
| 194 | iotw.getServiceProfile({ Id: updated.serviceProfile.serviceProfileId }), |
| 195 | ); |
| 196 | expect(serviceProfileError._tag).toBe("ResourceNotFoundException"); |
| 197 | const deviceProfileError = yield* Effect.flip( |
| 198 | iotw.getDeviceProfile({ Id: updated.deviceProfile.deviceProfileId }), |
| 199 | ); |
| 200 | expect(deviceProfileError._tag).toBe("ResourceNotFoundException"); |
| 201 | }), |
| 202 | { |
| 203 | tags: ["provider:aws", "provider:aws:iam", "provider:aws:iotwireless", "live"], |
| 204 | timeout: 240_000, |
| 205 | }, |
| 206 | ); |
| 207 | |
| 208 | // Deterministic fabricated radio identities — devices and gateways can be |
| 209 | // registered without hardware (they just never join), but accounts shared |
| 210 | // with real LoRaWAN fleets should not accumulate phantom radios — such an |
| 211 | // account opts out with AWS_TEST_IOT_WIRELESS=0. The test destroys both. |
| 212 | const DEV_EUI = "1a2b3c4d5e6f7081"; |
| 213 | const APP_EUI = "0000000000000001"; |
| 214 | const APP_KEY = Redacted.make("000102030405060708090a0b0c0d0e0f"); |
| 215 | const GATEWAY_EUI = "aa555a0000000101"; |
| 216 | |
| 217 | test.provider.skipIf(process.env.AWS_TEST_IOT_WIRELESS === "0")( |
| 218 | "lifecycle: wireless device + wireless gateway", |
| 219 | (stack) => |
| 220 | Effect.gen(function* () { |
| 221 | yield* stack.destroy(); |
| 222 | |
| 223 | // Create — a full LoRaWAN registration: destination + profiles + |
| 224 | // OTAA v1.0.x device + gateway. |
| 225 | const deployed = yield* stack.deploy( |
| 226 | Effect.gen(function* () { |
| 227 | const role = yield* deliveryRole; |
| 228 | const destination = yield* Destination("DeviceUplinks", { |
| 229 | expressionType: "RuleName", |
| 230 | expression: "alchemy_iot_wireless_device_rule", |
| 231 | roleArn: role.roleArn, |
| 232 | tags: { fixture: "iot-wireless" }, |
| 233 | }); |
| 234 | const serviceProfile = yield* ServiceProfile("DeviceFleet", { |
| 235 | loRaWAN: { AddGwMetadata: true }, |
| 236 | }); |
| 237 | const deviceProfile = yield* DeviceProfile("DeviceModel", { |
| 238 | loRaWAN: { |
| 239 | MacVersion: "1.0.3", |
| 240 | RegParamsRevision: "RP002-1.0.1", |
| 241 | RfRegion: "US915", |
| 242 | MaxEirp: 10, |
| 243 | SupportsJoin: true, |
| 244 | }, |
| 245 | }); |
| 246 | const device = yield* WirelessDevice("Sensor", { |
| 247 | type: "LoRaWAN", |
| 248 | destinationName: destination.destinationName, |
| 249 | description: "alchemy test sensor", |
| 250 | loRaWAN: { |
| 251 | DevEui: DEV_EUI, |
| 252 | DeviceProfileId: deviceProfile.deviceProfileId, |
| 253 | ServiceProfileId: serviceProfile.serviceProfileId, |
| 254 | OtaaV1_0_x: { AppKey: APP_KEY, AppEui: APP_EUI }, |
| 255 | }, |
| 256 | tags: { fixture: "iot-wireless" }, |
| 257 | }); |
| 258 | const gateway = yield* WirelessGateway("RooftopGw", { |
| 259 | description: "alchemy test gateway", |
| 260 | loRaWAN: { GatewayEui: GATEWAY_EUI, RfRegion: "US915" }, |
| 261 | tags: { fixture: "iot-wireless" }, |
| 262 | }); |
| 263 | return { destination, serviceProfile, deviceProfile, device, gateway }; |
| 264 | }), |
| 265 | ); |
| 266 | |
| 267 | expect(deployed.device.wirelessDeviceId).toBeDefined(); |
| 268 | expect(deployed.device.type).toBe("LoRaWAN"); |
| 269 | expect(deployed.device.destinationName).toBe(deployed.destination.destinationName); |
| 270 | expect(deployed.gateway.wirelessGatewayId).toBeDefined(); |
| 271 | expect(deployed.gateway.gatewayEui).toBe(GATEWAY_EUI); |
| 272 | |
| 273 | // Out-of-band verification via distilled. |
| 274 | const liveDevice = yield* iotw.getWirelessDevice({ |
| 275 | Identifier: deployed.device.wirelessDeviceId, |
| 276 | IdentifierType: "WirelessDeviceId", |
| 277 | }); |
| 278 | expect(liveDevice.LoRaWAN?.DevEui?.toLowerCase()).toBe(DEV_EUI); |
| 279 | expect(liveDevice.LoRaWAN?.DeviceProfileId).toBe(deployed.deviceProfile.deviceProfileId); |
| 280 | expect(liveDevice.LoRaWAN?.ServiceProfileId).toBe(deployed.serviceProfile.serviceProfileId); |
| 281 | const liveGateway = yield* iotw.getWirelessGateway({ |
| 282 | Identifier: deployed.gateway.wirelessGatewayId, |
| 283 | IdentifierType: "WirelessGatewayId", |
| 284 | }); |
| 285 | expect(liveGateway.LoRaWAN?.GatewayEui?.toLowerCase()).toBe(GATEWAY_EUI); |
| 286 | expect(liveGateway.LoRaWAN?.RfRegion).toBe("US915"); |
| 287 | |
| 288 | // Update — device description and gateway MaxEirp change in place; |
| 289 | // identities (DevEui / GatewayEui) are stable. |
| 290 | const updated = yield* stack.deploy( |
| 291 | Effect.gen(function* () { |
| 292 | const role = yield* deliveryRole; |
| 293 | const destination = yield* Destination("DeviceUplinks", { |
| 294 | expressionType: "RuleName", |
| 295 | expression: "alchemy_iot_wireless_device_rule", |
| 296 | roleArn: role.roleArn, |
| 297 | tags: { fixture: "iot-wireless" }, |
| 298 | }); |
| 299 | const serviceProfile = yield* ServiceProfile("DeviceFleet", { |
| 300 | loRaWAN: { AddGwMetadata: true }, |
| 301 | }); |
| 302 | const deviceProfile = yield* DeviceProfile("DeviceModel", { |
| 303 | loRaWAN: { |
| 304 | MacVersion: "1.0.3", |
| 305 | RegParamsRevision: "RP002-1.0.1", |
| 306 | RfRegion: "US915", |
| 307 | MaxEirp: 10, |
| 308 | SupportsJoin: true, |
| 309 | }, |
| 310 | }); |
| 311 | const device = yield* WirelessDevice("Sensor", { |
| 312 | type: "LoRaWAN", |
| 313 | destinationName: destination.destinationName, |
| 314 | description: "alchemy test sensor v2", |
| 315 | loRaWAN: { |
| 316 | DevEui: DEV_EUI, |
| 317 | DeviceProfileId: deviceProfile.deviceProfileId, |
| 318 | ServiceProfileId: serviceProfile.serviceProfileId, |
| 319 | OtaaV1_0_x: { AppKey: APP_KEY, AppEui: APP_EUI }, |
| 320 | }, |
| 321 | tags: { fixture: "iot-wireless", env: "test" }, |
| 322 | }); |
| 323 | const gateway = yield* WirelessGateway("RooftopGw", { |
| 324 | description: "alchemy test gateway", |
| 325 | loRaWAN: { GatewayEui: GATEWAY_EUI, RfRegion: "US915", MaxEirp: 27 }, |
| 326 | tags: { fixture: "iot-wireless" }, |
| 327 | }); |
| 328 | return { device, gateway }; |
| 329 | }), |
| 330 | ); |
| 331 | |
| 332 | expect(updated.device.wirelessDeviceId).toBe(deployed.device.wirelessDeviceId); |
| 333 | expect(updated.gateway.wirelessGatewayId).toBe(deployed.gateway.wirelessGatewayId); |
| 334 | const updatedDevice = yield* iotw.getWirelessDevice({ |
| 335 | Identifier: updated.device.wirelessDeviceId, |
| 336 | IdentifierType: "WirelessDeviceId", |
| 337 | }); |
| 338 | expect(updatedDevice.Description).toBe("alchemy test sensor v2"); |
| 339 | const updatedGateway = yield* iotw.getWirelessGateway({ |
| 340 | Identifier: updated.gateway.wirelessGatewayId, |
| 341 | IdentifierType: "WirelessGatewayId", |
| 342 | }); |
| 343 | expect(updatedGateway.LoRaWAN?.MaxEirp).toBe(27); |
| 344 | const deviceTags = yield* iotw.listTagsForResource({ |
| 345 | ResourceArn: updated.device.wirelessDeviceArn, |
| 346 | }); |
| 347 | expect(deviceTags.Tags?.some((t) => t.Key === "env" && t.Value === "test")).toBe(true); |
| 348 | |
| 349 | // Destroy — device and gateway gone, typed. |
| 350 | yield* stack.destroy(); |
| 351 | const deviceError = yield* Effect.flip( |
| 352 | iotw.getWirelessDevice({ |
| 353 | Identifier: updated.device.wirelessDeviceId, |
| 354 | IdentifierType: "WirelessDeviceId", |
| 355 | }), |
| 356 | ); |
| 357 | expect(deviceError._tag).toBe("ResourceNotFoundException"); |
| 358 | const gatewayError = yield* Effect.flip( |
| 359 | iotw.getWirelessGateway({ |
| 360 | Identifier: updated.gateway.wirelessGatewayId, |
| 361 | IdentifierType: "WirelessGatewayId", |
| 362 | }), |
| 363 | ); |
| 364 | expect(gatewayError._tag).toBe("ResourceNotFoundException"); |
| 365 | }), |
| 366 | { |
| 367 | tags: ["provider:aws", "provider:aws:iam", "provider:aws:iotwireless", "live"], |
| 368 | timeout: 240_000, |
| 369 | }, |
| 370 | ); |