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

main
ts 450 lines 18.3 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import {
3 AddonInstance,
4 AddonSubscription,
5 AddressList,
6 Archive,
7 IngressPoint,
8 Relay,
9 RuleSet,
10 TrafficPolicy,
11 } from "@alchemy.run/aws/MailManager";
12 import { expect } from "@alchemy.run/test";
13 import * as Test from "@alchemy.run/test/Alchemy";
14 import * as mm from "@distilled.cloud/aws/mailmanager";
15 import * as Effect from "effect/Effect";
16 import * as Schedule from "effect/Schedule";
17
18 const { test } = Test.make({ providers: AWS.providers() });
19
20 // Ungated typed-error probes: prove the distilled error union carries the
21 // not-found tag every MailManager provider's read/delete path depends on.
22 // Runs in every CI pass at near-zero cost.
23 test.provider(
24 "getRuleSet on a bogus id fails with ResourceNotFoundException",
25 () =>
26 Effect.gen(function* () {
27 const error = yield* Effect.flip(
28 // Well-formed (rs- + 26 alphanumerics) but nonexistent — a malformed
29 // id fails ValidationException before the existence check.
30 mm.getRuleSet({ RuleSetId: "rs-00000000000000000000000000" }),
31 );
32 expect(error._tag).toBe("ResourceNotFoundException");
33 }),
34 { tags: ["provider:aws", "provider:aws:mailmanager", "live"] },
35 );
36
37 // Mail Manager deletes are natively idempotent — deleting a nonexistent
38 // (well-formed) id SUCCEEDS rather than failing ResourceNotFoundException,
39 // which is why deleteRuleSet's error union carries no not-found tag and the
40 // providers' delete paths need no catch. This probe pins that behavior.
41 test.provider(
42 "deleteRuleSet on a nonexistent id succeeds (idempotent)",
43 () =>
44 Effect.gen(function* () {
45 yield* mm.deleteRuleSet({ RuleSetId: "rs-00000000000000000000000000" });
46 yield* mm.deleteTrafficPolicy({ TrafficPolicyId: "tp-00000000000000000000000000" });
47 expect(true).toBe(true);
48 }),
49 { tags: ["provider:aws", "provider:aws:mailmanager", "live"] },
50 );
51
52 // Rule sets and traffic policies are free, provision instantly, and need no
53 // verified domains — full lifecycle runs ungated. Relays are also free
54 // config-only resources (creation does not validate connectivity).
55 test.provider(
56 "lifecycle: rule set + traffic policy + relay",
57 (stack) =>
58 Effect.gen(function* () {
59 yield* stack.destroy();
60
61 // Create.
62 const deployed = yield* stack.deploy(
63 Effect.gen(function* () {
64 const ruleSet = yield* RuleSet("Inbound", {
65 rules: [{ Name: "DropAll", Actions: [{ Drop: {} }] }],
66 tags: { fixture: "mailmanager" },
67 });
68 const trafficPolicy = yield* TrafficPolicy("Edge", {
69 defaultAction: "ALLOW",
70 policyStatements: [
71 {
72 Action: "DENY",
73 Conditions: [
74 {
75 IpExpression: {
76 Evaluate: { Attribute: "SENDER_IP" },
77 Operator: "CIDR_MATCHES",
78 Values: ["192.0.2.0/24"],
79 },
80 },
81 ],
82 },
83 ],
84 maxMessageSizeBytes: 10_485_760,
85 tags: { fixture: "mailmanager" },
86 });
87 const relay = yield* Relay("Downstream", {
88 serverName: "smtp.example.com",
89 serverPort: 25,
90 authentication: { NoAuthentication: {} },
91 tags: { fixture: "mailmanager" },
92 });
93 return { ruleSet, trafficPolicy, relay };
94 }),
95 );
96
97 expect(deployed.ruleSet.ruleSetId).toBeDefined();
98 expect(deployed.ruleSet.ruleSetArn).toContain("mailmanager");
99 expect(deployed.trafficPolicy.trafficPolicyId).toBeDefined();
100 expect(deployed.trafficPolicy.trafficPolicyArn).toContain("mailmanager");
101 expect(deployed.relay.relayId).toBeDefined();
102 expect(deployed.relay.relayArn).toContain("mailmanager");
103
104 // Out-of-band verification via distilled.
105 const liveRuleSet = yield* mm.getRuleSet({ RuleSetId: deployed.ruleSet.ruleSetId });
106 expect(liveRuleSet.RuleSetName).toBe(deployed.ruleSet.ruleSetName);
107 expect(liveRuleSet.Rules).toHaveLength(1);
108 expect(liveRuleSet.Rules[0]?.Name).toBe("DropAll");
109
110 const livePolicy = yield* mm.getTrafficPolicy({
111 TrafficPolicyId: deployed.trafficPolicy.trafficPolicyId,
112 });
113 expect(livePolicy.DefaultAction).toBe("ALLOW");
114 expect(livePolicy.MaxMessageSizeBytes).toBe(10_485_760);
115 expect(livePolicy.PolicyStatements).toHaveLength(1);
116
117 const liveRelay = yield* mm.getRelay({ RelayId: deployed.relay.relayId });
118 expect(liveRelay.ServerName).toBe("smtp.example.com");
119 expect(liveRelay.ServerPort).toBe(25);
120
121 const ruleSetTags = yield* mm.listTagsForResource({
122 ResourceArn: deployed.ruleSet.ruleSetArn,
123 });
124 expect(ruleSetTags.Tags?.some((t) => t.Key === "alchemy::id" && t.Value === "Inbound")).toBe(
125 true,
126 );
127 expect(ruleSetTags.Tags?.some((t) => t.Key === "fixture" && t.Value === "mailmanager")).toBe(
128 true,
129 );
130
131 // Update — rules, statements, relay port, and tags all change in
132 // place; identities are stable.
133 const updated = yield* stack.deploy(
134 Effect.gen(function* () {
135 const ruleSet = yield* RuleSet("Inbound", {
136 rules: [
137 {
138 Name: "DropSecret",
139 Conditions: [
140 {
141 StringExpression: {
142 Evaluate: { Attribute: "SUBJECT" },
143 Operator: "CONTAINS",
144 Values: ["secret"],
145 },
146 },
147 ],
148 Actions: [{ Drop: {} }],
149 },
150 { Name: "DropRest", Actions: [{ Drop: {} }] },
151 ],
152 tags: { fixture: "mailmanager", env: "test" },
153 });
154 const trafficPolicy = yield* TrafficPolicy("Edge", {
155 defaultAction: "DENY",
156 policyStatements: [
157 {
158 Action: "ALLOW",
159 Conditions: [
160 {
161 IpExpression: {
162 Evaluate: { Attribute: "SENDER_IP" },
163 Operator: "CIDR_MATCHES",
164 Values: ["198.51.100.0/24"],
165 },
166 },
167 ],
168 },
169 ],
170 maxMessageSizeBytes: 5_242_880,
171 tags: { fixture: "mailmanager" },
172 });
173 const relay = yield* Relay("Downstream", {
174 serverName: "smtp.example.com",
175 serverPort: 587,
176 authentication: { NoAuthentication: {} },
177 tags: { fixture: "mailmanager" },
178 });
179 return { ruleSet, trafficPolicy, relay };
180 }),
181 );
182
183 expect(updated.ruleSet.ruleSetId).toBe(deployed.ruleSet.ruleSetId);
184 expect(updated.trafficPolicy.trafficPolicyId).toBe(deployed.trafficPolicy.trafficPolicyId);
185 expect(updated.relay.relayId).toBe(deployed.relay.relayId);
186
187 const updatedRuleSet = yield* mm.getRuleSet({ RuleSetId: updated.ruleSet.ruleSetId });
188 expect(updatedRuleSet.Rules).toHaveLength(2);
189 const updatedPolicy = yield* mm.getTrafficPolicy({
190 TrafficPolicyId: updated.trafficPolicy.trafficPolicyId,
191 });
192 expect(updatedPolicy.DefaultAction).toBe("DENY");
193 expect(updatedPolicy.MaxMessageSizeBytes).toBe(5_242_880);
194 const updatedRelay = yield* mm.getRelay({ RelayId: updated.relay.relayId });
195 expect(updatedRelay.ServerPort).toBe(587);
196 const updatedRuleSetTags = yield* mm.listTagsForResource({
197 ResourceArn: updated.ruleSet.ruleSetArn,
198 });
199 expect(updatedRuleSetTags.Tags?.some((t) => t.Key === "env" && t.Value === "test")).toBe(
200 true,
201 );
202
203 // Destroy — everything gone, typed.
204 yield* stack.destroy();
205 const ruleSetError = yield* Effect.flip(
206 mm.getRuleSet({ RuleSetId: updated.ruleSet.ruleSetId }),
207 );
208 expect(ruleSetError._tag).toBe("ResourceNotFoundException");
209 const policyError = yield* Effect.flip(
210 mm.getTrafficPolicy({ TrafficPolicyId: updated.trafficPolicy.trafficPolicyId }),
211 );
212 expect(policyError._tag).toBe("ResourceNotFoundException");
213 const relayError = yield* Effect.flip(mm.getRelay({ RelayId: updated.relay.relayId }));
214 expect(relayError._tag).toBe("ResourceNotFoundException");
215 }),
216 { tags: ["provider:aws", "provider:aws:mailmanager", "live"], timeout: 240_000 },
217 );
218
219 // Address lists and archives are free and provision instantly — full
220 // lifecycle runs ungated. The destroy phase also pins the archive's
221 // PENDING_DELETION semantics (deleted archives tombstone for 30 days) and
222 // re-creates once to prove reruns don't collide with the tombstone.
223 test.provider(
224 "lifecycle: address list + archive",
225 (stack) =>
226 Effect.gen(function* () {
227 yield* stack.destroy();
228
229 // Create.
230 const deployed = yield* stack.deploy(
231 Effect.gen(function* () {
232 const addressList = yield* AddressList("Members", { tags: { fixture: "mailmanager" } });
233 const archive = yield* Archive("Mail", {
234 retentionPeriod: "THREE_MONTHS",
235 tags: { fixture: "mailmanager" },
236 });
237 return { addressList, archive };
238 }),
239 );
240
241 expect(deployed.addressList.addressListId).toBeDefined();
242 expect(deployed.addressList.addressListArn).toContain("mailmanager");
243 expect(deployed.archive.archiveId).toBeDefined();
244 expect(deployed.archive.archiveArn).toContain("mailmanager");
245 expect(deployed.archive.archiveState).toBe("ACTIVE");
246
247 // Out-of-band verification via distilled.
248 const liveList = yield* mm.getAddressList({
249 AddressListId: deployed.addressList.addressListId,
250 });
251 expect(liveList.AddressListName).toBe(deployed.addressList.addressListName);
252 const liveArchive = yield* mm.getArchive({ ArchiveId: deployed.archive.archiveId });
253 expect(liveArchive.ArchiveName).toBe(deployed.archive.archiveName);
254 expect(liveArchive.Retention?.RetentionPeriod).toBe("THREE_MONTHS");
255 const listTags = yield* mm.listTagsForResource({
256 ResourceArn: deployed.addressList.addressListArn,
257 });
258 expect(listTags.Tags?.some((t) => t.Key === "alchemy::id" && t.Value === "Members")).toBe(
259 true,
260 );
261
262 // Update — archive retention changes in place; address list tags
263 // sync; identities are stable.
264 const updated = yield* stack.deploy(
265 Effect.gen(function* () {
266 const addressList = yield* AddressList("Members", {
267 tags: { fixture: "mailmanager", env: "test" },
268 });
269 const archive = yield* Archive("Mail", {
270 retentionPeriod: "SIX_MONTHS",
271 tags: { fixture: "mailmanager" },
272 });
273 return { addressList, archive };
274 }),
275 );
276
277 expect(updated.addressList.addressListId).toBe(deployed.addressList.addressListId);
278 expect(updated.archive.archiveId).toBe(deployed.archive.archiveId);
279 const updatedArchive = yield* mm.getArchive({ ArchiveId: updated.archive.archiveId });
280 expect(updatedArchive.Retention?.RetentionPeriod).toBe("SIX_MONTHS");
281 const updatedListTags = yield* mm.listTagsForResource({
282 ResourceArn: updated.addressList.addressListArn,
283 });
284 expect(updatedListTags.Tags?.some((t) => t.Key === "env" && t.Value === "test")).toBe(true);
285
286 // Destroy — the list is gone (typed); the archive tombstones in
287 // PENDING_DELETION (or is already invisible).
288 yield* stack.destroy();
289 const listError = yield* Effect.flip(
290 mm.getAddressList({ AddressListId: updated.addressList.addressListId }),
291 );
292 expect(listError._tag).toBe("ResourceNotFoundException");
293 const archiveState = yield* mm.getArchive({ ArchiveId: updated.archive.archiveId }).pipe(
294 Effect.map((a) => a.ArchiveState),
295 Effect.catchTag("ResourceNotFoundException", () =>
296 Effect.succeed("PENDING_DELETION" as const),
297 ),
298 );
299 expect(archiveState).toBe("PENDING_DELETION");
300
301 // Re-create — proves a rerun does not collide with the
302 // pending-deletion tombstone of the same deterministic name.
303 const recreated = yield* stack.deploy(
304 Effect.gen(function* () {
305 const archive = yield* Archive("Mail", { retentionPeriod: "THREE_MONTHS" });
306 return { archive };
307 }),
308 );
309 expect(recreated.archive.archiveId).not.toBe(deployed.archive.archiveId);
310 expect(recreated.archive.archiveState).toBe("ACTIVE");
311
312 yield* stack.destroy();
313 }),
314 { tags: ["provider:aws", "provider:aws:mailmanager", "live"], timeout: 240_000 },
315 );
316
317 // Ingress points provision an SMTP endpoint asynchronously (~1-2 min each
318 // way) — the full lifecycle is gated behind AWS_TEST_MAILMANAGER=1.
319 const assertIngressPointGone = (ingressPointId: string) =>
320 Effect.gen(function* () {
321 const status = yield* mm.getIngressPoint({ IngressPointId: ingressPointId }).pipe(
322 Effect.map((r) => r.Status ?? "unknown"),
323 Effect.catchTag("ResourceNotFoundException", () => Effect.succeed("gone" as const)),
324 );
325 if (status !== "gone" && status !== "DEPROVISIONING") {
326 return yield* Effect.fail(new Error(`ingress point still exists (status: ${status})`));
327 }
328 }).pipe(
329 Effect.retry({ schedule: Schedule.max([Schedule.fixed("10 seconds"), Schedule.recurs(12)]) }),
330 );
331
332 test.provider.skipIf(!process.env.AWS_TEST_MAILMANAGER)(
333 "lifecycle: open ingress point",
334 (stack) =>
335 Effect.gen(function* () {
336 yield* stack.destroy();
337
338 const deployed = yield* stack.deploy(
339 Effect.gen(function* () {
340 const ruleSet = yield* RuleSet("IngressRules", {
341 rules: [{ Name: "DropAll", Actions: [{ Drop: {} }] }],
342 });
343 const trafficPolicy = yield* TrafficPolicy("IngressPolicy", { defaultAction: "ALLOW" });
344 const ingress = yield* IngressPoint("Smtp", {
345 type: "OPEN",
346 ruleSetId: ruleSet.ruleSetId,
347 trafficPolicyId: trafficPolicy.trafficPolicyId,
348 // An omitted TlsPolicy defaults to FIPS, and AWS rejects any
349 // update from or to FIPS — start in the updatable regime.
350 tlsPolicy: "REQUIRED",
351 tags: { fixture: "mailmanager" },
352 });
353 return { ruleSet, trafficPolicy, ingress };
354 }),
355 );
356
357 expect(deployed.ingress.ingressPointId).toBeDefined();
358 expect(deployed.ingress.ingressPointArn).toContain("mailmanager");
359
360 // Out-of-band verification via distilled.
361 const live = yield* mm.getIngressPoint({ IngressPointId: deployed.ingress.ingressPointId });
362 expect(live.Type).toBe("OPEN");
363 expect(live.RuleSetId).toBe(deployed.ruleSet.ruleSetId);
364 expect(live.TrafficPolicyId).toBe(deployed.trafficPolicy.trafficPolicyId);
365 expect(live.TlsPolicy).toBe("REQUIRED");
366
367 // Update in place — TLS policy changes REQUIRED -> OPTIONAL; identity
368 // is stable. Both dependencies stay deployed (engine replace+remove
369 // deadlock).
370 const updated = yield* stack.deploy(
371 Effect.gen(function* () {
372 const ruleSet = yield* RuleSet("IngressRules", {
373 rules: [{ Name: "DropAll", Actions: [{ Drop: {} }] }],
374 });
375 const trafficPolicy = yield* TrafficPolicy("IngressPolicy", { defaultAction: "ALLOW" });
376 const ingress = yield* IngressPoint("Smtp", {
377 type: "OPEN",
378 ruleSetId: ruleSet.ruleSetId,
379 trafficPolicyId: trafficPolicy.trafficPolicyId,
380 tlsPolicy: "OPTIONAL",
381 tags: { fixture: "mailmanager" },
382 });
383 return { ingress };
384 }),
385 );
386
387 expect(updated.ingress.ingressPointId).toBe(deployed.ingress.ingressPointId);
388 const updatedLive = yield* mm.getIngressPoint({
389 IngressPointId: updated.ingress.ingressPointId,
390 });
391 expect(updatedLive.TlsPolicy).toBe("OPTIONAL");
392
393 // Destroy — deletion is asynchronous; verify it is at least
394 // irreversibly deprovisioning.
395 yield* stack.destroy();
396 yield* assertIngressPointGone(updated.ingress.ingressPointId);
397 }),
398 { tags: ["provider:aws", "provider:aws:mailmanager", "live"], timeout: 600_000 },
399 );
400
401 // Add On subscriptions accept third-party terms of use and ADDITIONAL
402 // PRICING — never created outside an explicitly opted-in account.
403 test.provider.skipIf(!process.env.AWS_TEST_MAILMANAGER_ADDONS)(
404 "lifecycle: addon subscription + addon instance",
405 (stack) =>
406 Effect.gen(function* () {
407 yield* stack.destroy();
408
409 const deployed = yield* stack.deploy(
410 Effect.gen(function* () {
411 const subscription = yield* AddonSubscription("Spamhaus", {
412 addonName: "SPAMHAUS_DBL",
413 tags: { fixture: "mailmanager" },
414 });
415 const instance = yield* AddonInstance("SpamhausInstance", {
416 addonSubscriptionId: subscription.addonSubscriptionId,
417 tags: { fixture: "mailmanager" },
418 });
419 return { subscription, instance };
420 }),
421 );
422
423 expect(deployed.subscription.addonSubscriptionId).toBeDefined();
424 expect(deployed.subscription.addonName).toBe("SPAMHAUS_DBL");
425 expect(deployed.instance.addonInstanceId).toBeDefined();
426 expect(deployed.instance.addonSubscriptionId).toBe(deployed.subscription.addonSubscriptionId);
427
428 // Out-of-band verification via distilled.
429 const liveSubscription = yield* mm.getAddonSubscription({
430 AddonSubscriptionId: deployed.subscription.addonSubscriptionId,
431 });
432 expect(liveSubscription.AddonName).toBe("SPAMHAUS_DBL");
433 const liveInstance = yield* mm.getAddonInstance({
434 AddonInstanceId: deployed.instance.addonInstanceId,
435 });
436 expect(liveInstance.AddonSubscriptionId).toBe(deployed.subscription.addonSubscriptionId);
437
438 // Destroy — both gone, typed.
439 yield* stack.destroy();
440 const instanceError = yield* Effect.flip(
441 mm.getAddonInstance({ AddonInstanceId: deployed.instance.addonInstanceId }),
442 );
443 expect(instanceError._tag).toBe("ResourceNotFoundException");
444 const subscriptionError = yield* Effect.flip(
445 mm.getAddonSubscription({ AddonSubscriptionId: deployed.subscription.addonSubscriptionId }),
446 );
447 expect(subscriptionError._tag).toBe("ResourceNotFoundException");
448 }),
449 { tags: ["provider:aws", "provider:aws:mailmanager", "live"], timeout: 240_000 },
450 );