1import * as AWS from "@/AWS";
2import { MultiRegionEndpoint } from "@/AWS/SES";
3import * as Test from "@/Test/Alchemy";
4import * as sesv2 from "@distilled.cloud/aws/sesv2";
5import { expect } from "alchemy-test";
6import * as Data from "effect/Data";
7import * as Effect from "effect/Effect";
8import * as Schedule from "effect/Schedule";
9
10const { test } = Test.make({ providers: AWS.providers() });
11
12class EndpointStillExists extends Data.TaggedError("EndpointStillExists")<{
13 readonly name: string;
14}> {}
15
16const getEndpoint = (name: string) =>
17 sesv2
18 .getMultiRegionEndpoint({ EndpointName: name })
19 .pipe(
20 Effect.catchTag("NotFoundException", () => Effect.succeed(undefined)),
21 );
22
23const assertEndpointDeleted = (name: string) =>
24 getEndpoint(name).pipe(
25 Effect.flatMap((found) =>
26 found ? Effect.fail(new EndpointStillExists({ name })) : Effect.void,
27 ),
28 Effect.retry({
29 while: (e) => e._tag === "EndpointStillExists",
30 // Deletion is asynchronous (DELETING -> gone); poll a bounded window.
31 schedule: Schedule.max([Schedule.exponential(1000), Schedule.recurs(10)]),
32 }),
33 );
34
35// A multi-region endpoint costs nothing to provision — SES charges $0.03/1k
36// emails routed through it and nothing for the endpoint itself — so this runs
37// ungated. Provisioning is asynchronous: create returns CREATING and reaching
38// READY exceeds the polling budget, so the lifecycle asserts CREATING and
39// deletes from there rather than waiting.
40test.provider(
41 "multi-region endpoint lifecycle: create (CREATING), verify, delete",
42 (stack) =>
43 Effect.gen(function* () {
44 yield* stack.destroy();
45
46 const endpoint = yield* stack.deploy(
47 Effect.gen(function* () {
48 return yield* MultiRegionEndpoint("Global", {
49 regions: ["eu-west-1"],
50 });
51 }),
52 );
53
54 expect(endpoint.endpointName).toBeDefined();
55 expect(endpoint.endpointId).toBeDefined();
56 // Reconcile does not wait for READY, so the status is CREATING (or READY
57 // if provisioning was unusually fast).
58 expect(["CREATING", "READY"]).toContain(endpoint.status);
59
60 // out-of-band verification via distilled
61 const observed = yield* getEndpoint(endpoint.endpointName);
62 expect(observed?.EndpointId).toBe(endpoint.endpointId);
63
64 yield* stack.destroy();
65 yield* assertEndpointDeleted(endpoint.endpointName);
66 }),
67 // Measured ~113s: create returns immediately at CREATING, but the delete
68 // cannot land until SES finishes provisioning (it answers
69 // ConcurrentModificationException until then), so the wall-clock floor is
70 // AWS-side provisioning time. 120s left no headroom and flaked; the wait is
71 // bounded by the provider's retry schedule, not by this timeout.
72 { tags: ["provider:aws", "provider:aws:ses", "live"], timeout: 180_000 },
73);