Skip to content

Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors

14.6 KB
Raw Blame
import test from "node:test";
import assert from "node:assert/strict";

const { handleImageGeneration } = await import("../../open-sse/handlers/imageGeneration.ts");
const { fetchImageEndpoint } =
  await import("../../open-sse/handlers/imageGeneration/openAiImageFetch.ts");
const { getConfiguredTimeout } = await import("../../src/shared/utils/fetchTimeout.ts");
const { resolveImageGenerationTimeoutMs } =
  await import("../../src/lib/images/imageGenerationLongJobs.ts");

/** What the routes inject with IMAGE_GENERATION_LONG_JOBS on (#16260). */
const LONG_JOBS = { resolveTimeoutMs: (psd: unknown) => resolveImageGenerationTimeoutMs(psd) };

const TIMEOUT_ENV_KEYS = [
  "FETCH_TIMEOUT_MS",
  "REQUEST_TIMEOUT_MS",
  "OMNIROUTE_DEFAULT_FETCH_TIMEOUT_MS",
] as const;

type TimeoutEnvKey = (typeof TIMEOUT_ENV_KEYS)[number];

async function withTimeoutEnv<T>(
  values: Partial<Record<TimeoutEnvKey, string>>,
  fn: () => Promise<T>
): Promise<T> {
  const originals = Object.fromEntries(
    TIMEOUT_ENV_KEYS.map((key) => [key, process.env[key]])
  ) as Record<TimeoutEnvKey, string | undefined>;

  for (const key of TIMEOUT_ENV_KEYS) {
    const value = values[key];
    if (value === undefined) delete process.env[key];
    else process.env[key] = value;
  }

  try {
    return await fn();
  } finally {
    for (const key of TIMEOUT_ENV_KEYS) {
      const value = originals[key];
      if (value === undefined) delete process.env[key];
      else process.env[key] = value;
    }
  }
}

async function captureImageFetchTimeout(
  options: {
    body?: { model: string; prompt: string };
    credentials?: Record<string, unknown>;
    resolvedProvider?: string;
    longJobs?: typeof LONG_JOBS | null;
  } = {}
): Promise<number | undefined> {
  const originalSetTimeout = globalThis.setTimeout;
  let capturedTimeout: number | undefined;

  globalThis.setTimeout = ((handler: TimerHandler, timeout?: number, ...args: unknown[]) => {
    capturedTimeout ??= Number(timeout);
    return originalSetTimeout(handler, 60_000, ...args);
  }) as typeof globalThis.setTimeout;

  try {
    globalThis.fetch = async () =>
      new Response(
        JSON.stringify({
          created: 999,
          data: [{ url: "https://cdn.example.com/timeout-test.png" }],
        }),
        { status: 200, headers: { "content-type": "application/json" } }
      );

    const result = await handleImageGeneration({
      body: options.body ?? { model: "openai/gpt-image-2", prompt: "timeout selection" },
      credentials: options.credentials ?? { apiKey: "test-key" },
      resolvedProvider: options.resolvedProvider,
      longJobs: options.longJobs ?? null,
      log: null,
    });
    assert.equal(result.success, true);
    return capturedTimeout;
  } finally {
    globalThis.setTimeout = originalSetTimeout;
  }
}

async function restore<T>(fn: () => Promise<T>): Promise<T> {
  const originalFetch = globalThis.fetch;
  try {
    return await fn();
  } finally {
    globalThis.fetch = originalFetch;
  }
}

function makeAbortError() {
  return Object.create(Error.prototype, {
    message: { value: "The operation was aborted", writable: true, configurable: true },
    name: { value: "AbortError", writable: true, configurable: true },
  });
}

test("fetch timeout in OpenAI provider path returns 504 and sanitized error", () =>
  restore(async () => {
    globalThis.fetch = async () => {
      throw makeAbortError();
    };

    const result = await handleImageGeneration({
      body: { model: "openai/gpt-image-2", prompt: "timeout test" },
      credentials: { apiKey: "test-key" },
      log: null,
    });

    assert.equal(result.success, false);
    assert.equal(result.status, 504);
    assert.match(result.error, /Image provider error:/);
  }));

test("non-timeout fetch error still returns 502", () =>
  restore(async () => {
    globalThis.fetch = async () => {
      throw new Error("network down");
    };

    const result = await handleImageGeneration({
      body: { model: "openai/gpt-image-2", prompt: "network error test" },
      credentials: { apiKey: "test-key" },
      log: null,
    });

    assert.equal(result.success, false);
    assert.equal(result.status, 502);
  }));

test("successful image gen passes AbortSignal and returns URL", () =>
  restore(async () => {
    let seenSignal: AbortSignal | null = null;

    globalThis.fetch = async (url, options) => {
      seenSignal = (options as RequestInit).signal ?? null;
      return new Response(
        JSON.stringify({
          created: 999,
          data: [{ url: "https://cdn.example.com/timeout-test.png" }],
        }),
        { status: 200, headers: { "content-type": "application/json" } }
      );
    };

    const result = await handleImageGeneration({
      body: { model: "openai/gpt-image-2", prompt: "success test" },
      credentials: { apiKey: "test-key" },
      log: null,
    });

    assert.equal(result.success, true);
    assert.ok(seenSignal, "AbortSignal should be passed through fetchWithTimeout");
    assert.equal(result.data.data[0].url, "https://cdn.example.com/timeout-test.png");
  }));

