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

main
ts 277 lines 10 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { ConfigurationSet, EmailIdentity } from "@alchemy.run/aws/SES";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as sesv2 from "@distilled.cloud/aws/sesv2";
6 import * as Data from "effect/Data";
7 import * as Effect from "effect/Effect";
8 import * as Schedule from "effect/Schedule";
9
10 const { test } = Test.make({ providers: AWS.providers() });
11
12 // Deterministic identity names — never verified, creation alone is the test.
13 const TEST_DOMAIN = "ses-identity.alchemy-test.example.com";
14 const TEST_DOMAIN_B = "ses-identity-b.alchemy-test.example.com";
15 const TEST_DOMAIN_TOGGLES = "ses-identity-toggles.alchemy-test.example.com";
16 const TEST_EMAIL = "alchemy-ses-test@example.com";
17
18 // A custom MAIL FROM domain only takes effect on a domain we actually own,
19 // with MX and SPF records published for the subdomain. Point these at such a
20 // pair to exercise the mailFrom sync.
21 const MAIL_FROM_IDENTITY = process.env.AWS_TEST_SES_MAIL_FROM_IDENTITY;
22 const MAIL_FROM_DOMAIN = process.env.AWS_TEST_SES_MAIL_FROM_DOMAIN;
23
24 class IdentityStillExists extends Data.TaggedError("IdentityStillExists")<{
25 readonly name: string;
26 }> {}
27
28 const assertIdentityDeleted = (name: string) =>
29 sesv2.getEmailIdentity({ EmailIdentity: name }).pipe(
30 Effect.flatMap(() => Effect.fail(new IdentityStillExists({ name }))),
31 Effect.catchTag("NotFoundException", () => Effect.void),
32 Effect.retry({
33 while: (e) => e._tag === "IdentityStillExists",
34 schedule: Schedule.max([Schedule.exponential(500), Schedule.recurs(8)]),
35 }),
36 );
37
38 test.provider(
39 "domain identity lifecycle: DKIM tokens, tags, replacement",
40 (stack) =>
41 Effect.gen(function* () {
42 yield* stack.destroy();
43
44 const identity = yield* stack.deploy(
45 Effect.gen(function* () {
46 return yield* EmailIdentity("DomainIdentity", {
47 emailIdentity: TEST_DOMAIN,
48 tags: { Environment: "test" },
49 });
50 }),
51 );
52
53 expect(identity.emailIdentity).toBe(TEST_DOMAIN);
54 expect(identity.identityArn).toContain(`:identity/${TEST_DOMAIN}`);
55 expect(identity.identityType).toBe("DOMAIN");
56 // sandbox: DKIM tokens are returned immediately, verification stays
57 // pending — never block on VerificationStatus=SUCCESS.
58 expect(identity.dkimTokens).toHaveLength(3);
59 expect(identity.verifiedForSendingStatus).toBe(false);
60
61 // out-of-band verification via distilled
62 const observed = yield* sesv2.getEmailIdentity({
63 EmailIdentity: TEST_DOMAIN,
64 });
65 expect(observed.IdentityType).toBe("DOMAIN");
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("DomainIdentity");
69
70 // update tags in place
71 yield* stack.deploy(
72 Effect.gen(function* () {
73 return yield* EmailIdentity("DomainIdentity", {
74 emailIdentity: TEST_DOMAIN,
75 tags: { Environment: "test", Extra: "1" },
76 });
77 }),
78 );
79 const afterTagUpdate = yield* sesv2.getEmailIdentity({
80 EmailIdentity: TEST_DOMAIN,
81 });
82 const updatedTags = Object.fromEntries(
83 (afterTagUpdate.Tags ?? []).map((t) => [t.Key, t.Value]),
84 );
85 expect(updatedTags.Extra).toBe("1");
86
87 // changing the identity replaces it
88 const replaced = yield* stack.deploy(
89 Effect.gen(function* () {
90 return yield* EmailIdentity("DomainIdentity", {
91 emailIdentity: TEST_DOMAIN_B,
92 tags: { Environment: "test" },
93 });
94 }),
95 );
96 expect(replaced.emailIdentity).toBe(TEST_DOMAIN_B);
97 yield* assertIdentityDeleted(TEST_DOMAIN);
98
99 yield* stack.destroy();
100 yield* assertIdentityDeleted(TEST_DOMAIN_B);
101 }),
102 { tags: ["provider:aws", "provider:aws:ses", "live"], timeout: 120_000 },
103 );
104
105 test.provider(
106 "email-address identity is created pending verification",
107 (stack) =>
108 Effect.gen(function* () {
109 yield* stack.destroy();
110
111 const identity = yield* stack.deploy(
112 Effect.gen(function* () {
113 return yield* EmailIdentity("AddressIdentity", {
114 emailIdentity: TEST_EMAIL,
115 });
116 }),
117 );
118
119 expect(identity.identityType).toBe("EMAIL_ADDRESS");
120 // sandbox: verification email goes out; the identity exists but is
121 // not verified for sending — assert pending, never wait for SUCCESS.
122 expect(identity.verifiedForSendingStatus).toBe(false);
123
124 yield* stack.destroy();
125 yield* assertIdentityDeleted(TEST_EMAIL);
126 }),
127 { tags: ["provider:aws", "provider:aws:ses", "live"], timeout: 120_000 },
128 );
129
130 test.provider(
131 "identity with configuration-set association",
132 (stack) =>
133 Effect.gen(function* () {
134 yield* stack.destroy();
135
136 const { identity, configSet } = yield* stack.deploy(
137 Effect.gen(function* () {
138 const configSet = yield* ConfigurationSet("IdentityConfigSet", {});
139 const identity = yield* EmailIdentity("AssociatedIdentity", {
140 emailIdentity: "ses-assoc.alchemy-test.example.com",
141 configurationSetName: configSet.configurationSetName,
142 });
143 return { identity, configSet };
144 }),
145 );
146
147 const observed = yield* sesv2.getEmailIdentity({
148 EmailIdentity: identity.emailIdentity,
149 });
150 expect(observed.ConfigurationSetName).toBe(configSet.configurationSetName);
151
152 // remove the association in place (keep the config set deployed so
153 // the update never races a dependency removal)
154 yield* stack.deploy(
155 Effect.gen(function* () {
156 const configSet = yield* ConfigurationSet("IdentityConfigSet", {});
157 const identity = yield* EmailIdentity("AssociatedIdentity", {
158 emailIdentity: "ses-assoc.alchemy-test.example.com",
159 });
160 return { identity, configSet };
161 }),
162 );
163 const afterRemoval = yield* sesv2.getEmailIdentity({
164 EmailIdentity: identity.emailIdentity,
165 });
166 expect(afterRemoval.ConfigurationSetName).toBeUndefined();
167
168 yield* stack.destroy();
169 yield* assertIdentityDeleted(identity.emailIdentity);
170 }),
171 { tags: ["provider:aws", "provider:aws:ses", "live"], timeout: 120_000 },
172 );
173
174 // The DKIM-signing and feedback-forwarding toggles apply to any identity, so
175 // this runs ungated on the standing (never-verified) test domain.
176 test.provider(
177 "syncs DKIM signing and feedback forwarding in place",
178 (stack) =>
179 Effect.gen(function* () {
180 yield* stack.destroy();
181
182 const identity = yield* stack.deploy(
183 Effect.gen(function* () {
184 return yield* EmailIdentity("TogglesIdentity", {
185 emailIdentity: TEST_DOMAIN_TOGGLES,
186 dkimSigningEnabled: true,
187 feedbackForwardingEnabled: true,
188 });
189 }),
190 );
191
192 const created = yield* sesv2.getEmailIdentity({
193 EmailIdentity: identity.emailIdentity,
194 });
195 expect(created.DkimAttributes?.SigningEnabled).toBe(true);
196 expect(created.FeedbackForwardingStatus).toBe(true);
197
198 // flip both off in place — no replacement
199 yield* stack.deploy(
200 Effect.gen(function* () {
201 return yield* EmailIdentity("TogglesIdentity", {
202 emailIdentity: TEST_DOMAIN_TOGGLES,
203 dkimSigningEnabled: false,
204 feedbackForwardingEnabled: false,
205 });
206 }),
207 );
208 const toggled = yield* sesv2.getEmailIdentity({
209 EmailIdentity: identity.emailIdentity,
210 });
211 expect(toggled.DkimAttributes?.SigningEnabled).toBe(false);
212 expect(toggled.FeedbackForwardingStatus).toBe(false);
213
214 // omitting the props must leave SES's current setting untouched
215 yield* stack.deploy(
216 Effect.gen(function* () {
217 return yield* EmailIdentity("TogglesIdentity", {
218 emailIdentity: TEST_DOMAIN_TOGGLES,
219 });
220 }),
221 );
222 const afterOmit = yield* sesv2.getEmailIdentity({
223 EmailIdentity: identity.emailIdentity,
224 });
225 expect(afterOmit.DkimAttributes?.SigningEnabled).toBe(false);
226 expect(afterOmit.FeedbackForwardingStatus).toBe(false);
227
228 yield* stack.destroy();
229 yield* assertIdentityDeleted(identity.emailIdentity);
230 }),
231 { tags: ["provider:aws", "provider:aws:ses", "live"], timeout: 120_000 },
232 );
233
234 // A custom MAIL FROM domain must be a subdomain of a real identity with MX and
235 // SPF published, so the sync is only observable on a domain we control.
236 test.provider.skipIf(!MAIL_FROM_DOMAIN || !MAIL_FROM_IDENTITY)(
237 "syncs a custom MAIL FROM domain (AWS_TEST_SES_MAIL_FROM_IDENTITY + _DOMAIN)",
238 (stack) =>
239 Effect.gen(function* () {
240 yield* stack.destroy();
241
242 const identity = yield* stack.deploy(
243 Effect.gen(function* () {
244 return yield* EmailIdentity("MailFromIdentity", {
245 emailIdentity: MAIL_FROM_IDENTITY!,
246 mailFromDomain: MAIL_FROM_DOMAIN,
247 mailFromBehaviorOnMxFailure: "USE_DEFAULT_VALUE",
248 });
249 }),
250 );
251
252 const created = yield* sesv2.getEmailIdentity({
253 EmailIdentity: identity.emailIdentity,
254 });
255 expect(created.MailFromAttributes?.MailFromDomain).toBe(MAIL_FROM_DOMAIN);
256 expect(created.MailFromAttributes?.BehaviorOnMxFailure).toBe("USE_DEFAULT_VALUE");
257
258 // change only the MX-failure behavior — applied in place
259 yield* stack.deploy(
260 Effect.gen(function* () {
261 return yield* EmailIdentity("MailFromIdentity", {
262 emailIdentity: MAIL_FROM_IDENTITY!,
263 mailFromDomain: MAIL_FROM_DOMAIN,
264 mailFromBehaviorOnMxFailure: "REJECT_MESSAGE",
265 });
266 }),
267 );
268 const updated = yield* sesv2.getEmailIdentity({
269 EmailIdentity: identity.emailIdentity,
270 });
271 expect(updated.MailFromAttributes?.BehaviorOnMxFailure).toBe("REJECT_MESSAGE");
272
273 yield* stack.destroy();
274 yield* assertIdentityDeleted(identity.emailIdentity);
275 }),
276 { tags: ["provider:aws", "provider:aws:ses", "live"], timeout: 120_000 },
277 );