sailfork / t3code-github / .repos/ alchemy-effect/ packages/ integration-tests/ test/ AWS/ SES/ ConfigurationSet.test.ts

import * as AWS from "@alchemy.run/aws";
import {
  ConfigurationSet,
  ConfigurationSetEventDestination,
  EmailIdentity,
} from "@alchemy.run/aws/SES";
import { Topic } from "@alchemy.run/aws/SNS";
import { expect } from "@alchemy.run/test";
import * as Test from "@alchemy.run/test/Alchemy";
import * as sesv2 from "@distilled.cloud/aws/sesv2";
import * as Data from "effect/Data";
import * as Effect from "effect/Effect";
import * as Schedule from "effect/Schedule";
import { driftActions, repairDrift, waitForDrift } from "../drift.ts";

const { test } = Test.make({ providers: AWS.providers() });

// A verified subdomain with a valid certificate, used to host open/click
// tracking links. SES rejects a redirect domain the account does not own.
const TRACKING_REDIRECT_DOMAIN = process.env.AWS_TEST_SES_REDIRECT_DOMAIN;

class ConfigurationSetStillExists extends Data.TaggedError("ConfigurationSetStillExists")<{
  readonly name: string;
}> {}

const assertConfigurationSetDeleted = (name: string) =>
  sesv2.getConfigurationSet({ ConfigurationSetName: name }).pipe(
    Effect.flatMap(() => Effect.fail(new ConfigurationSetStillExists({ name }))),
    Effect.catchTag("NotFoundException", () => Effect.void),
    Effect.retry({
      while: (e) => e._tag === "ConfigurationSetStillExists",
      schedule: Schedule.max([Schedule.exponential(500), Schedule.recurs(8)]),
    }),
  );

test.provider(
  "configuration set lifecycle: options, tags, no-op convergence",
  (stack) =>
    Effect.gen(function* () {
      yield* stack.destroy();

      const configSet = yield* stack.deploy(
        Effect.gen(function* () {
          return yield* ConfigurationSet("LifecycleConfigSet", {
            reputationMetricsEnabled: true,
            tlsPolicy: "REQUIRE",
            maxDelivery: "1 hour",
            suppressedReasons: ["BOUNCE"],
            tags: { Environment: "test" },
          });
        }),
      );

      expect(configSet.configurationSetName).toBeDefined();
      expect(configSet.configurationSetArn).toContain(":configuration-set/");

      // out-of-band verification via distilled
      const observed = yield* sesv2.getConfigurationSet({
        ConfigurationSetName: configSet.configurationSetName,
      });
      expect(observed.ReputationOptions?.ReputationMetricsEnabled).toBe(true);
      expect(observed.DeliveryOptions?.TlsPolicy).toBe("REQUIRE");
      // Duration.Input prop converted to whole wire seconds
      expect(observed.DeliveryOptions?.MaxDeliverySeconds).toBe(3600);
      expect(observed.SuppressionOptions?.SuppressedReasons).toEqual(["BOUNCE"]);
      const tags = Object.fromEntries((observed.Tags ?? []).map((t) => [t.Key, t.Value]));
      expect(tags.Environment).toBe("test");
      expect(tags["alchemy::id"]).toBe("LifecycleConfigSet");

      // sync options in place: disable sending, relax TLS, widen suppression
      yield* stack.deploy(
        Effect.gen(function* () {
          return yield* ConfigurationSet("LifecycleConfigSet", {
            sendingEnabled: false,
            reputationMetricsEnabled: false,
            tlsPolicy: "OPTIONAL",
            maxDelivery: "30 minutes",
            suppressedReasons: ["BOUNCE", "COMPLAINT"],
            tags: { Environment: "test", Extra: "1" },
          });
        }),
      );
      const updated = yield* sesv2.getConfigurationSet({
        ConfigurationSetName: configSet.configurationSetName,
      });
      expect(updated.SendingOptions?.SendingEnabled).toBe(false);
      expect(updated.ReputationOptions?.ReputationMetricsEnabled).toBe(false);
      expect(updated.DeliveryOptions?.TlsPolicy ?? "OPTIONAL").toBe("OPTIONAL");
      expect(updated.DeliveryOptions?.MaxDeliverySeconds).toBe(1800);
      expect([...(updated.SuppressionOptions?.SuppressedReasons ?? [])].sort()).toEqual([
        "BOUNCE",
        "COMPLAINT",
      ]);
      const updatedTags = Object.fromEntries((updated.Tags ?? []).map((t) => [t.Key, t.Value]));
      expect(updatedTags.Extra).toBe("1");

      yield* stack.destroy();
      yield* assertConfigurationSetDeleted(configSet.configurationSetName);
    }),
  { tags: ["provider:aws", "provider:aws:ses", "live"], timeout: 120_000 },
);

