@devries-cameron20 / t3code Mirror · read-only
Fork 0

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, 06:00 UTC

main
ts 148 lines 5.77 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, NotificationConfiguration } 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
19 // gated lifecycle below requires an AWS profile whose region IS a supported
20 // one, e.g. a profile with region = eu-west-1.
21 const MI_REGION = "eu-west-1";
22 const pin = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
23 effect.pipe(Effect.provideService(Region, Effect.succeed(MI_REGION)));
24
25 // Ungated typed-error probe: prove the distilled error union carries the
26 // not-found tag this provider's read/delete paths depend on.
27 test.provider(
28 "getNotificationConfiguration on an unconfigured event type fails with ResourceNotFoundException",
29 () =>
30 Effect.gen(function* () {
31 const error = yield* Effect.flip(
32 pin(
33 mi.getNotificationConfiguration({
34 EventType: "CONNECTOR_ERROR_REPORT",
35 }),
36 ),
37 );
38 expect(error._tag).toBe("ResourceNotFoundException");
39 }),
40 { tags: ["provider:aws", "provider:aws:iotmanagedintegrations", "live"] },
41 );
42
43 class ConfigurationStillExists extends Data.TaggedError("ConfigurationStillExists")<{
44 readonly eventType: string;
45 }> {}
46
47 const assertConfigurationGone = (eventType: string) =>
48 mi.getNotificationConfiguration({ EventType: eventType }).pipe(
49 Effect.flatMap(() => Effect.fail(new ConfigurationStillExists({ eventType }))),
50 Effect.catchTag("ResourceNotFoundException", () => Effect.void),
51 Effect.retry({
52 while: (e) => e._tag === "ConfigurationStillExists",
53 schedule: Schedule.max([Schedule.exponential(500), Schedule.recurs(8)]),
54 }),
55 );
56
57 // Full lifecycle is gated: the service only exists in a few regions. Run with
58 // AWS_TEST_IOT_MI=1 and an AWS profile configured in a supported region.
59 test.provider.skipIf(!process.env.AWS_TEST_IOT_MI)(
60 "create notification configuration, verify, retarget destination, destroy",
61 (stack) =>
62 Effect.gen(function* () {
63 yield* stack.destroy();
64
65 const makeStack = (target: "A" | "B") =>
66 Effect.gen(function* () {
67 const stream = yield* Stream("Events", {});
68 const assumeRolePolicyDocument: PolicyDocument = {
69 Version: "2012-10-17",
70 Statement: [
71 {
72 Effect: "Allow",
73 Principal: {
74 Service: "iotmanagedintegrations.amazonaws.com",
75 },
76 Action: ["sts:AssumeRole"],
77 },
78 ],
79 };
80 const role = yield* Role("DeliveryRole", {
81 assumeRolePolicyDocument,
82 inlinePolicies: {
83 kinesis: {
84 Version: "2012-10-17",
85 Statement: [
86 {
87 Effect: "Allow",
88 Action: ["kinesis:PutRecord", "kinesis:PutRecords"],
89 Resource: [stream.streamArn],
90 },
91 ],
92 },
93 },
94 });
95 const destinationA = yield* Destination("DestinationA", {
96 deliveryDestinationArn: stream.streamArn,
97 roleArn: role.roleArn,
98 });
99 const destinationB = yield* Destination("DestinationB", {
100 deliveryDestinationArn: stream.streamArn,
101 roleArn: role.roleArn,
102 });
103 const routing = yield* NotificationConfiguration("Routing", {
104 eventType: "DEVICE_LIFE_CYCLE",
105 destinationName:
106 target === "A" ? destinationA.destinationName : destinationB.destinationName,
107 tags: { fixture: "iot-mi-notification-configuration" },
108 });
109 return { routing, destinationA, destinationB };
110 });
111
112 const { routing, destinationA } = yield* stack.deploy(makeStack("A"));
113
114 expect(routing.eventType).toBe("DEVICE_LIFE_CYCLE");
115 expect(routing.destinationName).toBe(destinationA.destinationName);
116 expect(routing.tags.fixture).toBe("iot-mi-notification-configuration");
117
118 // Out-of-band verification via distilled.
119 const observed = yield* mi.getNotificationConfiguration({
120 EventType: "DEVICE_LIFE_CYCLE",
121 });
122 expect(observed.DestinationName).toBe(destinationA.destinationName);
123 expect(observed.Tags?.fixture).toBe("iot-mi-notification-configuration");
124
125 // Retarget the configuration at destination B in place (no replace —
126 // the event type is unchanged).
127 const { routing: updated, destinationB } = yield* stack.deploy(makeStack("B"));
128 expect(updated.eventType).toBe("DEVICE_LIFE_CYCLE");
129 expect(updated.destinationName).toBe(destinationB.destinationName);
130 const retargeted = yield* mi.getNotificationConfiguration({
131 EventType: "DEVICE_LIFE_CYCLE",
132 });
133 expect(retargeted.DestinationName).toBe(destinationB.destinationName);
134
135 yield* stack.destroy();
136 yield* assertConfigurationGone("DEVICE_LIFE_CYCLE");
137 }),
138 {
139 tags: [
140 "provider:aws",
141 "provider:aws:iam",
142 "provider:aws:iotmanagedintegrations",
143 "provider:aws:kinesis",
144 "live",
145 ],
146 timeout: 240_000,
147 },
148 );