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 { |
| 3 | Campaign, |
| 4 | DecoderManifest, |
| 5 | Fleet, |
| 6 | ModelManifest, |
| 7 | SignalCatalog, |
| 8 | StateTemplate, |
| 9 | Vehicle, |
| 10 | } from "@alchemy.run/aws/IoTFleetWise"; |
| 11 | import { Bucket } from "@alchemy.run/aws/S3"; |
| 12 | import { expect } from "@alchemy.run/test"; |
| 13 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 14 | import * as iotfleetwise from "@distilled.cloud/aws/iotfleetwise"; |
| 15 | import { Region } from "@distilled.cloud/aws/Region"; |
| 16 | import * as Effect from "effect/Effect"; |
| 17 | import * as Schedule from "effect/Schedule"; |
| 18 | |
| 19 | const { test } = Test.make({ providers: AWS.providers() }); |
| 20 | |
| 21 | // Deterministic bucket name (constant per test suite — never Date.now()) |
| 22 | // so the FleetWise bucket policy can reference the ARN as a literal. |
| 23 | const TELEMETRY_BUCKET = "alchemy-test-iotfleetwise-telemetry"; |
| 24 | |
| 25 | // AWS IoT FleetWise is offered in us-east-1/eu-central-1 only — pin every |
| 26 | // out-of-band distilled call to the service's home region (the providers |
| 27 | // pin themselves the same way). |
| 28 | const inHomeRegion = <A, E, R>(effect: Effect.Effect<A, E, R>): Effect.Effect<A, E, R> => |
| 29 | effect.pipe(Effect.provideService(Region, Effect.succeed("us-east-1"))); |
| 30 | |
| 31 | // Ungated typed-error probe: prove the distilled error union carries the |
| 32 | // typed tags the providers' read/delete paths depend on. AWS IoT FleetWise |
| 33 | // is allowlist-gated — accounts without service access observe |
| 34 | // AccessDeniedException (with an empty message) on every operation, while |
| 35 | // allowlisted accounts observe ResourceNotFoundException for a bogus name. |
| 36 | // Both are typed tags in the operation's error union; the probe proves the |
| 37 | // decode path for whichever gate this account is behind. |
| 38 | test.provider( |
| 39 | "getSignalCatalog on a nonexistent catalog fails with a typed tag", |
| 40 | () => |
| 41 | Effect.gen(function* () { |
| 42 | const error = yield* Effect.flip( |
| 43 | iotfleetwise |
| 44 | .getSignalCatalog({ name: "alchemy-nonexistent-catalog-probe" }) |
| 45 | .pipe(inHomeRegion), |
| 46 | ); |
| 47 | expect(["ResourceNotFoundException", "AccessDeniedException"]).toContain(error._tag); |
| 48 | }), |
| 49 | { tags: ["provider:aws", "provider:aws:iotfleetwise", "live"] }, |
| 50 | ); |
| 51 | |
| 52 | // Same ungated probe for the state-template API — proves the typed tags the |
| 53 | // StateTemplate provider's read path depends on decode on every account. |
| 54 | test.provider( |
| 55 | "getStateTemplate on a nonexistent template fails with a typed tag", |
| 56 | () => |
| 57 | Effect.gen(function* () { |
| 58 | const error = yield* Effect.flip( |
| 59 | iotfleetwise |
| 60 | .getStateTemplate({ identifier: "alchemy-nonexistent-state-template-probe" }) |
| 61 | .pipe(inHomeRegion), |
| 62 | ); |
| 63 | expect(["ResourceNotFoundException", "AccessDeniedException"]).toContain(error._tag); |
| 64 | }), |
| 65 | { tags: ["provider:aws", "provider:aws:iotfleetwise", "live"] }, |
| 66 | ); |
| 67 | |
| 68 | // Typed wait-until-gone for the gated lifecycle teardown. |
| 69 | const assertCatalogGone = (name: string) => |
| 70 | Effect.gen(function* () { |
| 71 | const found = yield* iotfleetwise.getSignalCatalog({ name }).pipe( |
| 72 | inHomeRegion, |
| 73 | Effect.catchTag("ResourceNotFoundException", () => Effect.succeed(undefined)), |
| 74 | ); |
| 75 | if (found !== undefined) { |
| 76 | return yield* Effect.fail(new Error(`signal catalog '${name}' still exists`)); |
| 77 | } |
| 78 | }).pipe( |
| 79 | Effect.retry({ schedule: Schedule.max([Schedule.fixed("5 seconds"), Schedule.recurs(12)]) }), |
| 80 | ); |
| 81 | |
| 82 | // Full lifecycle across all six resources. AWS IoT FleetWise requires the |
| 83 | // account to be allowlisted for the service (and registered via |
| 84 | // RegisterAccount for legacy Timestream destinations) — the standing test |
| 85 | // account is not, so the live run is gated behind AWS_TEST_IOTFLEETWISE=1. |
| 86 | // All six resources are free and provision in seconds once entitled. |
| 87 | test.provider.skipIf(!process.env.AWS_TEST_IOTFLEETWISE)( |
| 88 | "signal catalog -> model -> decoder -> fleet -> vehicle -> campaign lifecycle", |
| 89 | (stack) => |
| 90 | Effect.gen(function* () { |
| 91 | yield* stack.destroy(); |
| 92 | |
| 93 | const deployed = yield* stack.deploy( |
| 94 | Effect.gen(function* () { |
| 95 | const catalog = yield* SignalCatalog("Signals", { |
| 96 | description: "alchemy fleetwise test signals", |
| 97 | nodes: [ |
| 98 | { branch: { fullyQualifiedName: "Vehicle" } }, |
| 99 | { sensor: { fullyQualifiedName: "Vehicle.Speed", dataType: "DOUBLE", unit: "km/h" } }, |
| 100 | { attribute: { fullyQualifiedName: "Vehicle.VIN", dataType: "STRING" } }, |
| 101 | ], |
| 102 | tags: { fixture: "iotfleetwise" }, |
| 103 | }); |
| 104 | |
| 105 | const model = yield* ModelManifest("SedanModel", { |
| 106 | signalCatalogArn: catalog.signalCatalogArn, |
| 107 | nodes: ["Vehicle.Speed", "Vehicle.VIN"], |
| 108 | description: "alchemy fleetwise test model", |
| 109 | status: "ACTIVE", |
| 110 | }); |
| 111 | |
| 112 | const decoder = yield* DecoderManifest("SedanDecoder", { |
| 113 | modelManifestArn: model.modelManifestArn, |
| 114 | description: "alchemy fleetwise test decoder", |
| 115 | networkInterfaces: [ |
| 116 | { |
| 117 | interfaceId: "obd0", |
| 118 | type: "OBD_INTERFACE", |
| 119 | obdInterface: { name: "obd", requestMessageId: 2015 }, |
| 120 | }, |
| 121 | ], |
| 122 | signalDecoders: [ |
| 123 | { |
| 124 | fullyQualifiedName: "Vehicle.Speed", |
| 125 | type: "OBD_SIGNAL", |
| 126 | interfaceId: "obd0", |
| 127 | obdSignal: { |
| 128 | pidResponseLength: 1, |
| 129 | serviceMode: 1, |
| 130 | pid: 13, |
| 131 | scaling: 1, |
| 132 | offset: 0, |
| 133 | startByte: 0, |
| 134 | byteLength: 1, |
| 135 | }, |
| 136 | }, |
| 137 | ], |
| 138 | status: "ACTIVE", |
| 139 | }); |
| 140 | |
| 141 | const fleet = yield* Fleet("TestFleet", { |
| 142 | signalCatalogArn: catalog.signalCatalogArn, |
| 143 | description: "alchemy fleetwise test fleet", |
| 144 | }); |
| 145 | |
| 146 | const vehicle = yield* Vehicle("TestVehicle", { |
| 147 | modelManifestArn: model.modelManifestArn, |
| 148 | decoderManifestArn: decoder.decoderManifestArn, |
| 149 | attributes: { "Vehicle.VIN": "1HGBH41JXMN109186" }, |
| 150 | }); |
| 151 | |
| 152 | const stateTemplate = yield* StateTemplate("SpeedState", { |
| 153 | signalCatalogArn: catalog.signalCatalogArn, |
| 154 | stateTemplateProperties: ["Vehicle.Speed"], |
| 155 | description: "alchemy fleetwise test state template", |
| 156 | }); |
| 157 | |
| 158 | // Campaign data lands in S3; FleetWise writes require a bucket |
| 159 | // policy trusting the service principal. Deterministic name so |
| 160 | // the policy can reference the bucket ARN as a literal. |
| 161 | const bucket = yield* Bucket("TelemetryBucket", { |
| 162 | bucketName: TELEMETRY_BUCKET, |
| 163 | forceDestroy: true, |
| 164 | policy: [ |
| 165 | { |
| 166 | Effect: "Allow", |
| 167 | Principal: { Service: "iotfleetwise.amazonaws.com" }, |
| 168 | Action: ["s3:GetBucketLocation", "s3:PutObject"], |
| 169 | Resource: [ |
| 170 | `arn:aws:s3:::${TELEMETRY_BUCKET}`, |
| 171 | `arn:aws:s3:::${TELEMETRY_BUCKET}/*`, |
| 172 | ], |
| 173 | }, |
| 174 | ], |
| 175 | }); |
| 176 | |
| 177 | const campaign = yield* Campaign("SpeedTelemetry", { |
| 178 | signalCatalogArn: catalog.signalCatalogArn, |
| 179 | targetArn: fleet.fleetArn, |
| 180 | collectionScheme: { timeBasedCollectionScheme: { period: "10 seconds" } }, |
| 181 | signalsToCollect: [{ name: "Vehicle.Speed" }], |
| 182 | dataDestinationConfigs: [{ s3Config: { bucketArn: bucket.bucketArn } }], |
| 183 | }); |
| 184 | |
| 185 | return { catalog, model, decoder, fleet, vehicle, stateTemplate, campaign }; |
| 186 | }), |
| 187 | ); |
| 188 | |
| 189 | expect(deployed.catalog.signalCatalogArn).toContain(":signal-catalog/"); |
| 190 | expect(deployed.stateTemplate.stateTemplateArn).toContain(":state-template/"); |
| 191 | expect(deployed.stateTemplate.stateTemplateProperties).toEqual(["Vehicle.Speed"]); |
| 192 | expect(deployed.model.status).toBe("ACTIVE"); |
| 193 | expect(deployed.decoder.status).toBe("ACTIVE"); |
| 194 | expect(deployed.fleet.fleetArn).toContain(":fleet/"); |
| 195 | expect(deployed.vehicle.vehicleArn).toContain(":vehicle/"); |
| 196 | expect(deployed.campaign.status).toBe("WAITING_FOR_APPROVAL"); |
| 197 | |
| 198 | // Out-of-band verification via distilled. |
| 199 | const observedModel = yield* iotfleetwise |
| 200 | .getModelManifest({ name: deployed.model.modelManifestName }) |
| 201 | .pipe(inHomeRegion); |
| 202 | expect(observedModel.status).toBe("ACTIVE"); |
| 203 | const observedVehicle = yield* iotfleetwise |
| 204 | .getVehicle({ vehicleName: deployed.vehicle.vehicleName }) |
| 205 | .pipe(inHomeRegion); |
| 206 | expect(observedVehicle.attributes?.["Vehicle.VIN"]).toBe("1HGBH41JXMN109186"); |
| 207 | |
| 208 | yield* stack.destroy(); |
| 209 | yield* assertCatalogGone(deployed.catalog.signalCatalogName); |
| 210 | }), |
| 211 | { |
| 212 | tags: ["provider:aws", "provider:aws:iotfleetwise", "provider:aws:s3", "live"], |
| 213 | timeout: 600_000, |
| 214 | }, |
| 215 | ); |