sailfork / t3code-github / .repos/ alchemy-effect/ packages/ integration-tests/ test/ AWS/ Neptune/ DBSubnetGroup.test.ts

import * as AWS from "@alchemy.run/aws";
import { DBSubnetGroup } from "@alchemy.run/aws/Neptune";
import { expect } from "@alchemy.run/test";
import * as Test from "@alchemy.run/test/Alchemy";
import * as EC2 from "@distilled.cloud/aws/ec2";
import * as neptune from "@distilled.cloud/aws/neptune";
import * as Effect from "effect/Effect";
import * as Schedule from "effect/Schedule";
import { getDefaultVpc } from "../DefaultVpc.ts";

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

// Resolve two default-for-AZ subnets from the account's default VPC.
const defaultSubnetIds = Effect.gen(function* () {
  const vpc = yield* getDefaultVpc;
  const subnets = yield* EC2.describeSubnets({
    Filters: [
      { Name: "vpc-id", Values: [vpc.vpcId] },
      { Name: "default-for-az", Values: ["true"] },
    ],
  });
  const ids = (subnets.Subnets ?? [])
    .map((s) => s.SubnetId)
    .filter((id): id is `subnet-${string}` => id !== undefined)
    .sort();
  if (ids.length < 2) {
    return yield* Effect.die(new Error("default VPC has fewer than 2 default-for-AZ subnets"));
  }
  return ids;
});

const assertGone = (name: string) =>
  neptune.describeDBSubnetGroups({ DBSubnetGroupName: name }).pipe(
    Effect.flatMap(() => Effect.fail(new Error(`subnet group '${name}' still exists`))),
    Effect.catchTag("DBSubnetGroupNotFoundFault", () => Effect.void),
    Effect.retry({
      schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(10)]),
    }),
  );

test.provider(
  "create, update description, delete a Neptune DB subnet group",
  (stack) =>
    Effect.gen(function* () {
      yield* stack.destroy();

      const subnetIds = yield* defaultSubnetIds;

      const { group } = yield* stack.deploy(
        Effect.gen(function* () {
          const group = yield* DBSubnetGroup("Subnets", {
            description: "alchemy neptune subnet group",
            subnetIds: subnetIds.slice(0, 2),
            tags: { fixture: "neptune-subnet-group" },
          });
          return { group };
        }),
      );

      expect(group.dbSubnetGroupName).toBeDefined();
      expect(group.dbSubnetGroupArn).toContain(":subgrp:");
      expect(new Set(group.subnetIds)).toEqual(new Set(subnetIds.slice(0, 2)));

      // Out-of-band verification.
      const described = yield* neptune.describeDBSubnetGroups({
        DBSubnetGroupName: group.dbSubnetGroupName,
      });
      const observed = described.DBSubnetGroups?.[0];
      expect(observed?.DBSubnetGroupDescription).toBe("alchemy neptune subnet group");
      expect(observed?.VpcId).toBeDefined();

      // Update the description in place (name unchanged).
      const { group: updated } = yield* stack.deploy(
        Effect.gen(function* () {
          const group = yield* DBSubnetGroup("Subnets", {
            description: "alchemy neptune subnet group v2",
            subnetIds: subnetIds.slice(0, 2),
            tags: { fixture: "neptune-subnet-group", env: "test" },
          });
          return { group };
        }),
      );
      expect(updated.dbSubnetGroupName).toBe(group.dbSubnetGroupName);

      const redescribed = yield* neptune.describeDBSubnetGroups({
        DBSubnetGroupName: group.dbSubnetGroupName,
      });
      expect(redescribed.DBSubnetGroups?.[0]?.DBSubnetGroupDescription).toBe(
        "alchemy neptune subnet group v2",
      );

      yield* stack.destroy();
      yield* assertGone(group.dbSubnetGroupName);
    }),
  {
    tags: ["provider:aws", "provider:aws:ec2", "provider:aws:neptune", "live"],
    timeout: 240_000,
  },
);