test.provider(
  "custom name replaces on rename",
  (stack) =>
    Effect.gen(function* () {
      yield* stack.destroy();

      const first = yield* stack.deploy(
        Effect.gen(function* () {
          return yield* ConfigurationSet("NamedConfigSet", {
            configurationSetName: "alchemy-test-ses-config-a",
          });
        }),
      );
      expect(first.configurationSetName).toBe("alchemy-test-ses-config-a");

      const second = yield* stack.deploy(
        Effect.gen(function* () {
          return yield* ConfigurationSet("NamedConfigSet", {
            configurationSetName: "alchemy-test-ses-config-b",
          });
        }),
      );
      expect(second.configurationSetName).toBe("alchemy-test-ses-config-b");
      yield* assertConfigurationSetDeleted("alchemy-test-ses-config-a");

      yield* stack.destroy();
      yield* assertConfigurationSetDeleted("alchemy-test-ses-config-b");
    }),
  { tags: ["provider:aws", "provider:aws:ses", "live"], timeout: 120_000 },
);

test.provider(
  "event destination lifecycle: SNS destination, update, delete",
  (stack) =>
    Effect.gen(function* () {
      yield* stack.destroy();

      const { topic, configSet, destination } = yield* stack.deploy(
        Effect.gen(function* () {
          const topic = yield* Topic("SesEventsTopic", {});
          const configSet = yield* ConfigurationSet("EventsConfigSet", {});
          const destination = yield* ConfigurationSetEventDestination("SnsDestination", {
            configurationSetName: configSet.configurationSetName,
            matchingEventTypes: ["SEND", "DELIVERY"],
            snsDestination: { topicArn: topic.topicArn },
          });
          return { topic, configSet, destination };
        }),
      );

      expect(destination.eventDestinationName).toBeDefined();

      // out-of-band verification via distilled
      const observed = yield* sesv2.getConfigurationSetEventDestinations({
        ConfigurationSetName: configSet.configurationSetName,
      });
      const found = (observed.EventDestinations ?? []).find(
        (d) => d.Name === destination.eventDestinationName,
      );
      expect(found).toBeDefined();
      expect(found!.Enabled).toBe(true);
      expect([...found!.MatchingEventTypes].sort()).toEqual(["DELIVERY", "SEND"]);
      expect(found!.SnsDestination?.TopicArn).toBe(topic.topicArn);

      // update event types + disable in place
      yield* stack.deploy(
        Effect.gen(function* () {
          const topic = yield* Topic("SesEventsTopic", {});
          const configSet = yield* ConfigurationSet("EventsConfigSet", {});
          const destination = yield* ConfigurationSetEventDestination("SnsDestination", {
            configurationSetName: configSet.configurationSetName,
            enabled: false,
            matchingEventTypes: ["SEND", "DELIVERY", "BOUNCE", "COMPLAINT"],
            snsDestination: { topicArn: topic.topicArn },
          });
          return { configSet, destination };
        }),
      );
      const updated = yield* sesv2.getConfigurationSetEventDestinations({
        ConfigurationSetName: configSet.configurationSetName,
      });
      const updatedFound = (updated.EventDestinations ?? []).find(
        (d) => d.Name === destination.eventDestinationName,
      );
      expect(updatedFound!.Enabled).toBe(false);
      expect(updatedFound!.MatchingEventTypes).toHaveLength(4);

      yield* stack.destroy();
      yield* assertConfigurationSetDeleted(configSet.configurationSetName);
    }),
  {
    tags: ["provider:aws", "provider:aws:ses", "provider:aws:sns", "live"],
    timeout: 120_000,
  },
);