test("flag off (default): the OpenAI image path keeps the fixed configured budget", () =>
  restore(async () => {
    await withTimeoutEnv({ FETCH_TIMEOUT_MS: "111", REQUEST_TIMEOUT_MS: "222" }, async () => {
      assert.equal(await captureImageFetchTimeout(), getConfiguredTimeout());
      assert.equal(
        await captureImageFetchTimeout({
          credentials: { apiKey: "test-key", providerSpecificData: { timeoutMs: 4_321 } },
        }),
        getConfiguredTimeout()
      );
    });
  }));

test("flag on: OpenAI image timeout defaults to 600s and honors central then legacy env precedence", () =>
  restore(async () => {
    await withTimeoutEnv({}, async () => {
      assert.equal(await captureImageFetchTimeout({ longJobs: LONG_JOBS }), 600_000);
    });

    await withTimeoutEnv(
      {
        FETCH_TIMEOUT_MS: "111",
        REQUEST_TIMEOUT_MS: "222",
        OMNIROUTE_DEFAULT_FETCH_TIMEOUT_MS: "333",
      },
      async () => {
        assert.equal(await captureImageFetchTimeout({ longJobs: LONG_JOBS }), 111);
      }
    );

    await withTimeoutEnv(
      { REQUEST_TIMEOUT_MS: "222", OMNIROUTE_DEFAULT_FETCH_TIMEOUT_MS: "333" },
      async () => {
        assert.equal(await captureImageFetchTimeout({ longJobs: LONG_JOBS }), 222);
      }
    );

    await withTimeoutEnv({ OMNIROUTE_DEFAULT_FETCH_TIMEOUT_MS: "333" }, async () => {
      assert.equal(await captureImageFetchTimeout({ longJobs: LONG_JOBS }), 333);
    });
  }));

test("flag on: connection timeout overrides env for built-in and custom OpenAI image providers", () =>
  restore(async () => {
    await withTimeoutEnv({ FETCH_TIMEOUT_MS: "111" }, async () => {
      assert.equal(
        await captureImageFetchTimeout({
          credentials: { apiKey: "test-key", providerSpecificData: { timeoutMs: 4_321 } },
          longJobs: LONG_JOBS,
        }),
        4_321
      );

      assert.equal(
        await captureImageFetchTimeout({
          body: { model: "custom/image-model", prompt: "custom timeout" },
          credentials: {
            apiKey: "test-key",
            providerSpecificData: {
              baseUrl: "https://images.example.test/v1",
              timeoutMs: 5_432,
            },
          },
          resolvedProvider: "custom",
          longJobs: LONG_JOBS,
        }),
        5_432
      );
    });
  }));

test("flag on: zero timeout disables the image timer and keeps the caller signal", () =>
  restore(async () => {
    await withTimeoutEnv({ FETCH_TIMEOUT_MS: "0" }, async () => {
      const controller = new AbortController();
      let seenSignal: AbortSignal | null | undefined;
      globalThis.fetch = async (_url, init) => {
        seenSignal = init?.signal;
        return new Response(
          JSON.stringify({ data: [{ url: "https://cdn.example.com/no-timeout.png" }] }),
          { status: 200, headers: { "content-type": "application/json" } }
        );
      };

      const result = await handleImageGeneration({
        body: { model: "openai/gpt-image-2", prompt: "no timeout" },
        credentials: { apiKey: "test-key" },
        signal: controller.signal,
        longJobs: LONG_JOBS,
        log: null,
      });

      assert.equal(result.success, true);
      assert.equal(seenSignal, controller.signal);
    });
  }));

function hangingFetch(state: { seenSignal?: AbortSignal | null }) {
  return async (_url: string | URL | Request, init?: RequestInit) => {
    state.seenSignal = init?.signal;
    return new Promise<Response>((_resolve, reject) => {
      if (init?.signal?.aborted) {
        reject(makeAbortError());
        return;
      }
      init?.signal?.addEventListener("abort", () => reject(makeAbortError()), { once: true });
      setTimeout(() => reject(makeAbortError()), 50);
    });
  };
}

test("flag on: caller abort is classified as 499 before the image timeout", () =>
  restore(async () => {
    await withTimeoutEnv({ FETCH_TIMEOUT_MS: "200" }, async () => {
      const controller = new AbortController();
      const state: { seenSignal?: AbortSignal | null } = {};
      globalThis.fetch = hangingFetch(state) as typeof fetch;

      const pending = handleImageGeneration({
        body: { model: "openai/gpt-image-2", prompt: "cancel me" },
        credentials: { apiKey: "test-key" },
        signal: controller.signal,
        longJobs: LONG_JOBS,
        log: null,
      });
      controller.abort();

      const result = await pending;
      assert.equal(state.seenSignal?.aborted, true);
      assert.equal(result.success, false);
      assert.equal(result.status, 499);
    });
  }));

