sailfork / t3code-github / .repos/ alchemy-effect/ packages/ integration-tests/ test/ AWS/ RDS/ DBInstance.test.ts

import * as AWS from "@alchemy.run/aws";
import { Network } from "@alchemy.run/aws/EC2/Network";
import { SecurityGroup } from "@alchemy.run/aws/EC2/SecurityGroup";
import { AWSEnvironment } from "@alchemy.run/aws/Environment";
import { normalizePolicyDocument, type PolicyDocument } from "@alchemy.run/aws/IAM/Policy";
import { Key } from "@alchemy.run/aws/KMS/Key";
import { DBCluster, DBInstance, MigrationRunner, type DBInstanceProps } from "@alchemy.run/aws/RDS";
import { Aurora, type AuroraProps } from "@alchemy.run/aws/RDS/Aurora";
import { DBParameterGroup } from "@alchemy.run/aws/RDS/DBParameterGroup";
import { DBSubnetGroup } from "@alchemy.run/aws/RDS/DBSubnetGroup";
import { describe, expect } from "@alchemy.run/test";
import * as Test from "@alchemy.run/test/Alchemy";
import * as ec2 from "@distilled.cloud/aws/ec2";
import * as Lambda from "@distilled.cloud/aws/lambda";
import * as rds from "@distilled.cloud/aws/rds";
import * as rdsdata from "@distilled.cloud/aws/rds-data";
import * as secretsmanager from "@distilled.cloud/aws/secrets-manager";
import * as PgClient from "@effect/sql-pg/PgClient";
import * as Drift from "alchemy/Drift";
import * as Output from "alchemy/Output";
import * as Provider from "alchemy/Provider";
import { State } from "alchemy/State";
import * as Cause from "effect/Cause";
import * as Duration from "effect/Duration";
import * as Effect from "effect/Effect";
import * as Exit from "effect/Exit";
import * as FileSystem from "effect/FileSystem";
import * as HttpClient from "effect/http/HttpClient";
import * as HttpClientResponse from "effect/http/HttpClientResponse";
import * as Path from "effect/Path";
import * as Redacted from "effect/Redacted";
import * as Result from "effect/Result";
import * as Schedule from "effect/Schedule";
import * as Stream from "effect/Stream";
import { driftOf } from "../drift.ts";

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

// Gated lifecycle budget, measured on a live run (us-west-2): a postgres
// db.t3.micro creates in 283–343s, a reboot-applied modify takes 137–199s and
// a delete 90–210s; an Aurora serverless-v2 writer creates in ~380s and
// deletes in ~315s. A create + up to three applied modifies + delete fits in
// 30 minutes; cases that apply four or more modifies (or measured >25 min)
// get 45.
const INSTANCE_TIMEOUT = 1_800_000;
const LONG_INSTANCE_TIMEOUT = 2_700_000;

// Every gated case provisions its own Network + instance and spends 10–25
// minutes doing it, so running them one after another takes ~7 hours. They
// are registered into lanes instead: lanes run concurrently, the cases within
// a lane run sequentially. Nine lanes bound the VPCs and instances alive at
// once (well under the account's VPC quota of 20) while the whole file
// finishes in about an hour. Cases cannot share instances: RDS allows one
// storage modification per instance every six hours, and the others assert
// exact request sequences, adoption, or drift on an instance they own.
type LifecycleCase = Parameters<ReturnType<typeof test.provider.skipIf>>;
const LANES = 9;
const lanes: Array<Array<{ enabled: boolean; args: LifecycleCase }>> = Array.from(
  { length: LANES },
  () => [],
);
const lifecycle =
  (lane: number, gate: string | undefined) =>
  (...args: LifecycleCase) => {
    lanes[lane - 1]!.push({ enabled: !!gate, args });
  };

// Render a deploy failure (whatever engine wrapper it arrives in) to a string
// we can assert AWS's parameter-validation message against.
const renderFailure = (attempt: Result.Result<unknown, unknown>): string => {
  if (!Result.isFailure(attempt)) {
    return "";
  }
  const failure = attempt.failure;
  const json = (() => {
    try {
      return JSON.stringify(failure);
    } catch {
      return "";
    }
  })();
  return `${String(failure)} ${json}`;
};

// Live wire probes for this PR's Redacted/Duration prop conversions on
// DBInstance (the instance reconcile has its own conversion code, separate
// from DBCluster's). Both drive the full engine + provider `reconcile` path
// into a real `createDBInstance` call that AWS rejects at
// parameter-validation time — nothing is provisioned and the probe completes
// in seconds. Probe 1 proves `masterUserPassword: Redacted.Redacted<string>`
// serializes to the actual secret characters on the wire; probe 2 proves
// `backupRetentionPeriod: Duration.Input` ("60 days") arrives as integer days
// (rejected as > the 35-day maximum). These rejected requests do not verify
// the RDS_TEST_LIFECYCLE-gated storage lifecycle below.
test.provider(
  "wire probe: Redacted password + Duration retention reach createDBInstance",
  (stack) =>
    Effect.gen(function* () {
      yield* stack.destroy();

      const network = Effect.gen(function* () {
        const net = yield* Network("ProbeNet", { cidrBlock: "10.47.0.0/16" });
        return yield* DBSubnetGroup("ProbeSubnetGroup", {
          description: "RDS serialization probe network",
          subnetIds: net.privateSubnetIds,
        });
      });
      const badPassword = yield* Effect.result(
        stack.deploy(
          Effect.gen(function* () {
            const subnetGroup = yield* network;
            return yield* DBInstance("AuditProbeInstance", {
              dbInstanceIdentifier: "alchemy-audit-probe-instance",
              dbSubnetGroupName: subnetGroup.dbSubnetGroupName,
              engine: "postgres",
              dbInstanceClass: "db.t3.micro",
              allocatedStorage: 20,
              masterUsername: "alchemy",
              // '@' and ' ' are forbidden password characters — AWS rejects
              // the create before provisioning anything.
              masterUserPassword: Redacted.make("bad@pass word1"),
              backupRetentionPeriod: "3 days",
            });
          }),
        ),
      );
      expect(Result.isFailure(badPassword)).toBe(true);
      expect(renderFailure(badPassword)).toContain("InvalidParameterValue");
      expect(renderFailure(badPassword)).toContain("MasterUserPassword");

      const badRetention = yield* Effect.result(
        stack.deploy(
          Effect.gen(function* () {
            const subnetGroup = yield* network;
            return yield* DBInstance("AuditProbeInstance", {
              dbInstanceIdentifier: "alchemy-audit-probe-instance",
              dbSubnetGroupName: subnetGroup.dbSubnetGroupName,
              engine: "postgres",
              dbInstanceClass: "db.t3.micro",
              allocatedStorage: 20,
              masterUsername: "alchemy",
              masterUserPassword: Redacted.make("ValidPassw0rd"),
              // 60 days is above the 1-35 day API maximum — AWS can only
              // reject it if the converted integer arrived on the wire.
              backupRetentionPeriod: "60 days",
            });
          }),
        ),
      );
      expect(Result.isFailure(badRetention)).toBe(true);
      expect(renderFailure(badRetention)).toContain("InvalidParameterValue");
      expect(renderFailure(badRetention)).toMatch(/retention/i);

      const invalidFlags = yield* rds
        .describeDBEngineVersions({
          Engine: "postgres",
          IncludeAll: true,
          DefaultOnly: true,
        })
        .pipe(Effect.result);
      expect(Result.isFailure(invalidFlags)).toBe(true);
      if (Result.isFailure(invalidFlags)) {
        expect(invalidFlags.failure._tag).toBe("InvalidParameterCombination");
      }
      const emptyGroups = yield* stack
        .deploy(
          Effect.gen(function* () {
            const subnetGroup = yield* network;
            return yield* DBInstance("AuditProbeInstance", {
              dbInstanceIdentifier: "alchemy-audit-probe-instance",
              engine: "postgres",
              dbInstanceClass: "db.t3.micro",
              dbSubnetGroupName: subnetGroup.dbSubnetGroupName,
              vpcSecurityGroupIds: [],
              masterUsername: "alchemy",
              masterUserPassword: Redacted.make("bad@pass word1"),
            });
          }),
        )
        .pipe(Effect.result);
      expect(Result.isFailure(emptyGroups)).toBe(true);
      expect(renderFailure(emptyGroups)).toContain("InvalidDBInstanceAssociations");

      yield* stack.destroy();
    }),
  {
    tags: ["provider:aws", "provider:aws:ec2", "provider:aws:rds", "live"],
    timeout: 120_000,
  },
);

