sailfork / t3code-github / .repos/ alchemy-effect/ packages/ integration-tests/ test/ AWS/ DocDBElastic/ Bindings.test.ts

import * as AWS from "@alchemy.run/aws";
import { AWSEnvironment } from "@alchemy.run/aws/Environment";
import { describe, expect } from "@alchemy.run/test";
import * as Test from "@alchemy.run/test/Alchemy";
import * as Core from "@alchemy.run/test/Core";
import * as docdbelastic from "@distilled.cloud/aws/docdb-elastic";
import * as EC2 from "@distilled.cloud/aws/ec2";
import * as Effect from "effect/Effect";
import * as HttpClient from "effect/http/HttpClient";
import * as Schedule from "effect/Schedule";
import { getDefaultVpc } from "../DefaultVpc.ts";
import DocDBElasticTestFunctionLive, { DocDBElasticTestFunction } from "./handler";
import DocDBElasticSlowTestFunctionLive, {
  DocDBElasticSlowTestFunction,
  SECURITY_GROUPS_ENV,
  SUBNETS_ENV,
} from "./slow-handler";

const testOptions = { providers: AWS.providers() };
const { test, beforeAll, afterAll } = Test.make(testOptions);
const sharedStack = Core.scratchStack(testOptions, "DocDBElasticBindings");

const NONEXISTENT_UUID = "00000000-0000-0000-0000-000000000000";

// Lambda function URL cold-start (DNS, IAM propagation, init) can take well
// over 60s on a fresh deploy.
const readinessPolicy = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]);

let baseUrl: string;

// Well-formed-but-nonexistent ARNs the probe routes are driven against.
// Computed inside test bodies (test.provider provides AWSEnvironment).
const probeArns = Effect.gen(function* () {
  const { accountId, region } = yield* AWSEnvironment.current;
  return {
    snapshot: `arn:aws:docdb-elastic:${region}:${accountId}:cluster-snapshot/${NONEXISTENT_UUID}`,
    cluster: `arn:aws:docdb-elastic:${region}:${accountId}:cluster/${NONEXISTENT_UUID}`,
  };
});

const getJson = (path: string) =>
  HttpClient.get(`${baseUrl}${path}`).pipe(
    Effect.flatMap((response) =>
      response.status >= 500
        ? Effect.fail(new Error(`transient upstream ${response.status}`))
        : Effect.succeed(response),
    ),
    Effect.retry({
      schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(6)]),
    }),
    Effect.flatMap((r) => r.json),
  );

