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

import * as AWS from "@alchemy.run/aws";
import { describe, expect } from "@alchemy.run/test";
import * as Core from "@alchemy.run/test/Core";
import * as Effect from "effect/Effect";
import * as HttpClient from "effect/http/HttpClient";
import * as Schedule from "effect/Schedule";
import NetworkTestFunctionLive, { NetworkTestFunction } from "./fixtures/network-function";
import * as Test from "./VpcTest.ts";

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

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

let baseUrl: string;

describe(
  "EC2.Network composed in a Lambda layer",
  { tags: ["provider:aws", "provider:aws:ec2", "provider:aws:lambda", "live"] },
  () => {
    beforeAll(
      Effect.gen(function* () {
        yield* sharedStack.destroy();

        const { functionUrl } = yield* sharedStack.deploy(
          Effect.gen(function* () {
            return yield* NetworkTestFunction;
          }).pipe(Effect.provide(NetworkTestFunctionLive)),
        );

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

        // The function role has NO ec2:Describe* permissions and the runtime
        // does not provide `AWSEnvironment` — a 200 here proves the `Network`
        // composition re-executes cleanly at Lambda init.
        yield* HttpClient.get(`${baseUrl}/network`).pipe(
          Effect.timeout("4 seconds"),
          Effect.flatMap((response) =>
            response.status === 200
              ? Effect.succeed(response)
              : Effect.fail(new Error(`Function not ready: ${response.status}`)),
          ),
          Effect.retry({ schedule: readinessPolicy }),
        );
      }),
      // This nested setup hook is scheduled while the file may still be queued
      // on the shared VPC-capacity lease. Give coordination the suite wall;
      // every cloud/HTTP wait inside remains independently bounded.
      { timeout: 3_600_000 },
    );

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

    test.provider(
      "Lambda INITs cleanly and resolves Network outputs at runtime",
      (_stack) =>
        Effect.gen(function* () {
          const response = yield* HttpClient.get(`${baseUrl}/network`);
          expect(response.status).toBe(200);

          const body = (yield* response.json) as {
            vpcId: string;
            subnetId: string;
          };
          expect(body.vpcId).toMatch(/^vpc-/);
          expect(body.subnetId).toMatch(/^subnet-/);
        }),
      { timeout: 60_000 },
    );
  },
);