// Default (read-only) path: an RDS instance takes many minutes to create and
// delete — far beyond the 240s test budget — so the canonical `list()` test
// here does NOT deploy. It resolves the provider via the typed
// `Provider.findProvider(DBInstance)` helper and calls `list()` directly,
// asserting it returns a well-typed `DBInstance["Attributes"][]`. On a fresh
// account this is typically empty; either way every element must conform to
// the exact `read` shape.
test.provider(
  "list returns well-typed DB instance attributes",
  () =>
    Effect.gen(function* () {
      const provider = yield* Provider.findProvider(DBInstance);
      const all = yield* provider.list();

      expect(Array.isArray(all)).toBe(true);

      // Every element must match the exact `Attributes` shape `read` produces.
      for (const instance of all) {
        expect(typeof instance.dbInstanceIdentifier).toBe("string");
        expect(typeof instance.dbInstanceArn).toBe("string");
        expect(Array.isArray(instance.dbParameterGroupNames)).toBe(true);
        expect(typeof instance.tags).toBe("object");
      }
    }),
  { tags: ["provider:aws", "provider:aws:rds", "live"] },
);

// Full lifecycle is gated: provisioning an Aurora cluster + instance and then
// tearing it down takes many minutes, exceeding the 240s budget. Set
// AWS_TEST_RDS_DBINSTANCE=1 on an account that can afford the wait to run it.
// It deploys a serverless-v2 Aurora cluster + instance and asserts the
// instance appears in the exhaustively-paginated `list()` result.
lifecycle(3, process.env.AWS_TEST_RDS_DBINSTANCE)(
  "list enumerates the deployed DB instance",
  (stack) =>
    Effect.gen(function* () {
      yield* stack.destroy();

      const { client, requests } = yield* observeInstanceRequests;
      const program = (round: string) =>
        Effect.gen(function* () {
          const network = yield* Network("ListNet", {
            cidrBlock: "10.43.0.0/16",
          });
          const subnetGroup = yield* DBSubnetGroup("ListSubnetGroup", {
            description: "alchemy instance list lifecycle",
            subnetIds: network.privateSubnetIds,
          });
          const clusterGroup = yield* SecurityGroup("ListClusterGroup", {
            vpcId: network.vpcId,
            description: "Aurora cluster group",
          });
          const ignoredInstanceGroup = yield* SecurityGroup("ListIgnoredInstanceGroup", {
            vpcId: network.vpcId,
            description: "Ignored instance group",
          });
          const cluster = yield* DBCluster("ListCluster", {
            dbSubnetGroupName: subnetGroup.dbSubnetGroupName,
            vpcSecurityGroupIds: [clusterGroup.groupId],
            engine: "aurora-postgresql",
            engineMode: "provisioned",
            port: 5434,
            serverlessV2ScalingConfiguration: {
              MinCapacity: 0.5,
              MaxCapacity: 1,
            },
            manageMasterUserPassword: true,
            masterUsername: "alchemy",
          });

          return yield* DBInstance("ListInstance", {
            dbClusterIdentifier: cluster.dbClusterIdentifier,
            dbInstanceClass: "db.serverless",
            engine: "aurora-postgresql",
            port: 5435,
            vpcSecurityGroupIds: [ignoredInstanceGroup.groupId],
            tags: { round },
          });
        });
      const instance = yield* stack
        .deploy(program("created"))
        .pipe(Effect.provideService(HttpClient.HttpClient, client));
      expect(instance.endpointPort).toBe(5434);
      const updated = yield* stack
        .deploy(program("updated"))
        .pipe(Effect.provideService(HttpClient.HttpClient, client));
      expect(updated.endpointPort).toBe(5434);
      // #2688: drift observes autoMinorVersionUpgrade.
      const observedInstance = (yield* rds.describeDBInstances({
        DBInstanceIdentifier: updated.dbInstanceIdentifier,
      })).DBInstances![0]!;
      expect((yield* driftOf(stack, "ListInstance"))?.attr?.autoMinorVersionUpgrade).toBe(
        observedInstance.AutoMinorVersionUpgrade,
      );
      expect(requests.filter((request) => request.port !== null)).toEqual([]);
      expect(requests.filter((request) => request.securityGroups.length > 0)).toEqual([]);
      const cluster = (yield* rds.describeDBClusters({
        DBClusterIdentifier: instance.dbClusterIdentifier!,
      })).DBClusters![0]!;
      expect([...updated.vpcSecurityGroupIds].sort()).toEqual(
        cluster.VpcSecurityGroups!.map((group) => group.VpcSecurityGroupId!).sort(),
      );
      const family = (yield* rds.describeDBEngineVersions({
        Engine: "aurora-postgresql",
        EngineVersion: cluster.EngineVersion,
      })).DBEngineVersions![0]!.DBParameterGroupFamily!;
      expect(updated.dbParameterGroupNames).toEqual([`default.${family}`]);
      expect(
        requests.filter(
          (request) => request.action === "ModifyDBInstance" && request.parameterGroup !== null,
        ),
      ).toEqual([]);
      expect(requests.some((request) => request.action === "CreateDBInstance")).toBe(true);
      yield* assertPort(instance.dbInstanceIdentifier, 5434);
      expect((yield* stack.plan(program("updated"))).resources.ListInstance).toMatchObject({
        action: "noop",
      });

      const provider = yield* Provider.findProvider(DBInstance);
      const all = yield* provider.list();

      expect(all.some((i) => i.dbInstanceIdentifier === instance.dbInstanceIdentifier)).toBe(true);

      yield* stack.destroy();
    }),
  {
    tags: ["provider:aws", "provider:aws:ec2", "provider:aws:rds", "live"],
    timeout: INSTANCE_TIMEOUT,
  },
);

