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

main
ts 274 lines 11.1 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import {
3 ChannelAssociation,
4 EventRule,
5 NotificationConfiguration,
6 NotificationHub,
7 } from "@alchemy.run/aws/Notifications";
8 import { pinNotificationsRegion } from "@alchemy.run/aws/Notifications/internal";
9 import { EmailContact } from "@alchemy.run/aws/NotificationsContacts";
10 import { describe, expect } from "@alchemy.run/test";
11 import * as Test from "@alchemy.run/test/Alchemy";
12 import * as notifications from "@distilled.cloud/aws/notifications";
13 import * as Effect from "effect/Effect";
14 import * as Schedule from "effect/Schedule";
15 import * as Stream from "effect/Stream";
16
17 const { test } = Test.make({ providers: AWS.providers() });
18
19 const CONFIG_NAME = "alchemy-test-notif-config";
20 const CONFIG_NAME_RENAMED = "alchemy-test-notif-config-renamed";
21
22 const getConfig = (arn: string) =>
23 pinNotificationsRegion(notifications.getNotificationConfiguration({ arn }));
24
25 const getRule = (arn: string) => pinNotificationsRegion(notifications.getEventRule({ arn }));
26
27 const assertConfigGone = (arn: string) =>
28 pinNotificationsRegion(
29 notifications.getNotificationConfiguration({ arn }).pipe(
30 Effect.flatMap(() => Effect.fail(new Error(`configuration ${arn} still exists`))),
31 Effect.catchTag("ResourceNotFoundException", () => Effect.void),
32 Effect.retry({
33 while: (e) => e instanceof Error,
34 schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(10)]),
35 }),
36 ),
37 );
38
39 const assertRuleGone = (arn: string) =>
40 pinNotificationsRegion(
41 notifications.getEventRule({ arn }).pipe(
42 Effect.flatMap(() => Effect.fail(new Error(`rule ${arn} still exists`))),
43 Effect.catchTag("ResourceNotFoundException", () => Effect.void),
44 Effect.retry({
45 while: (e) => e instanceof Error,
46 schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(10)]),
47 }),
48 ),
49 );
50
51 const listHubRegions = pinNotificationsRegion(
52 notifications.listNotificationHubs.items({}).pipe(
53 Stream.runCollect,
54 Effect.map((chunk) => Array.from(chunk).map((hub) => hub.notificationHubRegion)),
55 ),
56 );
57
58 describe(
59 "AWS.Notifications",
60 { tags: ["provider:aws", "provider:aws:notifications", "live"] },
61 () => {
62 test.provider(
63 "notification configuration + event rule lifecycle",
64 (stack) =>
65 Effect.gen(function* () {
66 yield* stack.destroy();
67
68 const deploy = (props: {
69 name: string;
70 description: string;
71 aggregationDuration: "LONG" | "SHORT" | "NONE";
72 tags: Record<string, string>;
73 rule: { eventType: string; eventPattern?: Record<string, any>; regions: string[] };
74 }) =>
75 stack.deploy(
76 Effect.gen(function* () {
77 const config = yield* NotificationConfiguration("Config", {
78 name: props.name,
79 description: props.description,
80 aggregationDuration: props.aggregationDuration,
81 tags: props.tags,
82 });
83 const rule = yield* EventRule("Rule", {
84 notificationConfigurationArn: config.notificationConfigurationArn,
85 source: "aws.s3",
86 eventType: props.rule.eventType,
87 eventPattern: props.rule.eventPattern,
88 regions: props.rule.regions,
89 });
90 return {
91 configArn: config.notificationConfigurationArn,
92 configName: config.name,
93 configStatus: config.status,
94 ruleArn: rule.eventRuleArn,
95 ruleRegions: rule.regions,
96 };
97 }),
98 );
99
100 // CREATE
101 const created = yield* deploy({
102 name: CONFIG_NAME,
103 description: "created by alchemy test",
104 aggregationDuration: "SHORT",
105 tags: { purpose: "alchemy-test" },
106 rule: { eventType: "Object Created", regions: ["us-west-2"] },
107 });
108 expect(created.configArn).toContain(":configuration/");
109 expect(created.configName).toBe(CONFIG_NAME);
110 expect(created.ruleArn).toContain("/rule/");
111
112 // Out-of-band: config, rule, and tags are live.
113 const observed = yield* getConfig(created.configArn);
114 expect(observed.name).toBe(CONFIG_NAME);
115 expect(observed.description).toBe("created by alchemy test");
116 expect(observed.aggregationDuration).toBe("SHORT");
117 const observedRule = yield* getRule(created.ruleArn);
118 expect(observedRule.source).toBe("aws.s3");
119 expect(observedRule.eventType).toBe("Object Created");
120 expect(observedRule.regions).toEqual(["us-west-2"]);
121 const tags = yield* pinNotificationsRegion(
122 notifications.listTagsForResource({ arn: created.configArn }),
123 );
124 expect(tags.tags?.purpose).toBe("alchemy-test");
125 expect(tags.tags?.["alchemy::id"]).toBe("Config");
126
127 // UPDATE — rename the config in place, change aggregation, widen the
128 // rule with a pattern and a second region. ARNs must be stable.
129 const updated = yield* deploy({
130 name: CONFIG_NAME_RENAMED,
131 description: "updated by alchemy test",
132 aggregationDuration: "LONG",
133 tags: { purpose: "alchemy-test", updated: "true" },
134 rule: {
135 eventType: "Object Created",
136 eventPattern: { detail: { bucket: { name: ["alchemy-test"] } } },
137 regions: ["us-west-2", "us-east-2"],
138 },
139 });
140 expect(updated.configArn).toBe(created.configArn);
141 expect(updated.ruleArn).toBe(created.ruleArn);
142 const afterUpdate = yield* getConfig(created.configArn);
143 expect(afterUpdate.name).toBe(CONFIG_NAME_RENAMED);
144 expect(afterUpdate.description).toBe("updated by alchemy test");
145 expect(afterUpdate.aggregationDuration).toBe("LONG");
146 const ruleAfterUpdate = yield* getRule(created.ruleArn);
147 expect([...ruleAfterUpdate.regions].sort()).toEqual(["us-east-2", "us-west-2"]);
148 expect(ruleAfterUpdate.eventPattern).toContain("alchemy-test");
149 const tagsAfterUpdate = yield* pinNotificationsRegion(
150 notifications.listTagsForResource({ arn: created.configArn }),
151 );
152 expect(tagsAfterUpdate.tags?.updated).toBe("true");
153
154 // REPLACE — changing the event type replaces the rule (new ARN);
155 // the config is untouched.
156 const replaced = yield* deploy({
157 name: CONFIG_NAME_RENAMED,
158 description: "updated by alchemy test",
159 aggregationDuration: "LONG",
160 tags: { purpose: "alchemy-test", updated: "true" },
161 rule: { eventType: "Object Deleted", regions: ["us-west-2"] },
162 });
163 expect(replaced.configArn).toBe(created.configArn);
164 expect(replaced.ruleArn).not.toBe(created.ruleArn);
165 const replacedRule = yield* getRule(replaced.ruleArn);
166 expect(replacedRule.eventType).toBe("Object Deleted");
167 yield* assertRuleGone(created.ruleArn);
168
169 // DESTROY — everything gone, verified out-of-band.
170 yield* stack.destroy();
171 yield* assertRuleGone(replaced.ruleArn);
172 yield* assertConfigGone(created.configArn);
173 }),
174 { timeout: 240_000 },
175 );
176
177 test.provider(
178 "channel association lifecycle",
179 (stack) =>
180 Effect.gen(function* () {
181 yield* stack.destroy();
182
183 const deploy = () =>
184 stack.deploy(
185 Effect.gen(function* () {
186 const config = yield* NotificationConfiguration("ChannelConfig", {
187 name: "alchemy-test-notif-channel-config",
188 description: "channel association test",
189 });
190 const contact = yield* EmailContact("Contact", {
191 name: "alchemy-test-notif-contact",
192 emailAddress: "alchemy-test-notif-channel@example.com",
193 });
194 const association = yield* ChannelAssociation("Assoc", {
195 notificationConfigurationArn: config.notificationConfigurationArn,
196 channelArn: contact.emailContactArn,
197 });
198 return {
199 configArn: config.notificationConfigurationArn,
200 contactArn: contact.emailContactArn,
201 associatedChannel: association.channelArn,
202 };
203 }),
204 );
205
206 // CREATE — the association's channel is the contact.
207 const created = yield* deploy();
208 expect(created.associatedChannel).toBe(created.contactArn);
209
210 // Out-of-band: the channel is associated with the configuration.
211 const channels = yield* pinNotificationsRegion(
212 notifications.listChannels({ notificationConfigurationArn: created.configArn }),
213 );
214 expect(channels.channels).toContain(created.contactArn);
215
216 // Idempotent re-deploy (no change).
217 const again = yield* deploy();
218 expect(again.associatedChannel).toBe(created.contactArn);
219
220 // DESTROY — association, contact and configuration all gone.
221 yield* stack.destroy();
222 yield* assertConfigGone(created.configArn);
223 }),
224 { tags: ["provider:aws:notificationscontacts"], timeout: 240_000 },
225 );
226
227 test.provider(
228 "notification hub lifecycle",
229 (stack) =>
230 Effect.gen(function* () {
231 yield* stack.destroy();
232
233 // AWS refuses to deregister the last ACTIVE hub, so keep a baseline
234 // hub (us-west-2) registered outside the stack. Registration is an
235 // idempotent upsert, so this is a no-op when it already exists.
236 yield* pinNotificationsRegion(
237 notifications.registerNotificationHub({ notificationHubRegion: "us-west-2" }),
238 );
239
240 const deployHub = (region: string) =>
241 stack.deploy(
242 Effect.gen(function* () {
243 const hub = yield* NotificationHub("Hub", { region });
244 return { region: hub.notificationHubRegion, status: hub.status };
245 }),
246 );
247
248 const created = yield* deployHub("us-east-2");
249 expect(created.region).toBe("us-east-2");
250 expect(created.status).toBe("ACTIVE");
251
252 // Out-of-band: the hub is registered.
253 expect(yield* listHubRegions).toContain("us-east-2");
254
255 // Idempotent re-deploy (no change).
256 const again = yield* deployHub("us-east-2");
257 expect(again.region).toBe("us-east-2");
258
259 // DESTROY — the stack hub deregisters; the baseline hub remains.
260 yield* stack.destroy();
261 const regions = yield* listHubRegions.pipe(
262 Effect.repeat({
263 schedule: Schedule.fixed("2 seconds"),
264 until: (rs) => !rs.includes("us-east-2"),
265 times: 15,
266 }),
267 );
268 expect(regions).not.toContain("us-east-2");
269 expect(regions).toContain("us-west-2");
270 }),
271 { timeout: 180_000 },
272 );
273 },
274 );