@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 441 lines 16.6 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import {
3 ConfigurationSet,
4 ConfigurationSetEventDestination,
5 EmailIdentity,
6 } from "@alchemy.run/aws/SES";
7 import { Topic } from "@alchemy.run/aws/SNS";
8 import { expect } from "@alchemy.run/test";
9 import * as Test from "@alchemy.run/test/Alchemy";
10 import * as sesv2 from "@distilled.cloud/aws/sesv2";
11 import * as Data from "effect/Data";
12 import * as Effect from "effect/Effect";
13 import * as Schedule from "effect/Schedule";
14 import { driftActions, repairDrift, waitForDrift } from "../drift.ts";
15
16 const { test } = Test.make({ providers: AWS.providers() });
17
18 // A verified subdomain with a valid certificate, used to host open/click
19 // tracking links. SES rejects a redirect domain the account does not own.
20 const TRACKING_REDIRECT_DOMAIN = process.env.AWS_TEST_SES_REDIRECT_DOMAIN;
21
22 class ConfigurationSetStillExists extends Data.TaggedError("ConfigurationSetStillExists")<{
23 readonly name: string;
24 }> {}
25
26 const assertConfigurationSetDeleted = (name: string) =>
27 sesv2.getConfigurationSet({ ConfigurationSetName: name }).pipe(
28 Effect.flatMap(() => Effect.fail(new ConfigurationSetStillExists({ name }))),
29 Effect.catchTag("NotFoundException", () => Effect.void),
30 Effect.retry({
31 while: (e) => e._tag === "ConfigurationSetStillExists",
32 schedule: Schedule.max([Schedule.exponential(500), Schedule.recurs(8)]),
33 }),
34 );
35
36 test.provider(
37 "configuration set lifecycle: options, tags, no-op convergence",
38 (stack) =>
39 Effect.gen(function* () {
40 yield* stack.destroy();
41
42 const configSet = yield* stack.deploy(
43 Effect.gen(function* () {
44 return yield* ConfigurationSet("LifecycleConfigSet", {
45 reputationMetricsEnabled: true,
46 tlsPolicy: "REQUIRE",
47 maxDelivery: "1 hour",
48 suppressedReasons: ["BOUNCE"],
49 tags: { Environment: "test" },
50 });
51 }),
52 );
53
54 expect(configSet.configurationSetName).toBeDefined();
55 expect(configSet.configurationSetArn).toContain(":configuration-set/");
56
57 // out-of-band verification via distilled
58 const observed = yield* sesv2.getConfigurationSet({
59 ConfigurationSetName: configSet.configurationSetName,
60 });
61 expect(observed.ReputationOptions?.ReputationMetricsEnabled).toBe(true);
62 expect(observed.DeliveryOptions?.TlsPolicy).toBe("REQUIRE");
63 // Duration.Input prop converted to whole wire seconds
64 expect(observed.DeliveryOptions?.MaxDeliverySeconds).toBe(3600);
65 expect(observed.SuppressionOptions?.SuppressedReasons).toEqual(["BOUNCE"]);
66 const tags = Object.fromEntries((observed.Tags ?? []).map((t) => [t.Key, t.Value]));
67 expect(tags.Environment).toBe("test");
68 expect(tags["alchemy::id"]).toBe("LifecycleConfigSet");
69
70 // sync options in place: disable sending, relax TLS, widen suppression
71 yield* stack.deploy(
72 Effect.gen(function* () {
73 return yield* ConfigurationSet("LifecycleConfigSet", {
74 sendingEnabled: false,
75 reputationMetricsEnabled: false,
76 tlsPolicy: "OPTIONAL",
77 maxDelivery: "30 minutes",
78 suppressedReasons: ["BOUNCE", "COMPLAINT"],
79 tags: { Environment: "test", Extra: "1" },
80 });
81 }),
82 );
83 const updated = yield* sesv2.getConfigurationSet({
84 ConfigurationSetName: configSet.configurationSetName,
85 });
86 expect(updated.SendingOptions?.SendingEnabled).toBe(false);
87 expect(updated.ReputationOptions?.ReputationMetricsEnabled).toBe(false);
88 expect(updated.DeliveryOptions?.TlsPolicy ?? "OPTIONAL").toBe("OPTIONAL");
89 expect(updated.DeliveryOptions?.MaxDeliverySeconds).toBe(1800);
90 expect([...(updated.SuppressionOptions?.SuppressedReasons ?? [])].sort()).toEqual([
91 "BOUNCE",
92 "COMPLAINT",
93 ]);
94 const updatedTags = Object.fromEntries((updated.Tags ?? []).map((t) => [t.Key, t.Value]));
95 expect(updatedTags.Extra).toBe("1");
96
97 yield* stack.destroy();
98 yield* assertConfigurationSetDeleted(configSet.configurationSetName);
99 }),
100 { tags: ["provider:aws", "provider:aws:ses", "live"], timeout: 120_000 },
101 );
102
103 test.provider(
104 "custom name replaces on rename",
105 (stack) =>
106 Effect.gen(function* () {
107 yield* stack.destroy();
108
109 const first = yield* stack.deploy(
110 Effect.gen(function* () {
111 return yield* ConfigurationSet("NamedConfigSet", {
112 configurationSetName: "alchemy-test-ses-config-a",
113 });
114 }),
115 );
116 expect(first.configurationSetName).toBe("alchemy-test-ses-config-a");
117
118 const second = yield* stack.deploy(
119 Effect.gen(function* () {
120 return yield* ConfigurationSet("NamedConfigSet", {
121 configurationSetName: "alchemy-test-ses-config-b",
122 });
123 }),
124 );
125 expect(second.configurationSetName).toBe("alchemy-test-ses-config-b");
126 yield* assertConfigurationSetDeleted("alchemy-test-ses-config-a");
127
128 yield* stack.destroy();
129 yield* assertConfigurationSetDeleted("alchemy-test-ses-config-b");
130 }),
131 { tags: ["provider:aws", "provider:aws:ses", "live"], timeout: 120_000 },
132 );
133
134 test.provider(
135 "event destination lifecycle: SNS destination, update, delete",
136 (stack) =>
137 Effect.gen(function* () {
138 yield* stack.destroy();
139
140 const { topic, configSet, destination } = yield* stack.deploy(
141 Effect.gen(function* () {
142 const topic = yield* Topic("SesEventsTopic", {});
143 const configSet = yield* ConfigurationSet("EventsConfigSet", {});
144 const destination = yield* ConfigurationSetEventDestination("SnsDestination", {
145 configurationSetName: configSet.configurationSetName,
146 matchingEventTypes: ["SEND", "DELIVERY"],
147 snsDestination: { topicArn: topic.topicArn },
148 });
149 return { topic, configSet, destination };
150 }),
151 );
152
153 expect(destination.eventDestinationName).toBeDefined();
154
155 // out-of-band verification via distilled
156 const observed = yield* sesv2.getConfigurationSetEventDestinations({
157 ConfigurationSetName: configSet.configurationSetName,
158 });
159 const found = (observed.EventDestinations ?? []).find(
160 (d) => d.Name === destination.eventDestinationName,
161 );
162 expect(found).toBeDefined();
163 expect(found!.Enabled).toBe(true);
164 expect([...found!.MatchingEventTypes].sort()).toEqual(["DELIVERY", "SEND"]);
165 expect(found!.SnsDestination?.TopicArn).toBe(topic.topicArn);
166
167 // update event types + disable in place
168 yield* stack.deploy(
169 Effect.gen(function* () {
170 const topic = yield* Topic("SesEventsTopic", {});
171 const configSet = yield* ConfigurationSet("EventsConfigSet", {});
172 const destination = yield* ConfigurationSetEventDestination("SnsDestination", {
173 configurationSetName: configSet.configurationSetName,
174 enabled: false,
175 matchingEventTypes: ["SEND", "DELIVERY", "BOUNCE", "COMPLAINT"],
176 snsDestination: { topicArn: topic.topicArn },
177 });
178 return { configSet, destination };
179 }),
180 );
181 const updated = yield* sesv2.getConfigurationSetEventDestinations({
182 ConfigurationSetName: configSet.configurationSetName,
183 });
184 const updatedFound = (updated.EventDestinations ?? []).find(
185 (d) => d.Name === destination.eventDestinationName,
186 );
187 expect(updatedFound!.Enabled).toBe(false);
188 expect(updatedFound!.MatchingEventTypes).toHaveLength(4);
189
190 yield* stack.destroy();
191 yield* assertConfigurationSetDeleted(configSet.configurationSetName);
192 }),
193 {
194 tags: ["provider:aws", "provider:aws:ses", "provider:aws:sns", "live"],
195 timeout: 120_000,
196 },
197 );
198
199 // Tracking options and VDM options are the two aspects added alongside the
200 // SESv2 coverage work; both are synced in place, never replaced.
201 test.provider(
202 "syncs VDM options in place",
203 (stack) =>
204 Effect.gen(function* () {
205 yield* stack.destroy();
206
207 const configSet = yield* stack.deploy(
208 Effect.gen(function* () {
209 return yield* ConfigurationSet("VdmConfigSet", {
210 vdm: {
211 dashboardEngagementMetrics: "ENABLED",
212 guardianOptimizedSharedDelivery: "ENABLED",
213 },
214 });
215 }),
216 );
217
218 // out-of-band verification via distilled
219 const created = yield* sesv2.getConfigurationSet({
220 ConfigurationSetName: configSet.configurationSetName,
221 });
222 expect(created.VdmOptions?.DashboardOptions?.EngagementMetrics).toBe("ENABLED");
223 expect(created.VdmOptions?.GuardianOptions?.OptimizedSharedDelivery).toBe("ENABLED");
224
225 // flip both off in place — no replacement
226 yield* stack.deploy(
227 Effect.gen(function* () {
228 return yield* ConfigurationSet("VdmConfigSet", {
229 vdm: {
230 dashboardEngagementMetrics: "DISABLED",
231 guardianOptimizedSharedDelivery: "DISABLED",
232 },
233 });
234 }),
235 );
236 const updated = yield* sesv2.getConfigurationSet({
237 ConfigurationSetName: configSet.configurationSetName,
238 });
239 expect(updated.VdmOptions?.DashboardOptions?.EngagementMetrics).toBe("DISABLED");
240 expect(updated.VdmOptions?.GuardianOptions?.OptimizedSharedDelivery).toBe("DISABLED");
241
242 yield* stack.destroy();
243 yield* assertConfigurationSetDeleted(configSet.configurationSetName);
244 }),
245 { tags: ["provider:aws", "provider:aws:ses", "live"], timeout: 120_000 },
246 );
247
248 // Open/click tracking needs a verified subdomain with a valid certificate,
249 // which a bare account has none of — SES rejects an unowned redirect domain.
250 test.provider.skipIf(!TRACKING_REDIRECT_DOMAIN)(
251 "syncs custom tracking options (AWS_TEST_SES_REDIRECT_DOMAIN)",
252 (stack) =>
253 Effect.gen(function* () {
254 yield* stack.destroy();
255
256 const configSet = yield* stack.deploy(
257 Effect.gen(function* () {
258 return yield* ConfigurationSet("TrackingConfigSet", {
259 tracking: {
260 customRedirectDomain: TRACKING_REDIRECT_DOMAIN!,
261 httpsPolicy: "REQUIRE",
262 },
263 });
264 }),
265 );
266
267 const created = yield* sesv2.getConfigurationSet({
268 ConfigurationSetName: configSet.configurationSetName,
269 });
270 expect(created.TrackingOptions?.CustomRedirectDomain).toBe(TRACKING_REDIRECT_DOMAIN);
271 expect(created.TrackingOptions?.HttpsPolicy).toBe("REQUIRE");
272
273 // change only the HTTPS policy — applied in place
274 yield* stack.deploy(
275 Effect.gen(function* () {
276 return yield* ConfigurationSet("TrackingConfigSet", {
277 tracking: {
278 customRedirectDomain: TRACKING_REDIRECT_DOMAIN!,
279 httpsPolicy: "OPTIONAL",
280 },
281 });
282 }),
283 );
284 const updated = yield* sesv2.getConfigurationSet({
285 ConfigurationSetName: configSet.configurationSetName,
286 });
287 expect(updated.TrackingOptions?.HttpsPolicy).toBe("OPTIONAL");
288
289 yield* stack.destroy();
290 yield* assertConfigurationSetDeleted(configSet.configurationSetName);
291 }),
292 { tags: ["provider:aws", "provider:aws:ses", "live"], timeout: 120_000 },
293 );
294
295 // putConfigurationSetVdmOptions replaces VdmOptions wholesale, so a caller
296 // managing only one member would wipe the other. The provider backfills the
297 // unmanaged member from observed state; this pins that behavior.
298 test.provider(
299 "managing one VDM member preserves the other",
300 (stack) =>
301 Effect.gen(function* () {
302 yield* stack.destroy();
303
304 const configSet = yield* stack.deploy(
305 Effect.gen(function* () {
306 return yield* ConfigurationSet("VdmBackfill", {
307 vdm: {
308 dashboardEngagementMetrics: "ENABLED",
309 guardianOptimizedSharedDelivery: "ENABLED",
310 },
311 });
312 }),
313 );
314
315 // Re-deploy managing ONLY the dashboard member.
316 yield* stack.deploy(
317 Effect.gen(function* () {
318 return yield* ConfigurationSet("VdmBackfill", {
319 vdm: { dashboardEngagementMetrics: "DISABLED" },
320 });
321 }),
322 );
323
324 const observed = yield* sesv2.getConfigurationSet({
325 ConfigurationSetName: configSet.configurationSetName,
326 });
327 expect(observed.VdmOptions?.DashboardOptions?.EngagementMetrics).toBe("DISABLED");
328 // Guardian was never mentioned in the second deploy — it must survive.
329 expect(observed.VdmOptions?.GuardianOptions?.OptimizedSharedDelivery).toBe("ENABLED");
330
331 yield* stack.destroy();
332 yield* assertConfigurationSetDeleted(configSet.configurationSetName);
333 }),
334 { tags: ["provider:aws", "provider:aws:ses", "live"], timeout: 120_000 },
335 );
336
337 // #2688: `read` must observe configuration set options, event destination
338 // settings and identity settings, or out-of-band changes read as `unchanged`.
339 test.provider(
340 "drift detects and repairs out-of-band SES configuration changes",
341 (stack) =>
342 Effect.gen(function* () {
343 yield* stack.destroy();
344
345 const domain = `ses-drift-${stack.stage.toLowerCase().replace(/[^a-z0-9-]/g, "-")}.alchemy-test.example.com`;
346 const { configSet, destination, identity } = yield* stack.deploy(
347 Effect.gen(function* () {
348 const topic = yield* Topic("DriftSesTopic", {});
349 const configSet = yield* ConfigurationSet("DriftConfigSet", {
350 tlsPolicy: "REQUIRE",
351 tags: { team: "declared" },
352 });
353 const destination = yield* ConfigurationSetEventDestination("DriftDestination", {
354 configurationSetName: configSet.configurationSetName,
355 matchingEventTypes: ["SEND", "DELIVERY"],
356 snsDestination: { topicArn: topic.topicArn },
357 });
358 const identity = yield* EmailIdentity("DriftIdentity", {
359 emailIdentity: domain,
360 configurationSetName: configSet.configurationSetName,
361 feedbackForwardingEnabled: true,
362 });
363 return { configSet, destination, identity };
364 }),
365 );
366 expect(yield* driftActions(stack)).toEqual({
367 DriftSesTopic: "unchanged",
368 DriftConfigSet: "unchanged",
369 DriftDestination: "unchanged",
370 DriftIdentity: "unchanged",
371 });
372
373 const name = configSet.configurationSetName;
374 yield* sesv2.putConfigurationSetDeliveryOptions({
375 ConfigurationSetName: name,
376 TlsPolicy: "OPTIONAL",
377 });
378 yield* sesv2.updateConfigurationSetEventDestination({
379 ConfigurationSetName: name,
380 EventDestinationName: destination.eventDestinationName,
381 EventDestination: {
382 Enabled: false,
383 MatchingEventTypes: ["SEND"],
384 SnsDestination: destination.configuration?.snsTopicArn
385 ? { TopicArn: destination.configuration.snsTopicArn }
386 : undefined,
387 },
388 });
389 yield* sesv2.putEmailIdentityConfigurationSetAttributes({
390 EmailIdentity: identity.emailIdentity,
391 });
392 yield* sesv2.putEmailIdentityFeedbackAttributes({
393 EmailIdentity: identity.emailIdentity,
394 EmailForwardingEnabled: false,
395 });
396
397 expect(
398 (yield* waitForDrift(stack, "DriftConfigSet", "drifted"))?.attr?.configuration,
399 ).toMatchObject({
400 tlsPolicy: "OPTIONAL",
401 });
402 expect(
403 (yield* waitForDrift(stack, "DriftDestination", "drifted"))?.attr?.configuration,
404 ).toMatchObject({
405 enabled: false,
406 matchingEventTypes: ["SEND"],
407 });
408 expect(
409 (yield* waitForDrift(stack, "DriftIdentity", "drifted"))?.attr?.configuration,
410 ).toMatchObject({
411 configurationSetName: undefined,
412 feedbackForwardingEnabled: false,
413 });
414
415 const repaired = yield* repairDrift(stack);
416 expect(repaired.resources.DriftConfigSet?.action).toBe("repaired");
417 expect(repaired.resources.DriftDestination?.action).toBe("repaired");
418 expect(repaired.resources.DriftIdentity?.action).toBe("repaired");
419 expect(yield* driftActions(stack)).toEqual({
420 DriftSesTopic: "unchanged",
421 DriftConfigSet: "unchanged",
422 DriftDestination: "unchanged",
423 DriftIdentity: "unchanged",
424 });
425
426 const restored = yield* sesv2.getConfigurationSet({ ConfigurationSetName: name });
427 expect(restored.DeliveryOptions?.TlsPolicy).toBe("REQUIRE");
428 const restoredIdentity = yield* sesv2.getEmailIdentity({
429 EmailIdentity: identity.emailIdentity,
430 });
431 expect(restoredIdentity.ConfigurationSetName).toBe(name);
432 expect(restoredIdentity.FeedbackForwardingStatus).toBe(true);
433
434 yield* stack.destroy();
435 yield* assertConfigurationSetDeleted(name);
436 }),
437 {
438 tags: ["provider:aws", "provider:aws:ses", "provider:aws:sns", "live"],
439 timeout: 180_000,
440 },
441 );