test("flag off (default): a caller abort is not propagated and never yields 499", () =>
  restore(async () => {
    const controller = new AbortController();
    const state: { seenSignal?: AbortSignal | null } = {};
    globalThis.fetch = hangingFetch(state) as typeof fetch;

    const pending = handleImageGeneration({
      body: { model: "openai/gpt-image-2", prompt: "cancel me" },
      credentials: { apiKey: "test-key" },
      signal: controller.signal,
      log: null,
    });
    controller.abort();

    const result = await pending;
    assert.notEqual(state.seenSignal, controller.signal);
    assert.equal(result.success, false);
    assert.equal(result.status, 504);
  }));

test("flag on: caller abort while reading the image body is still classified as 499", () =>
  restore(async () => {
    await withTimeoutEnv({ FETCH_TIMEOUT_MS: "200" }, async () => {
      const controller = new AbortController();
      globalThis.fetch = async (_url, init) =>
        new Response(
          new ReadableStream<Uint8Array>({
            start(streamController) {
              const abort = () => streamController.error(makeAbortError());
              if (init?.signal?.aborted) abort();
              else init?.signal?.addEventListener("abort", abort, { once: true });
              // Fail (502) instead of hanging when the caller abort never reaches the body.
              setTimeout(() => streamController.error(new Error("body stayed open")), 1000);
            },
          }),
          { status: 200, headers: { "content-type": "application/json" } }
        );

      const pending = handleImageGeneration({
        body: { model: "openai/gpt-image-2", prompt: "cancel body read" },
        credentials: { apiKey: "test-key" },
        signal: controller.signal,
        longJobs: LONG_JOBS,
        log: null,
      });
      controller.abort();

      const result = await pending;
      assert.equal(result.success, false);
      assert.equal(result.status, 499);
    });
  }));

function stalledBodyFetch() {
  return async (_url: string | URL | Request, init?: RequestInit) =>
    new Response(
      new ReadableStream<Uint8Array>({
        start(streamController) {
          init?.signal?.addEventListener(
            "abort",
            () => {
              const error = new Error("aborted");
              error.name = "AbortError";
              streamController.error(error);
            },
            { once: true }
          );
          setTimeout(() => streamController.error(new Error("body stayed open")), 2000);
        },
      }),
      { status: 200, headers: { "content-type": "application/json" } }
    );
}

test("default-on: the image deadline stays armed while reading the response body", () =>
  restore(async () => {
    globalThis.fetch = stalledBodyFetch() as typeof fetch;
    const startedAt = Date.now();
    const result = await fetchImageEndpoint(
      "https://api.example.test/v1/images/generations",
      {},
      "{}",
      "openai",
      null,
      { timeoutMs: 20 }
    );

    assert.equal(result.success, false);
    assert.equal(result.status, 504);
    assert.ok(Date.now() - startedAt < 1500, "body timeout should use the configured deadline");
  }));

test("default-on: a stalled body through the dispatcher (flag off) ends with 504 at the budget", () =>
  restore(async () => {
    const originalSetTimeout = globalThis.setTimeout;
    let deadlineArmed = false;
    // Shrink only the image deadline timer (the configured budget) so the test stays fast.
    globalThis.setTimeout = ((handler: TimerHandler, timeout?: number, ...args: unknown[]) => {
      if (!deadlineArmed && timeout === getConfiguredTimeout()) {
        deadlineArmed = true;
        return originalSetTimeout(handler, 20, ...args);
      }
      return originalSetTimeout(handler, timeout, ...args);
    }) as typeof globalThis.setTimeout;
    try {
      globalThis.fetch = stalledBodyFetch() as typeof fetch;
      const result = await handleImageGeneration({
        body: { model: "openai/gpt-image-2", prompt: "body timeout" },
        credentials: { apiKey: "test-key" },
        log: null,
      });
      assert.equal(deadlineArmed, true);
      assert.equal(result.success, false);
      assert.equal(result.status, 504);
    } finally {
      globalThis.setTimeout = originalSetTimeout;
    }
  }));

test("flag on: an already-aborted image request does not dispatch a fallback URL", () =>
  restore(async () => {
    const controller = new AbortController();
    controller.abort();
    let fetchCalls = 0;
    globalThis.fetch = async () => {
      fetchCalls += 1;
      return new Response("temporarily unavailable", { status: 503 });
    };

    const result = await handleImageGeneration({
      body: {
        model: "nebius/black-forest-labs/flux-schnell",
        prompt: "do not retry cancellation",
      },
      credentials: { apiKey: "test-key" },
      signal: controller.signal,
      longJobs: LONG_JOBS,
      log: null,
    });

    assert.equal(result.success, false);
    assert.equal(result.status, 499);
    assert.ok(fetchCalls <= 1, `expected at most one dispatch, got ${fetchCalls}`);
  }));