The best way to control Claude Code, Codex and more from any computer or phone

main
ts 105 lines 4.45 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { CredentialLocker, ManagedThing } from "@alchemy.run/aws/IoTManagedIntegrations";
3 import { Region } from "@alchemy.run/aws/Region";
4 import { expect } from "@alchemy.run/test";
5 import * as Test from "@alchemy.run/test/Alchemy";
6 import * as mi from "@distilled.cloud/aws/iot-managed-integrations";
7 import * as Data from "effect/Data";
8 import * as Effect from "effect/Effect";
9 import * as Redacted from "effect/Redacted";
10 import * as Schedule from "effect/Schedule";
11
12 const { test } = Test.make({ providers: AWS.providers() });
13
14 // IoT Managed Integrations is only deployed in a few regions (eu-west-1,
15 // ca-central-1, ...). The testing profile's default region (us-west-2) has no
16 // endpoint, so the ungated probe pins a supported region explicitly. The gated
17 // lifecycle below requires an AWS profile whose region IS a supported one
18 // (SSO profiles resolve region from ~/.aws/config; the AWS_REGION env var is
19 // not consulted), 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 // Ungated typed-error probe: prove the distilled error union carries the
25 // not-found tag this provider's read/delete paths depend on.
26 test.provider(
27 "getManagedThing on a nonexistent thing fails with ResourceNotFoundException",
28 () =>
29 Effect.gen(function* () {
30 const error = yield* Effect.flip(
31 pin(mi.getManagedThing({ Identifier: "alchemynonexistentthingprobe" })),
32 );
33 expect(error._tag).toBe("ResourceNotFoundException");
34 }),
35 { tags: ["provider:aws", "provider:aws:iotmanagedintegrations", "live"] },
36 );
37
38 class ThingStillExists extends Data.TaggedError("ThingStillExists")<{
39 readonly managedThingId: string;
40 }> {}
41
42 const assertThingGone = (managedThingId: string) =>
43 mi.getManagedThing({ Identifier: managedThingId }).pipe(
44 Effect.flatMap((thing) =>
45 thing.ProvisioningStatus === "DELETED" || thing.ProvisioningStatus === "DELETE_IN_PROGRESS"
46 ? Effect.void
47 : Effect.fail(new ThingStillExists({ managedThingId })),
48 ),
49 Effect.catchTag("ResourceNotFoundException", () => Effect.void),
50 Effect.retry({
51 while: (e) => e._tag === "ThingStillExists",
52 schedule: Schedule.max([Schedule.exponential(500), Schedule.recurs(8)]),
53 }),
54 );
55
56 // A synthetic Zigbee install-code QR payload. CreateManagedThing validates the
57 // payload shape, not that the device exists, so a well-formed constant is
58 // accepted for a DEVICE role. Checked in as a constant (never generated at
59 // test time).
60 const ZIGBEE_QR_PAYLOAD = "Z:24FD5B0000015C63$I:83FED3407A939738";
61
62 // Full lifecycle is gated: the service only exists in a few regions and a
63 // managed thing represents onboarded device state. Run with
64 // AWS_TEST_IOT_MI=1 and an AWS profile configured in a supported region.
65 test.provider.skipIf(!process.env.AWS_TEST_IOT_MI)(
66 "create managed thing, verify, update, destroy",
67 (stack) =>
68 Effect.gen(function* () {
69 yield* stack.destroy();
70
71 const makeStack = (brand: string) =>
72 Effect.gen(function* () {
73 const locker = yield* CredentialLocker("Locker", {});
74 const thing = yield* ManagedThing("Device", {
75 role: "DEVICE",
76 authenticationMaterial: Redacted.make(ZIGBEE_QR_PAYLOAD),
77 authenticationMaterialType: "ZIGBEE_QR_BAR_CODE",
78 credentialLockerId: locker.credentialLockerId,
79 brand,
80 serialNumber: "SN-ALCHEMY-0001",
81 tags: { fixture: "iot-mi-managed-thing" },
82 });
83 return { thing };
84 });
85
86 const { thing } = yield* stack.deploy(makeStack("alchemy"));
87
88 expect(thing.managedThingId).toBeDefined();
89 expect(thing.managedThingArn).toContain(":managed-thing/");
90 expect(thing.role).toBe("DEVICE");
91 expect(thing.tags.fixture).toBe("iot-mi-managed-thing");
92
93 // Out-of-band verification via distilled.
94 const observed = yield* mi.getManagedThing({ Identifier: thing.managedThingId });
95 expect(observed.Arn).toBe(thing.managedThingArn);
96
97 // Update a mutable field in place.
98 const { thing: updated } = yield* stack.deploy(makeStack("alchemy-two"));
99 expect(updated.managedThingId).toBe(thing.managedThingId);
100
101 yield* stack.destroy();
102 yield* assertThingGone(thing.managedThingId);
103 }),
104 { tags: ["provider:aws", "provider:aws:iotmanagedintegrations", "live"], timeout: 240_000 },
105 );