// RDS provisioning and storage optimization exceed the default test budget.
lifecycle(1, process.env.RDS_TEST_LIFECYCLE)(
  "standalone instance: autoscaling defaults, drift, and allocation floor",
  (stack) =>
    Effect.gen(function* () {
      yield* stack.destroy();

      // The testing account has no default VPC/subnets, so provision a
      // production-shaped network (VPC + subnets across 2 AZs) and a DB subnet
      // group for the instance to live in.
      const network = Effect.gen(function* () {
        const net = yield* Network("RdsNet", { cidrBlock: "10.41.0.0/16" });
        // No fixed name — let the engine generate a unique physical name so a
        // leftover group from an interrupted run can't force a cross-VPC
        // ModifyDBSubnetGroup ("new Subnets are not in the same Vpc").
        const subnetGroup = yield* DBSubnetGroup("RdsSubnetGroup", {
          description: "alchemy standalone instance lifecycle",
          subnetIds: net.privateSubnetIds,
        });
        return { dbSubnetGroupName: subnetGroup.dbSubnetGroupName };
      });

      const program = (
        allocatedStorage: number,
        maxAllocatedStorage?: number,
        backupRetentionPeriod: "1 day" | "3 days" = "1 day",
        enablePerformanceInsights = false,
      ) =>
        Effect.gen(function* () {
          const { dbSubnetGroupName } = yield* network;
          return yield* DBInstance("StandaloneInstance", {
            engine: "postgres",
            dbInstanceClass: "db.t3.micro",
            allocatedStorage,
            ...(maxAllocatedStorage === undefined ? {} : { maxAllocatedStorage }),
            storageType: "gp2",
            masterUsername: "alchemy",
            manageMasterUserPassword: true,
            backupRetentionPeriod,
            enablePerformanceInsights,
            deletionProtection: false,
            dbSubnetGroupName,
            publiclyAccessible: false,
          });
        });
      const created = yield* stack.deploy(program(20));
      expect(created.allocatedStorage).toBe(20);
      expect(created.storageType).toBe("gp2");
      expect(created.backupRetentionPeriod).toBe(1);
      expect([0, 20]).toContain(created.maxAllocatedStorage ?? 0);
      const describe = rds.describeDBInstances({
        DBInstanceIdentifier: created.dbInstanceIdentifier,
      });
      expect([0, 20]).toContain((yield* describe).DBInstances?.[0]?.MaxAllocatedStorage ?? 0);

      const enabled = yield* stack.deploy(program(20, 40));
      expect(enabled.maxAllocatedStorage).toBe(40);
      expect((yield* describe).DBInstances?.[0]?.MaxAllocatedStorage).toBe(40);
      // Grow beyond the old ceiling while disabling autoscaling in the same request.
      const updated = yield* stack.deploy(program(50, undefined, "3 days", true));
      expect(updated.dbInstanceArn).toBe(created.dbInstanceArn);
      expect(updated.backupRetentionPeriod).toBe(3);
      expect(updated.allocatedStorage).toBe(50);
      expect([0, 50]).toContain(updated.maxAllocatedStorage ?? 0);
      const grown = (yield* describe).DBInstances?.[0];
      expect(grown?.AllocatedStorage).toBe(50);
      expect([0, 50]).toContain(grown?.MaxAllocatedStorage ?? 0);
      expect(grown?.PendingModifiedValues?.AllocatedStorage).toBeUndefined();

      const explicit = program(20, 100, "3 days", true);
      const reenabled = yield* stack.deploy(explicit);
      expect(reenabled.allocatedStorage).toBe(50);
      expect(reenabled.maxAllocatedStorage).toBe(100);
      expect((yield* describe).DBInstances?.[0]?.MaxAllocatedStorage).toBe(100);

      const injectCeiling = Effect.fn(function* (ceiling: number) {
        yield* rds.modifyDBInstance({
          DBInstanceIdentifier: created.dbInstanceIdentifier,
          MaxAllocatedStorage: ceiling,
          ApplyImmediately: true,
        });
        const injected = yield* describe.pipe(
          Effect.repeat({
            schedule: Schedule.spaced("5 seconds"),
            times: 8,
            until: (response) => response.DBInstances?.[0]?.MaxAllocatedStorage === ceiling,
          }),
        );
        expect(injected.DBInstances?.[0]?.MaxAllocatedStorage).toBe(ceiling);
      });
      yield* injectCeiling(120);
      expect((yield* stack.plan(explicit)).resources.StandaloneInstance).toMatchObject({
        action: "update",
      });
      const repairedExplicit = yield* stack.deploy(explicit);
      expect(repairedExplicit.maxAllocatedStorage).toBe(100);
      expect((yield* describe).DBInstances?.[0]?.MaxAllocatedStorage).toBe(100);

      // Lowering the minimum cannot shrink capacity, but omission disables autoscaling.
      const lowerMinimum = program(20);
      expect((yield* stack.plan(lowerMinimum)).resources.StandaloneInstance).toMatchObject({
        action: "update",
      });
      const preserved = yield* stack.deploy(lowerMinimum);
      expect(preserved.dbInstanceArn).toBe(created.dbInstanceArn);
      expect(preserved.allocatedStorage).toBe(50);
      expect([0, 50]).toContain(preserved.maxAllocatedStorage ?? 0);
      const observed = (yield* describe).DBInstances?.[0];
      expect(observed?.AllocatedStorage).toBe(50);
      expect([0, 50]).toContain(observed?.MaxAllocatedStorage ?? 0);
      expect(observed?.PendingModifiedValues?.AllocatedStorage).toBeUndefined();

      yield* injectCeiling(80);
      const drift = yield* Drift.detect({
        name: stack.name,
        stage: stack.stage,
      });
      expect(drift.resources.StandaloneInstance?.action).toBe("drifted");
      expect((yield* stack.plan(lowerMinimum)).resources.StandaloneInstance).toMatchObject({
        action: "update",
      });
      const repaired = yield* stack.deploy(lowerMinimum);
      expect(repaired.dbInstanceArn).toBe(created.dbInstanceArn);
      expect(repaired.allocatedStorage).toBe(50);
      expect([0, 50]).toContain(repaired.maxAllocatedStorage ?? 0);
      const settled = (yield* describe).DBInstances?.[0];
      expect([0, 50]).toContain(settled?.MaxAllocatedStorage ?? 0);
      expect(settled?.AllocatedStorage).toBe(50);
      expect((yield* stack.plan(lowerMinimum)).resources.StandaloneInstance).toMatchObject({
        action: "noop",
      });

      const disabled = program(20, 0);
      const explicitZero = yield* stack.deploy(disabled);
      expect([0, 50]).toContain(explicitZero.maxAllocatedStorage ?? 0);
      expect(explicitZero.allocatedStorage).toBe(50);
      expect((yield* stack.plan(disabled)).resources.StandaloneInstance).toMatchObject({
        action: "noop",
      });

      yield* stack.destroy();
      const gone = yield* describe.pipe(
        Effect.as(false),
        Effect.catchTag("DBInstanceNotFoundFault", () => Effect.succeed(true)),
        Effect.repeat({
          schedule: Schedule.spaced("5 seconds"),
          times: 8,
          until: (absent) => absent,
        }),
      );
      expect(gone).toBe(true);
    }),
  {
    tags: ["provider:aws", "provider:aws:ec2", "provider:aws:rds", "live"],
    timeout: LONG_INSTANCE_TIMEOUT,
  },
);

type StorageProps = Pick<
  DBInstanceProps,
  "allocatedStorage" | "storageType" | "iops" | "storageThroughput" | "maxAllocatedStorage"
>;

type StorageState = readonly [
  allocatedStorage: number,
  storageType: string,
  iops: number,
  storageThroughput: number,
];

const storageProgram = (props: StorageProps) =>
  Effect.gen(function* () {
    const network = yield* Network("StorageNet", { cidrBlock: "10.44.0.0/16" });
    const subnetGroup = yield* DBSubnetGroup("StorageSubnetGroup", {
      description: "alchemy coupled storage lifecycle",
      subnetIds: network.privateSubnetIds,
    });
    return yield* DBInstance("StorageInstance", {
      engine: "postgres",
      dbInstanceClass: "db.t3.micro",
      masterUsername: "alchemy",
      masterUserPassword: Redacted.make("StorageCouplingPass123"),
      dbSubnetGroupName: subnetGroup.dbSubnetGroupName,
      backupRetentionPeriod: "0 days",
      deletionProtection: false,
      skipFinalSnapshot: true,
      publiclyAccessible: false,
      ...props,
    });
  });

const assertStorageState = Effect.fn(function* (
  identifier: string,
  expected: StorageState,
  maximum = 0,
) {
  const observed = (yield* rds.describeDBInstances({
    DBInstanceIdentifier: identifier,
  })).DBInstances?.[0];
  expect([
    observed?.AllocatedStorage,
    observed?.StorageType,
    observed?.Iops ?? 0,
    observed?.StorageThroughput ?? 0,
  ]).toEqual(expected);
  if (maximum === 0) {
    expect([0, expected[0]]).toContain(observed?.MaxAllocatedStorage ?? 0);
  } else {
    expect(observed?.MaxAllocatedStorage).toBe(maximum);
  }
  expect(observed?.PendingModifiedValues?.AllocatedStorage).toBeUndefined();
  expect(observed?.PendingModifiedValues?.StorageType).toBeUndefined();
  expect(observed?.PendingModifiedValues?.Iops).toBeUndefined();
  expect(observed?.PendingModifiedValues?.StorageThroughput).toBeUndefined();
});

