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

import type { TestOptions } from "@alchemy.run/test";
import * as Test from "@alchemy.run/test/Alchemy";
import { makeApiGatewayTestLease } from "./TestLease.ts";

const apiGatewayOptions = (options: TestOptions | undefined): TestOptions => {
  const normalized = typeof options === "number" ? { timeout: options } : options;
  return {
    ...normalized,
    // Once a file owns the service lease an individual lifecycle is fast —
    // observed passes at suite concurrency 32 run 3–30s. The failures that
    // motivated this budget were throttle storms at concurrency 64: the
    // account-wide REST control-plane quota is shared with ApiGatewayV2 and
    // the rest of the run, so distilled's TooManyRequests backoff can stack
    // a 3s operation past 120s. 300s absorbs the storm while still failing
    // a genuine provider hang fast relative to the full-run timeout.
    timeout: normalized?.timeout ?? 300_000,
    // API Gateway REST mutations share an account-wide throttle. A timed-out
    // retry starts with a fresh scratch state and therefore cannot reclaim the
    // physical API from the interrupted attempt, so these live tests run once.
    // TestLease serializes only this service's files; do not use the runner's
    // global `exclusive` lock here because that would also block unrelated AWS
    // services while API Gateway waits on its slow control plane.
    retry: 0,
  };
};

/**
 * API Gateway REST test adapter.
 *
 * The old Vitest project ran this directory in a single fork with file
 * concurrency disabled. A shared root-hook lease restores that scheduling
 * contract without teaching the generic runner about service paths.
 */
export const make = <ROut = any>(options: Test.MakeOptions<ROut>) => {
  const api = Test.make(options);
  const testLease = makeApiGatewayTestLease();

  // The first 12 files can enter the runner together. Waiting here is
  // intentional service scheduling rather than a cloud operation, so allow
  // the queue to span the full hard-walled AWS run.
  api.beforeAll(testLease.acquire, { timeout: 3_600_000 });
  api.afterAll(testLease.release);

  const provider = api.test.provider;

  const providerWithOptions = ((name, fn, testOptions) =>
    provider(name, fn, apiGatewayOptions(testOptions))) as typeof provider;
  providerWithOptions.skip = (name, fn, testOptions) =>
    provider.skip(name, fn, apiGatewayOptions(testOptions));
  providerWithOptions.skipIf = (condition) => (name, fn, testOptions) =>
    provider.skipIf(condition)(name, fn, apiGatewayOptions(testOptions));

  api.test.provider = providerWithOptions;
  return api;
};