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

1.4 KB
Raw Blame
import { test } from "node:test";
import assert from "node:assert/strict";
import Bottleneck from "bottleneck";

const { runAsProbe, isProbeContext } = await import("../../src/shared/utils/probeOrigin.ts");

test("runAsProbe propagates through nested async/await; false outside", async () => {
  assert.equal(isProbeContext(), false);
  await runAsProbe(async () => {
    assert.equal(isProbeContext(), true);
    await Promise.resolve();
    const inner = async () => {
      await Promise.resolve();
      return isProbeContext();
    };
    assert.equal(await inner(), true);
    await new Promise((r) => setTimeout(r, 5));
    assert.equal(isProbeContext(), true);
  });
  assert.equal(isProbeContext(), false);
});

test("queued scheduler job wrapped in runAsProbe keeps the probe context", async () => {
  const limiter = new Bottleneck({ maxConcurrent: 1, minTime: 50 });
  let insideQueuedJob: boolean | null = null;
  // Schedule job 1 WITHOUT awaiting it so job 2 really queues behind it
  // (maxConcurrent 1 + minTime 50) — the queued job must execute with the
  // probe context established at scheduling time.
  const job1 = limiter.schedule(() => new Promise((r) => setTimeout(r, 30)));
  const job2 = limiter.schedule(() =>
    runAsProbe(() => {
      insideQueuedJob = isProbeContext();
      return Promise.resolve();
    })
  );
  await Promise.all([job1, job2]);
  assert.equal(insideQueuedJob, true);
});