// A point-in-time restore swapped into place leaves the same identifier (and
// props) on a different physical instance. Do exactly that against the real
// API: restore the deployed instance to its latest restorable time under a
// temporary identifier, carrying every source setting over, then rename the
// original away and the restored copy into place. The redeploy must publish
// the restored server's `dbiResourceId`; on main it plans no change and keeps
// the old one.
lifecycle(1, process.env.RDS_TEST_LIFECYCLE)(
  "identity: a point-in-time restore swapped into place is picked up on redeploy",
  (stack) =>
    Effect.gen(function* () {
      yield* stack.destroy();
      const identifier = `alchemy-pitr-${stack.stage.toLowerCase().replace(/[^a-z0-9]+/g, "-")}`;
      const restoredId = `${identifier}-restored`;
      const displacedId = `${identifier}-old`;

      const describeInstance = (id: string) =>
        rds.describeDBInstances({ DBInstanceIdentifier: id }).pipe(
          Effect.map((r) => r.DBInstances?.[0]),
          Effect.catchTag("DBInstanceNotFoundFault", () => Effect.succeed(undefined)),
        );
      const waitUntil = (id: string, done: (instance: rds.DBInstance | undefined) => boolean) =>
        describeInstance(id).pipe(
          Effect.repeat({ schedule: Schedule.spaced("20 seconds"), times: 120, until: done }),
        );
      const waitAvailable = (id: string) =>
        waitUntil(id, (i) => i?.DBInstanceStatus === "available");
      const deleteIfPresent = (id: string) =>
        rds
          .deleteDBInstance({
            DBInstanceIdentifier: id,
            SkipFinalSnapshot: true,
            DeleteAutomatedBackups: true,
          })
          .pipe(
            Effect.catchTag("DBInstanceNotFoundFault", () => Effect.void),
            Effect.andThen(waitUntil(id, (i) => i === undefined)),
            Effect.ignore,
          );

      const program = Effect.gen(function* () {
        const net = yield* Network("PitrNet", { cidrBlock: "10.48.0.0/16" });
        const subnetGroup = yield* DBSubnetGroup("PitrSubnetGroup", {
          description: "alchemy DBInstance point-in-time restore identity",
          subnetIds: net.privateSubnetIds,
        });
        return yield* DBInstance("PitrInstance", {
          dbInstanceIdentifier: identifier,
          engine: "postgres",
          dbInstanceClass: "db.t3.micro",
          allocatedStorage: 20,
          masterUsername: "alchemy",
          masterUserPassword: Redacted.make("alchemy-pitr-test-password"),
          backupRetentionPeriod: "1 day",
          deletionProtection: false,
          skipFinalSnapshot: true,
          dbSubnetGroupName: subnetGroup.dbSubnetGroupName,
          publiclyAccessible: false,
        });
      });

      yield* Effect.gen(function* () {
        const first = yield* stack.deploy(program);

        // Automated backups make the instance restorable a few minutes in.
        const source = (yield* waitUntil(
          identifier,
          (i) => i?.LatestRestorableTime !== undefined,
        ))!;

        yield* rds.restoreDBInstanceToPointInTime({
          SourceDBInstanceIdentifier: identifier,
          TargetDBInstanceIdentifier: restoredId,
          UseLatestRestorableTime: true,
          DBInstanceClass: source.DBInstanceClass,
          DBSubnetGroupName: source.DBSubnetGroup?.DBSubnetGroupName,
          VpcSecurityGroupIds: (source.VpcSecurityGroups ?? []).flatMap((g) =>
            g.VpcSecurityGroupId ? [g.VpcSecurityGroupId] : [],
          ),
          DBParameterGroupName: source.DBParameterGroups?.[0]?.DBParameterGroupName,
          OptionGroupName: source.OptionGroupMemberships?.[0]?.OptionGroupName,
          StorageType: source.StorageType,
          AllocatedStorage: source.AllocatedStorage,
          MaxAllocatedStorage: source.MaxAllocatedStorage,
          Iops: source.Iops,
          StorageThroughput: source.StorageThroughput,
          Port: source.Endpoint?.Port,
          PubliclyAccessible: source.PubliclyAccessible,
          MultiAZ: source.MultiAZ,
          EnableIAMDatabaseAuthentication: source.IAMDatabaseAuthenticationEnabled,
          CACertificateIdentifier: source.CACertificateIdentifier,
          BackupRetentionPeriod: source.BackupRetentionPeriod,
          CopyTagsToSnapshot: source.CopyTagsToSnapshot,
          DeletionProtection: false,
          Tags: source.TagList,
        });
        const restored = (yield* waitAvailable(restoredId))!;
        expect(restored.DbiResourceId).not.toBe(first.dbiResourceId);

        // Swap: original out of the way, restored copy into place.
        yield* rds.modifyDBInstance({
          DBInstanceIdentifier: identifier,
          NewDBInstanceIdentifier: displacedId,
          ApplyImmediately: true,
        });
        yield* waitAvailable(displacedId);
        yield* rds.modifyDBInstance({
          DBInstanceIdentifier: restoredId,
          NewDBInstanceIdentifier: identifier,
          ApplyImmediately: true,
        });
        const swapped = (yield* waitUntil(
          identifier,
          (i) => i?.DBInstanceStatus === "available" && i.DbiResourceId === restored.DbiResourceId,
        ))!;
        expect(swapped.DbiResourceId).toBe(restored.DbiResourceId);

        // Unchanged props, new physical server: the redeploy publishes it.
        const second = yield* stack.deploy(program);
        expect(second.dbInstanceIdentifier).toBe(identifier);
        expect(second.dbiResourceId).toBe(restored.DbiResourceId);

        // The displaced original still sits in the stack's subnet group.
        yield* deleteIfPresent(displacedId);
        yield* stack.destroy();
        yield* assertInstanceGone(identifier);
      }).pipe(
        // The displaced original (and a stranded restore) are not tracked by
        // the stack; remove them whether the test passes or fails.
        Effect.ensuring(
          Effect.all([deleteIfPresent(displacedId), deleteIfPresent(restoredId)], {
            concurrency: 2,
          }),
        ),
      );
    }),
  {
    tags: ["provider:aws", "provider:aws:ec2", "provider:aws:rds", "live"],
    timeout: 5_400_000,
  },
);

const assertInstanceGone = Effect.fn(function* (identifier: string) {
  const gone = yield* rds.describeDBInstances({ DBInstanceIdentifier: identifier }).pipe(
    Effect.as(false),
    Effect.catchTag("DBInstanceNotFoundFault", () => Effect.succeed(true)),
    Effect.repeat({
      schedule: Schedule.spaced("5 seconds"),
      times: 8,
      until: (gone) => gone,
    }),
  );
  expect(gone).toBe(true);
});

interface StorageCase {
  name: string;
  initial: StorageProps;
  desired: StorageProps;
  before: StorageState;
  after: StorageState;
  equivalent?: StorageProps;
}

// Each database receives at most one storage modification; optimization can take hours.
const storageCases: StorageCase[] = [
  {
    name: "small gp3 defaults and resize",
    initial: {},
    desired: {
      allocatedStorage: 25,
      storageType: "gp3",
      iops: 3000,
      storageThroughput: 125,
    },
    before: [20, "gp3", 3000, 125],
    after: [25, "gp3", 3000, 125],
    equivalent: {},
  },
  {
    name: "gp3 resize retains provisioned performance",
    initial: { allocatedStorage: 400 },
    desired: { allocatedStorage: 500 },
    before: [400, "gp3", 12000, 500],
    after: [500, "gp3", 12000, 500],
  },
  {
    name: "gp3 IOPS update includes allocation",
    initial: { allocatedStorage: 400 },
    desired: { allocatedStorage: 400, iops: 16000 },
    before: [400, "gp3", 12000, 500],
    after: [400, "gp3", 16000, 500],
  },
  {
    name: "gp3 throughput update includes IOPS",
    initial: { allocatedStorage: 400 },
    desired: { allocatedStorage: 400, storageThroughput: 750 },
    before: [400, "gp3", 12000, 500],
    after: [400, "gp3", 12000, 750],
  },
  {
    name: "gp3 performance removal restores defaults",
    initial: { allocatedStorage: 400, iops: 16000, storageThroughput: 750 },
    desired: { allocatedStorage: 400 },
    before: [400, "gp3", 16000, 750],
    after: [400, "gp3", 12000, 500],
  },
  {
    name: "storage type removal restores gp3",
    initial: { allocatedStorage: 25, storageType: "gp2" },
    desired: {},
    before: [25, "gp2", 0, 0],
    after: [25, "gp3", 3000, 125],
  },
  ...(["io1", "io2"] as const).flatMap((storageType): StorageCase[] => [
    {
      name: `${storageType} resize includes unchanged IOPS`,
      initial: { storageType },
      desired: { storageType, allocatedStorage: 120 },
      before: [100, storageType, 1000, 0],
      after: [120, storageType, 1000, 0],
    },
    {
      name: `${storageType} performance and autoscaling removal are atomic`,
      initial: { storageType, iops: 3000, maxAllocatedStorage: 6000 },
      desired: { storageType },
      before: [100, storageType, 3000, 0],
      after: [100, storageType, 1000, 0],
      equivalent: { storageType, maxAllocatedStorage: 0 },
    },
  ]),
];

// Lane per storage case (index-aligned with `storageCases`).
const storageLanes = [6, 7, 7, 7, 8, 6, 8, 8, 9, 9];