describe.sequential(
  "DocDBElastic Bindings",
  { tags: ["provider:aws", "provider:aws:docdbelastic", "provider:aws:lambda", "live"] },
  () => {
    beforeAll(
      Effect.gen(function* () {
        yield* Effect.logInfo("DocDBElastic test setup: destroying previous resources");
        yield* sharedStack.destroy();

        yield* Effect.logInfo("DocDBElastic test setup: deploying fixture");
        const { functionUrl } = yield* sharedStack.deploy(
          Effect.gen(function* () {
            return yield* DocDBElasticTestFunction;
          }).pipe(Effect.provide(DocDBElasticTestFunctionLive)),
        );

        expect(functionUrl).toBeTruthy();
        baseUrl = functionUrl!.replace(/\/+$/, "");

        const readinessUrl = `${baseUrl}/bindings`;
        yield* HttpClient.get(readinessUrl).pipe(
          Effect.flatMap((response) =>
            response.status === 200
              ? Effect.succeed(response)
              : Effect.fail(new Error(`Function not ready: ${response.status}`)),
          ),
          Effect.retry({ schedule: readinessPolicy }),
        );
      }),
      { timeout: 240_000 },
    );

    afterAll(sharedStack.destroy(), { timeout: 120_000 });

    describe("binding registration", () => {
      test.provider("all eight capabilities initialize in the runtime", () =>
        Effect.gen(function* () {
          const response = yield* getJson("/bindings");
          expect((response as any).bound).toHaveLength(8);
        }),
      );
    });

    describe("ListClusterSnapshots", () => {
      test.provider("lists the account's snapshots", () =>
        Effect.gen(function* () {
          const response = yield* getJson("/snapshots");
          expect((response as any).count).toBeGreaterThanOrEqual(0);
        }),
      );
    });

    describe("ListPendingMaintenanceActions", () => {
      test.provider("lists the account's pending maintenance", () =>
        Effect.gen(function* () {
          const response = yield* getJson("/maintenance");
          expect((response as any).count).toBeGreaterThanOrEqual(0);
        }),
      );
    });

    describe("GetClusterSnapshot", () => {
      test.provider("surfaces the typed not-found tag", () =>
        Effect.gen(function* () {
          const arns = yield* probeArns;
          const response = yield* getJson(
            `/snapshot-probe?arn=${encodeURIComponent(arns.snapshot)}`,
          );
          expect((response as any).tag).toBe("ResourceNotFoundException");
        }),
      );
    });

    describe("DeleteClusterSnapshot", () => {
      test.provider("surfaces the typed not-found tag", () =>
        Effect.gen(function* () {
          const arns = yield* probeArns;
          const response = yield* getJson(`/delete-probe?arn=${encodeURIComponent(arns.snapshot)}`);
          expect((response as any).tag).toBe("ResourceNotFoundException");
        }),
      );
    });

    describe("CopyClusterSnapshot", () => {
      test.provider("rejects a nonexistent source snapshot with a typed tag", () =>
        Effect.gen(function* () {
          const arns = yield* probeArns;
          const response = yield* getJson(`/copy-probe?arn=${encodeURIComponent(arns.snapshot)}`);
          // Copy authorizes against the source-snapshot resource; a
          // nonexistent snapshot is reported as AccessDeniedException
          // (existence non-disclosure) rather than not-found.
          expect([
            "ResourceNotFoundException",
            "ValidationException",
            "AccessDeniedException",
          ]).toContain((response as any).tag);
        }),
      );
    });

    describe("RestoreClusterFromSnapshot", () => {
      test.provider("rejects a nonexistent source snapshot with a typed tag", () =>
        Effect.gen(function* () {
          const arns = yield* probeArns;
          const response = yield* getJson(
            `/restore-probe?arn=${encodeURIComponent(arns.snapshot)}`,
          );
          // Restore authorizes against the source-snapshot resource; a
          // nonexistent snapshot is reported as AccessDeniedException
          // (existence non-disclosure) rather than not-found.
          expect([
            "ResourceNotFoundException",
            "ValidationException",
            "AccessDeniedException",
          ]).toContain((response as any).tag);
        }),
      );
    });

    describe("GetPendingMaintenanceAction", () => {
      test.provider("surfaces a typed tag for a nonexistent cluster", () =>
        Effect.gen(function* () {
          const arns = yield* probeArns;
          const response = yield* getJson(`/pending-probe?arn=${encodeURIComponent(arns.cluster)}`);
          expect(["ResourceNotFoundException", "ValidationException"]).toContain(
            (response as any).tag,
          );
        }),
      );
    });

    describe("ApplyPendingMaintenanceAction", () => {
      test.provider("surfaces a typed tag for a nonexistent cluster", () =>
        Effect.gen(function* () {
          const arns = yield* probeArns;
          const response = yield* getJson(`/apply-probe?arn=${encodeURIComponent(arns.cluster)}`);
          expect(["ResourceNotFoundException", "ValidationException"]).toContain(
            (response as any).tag,
          );
        }),
      );
    });
  },
);

// ---------------------------------------------------------------------------
// Cluster-scoped bindings (CreateClusterSnapshot, StopCluster, StartCluster)
// need a real elastic cluster (~10 min to provision, billed per
// shard-vCPU-hour) — skipped under `--fast` like the Cluster lifecycle
// test.
// ---------------------------------------------------------------------------

const slowStack = Core.scratchStack(testOptions, "DocDBElasticSlowBindings");

const SLOW_SNAPSHOT_NAME = "alchemy-docdb-elastic-bindings-snap";

// Resolve two default-for-AZ subnets from the region's first three AZs
// (elastic clusters are not offered in every AZ, e.g. us-west-2d) and the
// default security group — same selection as Cluster.test.ts.
const defaultNetwork = 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 subnetIds = (subnets.Subnets ?? [])
    .filter((s) => /[abc]$/.test(s.AvailabilityZone ?? ""))
    .sort((l, r) => (l.AvailabilityZone ?? "").localeCompare(r.AvailabilityZone ?? ""))
    .map((s) => s.SubnetId)
    .filter((id): id is `subnet-${string}` => id !== undefined)
    .slice(0, 2);
  const groups = yield* EC2.describeSecurityGroups({
    Filters: [
      { Name: "vpc-id", Values: [vpc.vpcId] },
      { Name: "group-name", Values: ["default"] },
    ],
  });
  const securityGroupId = groups.SecurityGroups?.[0]?.GroupId;
  if (subnetIds.length < 2 || securityGroupId === undefined) {
    return yield* Effect.die(
      new Error("default VPC is missing subnets or its default security group"),
    );
  }
  return { subnetIds, securityGroupIds: [securityGroupId] };
});

