1import * as AWS from "@/AWS";
2import { ConfigurationSet, EventDestination } from "@/AWS/PinpointSMSVoiceV2";
3import { Topic } from "@/AWS/SNS";
4import * as Test from "@/Test/Alchemy";
5import * as smsvoice from "@distilled.cloud/aws/pinpoint-sms-voice-v2";
6import { expect } from "alchemy-test";
7import * as Effect from "effect/Effect";
8import * as Schedule from "effect/Schedule";
9
10const { test } = Test.make({ providers: AWS.providers() });
11
12const configSetName = "alchemy-test-smsvoice-event-dest-cs";
13
14const getConfigSet = (name: string) =>
15 smsvoice.describeConfigurationSets({ ConfigurationSetNames: [name] }).pipe(
16 Effect.map((r) => r.ConfigurationSets?.[0]),
17 Effect.catchTag("ResourceNotFoundException", () =>
18 Effect.succeed(undefined),
19 ),
20 );
21
22const assertConfigSetGone = (name: string) =>
23 Effect.gen(function* () {
24 const found = yield* getConfigSet(name);
25 if (found !== undefined) {
26 return yield* Effect.fail(
27 new Error(`configuration set '${name}' still exists`),
28 );
29 }
30 }).pipe(
31 Effect.retry({
32 schedule: Schedule.max([Schedule.fixed("3 seconds"), Schedule.recurs(8)]),
33 }),
34 );
35
36// Configuration sets, SNS topics, and event destinations are all free and
37// provision synchronously — the full lifecycle runs ungated.
38test.provider(
39 "create, update, and destroy an SNS event destination",
40 (stack) =>
41 Effect.gen(function* () {
42 yield* stack.destroy();
43
44 const program = (types: string[], enabled?: boolean) =>
45 Effect.gen(function* () {
46 // Grant End User Messaging SMS permission to publish events.
47 const topic = yield* Topic("Events", {
48 attributes: {
49 Policy: JSON.stringify({
50 Version: "2012-10-17",
51 Statement: [
52 {
53 Effect: "Allow",
54 Principal: { Service: "sms-voice.amazonaws.com" },
55 Action: "sns:Publish",
56 Resource: "*",
57 },
58 ],
59 }),
60 },
61 });
62 const configSet = yield* ConfigurationSet("Config", {
63 configurationSetName: configSetName,
64 });
65 const destination = yield* EventDestination("Dest", {
66 configurationSetName: configSet.configurationSetName,
67 eventDestinationName: "alchemy-test-smsvoice-event-dest",
68 matchingEventTypes: types,
69 ...(enabled === undefined ? {} : { enabled }),
70 snsDestination: { topicArn: topic.topicArn },
71 });
72 return { topic, configSet, destination };
73 });
74
75 // Create.
76 const created = yield* stack.deploy(program(["ALL"]));
77 expect(created.destination.configurationSetName).toBe(configSetName);
78 expect(created.destination.eventDestinationName).toBe(
79 "alchemy-test-smsvoice-event-dest",
80 );
81 expect(created.destination.enabled).toBe(true);
82 expect(created.destination.matchingEventTypes).toEqual(["ALL"]);
83
84 // Out-of-band verification via distilled.
85 const observedSet = yield* getConfigSet(configSetName);
86 const observed = observedSet?.EventDestinations?.find(
87 (ed) => ed.EventDestinationName === "alchemy-test-smsvoice-event-dest",
88 );
89 expect(observed?.Enabled).toBe(true);
90 expect([...(observed?.MatchingEventTypes ?? [])]).toEqual(["ALL"]);
91 expect(observed?.SnsDestination?.TopicArn).toBe(created.topic.topicArn);
92
93 // No-op redeploy keeps the same destination.
94 const noop = yield* stack.deploy(program(["ALL"]));
95 expect(noop.destination.configurationSetArn).toBe(
96 created.destination.configurationSetArn,
97 );
98
99 // Update in place — narrow the event types and disable delivery.
100 const updated = yield* stack.deploy(program(["TEXT_ALL"], false));
101 expect(updated.destination.enabled).toBe(false);
102 expect(updated.destination.matchingEventTypes).toEqual(["TEXT_ALL"]);
103 const reobservedSet = yield* getConfigSet(configSetName);
104 const reobserved = reobservedSet?.EventDestinations?.find(
105 (ed) => ed.EventDestinationName === "alchemy-test-smsvoice-event-dest",
106 );
107 expect(reobserved?.Enabled).toBe(false);
108 expect([...(reobserved?.MatchingEventTypes ?? [])]).toEqual(["TEXT_ALL"]);
109
110 // Destroy and verify out-of-band with a typed wait-until-gone.
111 yield* stack.destroy();
112 yield* assertConfigSetGone(configSetName);
113 }),
114 {
115 tags: [
116 "provider:aws",
117 "provider:aws:pinpointsmsvoicev2",
118 "provider:aws:sns",
119 "live",
120 ],
121 timeout: 240_000,
122 },
123);