The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as AWS from "@alchemy.run/aws"; |
| 2 | import { CustomVerificationEmailTemplate } 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 Result from "effect/Result"; |
| 9 | import * as Schedule from "effect/Schedule"; |
| 10 | |
| 11 | const { test } = Test.make({ providers: AWS.providers() }); |
| 12 | |
| 13 | // SES rejects CreateCustomVerificationEmailTemplate outright when its |
| 14 | // FromEmailAddress is not already a verified identity ("NotFoundException: The |
| 15 | // from email address <...> is not verified"), so the whole suite needs a |
| 16 | // verified sender. Point AWS_TEST_SES_FROM at one to run it. Sending through a |
| 17 | // template additionally requires production (out-of-sandbox) access. |
| 18 | const VERIFIED_FROM = process.env.AWS_TEST_SES_FROM; |
| 19 | const FROM = VERIFIED_FROM ?? "verify@alchemy-test.example.com"; |
| 20 | |
| 21 | class CvetStillExists extends Data.TaggedError("CvetStillExists")<{ |
| 22 | readonly name: string; |
| 23 | }> {} |
| 24 | |
| 25 | const assertCvetDeleted = (name: string) => |
| 26 | sesv2.getCustomVerificationEmailTemplate({ TemplateName: name }).pipe( |
| 27 | Effect.flatMap(() => Effect.fail(new CvetStillExists({ name }))), |
| 28 | Effect.catchTag("NotFoundException", () => Effect.void), |
| 29 | Effect.retry({ |
| 30 | while: (e) => e._tag === "CvetStillExists", |
| 31 | schedule: Schedule.max([Schedule.exponential(500), Schedule.recurs(8)]), |
| 32 | }), |
| 33 | ); |
| 34 | |
| 35 | // UNGATED probe. Both lifecycle tests below need a verified sender, so on a |
| 36 | // bare account this file would otherwise assert nothing at all. This proves |
| 37 | // the gate itself is real and correctly typed: SES refuses to create the |
| 38 | // template when its FromEmailAddress is unverified, and distilled surfaces |
| 39 | // that as NotFoundException rather than an untyped catch-all. If AWS ever |
| 40 | // changes the rejection, this fails instead of silently skipping forever. |
| 41 | test.provider( |
| 42 | "creating a template from an unverified sender fails with the typed tag", |
| 43 | (stack) => |
| 44 | Effect.gen(function* () { |
| 45 | yield* stack.destroy(); |
| 46 | |
| 47 | const result = yield* Effect.result( |
| 48 | stack.deploy( |
| 49 | Effect.gen(function* () { |
| 50 | return yield* CustomVerificationEmailTemplate("UnverifiedProbe", { |
| 51 | fromEmailAddress: "definitely-not-verified@alchemy-test.invalid", |
| 52 | templateSubject: "Please confirm your email", |
| 53 | templateContent: "<html><body>Click to verify your address.</body></html>", |
| 54 | successRedirectionURL: "https://example.com/verified", |
| 55 | failureRedirectionURL: "https://example.com/verify-failed", |
| 56 | }); |
| 57 | }), |
| 58 | ), |
| 59 | ); |
| 60 | |
| 61 | expect(Result.isFailure(result)).toBe(true); |
| 62 | if (Result.isFailure(result)) { |
| 63 | expect(JSON.stringify(result.failure)).toContain("NotFoundException"); |
| 64 | } |
| 65 | |
| 66 | yield* stack.destroy(); |
| 67 | }), |
| 68 | { tags: ["provider:aws", "provider:aws:ses", "live"], timeout: 120_000 }, |
| 69 | ); |
| 70 | |
| 71 | test.provider.skipIf(!VERIFIED_FROM)( |
| 72 | "custom verification template lifecycle: create, update, delete", |
| 73 | (stack) => |
| 74 | Effect.gen(function* () { |
| 75 | yield* stack.destroy(); |
| 76 | |
| 77 | const template = yield* stack.deploy( |
| 78 | Effect.gen(function* () { |
| 79 | return yield* CustomVerificationEmailTemplate("Verify", { |
| 80 | fromEmailAddress: FROM, |
| 81 | templateSubject: "Please confirm your email", |
| 82 | templateContent: "<html><body>Click to verify your address.</body></html>", |
| 83 | successRedirectionURL: "https://example.com/verified", |
| 84 | failureRedirectionURL: "https://example.com/verify-failed", |
| 85 | }); |
| 86 | }), |
| 87 | ); |
| 88 | |
| 89 | expect(template.templateName).toBeDefined(); |
| 90 | |
| 91 | // out-of-band verification via distilled |
| 92 | const observed = yield* sesv2.getCustomVerificationEmailTemplate({ |
| 93 | TemplateName: template.templateName, |
| 94 | }); |
| 95 | expect(observed.TemplateSubject).toBe("Please confirm your email"); |
| 96 | expect(observed.SuccessRedirectionURL).toBe("https://example.com/verified"); |
| 97 | |
| 98 | // update subject and content in place |
| 99 | yield* stack.deploy( |
| 100 | Effect.gen(function* () { |
| 101 | return yield* CustomVerificationEmailTemplate("Verify", { |
| 102 | fromEmailAddress: FROM, |
| 103 | templateSubject: "Confirm your email address", |
| 104 | templateContent: "<html><body>Verify to get started.</body></html>", |
| 105 | successRedirectionURL: "https://example.com/done", |
| 106 | failureRedirectionURL: "https://example.com/verify-failed", |
| 107 | }); |
| 108 | }), |
| 109 | ); |
| 110 | const updated = yield* sesv2.getCustomVerificationEmailTemplate({ |
| 111 | TemplateName: template.templateName, |
| 112 | }); |
| 113 | expect(updated.TemplateSubject).toBe("Confirm your email address"); |
| 114 | expect(updated.SuccessRedirectionURL).toBe("https://example.com/done"); |
| 115 | |
| 116 | yield* stack.destroy(); |
| 117 | yield* assertCvetDeleted(template.templateName); |
| 118 | }), |
| 119 | { tags: ["provider:aws", "provider:aws:ses", "live"], timeout: 120_000 }, |
| 120 | ); |
| 121 | |
| 122 | test.provider.skipIf(!VERIFIED_FROM)( |
| 123 | "custom name replaces on rename", |
| 124 | (stack) => |
| 125 | Effect.gen(function* () { |
| 126 | yield* stack.destroy(); |
| 127 | |
| 128 | const first = yield* stack.deploy( |
| 129 | Effect.gen(function* () { |
| 130 | return yield* CustomVerificationEmailTemplate("Named", { |
| 131 | templateName: "alchemy-test-cvet-a", |
| 132 | fromEmailAddress: FROM, |
| 133 | templateSubject: "A", |
| 134 | templateContent: "<html><body>A</body></html>", |
| 135 | successRedirectionURL: "https://example.com/a", |
| 136 | failureRedirectionURL: "https://example.com/a-fail", |
| 137 | }); |
| 138 | }), |
| 139 | ); |
| 140 | expect(first.templateName).toBe("alchemy-test-cvet-a"); |
| 141 | |
| 142 | const second = yield* stack.deploy( |
| 143 | Effect.gen(function* () { |
| 144 | return yield* CustomVerificationEmailTemplate("Named", { |
| 145 | templateName: "alchemy-test-cvet-b", |
| 146 | fromEmailAddress: FROM, |
| 147 | templateSubject: "B", |
| 148 | templateContent: "<html><body>B</body></html>", |
| 149 | successRedirectionURL: "https://example.com/b", |
| 150 | failureRedirectionURL: "https://example.com/b-fail", |
| 151 | }); |
| 152 | }), |
| 153 | ); |
| 154 | expect(second.templateName).toBe("alchemy-test-cvet-b"); |
| 155 | yield* assertCvetDeleted("alchemy-test-cvet-a"); |
| 156 | |
| 157 | yield* stack.destroy(); |
| 158 | yield* assertCvetDeleted("alchemy-test-cvet-b"); |
| 159 | }), |
| 160 | { tags: ["provider:aws", "provider:aws:ses", "live"], timeout: 120_000 }, |
| 161 | ); |