sailfork / t3code-github / .repos/ alchemy-effect/ packages/ integration-tests/ test/ AWS/ ApiGateway/ UsagePlan.test.ts

import * as AWS from "@alchemy.run/aws";
import { expect } from "@alchemy.run/test";
import * as ag from "@distilled.cloud/aws/api-gateway";
import * as Provider from "alchemy/Provider";
import * as Effect from "effect/Effect";
import { assertUsagePlanDeleted } from "./assertions.ts";
import * as Test from "./Test.ts";

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

test.provider.skipIf(!!process.env.FAST)(
  "create and delete usage plan",
  (stack) =>
    Effect.gen(function* () {
      yield* stack.destroy();

      const plan = yield* stack.deploy(
        Effect.gen(function* () {
          return yield* AWS.ApiGateway.UsagePlan("AgUsagePlan", { description: "test plan" });
        }),
      );

      expect(plan.id).toBeDefined();

      yield* stack.destroy();
      yield* assertUsagePlanDeleted(plan.id);
    }),
  { tags: ["provider:aws", "provider:aws:apigateway", "live"] },
);

test.provider.skipIf(!!process.env.FAST)(
  "usage plan throttle updates in place",
  (stack) =>
    Effect.gen(function* () {
      yield* stack.destroy();

      const plan = yield* stack.deploy(
        Effect.gen(function* () {
          return yield* AWS.ApiGateway.UsagePlan("AgUsagePlanThrottle", {
            throttle: { burstLimit: 10, rateLimit: 100 },
          });
        }),
      );

      yield* stack.deploy(
        Effect.gen(function* () {
          return yield* AWS.ApiGateway.UsagePlan("AgUsagePlanThrottle", {
            throttle: { burstLimit: 20, rateLimit: 200 },
          });
        }),
      );

      const remote = yield* ag.getUsagePlan({ usagePlanId: plan.id });
      expect(remote.throttle?.burstLimit).toEqual(20);
      expect(remote.throttle?.rateLimit).toEqual(200);

      yield* stack.destroy();
      yield* assertUsagePlanDeleted(plan.id);
    }),
  { tags: ["provider:aws", "provider:aws:apigateway", "live"] },
);

test.provider.skipIf(!!process.env.FAST)(
  "list enumerates the deployed usage plan",
  (stack) =>
    Effect.gen(function* () {
      yield* stack.destroy();

      const plan = yield* stack.deploy(
        Effect.gen(function* () {
          return yield* AWS.ApiGateway.UsagePlan("AgUsagePlanList", {
            description: "list test plan",
          });
        }),
      );

      const provider = yield* Provider.findProvider(AWS.ApiGateway.UsagePlan);
      const all = yield* provider.list();

      expect(all.some((p) => p.id === plan.id)).toBe(true);

      yield* stack.destroy();
      yield* assertUsagePlanDeleted(plan.id);
    }),
  { tags: ["provider:aws", "provider:aws:apigateway", "live"] },
);