for (const [index, scenario] of storageCases.entries()) {
  lifecycle(storageLanes[index]!, process.env.RDS_TEST_LIFECYCLE)(
    `storage coupling: ${scenario.name}`,
    (stack) =>
      Effect.gen(function* () {
        yield* stack.destroy();
        const created = yield* stack.deploy(storageProgram(scenario.initial));
        yield* assertStorageState(
          created.dbInstanceIdentifier,
          scenario.before,
          scenario.initial.maxAllocatedStorage,
        );
        const updated = yield* stack.deploy(storageProgram(scenario.desired));
        expect(updated.dbInstanceArn).toBe(created.dbInstanceArn);
        yield* assertStorageState(
          updated.dbInstanceIdentifier,
          scenario.after,
          scenario.desired.maxAllocatedStorage,
        );
        const finalProps = scenario.equivalent ?? scenario.desired;
        if (scenario.equivalent) {
          const equivalent = yield* stack.deploy(storageProgram(finalProps));
          expect(equivalent.dbInstanceArn).toBe(created.dbInstanceArn);
          yield* assertStorageState(
            equivalent.dbInstanceIdentifier,
            scenario.after,
            finalProps.maxAllocatedStorage,
          );
        }
        expect(
          (yield* stack.plan(storageProgram(finalProps))).resources.StorageInstance,
        ).toMatchObject({ action: "noop" });
        yield* stack.destroy();
        yield* assertInstanceGone(created.dbInstanceIdentifier);
      }),
    {
      tags: ["provider:aws", "provider:aws:ec2", "provider:aws:rds", "live"],
      timeout: INSTANCE_TIMEOUT,
    },
  );
}

lifecycle(9, process.env.RDS_TEST_LIFECYCLE)(
  "storage coupling: plans correction for external storage drift",
  (stack) =>
    Effect.gen(function* () {
      yield* stack.destroy();
      const desired = storageProgram({});
      const created = yield* stack.deploy(desired);
      yield* assertStorageState(created.dbInstanceIdentifier, [20, "gp3", 3000, 125]);
      yield* rds.modifyDBInstance({
        DBInstanceIdentifier: created.dbInstanceIdentifier,
        StorageType: "gp2",
        ApplyImmediately: true,
      });
      yield* rds
        .describeDBInstances({
          DBInstanceIdentifier: created.dbInstanceIdentifier,
        })
        .pipe(
          Effect.repeat({
            schedule: Schedule.min([
              Schedule.exponential("5 seconds"),
              Schedule.spaced("1 minute"),
            ]),
            times: 10,
            until: (response) => {
              const instance = response.DBInstances?.[0];
              return (
                instance?.StorageType === "gp2" &&
                (instance.Iops ?? 0) === 0 &&
                (instance.StorageThroughput ?? 0) === 0 &&
                instance.PendingModifiedValues?.StorageType === undefined
              );
            },
          }),
        );
      yield* assertStorageState(created.dbInstanceIdentifier, [20, "gp2", 0, 0]);
      const drift = yield* Drift.detect({
        name: stack.name,
        stage: stack.stage,
      });
      expect(drift.resources.StorageInstance?.action).toBe("drifted");
      expect((yield* stack.plan(desired)).resources.StorageInstance).toMatchObject({
        action: "update",
      });
      // A second storage modification must wait for AWS's optimization cooldown.
      yield* stack.destroy();
      yield* assertInstanceGone(created.dbInstanceIdentifier);
    }),
  {
    tags: ["provider:aws", "provider:aws:ec2", "provider:aws:rds", "live"],
    timeout: INSTANCE_TIMEOUT,
  },
);

const observeInstanceRequests = Effect.gen(function* () {
  const client = yield* HttpClient.HttpClient;
  const requests: Array<{
    action: string;
    port: string | null;
    parameterGroup: string | null;
    securityGroups: string[];
    iamAuthentication: string | null;
    publiclyAccessible: string | null;
    deletionProtection: string | null;
    networkType: string | null;
    applyImmediately: string | null;
    enabledLogs: string[];
    disabledLogs: string[];
  }> = [];
  const observedClient = client.pipe(
    HttpClient.tapRequest((request) =>
      Effect.sync(() => {
        if (request.body._tag !== "Uint8Array") return;
        const parameters = new URLSearchParams(new TextDecoder().decode(request.body.body));
        const action = parameters.get("Action");
        if (
          action === "CreateDBInstance" ||
          action === "ModifyDBInstance" ||
          action === "DescribeDBInstances"
        ) {
          requests.push({
            action,
            iamAuthentication: parameters.get("EnableIAMDatabaseAuthentication"),
            publiclyAccessible: parameters.get("PubliclyAccessible"),
            deletionProtection: parameters.get("DeletionProtection"),
            networkType: parameters.get("NetworkType"),
            applyImmediately: parameters.get("ApplyImmediately"),
            enabledLogs: [...parameters.entries()]
              .filter(
                ([key]) =>
                  key.startsWith("EnableCloudwatchLogsExports.") ||
                  key.startsWith("CloudwatchLogsExportConfiguration.EnableLogTypes."),
              )
              .map(([, value]) => value)
              .sort(),
            disabledLogs: [...parameters.entries()]
              .filter(([key]) =>
                key.startsWith("CloudwatchLogsExportConfiguration.DisableLogTypes."),
              )
              .map(([, value]) => value)
              .sort(),
            parameterGroup: parameters.get("DBParameterGroupName"),
            securityGroups: [...parameters.entries()]
              .filter(([key]) => key.startsWith("VpcSecurityGroupIds."))
              .map(([, value]) => value)
              .sort(),
            port: parameters.get(action === "CreateDBInstance" ? "Port" : "DBPortNumber"),
          });
        }
      }),
    ),
  );
  return { client: observedClient, requests };
});

const portProgram = (port?: number, round = "ports", identifier?: string) =>
  Effect.gen(function* () {
    const network = yield* Network("PortNet", { cidrBlock: "10.45.0.0/16" });
    const subnetGroup = yield* DBSubnetGroup("PortSubnetGroup", {
      description: "alchemy listener port lifecycle",
      subnetIds: network.privateSubnetIds,
    });
    return yield* DBInstance("PortInstance", {
      dbInstanceIdentifier: identifier,
      engine: "postgres",
      dbInstanceClass: "db.t3.micro",
      masterUsername: "alchemy",
      manageMasterUserPassword: true,
      dbSubnetGroupName: subnetGroup.dbSubnetGroupName,
      backupRetentionPeriod: "0 days",
      deletionProtection: false,
      skipFinalSnapshot: true,
      publiclyAccessible: false,
      ...(port === undefined ? {} : { port }),
      tags: { round },
    });
  });

const assertPort = Effect.fn(function* (identifier: string, port: number) {
  const instance = (yield* rds.describeDBInstances({
    DBInstanceIdentifier: identifier,
  })).DBInstances?.[0];
  expect(instance?.Endpoint?.Port).toBe(port);
  expect(instance?.PendingModifiedValues?.Port).toBeUndefined();
  expect(["available", "storage-optimization"]).toContain(instance?.DBInstanceStatus);
  return instance;
});

const injectPort = Effect.fn(function* (identifier: string, port: number) {
  yield* rds.modifyDBInstance({
    DBInstanceIdentifier: identifier,
    DBPortNumber: port,
    ApplyImmediately: true,
  });
  yield* rds.describeDBInstances({ DBInstanceIdentifier: identifier }).pipe(
    Effect.repeat({
      schedule: Schedule.min([Schedule.exponential("5 seconds"), Schedule.spaced("1 minute")]),
      times: 10,
      until: (response) => {
        const instance = response.DBInstances?.[0];
        return (
          instance?.DBInstanceStatus === "available" &&
          instance.Endpoint?.Port === port &&
          instance.PendingModifiedValues?.Port === undefined
        );
      },
    }),
  );
  yield* assertPort(identifier, port);
});