// Tracking options and VDM options are the two aspects added alongside the
// SESv2 coverage work; both are synced in place, never replaced.
test.provider(
  "syncs VDM options in place",
  (stack) =>
    Effect.gen(function* () {
      yield* stack.destroy();

      const configSet = yield* stack.deploy(
        Effect.gen(function* () {
          return yield* ConfigurationSet("VdmConfigSet", {
            vdm: {
              dashboardEngagementMetrics: "ENABLED",
              guardianOptimizedSharedDelivery: "ENABLED",
            },
          });
        }),
      );

      // out-of-band verification via distilled
      const created = yield* sesv2.getConfigurationSet({
        ConfigurationSetName: configSet.configurationSetName,
      });
      expect(created.VdmOptions?.DashboardOptions?.EngagementMetrics).toBe("ENABLED");
      expect(created.VdmOptions?.GuardianOptions?.OptimizedSharedDelivery).toBe("ENABLED");

      // flip both off in place — no replacement
      yield* stack.deploy(
        Effect.gen(function* () {
          return yield* ConfigurationSet("VdmConfigSet", {
            vdm: {
              dashboardEngagementMetrics: "DISABLED",
              guardianOptimizedSharedDelivery: "DISABLED",
            },
          });
        }),
      );
      const updated = yield* sesv2.getConfigurationSet({
        ConfigurationSetName: configSet.configurationSetName,
      });
      expect(updated.VdmOptions?.DashboardOptions?.EngagementMetrics).toBe("DISABLED");
      expect(updated.VdmOptions?.GuardianOptions?.OptimizedSharedDelivery).toBe("DISABLED");

      yield* stack.destroy();
      yield* assertConfigurationSetDeleted(configSet.configurationSetName);
    }),
  { tags: ["provider:aws", "provider:aws:ses", "live"], timeout: 120_000 },
);

// Open/click tracking needs a verified subdomain with a valid certificate,
// which a bare account has none of — SES rejects an unowned redirect domain.
test.provider.skipIf(!TRACKING_REDIRECT_DOMAIN)(
  "syncs custom tracking options (AWS_TEST_SES_REDIRECT_DOMAIN)",
  (stack) =>
    Effect.gen(function* () {
      yield* stack.destroy();

      const configSet = yield* stack.deploy(
        Effect.gen(function* () {
          return yield* ConfigurationSet("TrackingConfigSet", {
            tracking: {
              customRedirectDomain: TRACKING_REDIRECT_DOMAIN!,
              httpsPolicy: "REQUIRE",
            },
          });
        }),
      );

      const created = yield* sesv2.getConfigurationSet({
        ConfigurationSetName: configSet.configurationSetName,
      });
      expect(created.TrackingOptions?.CustomRedirectDomain).toBe(TRACKING_REDIRECT_DOMAIN);
      expect(created.TrackingOptions?.HttpsPolicy).toBe("REQUIRE");

      // change only the HTTPS policy — applied in place
      yield* stack.deploy(
        Effect.gen(function* () {
          return yield* ConfigurationSet("TrackingConfigSet", {
            tracking: {
              customRedirectDomain: TRACKING_REDIRECT_DOMAIN!,
              httpsPolicy: "OPTIONAL",
            },
          });
        }),
      );
      const updated = yield* sesv2.getConfigurationSet({
        ConfigurationSetName: configSet.configurationSetName,
      });
      expect(updated.TrackingOptions?.HttpsPolicy).toBe("OPTIONAL");

      yield* stack.destroy();
      yield* assertConfigurationSetDeleted(configSet.configurationSetName);
    }),
  { tags: ["provider:aws", "provider:aws:ses", "live"], timeout: 120_000 },
);

// putConfigurationSetVdmOptions replaces VdmOptions wholesale, so a caller
// managing only one member would wipe the other. The provider backfills the
// unmanaged member from observed state; this pins that behavior.
test.provider(
  "managing one VDM member preserves the other",
  (stack) =>
    Effect.gen(function* () {
      yield* stack.destroy();

      const configSet = yield* stack.deploy(
        Effect.gen(function* () {
          return yield* ConfigurationSet("VdmBackfill", {
            vdm: {
              dashboardEngagementMetrics: "ENABLED",
              guardianOptimizedSharedDelivery: "ENABLED",
            },
          });
        }),
      );

      // Re-deploy managing ONLY the dashboard member.
      yield* stack.deploy(
        Effect.gen(function* () {
          return yield* ConfigurationSet("VdmBackfill", {
            vdm: { dashboardEngagementMetrics: "DISABLED" },
          });
        }),
      );

      const observed = yield* sesv2.getConfigurationSet({
        ConfigurationSetName: configSet.configurationSetName,
      });
      expect(observed.VdmOptions?.DashboardOptions?.EngagementMetrics).toBe("DISABLED");
      // Guardian was never mentioned in the second deploy — it must survive.
      expect(observed.VdmOptions?.GuardianOptions?.OptimizedSharedDelivery).toBe("ENABLED");

      yield* stack.destroy();
      yield* assertConfigurationSetDeleted(configSet.configurationSetName);
    }),
  { tags: ["provider:aws", "provider:aws:ses", "live"], timeout: 120_000 },
);

