sailfork / t3code-github / .repos/ alchemy-effect/ packages/ integration-tests/ test/ AWS/ EC2/ GetAmi.test.ts

import * as AWS from "@alchemy.run/aws";
import { expect } from "@alchemy.run/test";
import * as Test from "@alchemy.run/test/Alchemy";
import * as Effect from "effect/Effect";

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

// `GetAmi.execute` (aliased `getAmi`) is the data-source form of the
// capability: the lookup runs during plan/deploy resolution against the
// stack's services (GetAmiHttp is registered by AWS.providers()), never
// inside a deployed runtime. No cloud resources are created — the stack is
// outputs-only — so this pins the whole invoke path cheaply: Binding.Service
// execute → EffectExpr resolution → distilled describeImages.
test.provider(
  "getAmi resolves images as plan-time Outputs",
  (stack) =>
    Effect.gen(function* () {
      const { image, imageId, fallbackId } = yield* stack.deploy(
        Effect.gen(function* () {
          return {
            // The generic data source: the full image description.
            image: AWS.EC2.getAmi({
              owners: ["amazon"],
              name: ["al2023-ami-2023.*"],
            }),
            // The id-only helper built on it.
            imageId: AWS.EC2.amazonLinux2023(),
            // The preference-chain helper (flatMap over the data source).
            fallbackId: AWS.EC2.amazonLinux(),
          };
        }),
      );

      expect(image?.ImageId).toMatch(/^ami-/);
      expect(image?.CreationDate).toBeTruthy();
      expect(imageId).toMatch(/^ami-/);
      // AL2023 exists, so the fallback chain resolves to the same image id.
      expect(fallbackId).toBe(imageId);

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