lifecycle(4, process.env.RDS_TEST_LIFECYCLE)(
  "listener port: defaults, updates, removal, and no redundant writes",
  (stack) =>
    Effect.gen(function* () {
      yield* stack.destroy();
      const { client, requests } = yield* observeInstanceRequests;
      const deploy = (port?: number, round?: string) =>
        stack
          .deploy(portProgram(port, round))
          .pipe(Effect.provideService(HttpClient.HttpClient, client));
      const writes = () =>
        requests
          .filter((request) => request.action === "ModifyDBInstance" && request.port !== null)
          .map((request) => request.port);
      const created = yield* deploy();
      expect(created.endpointPort).toBe(5432);
      expect(
        requests
          .filter((request) => request.action === "CreateDBInstance")
          .map((request) => request.port),
      ).toEqual(["5432"]);
      expect((yield* assertPort(created.dbInstanceIdentifier, 5432))?.DbInstancePort).toBe(0);

      requests.length = 0;
      yield* deploy(5432, "same-port");
      expect(requests.some((request) => request.action === "DescribeDBInstances")).toBe(true);
      expect(writes()).toEqual([]);

      requests.length = 0;
      const changed = yield* deploy(5433);
      expect(changed.dbInstanceArn).toBe(created.dbInstanceArn);
      expect(changed.endpointPort).toBe(5433);
      expect(writes()).toEqual(["5433"]);
      yield* assertPort(created.dbInstanceIdentifier, 5433);

      requests.length = 0;
      yield* deploy(5433, "same-custom-port");
      expect(writes()).toEqual([]);
      yield* assertPort(created.dbInstanceIdentifier, 5433);

      requests.length = 0;
      const restored = yield* deploy();
      expect(restored.dbInstanceArn).toBe(created.dbInstanceArn);
      expect(restored.endpointPort).toBe(5432);
      expect(writes()).toEqual(["5432"]);
      yield* assertPort(created.dbInstanceIdentifier, 5432);
      expect((yield* stack.plan(portProgram())).resources.PortInstance).toMatchObject({
        action: "noop",
      });
      yield* stack.destroy();
      yield* assertInstanceGone(created.dbInstanceIdentifier);
    }),
  {
    tags: ["provider:aws", "provider:aws:ec2", "provider:aws:rds", "live"],
    timeout: INSTANCE_TIMEOUT,
  },
);

lifecycle(4, process.env.RDS_TEST_LIFECYCLE)(
  "listener port: unchanged-input drift repair and adoption defaults",
  (stack) =>
    Effect.gen(function* () {
      yield* stack.destroy();
      const identifier = "alchemy-rds-port-adoption";
      const program = (port?: number) => portProgram(port, "adoption", identifier);
      const created = yield* stack.deploy(program(5433));
      yield* assertPort(identifier, 5433);
      yield* injectPort(identifier, 5434);
      expect((yield* stack.plan(program(5433))).resources.PortInstance).toMatchObject({
        action: "update",
      });
      const drift = yield* Drift.detect({
        name: stack.name,
        stage: stack.stage,
      });
      expect(drift.resources.PortInstance?.action).toBe("drifted");
      const repaired = yield* stack.deploy(program(5433));
      expect(repaired.dbInstanceArn).toBe(created.dbInstanceArn);
      expect(repaired.endpointPort).toBe(5433);
      yield* assertPort(identifier, 5433);
      expect((yield* stack.plan(program(5433))).resources.PortInstance).toMatchObject({
        action: "noop",
      });

      // Removing the saved row exercises discovery with no previous props or attributes.
      yield* Effect.gen(function* () {
        const state = yield* yield* State;
        yield* state.delete({
          stack: stack.name,
          stage: stack.stage,
          fqn: "PortInstance",
        });
      }).pipe(Effect.provide(stack.state));
      const adopted = yield* stack.deploy(program());
      expect(adopted.dbInstanceArn).toBe(created.dbInstanceArn);
      expect(adopted.endpointPort).toBe(5432);
      yield* assertPort(identifier, 5432);
      expect((yield* stack.plan(program())).resources.PortInstance).toMatchObject({
        action: "noop",
      });

      yield* injectPort(identifier, 5434);
      expect((yield* stack.plan(program())).resources.PortInstance).toMatchObject({
        action: "update",
      });
      const defaultRepaired = yield* stack.deploy(program());
      expect(defaultRepaired.endpointPort).toBe(5432);
      yield* assertPort(identifier, 5432);
      expect((yield* stack.plan(program())).resources.PortInstance).toMatchObject({
        action: "noop",
      });
      yield* stack.destroy();
      yield* assertInstanceGone(identifier);
    }),
  {
    tags: ["provider:aws", "provider:aws:ec2", "provider:aws:rds", "live"],
    timeout: LONG_INSTANCE_TIMEOUT,
  },
);

lifecycle(4, process.env.RDS_TEST_LIFECYCLE)(
  "listener port: waits for an accepted change without resubmitting",
  (stack) =>
    Effect.gen(function* () {
      yield* stack.destroy();
      const created = yield* stack.deploy(portProgram());
      yield* assertPort(created.dbInstanceIdentifier, 5432);
      yield* rds.modifyDBInstance({
        DBInstanceIdentifier: created.dbInstanceIdentifier,
        DBPortNumber: 5433,
        ApplyImmediately: false,
      });
      const { client, requests } = yield* observeInstanceRequests;
      const settled = yield* stack
        .deploy(portProgram(5433))
        .pipe(Effect.provideService(HttpClient.HttpClient, client));
      expect(settled.dbInstanceArn).toBe(created.dbInstanceArn);
      expect(settled.endpointPort).toBe(5433);
      expect(requests.some((request) => request.action === "DescribeDBInstances")).toBe(true);
      expect(
        requests.filter(
          (request) => request.action === "ModifyDBInstance" && request.port !== null,
        ),
      ).toEqual([]);
      yield* assertPort(created.dbInstanceIdentifier, 5433);
      expect((yield* stack.plan(portProgram(5433))).resources.PortInstance).toMatchObject({
        action: "noop",
      });
      yield* stack.destroy();
      yield* assertInstanceGone(created.dbInstanceIdentifier);
    }),
  {
    tags: ["provider:aws", "provider:aws:ec2", "provider:aws:rds", "live"],
    timeout: INSTANCE_TIMEOUT,
  },
);

type SecurityProps = Pick<
  DBInstanceProps,
  | "enableIAMDatabaseAuthentication"
  | "publiclyAccessible"
  | "deletionProtection"
  | "networkType"
  | "enableCloudwatchLogsExports"
>;

const securityProgram = (
  security: SecurityProps = {},
  round = "security",
  identifier?: string,
  preferredMaintenanceWindow?: string,
) =>
  Effect.gen(function* () {
    const network = yield* Network("SecurityNet", {
      cidrBlock: "10.51.0.0/16",
    });
    const subnetGroup = yield* DBSubnetGroup("SecuritySubnetGroup", {
      description: "RDS security desired-state lifecycle",
      subnetIds: network.publicSubnetIds,
    });
    return yield* DBInstance("SecurityInstance", {
      dbInstanceIdentifier: identifier,
      engine: "postgres",
      dbInstanceClass: "db.t3.micro",
      masterUsername: "alchemy",
      manageMasterUserPassword: true,
      dbSubnetGroupName: subnetGroup.dbSubnetGroupName,
      backupRetentionPeriod: "0 days",
      preferredMaintenanceWindow,
      skipFinalSnapshot: true,
      ...security,
      tags: { round },
    });
  });

const assertSecurity = Effect.fn(function* (identifier: string, security: SecurityProps = {}) {
  const instance = (yield* rds.describeDBInstances({
    DBInstanceIdentifier: identifier,
  })).DBInstances?.[0];
  expect(["available", "storage-optimization"]).toContain(instance?.DBInstanceStatus);
  expect(instance?.IAMDatabaseAuthenticationEnabled).toBe(
    security.enableIAMDatabaseAuthentication ?? false,
  );
  expect(instance?.PendingModifiedValues?.IAMDatabaseAuthenticationEnabled).toBeUndefined();
  expect(instance?.PubliclyAccessible).toBe(security.publiclyAccessible ?? false);
  expect(instance?.DeletionProtection).toBe(security.deletionProtection ?? false);
  expect(instance?.NetworkType).toBe(security.networkType ?? "IPV4");
  expect([...(instance?.EnabledCloudwatchLogsExports ?? [])].sort()).toEqual(
    [...new Set(security.enableCloudwatchLogsExports ?? [])].sort(),
  );
  expect(
    instance?.PendingModifiedValues?.PendingCloudwatchLogsExports?.LogTypesToEnable ?? [],
  ).toEqual([]);
  expect(
    instance?.PendingModifiedValues?.PendingCloudwatchLogsExports?.LogTypesToDisable ?? [],
  ).toEqual([]);
  return instance;
});