// The snapshot the gated test creates is cleaned up out-of-band; deletion of
// a snapshot still CREATING conflicts, so retry (bounded) while it settles.
const deleteSnapshot = (snapshotArn: string) =>
  docdbelastic.deleteClusterSnapshot({ snapshotArn }).pipe(
    Effect.asVoid,
    Effect.catchTag("ResourceNotFoundException", () => Effect.void),
    Effect.retry({
      while: (e): boolean => e._tag === "ConflictException",
      schedule: Schedule.max([Schedule.fixed("15 seconds"), Schedule.recurs(40)]),
    }),
  );

// Snapshots outlive their cluster, so one left behind by an interrupted run
// makes the fixture's CreateClusterSnapshot fail with ConflictException ("A
// snapshot already exists with given name"). Clear it by name up front.
const deleteSnapshotsNamed = (name: string) =>
  docdbelastic.listClusterSnapshots({}).pipe(
    Effect.flatMap((r) =>
      Effect.forEach(
        (r.snapshots ?? []).filter((s) => s.snapshotName === name),
        (s) => deleteSnapshot(s.snapshotArn),
        { discard: true },
      ),
    ),
  );

test.provider.skipIf(!!process.env.FAST)(
  "snapshot + stop + start bindings against a live elastic cluster",
  () =>
    Effect.gen(function* () {
      yield* slowStack.destroy();
      yield* deleteSnapshotsNamed(SLOW_SNAPSHOT_NAME);
      const network = yield* defaultNetwork;
      yield* Effect.sync(() => {
        process.env[SUBNETS_ENV] = JSON.stringify(network.subnetIds);
        process.env[SECURITY_GROUPS_ENV] = JSON.stringify(network.securityGroupIds);
      });

      let snapshotArn: string | undefined;

      yield* Effect.gen(function* () {
        const { functionUrl } = yield* slowStack.deploy(
          Effect.gen(function* () {
            return yield* DocDBElasticSlowTestFunction;
          }).pipe(Effect.provide(DocDBElasticSlowTestFunctionLive)),
        );
        expect(functionUrl).toBeTruthy();
        const slowBaseUrl = functionUrl!.replace(/\/+$/, "");

        // A non-200 is a test failure carrying the status — the fixture
        // answers an empty 500 on a defect, which `r.json` decodes to null.
        const get = (path: string) =>
          HttpClient.get(`${slowBaseUrl}${path}`).pipe(
            Effect.retry({
              schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(10)]),
            }),
            Effect.flatMap((r) =>
              r.status === 200 ? r.json : Effect.fail(new Error(`${path} returned ${r.status}`)),
            ),
          );

        // Take an on-demand snapshot of the live cluster.
        const snapshot = (yield* get(`/snapshot?name=${SLOW_SNAPSHOT_NAME}`)) as {
          snapshotArn: string;
          status: string;
        };
        expect(snapshot.snapshotArn).toContain(":cluster-snapshot/");
        snapshotArn = snapshot.snapshotArn;

        // Wait (bounded) for the snapshot to become ACTIVE (the elastic
        // API's ready status) so the stop call below does not conflict with
        // the in-flight snapshot.
        const snapshotStatus = yield* docdbelastic
          .getClusterSnapshot({ snapshotArn: snapshot.snapshotArn })
          .pipe(
            Effect.map((r) => r.snapshot.status),
            Effect.repeat({
              schedule: Schedule.spaced("10 seconds"),
              until: (status): boolean => status === "ACTIVE",
              times: 60,
            }),
          );
        expect(snapshotStatus).toBe("ACTIVE");

        // Stop the cluster (compute billing pauses).
        const stop = (yield* get("/stop")) as { status: string };
        expect(["STOPPING", "STOPPED"]).toContain(stop.status);

        // Starting while STOPPING surfaces a typed rejection; a fully
        // stopped cluster reports STARTING. Both prove the grant.
        const start = (yield* get("/start")) as { status: string };
        expect(["STARTING", "ValidationException"]).toContain(start.status);
      }).pipe(
        Effect.ensuring(
          Effect.gen(function* () {
            yield* slowStack.destroy();
            if (snapshotArn !== undefined) {
              yield* deleteSnapshot(snapshotArn);
            }
          }).pipe(Effect.orDie),
        ),
      );
    }),
  // cluster create (~10-15 min) + snapshot + stop + destroy in one test.
  // Destroy is slow by necessity: a STOPPING cluster takes ~11 min to reach
  // STOPPED, and only an ACTIVE cluster is deletable, so the provider starts
  // it again (~10 min) before DeleteCluster.
  {
    tags: [
      "provider:aws",
      "provider:aws:docdbelastic",
      "provider:aws:ec2",
      "provider:aws:lambda",
      "live",
    ],
    timeout: 3_600_000,
  },
);