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, 07:15 UTC
| 1 | import * as AWS from "@alchemy.run/aws"; |
| 2 | import { Asset, AssetModel, Gateway } from "@alchemy.run/aws/IoTSiteWise"; |
| 3 | import { expect } from "@alchemy.run/test"; |
| 4 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 5 | import * as sitewise from "@distilled.cloud/aws/iotsitewise"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as Schedule from "effect/Schedule"; |
| 8 | |
| 9 | const { test } = Test.make({ providers: AWS.providers() }); |
| 10 | |
| 11 | // Ungated typed-error probe: prove the distilled error union carries the |
| 12 | // not-found tag this provider's read/delete/wait paths depend on. Runs in |
| 13 | // every CI pass at near-zero cost. |
| 14 | test.provider( |
| 15 | "describeAssetModel on a nonexistent id fails with ResourceNotFoundException", |
| 16 | () => |
| 17 | Effect.gen(function* () { |
| 18 | const error = yield* Effect.flip( |
| 19 | sitewise.describeAssetModel({ |
| 20 | // syntactically valid UUID that does not exist |
| 21 | assetModelId: "12345678-1234-4123-8123-123456789012", |
| 22 | }), |
| 23 | ); |
| 24 | expect(error._tag).toBe("ResourceNotFoundException"); |
| 25 | }), |
| 26 | { tags: ["provider:aws", "provider:aws:iotsitewise", "live"] }, |
| 27 | ); |
| 28 | |
| 29 | // Bounded wait until a describe reports the resource gone. |
| 30 | const untilGone = <R>(label: string, probe: Effect.Effect<"gone" | string, never, R>) => |
| 31 | Effect.gen(function* () { |
| 32 | const state = yield* probe; |
| 33 | if (state !== "gone") { |
| 34 | return yield* Effect.fail(new Error(`${label} still exists (state: ${state})`)); |
| 35 | } |
| 36 | }).pipe( |
| 37 | Effect.retry({ schedule: Schedule.max([Schedule.spaced("3 seconds"), Schedule.recurs(10)]) }), |
| 38 | ); |
| 39 | |
| 40 | test.provider( |
| 41 | "asset model + asset lifecycle (create, update, destroy)", |
| 42 | (stack) => |
| 43 | Effect.gen(function* () { |
| 44 | yield* stack.destroy(); |
| 45 | |
| 46 | const deploy = (description: string) => |
| 47 | stack.deploy( |
| 48 | Effect.gen(function* () { |
| 49 | const model = yield* AssetModel("PumpModel", { |
| 50 | assetModelDescription: description, |
| 51 | assetModelProperties: [ |
| 52 | { |
| 53 | name: "SerialNumber", |
| 54 | dataType: "STRING", |
| 55 | type: { attribute: { defaultValue: "unknown" } }, |
| 56 | }, |
| 57 | { |
| 58 | name: "Temperature", |
| 59 | dataType: "DOUBLE", |
| 60 | unit: "Celsius", |
| 61 | type: { measurement: {} }, |
| 62 | }, |
| 63 | ], |
| 64 | tags: { fixture: "iotsitewise" }, |
| 65 | }); |
| 66 | const asset = yield* Asset("Pump1", { |
| 67 | assetModelId: model.assetModelId, |
| 68 | assetDescription: description, |
| 69 | tags: { fixture: "iotsitewise" }, |
| 70 | }); |
| 71 | return { model, asset }; |
| 72 | }), |
| 73 | ); |
| 74 | |
| 75 | // Create. |
| 76 | const first = yield* deploy("pump model v1"); |
| 77 | expect(first.model.assetModelId).toBeDefined(); |
| 78 | expect(first.model.assetModelArn).toContain(":asset-model/"); |
| 79 | expect(first.model.state).toBe("ACTIVE"); |
| 80 | expect(first.asset.assetArn).toContain(":asset/"); |
| 81 | expect(first.asset.state).toBe("ACTIVE"); |
| 82 | expect(first.asset.assetModelId).toBe(first.model.assetModelId); |
| 83 | |
| 84 | // Out-of-band verification via distilled. |
| 85 | const describedModel = yield* sitewise.describeAssetModel({ |
| 86 | assetModelId: first.model.assetModelId, |
| 87 | }); |
| 88 | expect(describedModel.assetModelDescription).toBe("pump model v1"); |
| 89 | expect(describedModel.assetModelProperties.map((p) => p.name).sort()).toEqual([ |
| 90 | "SerialNumber", |
| 91 | "Temperature", |
| 92 | ]); |
| 93 | const describedAsset = yield* sitewise.describeAsset({ |
| 94 | assetId: first.asset.assetId, |
| 95 | excludeProperties: true, |
| 96 | }); |
| 97 | expect(describedAsset.assetDescription).toBe("pump model v1"); |
| 98 | const modelTags = yield* sitewise.listTagsForResource({ |
| 99 | resourceArn: first.model.assetModelArn, |
| 100 | }); |
| 101 | expect(modelTags.tags?.fixture).toBe("iotsitewise"); |
| 102 | expect(modelTags.tags?.["alchemy::id"]).toBe("PumpModel"); |
| 103 | |
| 104 | // Update in place — descriptions change, ids are stable. |
| 105 | const second = yield* deploy("pump model v2"); |
| 106 | expect(second.model.assetModelId).toBe(first.model.assetModelId); |
| 107 | expect(second.asset.assetId).toBe(first.asset.assetId); |
| 108 | const updatedModel = yield* sitewise.describeAssetModel({ |
| 109 | assetModelId: first.model.assetModelId, |
| 110 | excludeProperties: true, |
| 111 | }); |
| 112 | expect(updatedModel.assetModelDescription).toBe("pump model v2"); |
| 113 | const updatedAsset = yield* sitewise.describeAsset({ |
| 114 | assetId: first.asset.assetId, |
| 115 | excludeProperties: true, |
| 116 | }); |
| 117 | expect(updatedAsset.assetDescription).toBe("pump model v2"); |
| 118 | |
| 119 | // Destroy — asset must go before the model; verify both are gone. |
| 120 | yield* stack.destroy(); |
| 121 | yield* untilGone( |
| 122 | "asset", |
| 123 | sitewise.describeAsset({ assetId: first.asset.assetId, excludeProperties: true }).pipe( |
| 124 | Effect.map((r) => r.assetStatus.state as string), |
| 125 | Effect.catchTag("ResourceNotFoundException", () => Effect.succeed("gone" as const)), |
| 126 | Effect.orDie, |
| 127 | ), |
| 128 | ); |
| 129 | yield* untilGone( |
| 130 | "asset model", |
| 131 | sitewise |
| 132 | .describeAssetModel({ assetModelId: first.model.assetModelId, excludeProperties: true }) |
| 133 | .pipe( |
| 134 | Effect.map((r) => r.assetModelStatus.state as string), |
| 135 | Effect.catchTag("ResourceNotFoundException", () => Effect.succeed("gone" as const)), |
| 136 | Effect.orDie, |
| 137 | ), |
| 138 | ); |
| 139 | }), |
| 140 | { tags: ["provider:aws", "provider:aws:iotsitewise", "live"], timeout: 240_000 }, |
| 141 | ); |
| 142 | |
| 143 | test.provider( |
| 144 | "gateway lifecycle with platform replacement", |
| 145 | (stack) => |
| 146 | Effect.gen(function* () { |
| 147 | yield* stack.destroy(); |
| 148 | |
| 149 | const deploy = (coreDeviceThingName: string) => |
| 150 | stack.deploy( |
| 151 | Effect.gen(function* () { |
| 152 | const gateway = yield* Gateway("EdgeGateway", { |
| 153 | gatewayPlatform: { greengrassV2: { coreDeviceThingName } }, |
| 154 | tags: { fixture: "iotsitewise-gateway" }, |
| 155 | }); |
| 156 | return { gateway }; |
| 157 | }), |
| 158 | ); |
| 159 | |
| 160 | // Create — the referenced core device does not need to exist. |
| 161 | const first = yield* deploy("AlchemyIoTSiteWiseCoreA"); |
| 162 | expect(first.gateway.gatewayId).toBeDefined(); |
| 163 | expect(first.gateway.gatewayArn).toContain(":gateway/"); |
| 164 | |
| 165 | // Out-of-band verification via distilled. |
| 166 | const described = yield* sitewise.describeGateway({ gatewayId: first.gateway.gatewayId }); |
| 167 | expect(described.gatewayPlatform?.greengrassV2?.coreDeviceThingName).toBe( |
| 168 | "AlchemyIoTSiteWiseCoreA", |
| 169 | ); |
| 170 | const tags = yield* sitewise.listTagsForResource({ resourceArn: first.gateway.gatewayArn }); |
| 171 | expect(tags.tags?.fixture).toBe("iotsitewise-gateway"); |
| 172 | |
| 173 | // Changing the platform replaces the gateway. |
| 174 | const second = yield* deploy("AlchemyIoTSiteWiseCoreB"); |
| 175 | expect(second.gateway.gatewayId).not.toBe(first.gateway.gatewayId); |
| 176 | expect( |
| 177 | (yield* sitewise.describeGateway({ gatewayId: second.gateway.gatewayId })).gatewayPlatform |
| 178 | ?.greengrassV2?.coreDeviceThingName, |
| 179 | ).toBe("AlchemyIoTSiteWiseCoreB"); |
| 180 | yield* untilGone( |
| 181 | "replaced gateway", |
| 182 | sitewise.describeGateway({ gatewayId: first.gateway.gatewayId }).pipe( |
| 183 | Effect.map(() => "exists"), |
| 184 | Effect.catchTag("ResourceNotFoundException", () => Effect.succeed("gone" as const)), |
| 185 | Effect.orDie, |
| 186 | ), |
| 187 | ); |
| 188 | |
| 189 | // Destroy and verify gone. |
| 190 | yield* stack.destroy(); |
| 191 | yield* untilGone( |
| 192 | "gateway", |
| 193 | sitewise.describeGateway({ gatewayId: second.gateway.gatewayId }).pipe( |
| 194 | Effect.map(() => "exists"), |
| 195 | Effect.catchTag("ResourceNotFoundException", () => Effect.succeed("gone" as const)), |
| 196 | Effect.orDie, |
| 197 | ), |
| 198 | ); |
| 199 | }), |
| 200 | { tags: ["provider:aws", "provider:aws:iotsitewise", "live"], timeout: 180_000 }, |
| 201 | ); |