const waitForSecurityObservation = (
  identifier: string,
  until: (instance: rds.DBInstance) => boolean,
) =>
  rds.describeDBInstances({ DBInstanceIdentifier: identifier }).pipe(
    Effect.map((response) => response.DBInstances?.[0]),
    Effect.repeat({
      schedule: Schedule.min([Schedule.exponential("5 seconds"), Schedule.spaced("1 minute")]),
      times: 10,
      until: (instance) =>
        instance !== undefined &&
        ["available", "storage-optimization"].includes(instance.DBInstanceStatus ?? "") &&
        until(instance),
    }),
    Effect.tap((instance) =>
      Effect.sync(() => {
        expect(instance).toBeDefined();
        expect(["available", "storage-optimization"]).toContain(instance?.DBInstanceStatus);
        expect(instance !== undefined && until(instance)).toBe(true);
      }),
    ),
  );

const enabledSecurity: SecurityProps = {
  enableIAMDatabaseAuthentication: true,
  publiclyAccessible: true,
  deletionProtection: true,
  networkType: "IPV4",
  enableCloudwatchLogsExports: ["postgresql", "upgrade"],
};

lifecycle(2, process.env.RDS_TEST_LIFECYCLE)(
  "security: defaults, removal, drift, adoption, and no redundant writes",
  (stack) =>
    Effect.gen(function* () {
      yield* stack.destroy();
      const identifier = "alchemy-rds-security-defaults";
      const { client, requests } = yield* observeInstanceRequests;
      const program = (security: SecurityProps = {}, round = "security") =>
        securityProgram(security, round, identifier);
      const deploy = (security: SecurityProps = {}, round = "security") =>
        stack
          .deploy(program(security, round))
          .pipe(Effect.provideService(HttpClient.HttpClient, client));
      const created = yield* deploy();
      yield* assertSecurity(identifier);
      expect(created.iamDatabaseAuthenticationEnabled).toBe(false);
      expect(created.pendingIamDatabaseAuthenticationEnabled).toBeUndefined();
      expect(created.publiclyAccessible).toBe(false);
      expect(created.deletionProtection).toBe(false);
      expect(created.networkType).toBe("IPV4");
      expect(created.enabledCloudwatchLogsExports).toEqual([]);
      expect(Object.keys(created.vpcSecurityGroupStatuses).sort()).toEqual(
        [...created.vpcSecurityGroupIds].sort(),
      );
      expect(Object.values(created.vpcSecurityGroupStatuses)).toEqual(["active"]);
      expect(requests.filter((request) => request.action === "CreateDBInstance")).toMatchObject([
        {
          iamAuthentication: "false",
          publiclyAccessible: "false",
          deletionProtection: "false",
          networkType: "IPV4",
          enabledLogs: [],
        },
      ]);

      const enabled = yield* deploy(enabledSecurity);
      expect(enabled.dbInstanceArn).toBe(created.dbInstanceArn);
      expect(enabled.iamDatabaseAuthenticationEnabled).toBe(true);
      expect(enabled.pendingIamDatabaseAuthenticationEnabled).toBeUndefined();
      yield* assertSecurity(identifier, enabledSecurity);
      requests.length = 0;
      yield* deploy(
        {
          ...enabledSecurity,
          enableCloudwatchLogsExports: ["upgrade", "postgresql", "postgresql"],
        },
        "set-equivalent",
      );
      expect(requests.filter((request) => request.action === "ModifyDBInstance")).toEqual([]);

      requests.length = 0;
      const removed = yield* deploy();
      expect(removed.dbInstanceArn).toBe(created.dbInstanceArn);
      yield* assertSecurity(identifier);
      expect(requests.filter((request) => request.disabledLogs.length > 0)).toMatchObject([
        {
          disabledLogs: ["postgresql", "upgrade"],
          applyImmediately: "false",
        },
      ]);
      expect((yield* stack.plan(program())).resources.SecurityInstance).toMatchObject({
        action: "noop",
      });

      const inject = Effect.gen(function* () {
        yield* rds.modifyDBInstance({
          DBInstanceIdentifier: identifier,
          EnableIAMDatabaseAuthentication: true,
          PubliclyAccessible: true,
          DeletionProtection: true,
          CloudwatchLogsExportConfiguration: {
            EnableLogTypes: ["postgresql", "upgrade"],
          },
          ApplyImmediately: true,
        });
        yield* waitForSecurityObservation(
          identifier,
          (instance) =>
            instance.IAMDatabaseAuthenticationEnabled === true &&
            instance.PendingModifiedValues?.IAMDatabaseAuthenticationEnabled === undefined &&
            instance.PubliclyAccessible === true &&
            instance.DeletionProtection === true &&
            (instance.EnabledCloudwatchLogsExports ?? []).length === 2 &&
            (instance.PendingModifiedValues?.PendingCloudwatchLogsExports?.LogTypesToEnable
              ?.length ?? 0) === 0 &&
            (instance.PendingModifiedValues?.PendingCloudwatchLogsExports?.LogTypesToDisable
              ?.length ?? 0) === 0,
        );
        yield* assertSecurity(identifier, enabledSecurity);
      });
      yield* inject;
      const drift = yield* Drift.detect({
        name: stack.name,
        stage: stack.stage,
      });
      expect(drift.resources.SecurityInstance?.action).toBe("drifted");
      expect((yield* stack.plan(program())).resources.SecurityInstance).toMatchObject({
        action: "update",
      });
      const repaired = yield* deploy();
      expect(repaired.dbInstanceArn).toBe(created.dbInstanceArn);
      yield* assertSecurity(identifier);

      yield* inject;
      yield* Effect.gen(function* () {
        const state = yield* yield* State;
        yield* state.delete({
          stack: stack.name,
          stage: stack.stage,
          fqn: "SecurityInstance",
        });
      }).pipe(Effect.provide(stack.state));
      const adopted = yield* deploy();
      expect(adopted.dbInstanceArn).toBe(created.dbInstanceArn);
      yield* assertSecurity(identifier);
      requests.length = 0;
      yield* deploy({}, "tag-only");
      expect(requests.filter((request) => request.action === "ModifyDBInstance")).toEqual([]);
      expect((yield* stack.plan(program({}, "tag-only"))).resources.SecurityInstance).toMatchObject(
        { action: "noop" },
      );
      yield* stack.destroy();
      yield* assertInstanceGone(identifier);
    }),
  {
    tags: ["provider:aws", "provider:aws:ec2", "provider:aws:rds", "live"],
    timeout: LONG_INSTANCE_TIMEOUT,
  },
);

