sailfork / t3code-github / .repos/ alchemy-effect/ packages/ integration-tests/ test/ AWS/ FinSpace/ Environment.test.ts

import * as AWS from "@alchemy.run/aws";
import { Environment } from "@alchemy.run/aws/FinSpace";
import { expect } from "@alchemy.run/test";
import * as Test from "@alchemy.run/test/Alchemy";
import * as finspace from "@distilled.cloud/aws/finspace";
import * as finspaceData from "@distilled.cloud/aws/finspace-data";
import * as Effect from "effect/Effect";
import * as Schedule from "effect/Schedule";

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

// Amazon FinSpace reached end of support on 2026-10-07
// (https://docs.aws.amazon.com/finspace/latest/userguide/amazon-finspace-end-of-support.html).
// The control-plane hostname finspace.<region>.amazonaws.com no longer has
// A records in any region, so every control-plane call fails before reaching
// AWS with:
//   HttpClientError: Transport error (GET https://finspace.us-west-2.amazonaws.com/kx/environments)
//   (cause: getaddrinfo ENOTFOUND finspace.us-west-2.amazonaws.com)
// A transport failure can't be patched into a typed tag, so all control-plane
// tests are gated behind AWS_TEST_FINSPACE=1 for accounts that still have access.
const finspaceRetired = !process.env.AWS_TEST_FINSPACE;

// Ungated typed-error probe: prove the distilled error union carries the
// not-found tag the Environment provider's read/delete paths depend on.
// (environmentId must match ^[a-zA-Z0-9]{1,26}$ — malformed ids fail earlier
// with ValidationException.)
test.provider.skipIf(finspaceRetired)(
  "getEnvironment on a nonexistent environment fails with ResourceNotFoundException",
  () =>
    Effect.gen(function* () {
      const error = yield* Effect.flip(
        finspace.getEnvironment({
          environmentId: "zzzzzzzzzzzzzzzzzzzzzzzzzz",
        }),
      );
      expect(error._tag).toBe("ResourceNotFoundException");
    }),
  { tags: ["provider:aws", "provider:aws:finspace", "live"] },
);

// Ungated typed-error probe for the finspace-data (data-plane) SDK. The data
// API is environment-scoped and this account has no FinSpace environment, so
// it rejects with a plain-text 403 body ("Failed to retrieve environment").
// This proves the distilled rest-json fallback turns that into the typed
// AccessDeniedException instead of a ParseError.
test.provider(
  "finspace-data getDataset without an environment fails with AccessDeniedException",
  () =>
    Effect.gen(function* () {
      const error = yield* Effect.flip(
        finspaceData.getDataset({ datasetId: "zzzzzzzzzzzzzzzzzzzzzzzzzz" }),
      );
      expect(error._tag).toBe("AccessDeniedException");
    }),
  { tags: ["provider:aws", "provider:aws:finspace", "live"] },
);

// Deletion is verified as INITIATED (irreversible) or fully gone — full
// disappearance takes many more minutes server-side.
const assertEnvironmentDeleting = (environmentId: string) =>
  Effect.gen(function* () {
    const status = yield* finspace.getEnvironment({ environmentId }).pipe(
      Effect.map((r) => r.environment?.status ?? "gone"),
      Effect.catchTag("ResourceNotFoundException", () => Effect.succeed("gone" as const)),
    );
    if (
      status !== "gone" &&
      status !== "DELETED" &&
      status !== "DELETING" &&
      status !== "DELETE_REQUESTED"
    ) {
      return yield* Effect.fail(
        new Error(`environment '${environmentId}' still exists (${status})`),
      );
    }
  }).pipe(
    Effect.retry({
      schedule: Schedule.max([Schedule.fixed("10 seconds"), Schedule.recurs(18)]),
    }),
  );

// A FinSpace environment takes ~20 minutes to provision, bills while it
// exists, and FinSpace is closed to non-onboarded accounts. The full
// lifecycle is gated behind AWS_TEST_FINSPACE=1 and always destroys what it
// created.
test.provider.skipIf(finspaceRetired)(
  "create FinSpace environment, verify, update, destroy",
  (stack) =>
    Effect.gen(function* () {
      yield* stack.destroy();

      const { env } = yield* stack.deploy(
        Effect.gen(function* () {
          const env = yield* Environment("Analytics", {
            description: "alchemy finspace test environment",
            tags: { fixture: "finspace-environment" },
          });
          return { env };
        }),
      );

      expect(env.environmentId).toBeDefined();
      expect(env.environmentArn).toContain(":environment/");
      expect(env.status).toBe("CREATED");
      expect(env.tags.fixture).toBe("finspace-environment");

      // Out-of-band verification via distilled.
      const observed = yield* finspace.getEnvironment({
        environmentId: env.environmentId,
      });
      expect(observed.environment?.status).toBe("CREATED");
      expect(observed.environment?.name).toBe(env.name);

      // Update the description in place (no replacement).
      const { env: updated } = yield* stack.deploy(
        Effect.gen(function* () {
          const env = yield* Environment("Analytics", {
            description: "alchemy finspace test environment (updated)",
            tags: { fixture: "finspace-environment" },
          });
          return { env };
        }),
      );
      expect(updated.environmentId).toBe(env.environmentId);

      // Destroy immediately — environments bill while they exist — and
      // verify deletion was initiated out-of-band.
      yield* stack.destroy();
      yield* assertEnvironmentDeleting(env.environmentId);
    }),
  // environment create (~20 min) + update + delete, one test.
  {
    tags: ["provider:aws", "provider:aws:finspace", "live"],
    timeout: 3_000_000,
  },
);