// #2688: `read` must observe configuration set options, event destination
// settings and identity settings, or out-of-band changes read as `unchanged`.
test.provider(
  "drift detects and repairs out-of-band SES configuration changes",
  (stack) =>
    Effect.gen(function* () {
      yield* stack.destroy();

      const domain = `ses-drift-${stack.stage.toLowerCase().replace(/[^a-z0-9-]/g, "-")}.alchemy-test.example.com`;
      const { configSet, destination, identity } = yield* stack.deploy(
        Effect.gen(function* () {
          const topic = yield* Topic("DriftSesTopic", {});
          const configSet = yield* ConfigurationSet("DriftConfigSet", {
            tlsPolicy: "REQUIRE",
            tags: { team: "declared" },
          });
          const destination = yield* ConfigurationSetEventDestination("DriftDestination", {
            configurationSetName: configSet.configurationSetName,
            matchingEventTypes: ["SEND", "DELIVERY"],
            snsDestination: { topicArn: topic.topicArn },
          });
          const identity = yield* EmailIdentity("DriftIdentity", {
            emailIdentity: domain,
            configurationSetName: configSet.configurationSetName,
            feedbackForwardingEnabled: true,
          });
          return { configSet, destination, identity };
        }),
      );
      expect(yield* driftActions(stack)).toEqual({
        DriftSesTopic: "unchanged",
        DriftConfigSet: "unchanged",
        DriftDestination: "unchanged",
        DriftIdentity: "unchanged",
      });

      const name = configSet.configurationSetName;
      yield* sesv2.putConfigurationSetDeliveryOptions({
        ConfigurationSetName: name,
        TlsPolicy: "OPTIONAL",
      });
      yield* sesv2.updateConfigurationSetEventDestination({
        ConfigurationSetName: name,
        EventDestinationName: destination.eventDestinationName,
        EventDestination: {
          Enabled: false,
          MatchingEventTypes: ["SEND"],
          SnsDestination: destination.configuration?.snsTopicArn
            ? { TopicArn: destination.configuration.snsTopicArn }
            : undefined,
        },
      });
      yield* sesv2.putEmailIdentityConfigurationSetAttributes({
        EmailIdentity: identity.emailIdentity,
      });
      yield* sesv2.putEmailIdentityFeedbackAttributes({
        EmailIdentity: identity.emailIdentity,
        EmailForwardingEnabled: false,
      });

      expect(
        (yield* waitForDrift(stack, "DriftConfigSet", "drifted"))?.attr?.configuration,
      ).toMatchObject({
        tlsPolicy: "OPTIONAL",
      });
      expect(
        (yield* waitForDrift(stack, "DriftDestination", "drifted"))?.attr?.configuration,
      ).toMatchObject({
        enabled: false,
        matchingEventTypes: ["SEND"],
      });
      expect(
        (yield* waitForDrift(stack, "DriftIdentity", "drifted"))?.attr?.configuration,
      ).toMatchObject({
        configurationSetName: undefined,
        feedbackForwardingEnabled: false,
      });

      const repaired = yield* repairDrift(stack);
      expect(repaired.resources.DriftConfigSet?.action).toBe("repaired");
      expect(repaired.resources.DriftDestination?.action).toBe("repaired");
      expect(repaired.resources.DriftIdentity?.action).toBe("repaired");
      expect(yield* driftActions(stack)).toEqual({
        DriftSesTopic: "unchanged",
        DriftConfigSet: "unchanged",
        DriftDestination: "unchanged",
        DriftIdentity: "unchanged",
      });

      const restored = yield* sesv2.getConfigurationSet({ ConfigurationSetName: name });
      expect(restored.DeliveryOptions?.TlsPolicy).toBe("REQUIRE");
      const restoredIdentity = yield* sesv2.getEmailIdentity({
        EmailIdentity: identity.emailIdentity,
      });
      expect(restoredIdentity.ConfigurationSetName).toBe(name);
      expect(restoredIdentity.FeedbackForwardingStatus).toBe(true);

      yield* stack.destroy();
      yield* assertConfigurationSetDeleted(name);
    }),
  {
    tags: ["provider:aws", "provider:aws:ses", "provider:aws:sns", "live"],
    timeout: 180_000,
  },
);