lifecycle(2, process.env.RDS_TEST_LIFECYCLE)(
  "security: maintenance-queued IAM convergence and immediate-only queue isolation",
  (stack) =>
    Effect.gen(function* () {
      yield* stack.destroy();
      const maintenance = yield* Effect.sync(() => {
        const day = ["sun", "mon", "tue", "wed", "thu", "fri", "sat"][
          (new Date().getUTCDay() + 2) % 7
        ];
        return `${day}:03:00-${day}:04:00`;
      });
      const { client, requests } = yield* observeInstanceRequests;
      const program = (security: SecurityProps = {}, round = "security") =>
        securityProgram(security, round, undefined, maintenance);
      const deploy = (security: SecurityProps = {}, round = "security") =>
        stack
          .deploy(program(security, round))
          .pipe(Effect.provideService(HttpClient.HttpClient, client));
      const created = yield* deploy();
      const identifier = created.dbInstanceIdentifier;
      yield* rds.modifyDBInstance({
        DBInstanceIdentifier: identifier,
        BackupRetentionPeriod: 1,
        ApplyImmediately: false,
      });
      yield* waitForSecurityObservation(
        identifier,
        (instance) => instance.PendingModifiedValues?.BackupRetentionPeriod === 1,
      );
      requests.length = 0;
      const immediateOnly: SecurityProps = {
        publiclyAccessible: true,
        deletionProtection: true,
        enableCloudwatchLogsExports: ["postgresql"],
      };
      const changed = yield* deploy(immediateOnly);
      expect(changed.dbInstanceArn).toBe(created.dbInstanceArn);
      expect(
        (yield* assertSecurity(identifier, immediateOnly))?.PendingModifiedValues
          ?.BackupRetentionPeriod,
      ).toBe(1);
      expect(requests.filter((request) => request.action === "ModifyDBInstance")).toMatchObject([
        { applyImmediately: "false", iamAuthentication: null },
      ]);
      expect((yield* stack.plan(program(immediateOnly))).resources.SecurityInstance).toMatchObject({
        action: "noop",
      });
      yield* deploy();
      expect(
        (yield* assertSecurity(identifier))?.PendingModifiedValues?.BackupRetentionPeriod,
      ).toBe(1);
      // Cancel the test's unrelated queued change before exercising queue promotion.
      yield* rds.modifyDBInstance({
        DBInstanceIdentifier: identifier,
        BackupRetentionPeriod: 0,
        ApplyImmediately: true,
      });
      yield* waitForSecurityObservation(
        identifier,
        (instance) =>
          instance.BackupRetentionPeriod === 0 &&
          instance.PendingModifiedValues?.BackupRetentionPeriod === undefined,
      );

      yield* rds.modifyDBInstance({
        DBInstanceIdentifier: identifier,
        EnableIAMDatabaseAuthentication: true,
        ApplyImmediately: false,
      });
      yield* waitForSecurityObservation(
        identifier,
        (instance) =>
          instance.IAMDatabaseAuthenticationEnabled === false &&
          instance.PendingModifiedValues?.IAMDatabaseAuthenticationEnabled === true,
      );
      const pendingDrift = yield* Drift.detect({
        name: stack.name,
        stage: stack.stage,
      });
      expect(pendingDrift.resources.SecurityInstance).toMatchObject({
        action: "drifted",
        attr: {
          iamDatabaseAuthenticationEnabled: false,
          pendingIamDatabaseAuthenticationEnabled: true,
        },
      });
      requests.length = 0;
      const desired = { enableIAMDatabaseAuthentication: true };
      const promoted = yield* deploy(desired);
      expect(promoted.iamDatabaseAuthenticationEnabled).toBe(true);
      expect(promoted.pendingIamDatabaseAuthenticationEnabled).toBeUndefined();
      yield* assertSecurity(identifier, desired);
      // Matching pending IAM needs queue promotion, not another IAM assignment.
      expect(requests.filter((request) => request.action === "ModifyDBInstance")).toMatchObject([
        { applyImmediately: "true", iamAuthentication: null },
      ]);

      yield* rds.modifyDBInstance({
        DBInstanceIdentifier: identifier,
        EnableIAMDatabaseAuthentication: false,
        ApplyImmediately: false,
      });
      yield* waitForSecurityObservation(
        identifier,
        (instance) =>
          instance.IAMDatabaseAuthenticationEnabled === true &&
          instance.PendingModifiedValues?.IAMDatabaseAuthenticationEnabled === false,
      );
      expect((yield* stack.plan(program(desired))).resources.SecurityInstance).toMatchObject({
        action: "update",
      });
      requests.length = 0;
      const repaired = yield* deploy(desired);
      expect(repaired.dbInstanceArn).toBe(created.dbInstanceArn);
      yield* assertSecurity(identifier, desired);
      expect(requests.filter((request) => request.action === "ModifyDBInstance")).toMatchObject([
        { applyImmediately: "true", iamAuthentication: "true" },
      ]);
      requests.length = 0;
      yield* deploy(desired, "tag-only");
      expect(requests.filter((request) => request.action === "ModifyDBInstance")).toEqual([]);
      yield* rds.modifyDBInstance({
        DBInstanceIdentifier: identifier,
        EnableIAMDatabaseAuthentication: false,
        ApplyImmediately: false,
      });
      yield* waitForSecurityObservation(
        identifier,
        (instance) =>
          instance.IAMDatabaseAuthenticationEnabled === true &&
          instance.PendingModifiedValues?.IAMDatabaseAuthenticationEnabled === false,
      );
      requests.length = 0;
      yield* deploy();
      yield* assertSecurity(identifier);
      expect(requests.filter((request) => request.action === "ModifyDBInstance")).toMatchObject([
        { applyImmediately: "true", iamAuthentication: null },
      ]);
      expect((yield* stack.plan(program())).resources.SecurityInstance).toMatchObject({
        action: "noop",
      });
      yield* stack.destroy();
      yield* assertInstanceGone(identifier);
    }),
  {
    tags: ["provider:aws", "provider:aws:ec2", "provider:aws:rds", "live"],
    timeout: INSTANCE_TIMEOUT,
  },
);

lifecycle(3, process.env.AWS_TEST_RDS_DBINSTANCE)(
  "security: Aurora cluster ownership survives omitted membership and adoption",
  (stack) =>
    Effect.gen(function* () {
      yield* stack.destroy();
      const identifier = "alchemy-rds-security-aurora";
      const { client, requests } = yield* observeInstanceRequests;
      const program = (membership = true, explicit = true, round = "created") =>
        Effect.gen(function* () {
          const network = yield* Network("AuroraSecurityNet", {
            cidrBlock: "10.52.0.0/16",
          });
          const subnetGroup = yield* DBSubnetGroup("AuroraSecuritySubnetGroup", {
            description: "Aurora instance security ownership",
            subnetIds: network.privateSubnetIds,
          });
          const cluster = yield* DBCluster("AuroraSecurityCluster", {
            engine: "aurora-postgresql",
            engineMode: "provisioned",
            dbSubnetGroupName: subnetGroup.dbSubnetGroupName,
            masterUsername: "alchemy",
            manageMasterUserPassword: true,
            enableIAMDatabaseAuthentication: true,
            enableCloudwatchLogsExports: ["postgresql"],
            networkType: "IPV4",
            deletionProtection: false,
            serverlessV2ScalingConfiguration: {
              MinCapacity: 0.5,
              MaxCapacity: 1,
            },
          });
          return yield* DBInstance("AuroraSecurityInstance", {
            dbInstanceIdentifier: identifier,
            dbClusterIdentifier: membership ? cluster.dbClusterIdentifier : undefined,
            engine: "aurora-postgresql",
            dbInstanceClass: "db.serverless",
            ...(explicit
              ? {
                  enableIAMDatabaseAuthentication: false,
                  deletionProtection: true,
                  networkType: "DUAL",
                  enableCloudwatchLogsExports: ["not-an-instance-log"],
                }
              : {}),
            tags: { round, cluster: cluster.dbClusterIdentifier },
          });
        });
      const deploy = (membership = true, explicit = true, round = "created") =>
        stack
          .deploy(program(membership, explicit, round))
          .pipe(Effect.provideService(HttpClient.HttpClient, client));
      const created = yield* deploy();
      expect(created.publiclyAccessible).toBe(false);
      const check = Effect.gen(function* () {
        const instance = (yield* rds.describeDBInstances({
          DBInstanceIdentifier: identifier,
        })).DBInstances![0]!;
        const cluster = (yield* rds.describeDBClusters({
          DBClusterIdentifier: instance.DBClusterIdentifier!,
        })).DBClusters![0]!;
        expect(cluster.IAMDatabaseAuthenticationEnabled).toBe(true);
        expect(cluster.EnabledCloudwatchLogsExports).toEqual(["postgresql"]);
        expect(cluster.DeletionProtection).toBe(false);
        expect(cluster.NetworkType).toBe("IPV4");
        expect(instance.PubliclyAccessible).toBe(false);
        expect(
          requests
            .filter(
              (request) =>
                request.action === "CreateDBInstance" || request.action === "ModifyDBInstance",
            )
            .every(
              (request) =>
                request.iamAuthentication === null &&
                request.deletionProtection === null &&
                request.networkType === null &&
                request.enabledLogs.length === 0 &&
                request.disabledLogs.length === 0,
            ),
        ).toBe(true);
        return instance;
      });
      yield* check;
      const omitted = yield* deploy(true, false, "omitted");
      expect(omitted.dbInstanceArn).toBe(created.dbInstanceArn);
      yield* check;
      yield* Effect.gen(function* () {
        const state = yield* yield* State;
        yield* state.delete({
          stack: stack.name,
          stage: stack.stage,
          fqn: "AuroraSecurityInstance",
        });
      }).pipe(Effect.provide(stack.state));
      const adopted = yield* deploy(false, true, "adopted");
      expect(adopted.dbInstanceArn).toBe(created.dbInstanceArn);
      const observed = yield* check;
      expect(adopted.iamDatabaseAuthenticationEnabled).toBe(
        observed.IAMDatabaseAuthenticationEnabled,
      );
      expect(adopted.enabledCloudwatchLogsExports).toEqual(
        observed.EnabledCloudwatchLogsExports ?? [],
      );
      expect(
        (yield* stack.plan(program(false, true, "adopted"))).resources.AuroraSecurityInstance,
      ).toMatchObject({ action: "noop" });
      yield* stack.destroy();