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
TypeScript a2aai-agentsai-gatewayanthropicclaudeclaude-codeclinecodexcopilotcursordeepseekfree-aigeminikimillm-gatewaymcpopenaiopenai-proxyqwentoken-saver
import test from "node:test";
import assert from "node:assert/strict";
import http from "node:http";
import type { AddressInfo } from "node:net";
// End to end through the real patched fetch: the status of a provider dispatch reaches
// the applied-proxy sink, whichever fetch path serves it, while a side fetch made later in
// the same request does not overwrite it.
const proxyFetchModule = await import("../../open-sse/utils/proxyFetch.ts");
const { runWithCapture } = await import("../../open-sse/utils/providerRequestLogging.ts");
const capture = { capture: () => {}, body: (fallback: unknown) => fallback };
let server: http.Server;
let baseUrl = "";
test.before(async () => {
server = http.createServer((req, res) => {
const params = new URL(req.url ?? "/", "http://local").searchParams;
setTimeout(
() => {
// `drop` fails the call on the wire (no response) once the delay has passed.
if (params.has("drop")) {
req.socket.destroy();
return;
}
res.writeHead(Number(params.get("code") ?? 200), { "content-type": "application/json" });
res.end("{}");
},
Number(params.get("delay") ?? 0)
);
});
await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", () => resolve()));
baseUrl = `http://127.0.0.1:${(server.address() as AddressInfo).port}`;
});
test.after(async () => {
await new Promise<void>((resolve) => server.close(() => resolve()));
});
type Sink = { proxy: unknown; upstreamStatus?: number };
function inRequest(sink: Sink, fn: () => Promise<void>) {
return proxyFetchModule.runWithAppliedProxyCapture(sink, fn);
}
async function dispatch(fn: () => Promise<Response>) {
await runWithCapture(capture, async () => {
const res = await fn();
await res.text();
});
}
test("a provider dispatch records the received status", async () => {
const sink: Sink = { proxy: null };
await inRequest(sink, () => dispatch(() => fetch(`${baseUrl}/v1?code=429`)));
assert.equal(sink.upstreamStatus, 429);
});
test("the last response received during the dispatch wins", async () => {
const sink: Sink = { proxy: null };
await inRequest(sink, () =>
dispatch(async () => {
await (await fetch(`${baseUrl}/v1?code=200`)).text();
return fetch(`${baseUrl}/v1?code=500`);
})
);
assert.equal(sink.upstreamStatus, 500);
});
test("a fetch still running when the dispatch returns does not change the status", async () => {
const sink: Sink = { proxy: null };
let background: Promise<unknown> = Promise.resolve();
await inRequest(sink, async () => {
await dispatch(async () => {
const res = await fetch(`${baseUrl}/v1?code=429`);
// Both background calls must still be in flight when the dispatch settles. A refused
// port fails in about a millisecond now that loopback skips the bound-and-replay
// path (#14311), so the failing call is a delayed connection drop instead.
background = Promise.all([
fetch(`${baseUrl}/finish?code=200&delay=50`).then((r) => r.text()),
fetch(`${baseUrl}/finish?drop=1&delay=50`).then(
() => assert.fail("the dropped call must fail"),
() => null
),
]);
return res;
});
await background;
});
assert.equal(sink.upstreamStatus, 429);
});
test("a side fetch after the dispatch keeps the provider status", async () => {
const sink: Sink = { proxy: null };
await inRequest(sink, async () => {
await dispatch(() => fetch(`${baseUrl}/v1?code=429`));
const side = await fetch(`${baseUrl}/usage?code=500`);
await side.text();
});
assert.equal(sink.upstreamStatus, 429);
});
test("a second dispatch without any fetch leaves nothing stale behind", async () => {
const sink: Sink = { proxy: null };
await inRequest(sink, async () => {
await dispatch(() => fetch(`${baseUrl}/v1?code=429`));
assert.equal(sink.upstreamStatus, 429);
await runWithCapture(capture, async () => {});
});
assert.equal(sink.upstreamStatus, undefined);
});
test("a network error on the next dispatch clears the earlier status", async () => {
const sink: Sink = { proxy: null };
await inRequest(sink, async () => {
await dispatch(() => fetch(`${baseUrl}/v1?code=429`));
await assert.rejects(dispatch(() => fetch("http://127.0.0.1:1/v1")));
});
assert.equal(sink.upstreamStatus, undefined);
});
test("the explicit-dispatcher path is captured", async () => {
const sink: Sink = { proxy: null };
const undiciFetch = async () => new Response(null, { status: 502 });
await inRequest(sink, () =>
dispatch(() =>
proxyFetchModule.proxyFetch(`${baseUrl}/v1`, { dispatcher: {} } as RequestInit, {
undiciFetch,
})
)
);
assert.equal(sink.upstreamStatus, 502);
});
test("the explicit direct-context path is captured", async () => {
const sink: Sink = { proxy: null };
await inRequest(sink, () =>
dispatch(() =>
proxyFetchModule.runWithDirectFetchContext(() => fetch(`${baseUrl}/v1?code=403`))
)
);
assert.equal(sink.upstreamStatus, 403);
});
test("a dispatch without any sink in scope still works", async () => {
let status = 0;
await runWithCapture(capture, async () => {
const res = await fetch(`${baseUrl}/v1?code=200`);
status = res.status;
await res.text();
});
assert.equal(status, 200);
});