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

main
ts 132 lines 4.98 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import type { PolicyDocument } from "@alchemy.run/aws/IAM/Policy";
3 import { Role } from "@alchemy.run/aws/IAM/Role";
4 import { Destination } from "@alchemy.run/aws/IoTManagedIntegrations";
5 import { Stream } from "@alchemy.run/aws/Kinesis";
6 import { Region } from "@alchemy.run/aws/Region";
7 import { expect } from "@alchemy.run/test";
8 import * as Test from "@alchemy.run/test/Alchemy";
9 import * as mi from "@distilled.cloud/aws/iot-managed-integrations";
10 import * as Data from "effect/Data";
11 import * as Effect from "effect/Effect";
12 import * as Schedule from "effect/Schedule";
13
14 const { test } = Test.make({ providers: AWS.providers() });
15
16 // IoT Managed Integrations is only deployed in a few regions (eu-west-1,
17 // ca-central-1, ...). The testing profile's default region (us-west-2) has no
18 // endpoint, so the ungated probe pins a supported region explicitly. The gated
19 // lifecycle below requires an AWS profile whose region IS a supported one
20 // (SSO profiles resolve region from ~/.aws/config; the AWS_REGION env var is
21 // not consulted), e.g. a profile with region = eu-west-1.
22 const MI_REGION = "eu-west-1";
23 const pin = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
24 effect.pipe(Effect.provideService(Region, Effect.succeed(MI_REGION)));
25
26 // Ungated typed-error probe: prove the distilled error union carries the
27 // not-found tag this provider's read/delete paths depend on.
28 test.provider(
29 "getDestination on a nonexistent destination fails with ResourceNotFoundException",
30 () =>
31 Effect.gen(function* () {
32 const error = yield* Effect.flip(
33 pin(mi.getDestination({ Name: "alchemy-nonexistent-destination" })),
34 );
35 expect(error._tag).toBe("ResourceNotFoundException");
36 }),
37 { tags: ["provider:aws", "provider:aws:iotmanagedintegrations", "live"] },
38 );
39
40 class DestinationStillExists extends Data.TaggedError("DestinationStillExists")<{
41 readonly name: string;
42 }> {}
43
44 const assertDestinationGone = (name: string) =>
45 mi.getDestination({ Name: name }).pipe(
46 Effect.flatMap(() => Effect.fail(new DestinationStillExists({ name }))),
47 Effect.catchTag("ResourceNotFoundException", () => Effect.void),
48 Effect.retry({
49 while: (e) => e._tag === "DestinationStillExists",
50 schedule: Schedule.max([Schedule.exponential(500), Schedule.recurs(8)]),
51 }),
52 );
53
54 // Full lifecycle is gated: the service only exists in a few regions. Run with
55 // AWS_TEST_IOT_MI=1 and an AWS profile configured in a supported region.
56 test.provider.skipIf(!process.env.AWS_TEST_IOT_MI)(
57 "create kinesis destination, verify, update description, destroy",
58 (stack) =>
59 Effect.gen(function* () {
60 yield* stack.destroy();
61
62 const makeStack = (description: string) =>
63 Effect.gen(function* () {
64 const stream = yield* Stream("Events", {});
65 const assumeRolePolicyDocument: PolicyDocument = {
66 Version: "2012-10-17",
67 Statement: [
68 {
69 Effect: "Allow",
70 Principal: {
71 Service: "iotmanagedintegrations.amazonaws.com",
72 },
73 Action: ["sts:AssumeRole"],
74 },
75 ],
76 };
77 const role = yield* Role("DeliveryRole", {
78 assumeRolePolicyDocument,
79 inlinePolicies: {
80 kinesis: {
81 Version: "2012-10-17",
82 Statement: [
83 {
84 Effect: "Allow",
85 Action: ["kinesis:PutRecord", "kinesis:PutRecords"],
86 Resource: [stream.streamArn],
87 },
88 ],
89 },
90 },
91 });
92 const destination = yield* Destination("EventDestination", {
93 deliveryDestinationArn: stream.streamArn,
94 roleArn: role.roleArn,
95 description,
96 tags: { fixture: "iot-mi-destination" },
97 });
98 return { destination };
99 });
100
101 const { destination } = yield* stack.deploy(makeStack("phase one"));
102
103 expect(destination.destinationName).toBeDefined();
104 expect(destination.deliveryDestinationType).toBe("KINESIS");
105 expect(destination.description).toBe("phase one");
106
107 // Out-of-band verification via distilled.
108 const observed = yield* mi.getDestination({
109 Name: destination.destinationName,
110 });
111 expect(observed.DeliveryDestinationArn).toBe(destination.deliveryDestinationArn);
112 expect(observed.Tags?.fixture).toBe("iot-mi-destination");
113
114 // Update the description in place.
115 const { destination: updated } = yield* stack.deploy(makeStack("phase two"));
116 expect(updated.destinationName).toBe(destination.destinationName);
117 expect(updated.description).toBe("phase two");
118
119 yield* stack.destroy();
120 yield* assertDestinationGone(destination.destinationName);
121 }),
122 {
123 tags: [
124 "provider:aws",
125 "provider:aws:iam",
126 "provider:aws:iotmanagedintegrations",
127 "provider:aws:kinesis",
128 "live",
129 ],
130 timeout: 240_000,
131 },
132 );