fix(telegram): move network fallback to resolver-scoped dispatchers (#40740)

Merged via squash.

Prepared head SHA: a4456d48b4
Co-authored-by: sircrumpet <4436535+sircrumpet@users.noreply.github.com>
Co-authored-by: obviyus <22031114+obviyus@users.noreply.github.com>
Reviewed-by: @obviyus
This commit is contained in:
Eugene
2026-03-10 15:58:51 +10:00
committed by GitHub
parent d1a59557b5
commit 45b74fb56c
23 changed files with 1641 additions and 390 deletions

View File

@@ -1,25 +1,36 @@
import { afterEach, describe, expect, it, vi } from "vitest";
import { resolveFetch } from "../infra/fetch.js";
import { resetTelegramFetchStateForTests, resolveTelegramFetch } from "./fetch.js";
import { resolveTelegramFetch } from "./fetch.js";
const setDefaultAutoSelectFamily = vi.hoisted(() => vi.fn());
const setDefaultResultOrder = vi.hoisted(() => vi.fn());
const setDefaultAutoSelectFamily = vi.hoisted(() => vi.fn());
const undiciFetch = vi.hoisted(() => vi.fn());
const setGlobalDispatcher = vi.hoisted(() => vi.fn());
const getGlobalDispatcherState = vi.hoisted(() => ({ value: undefined as unknown }));
const getGlobalDispatcher = vi.hoisted(() => vi.fn(() => getGlobalDispatcherState.value));
const EnvHttpProxyAgentCtor = vi.hoisted(() =>
vi.fn(function MockEnvHttpProxyAgent(this: { options: unknown }, options: unknown) {
const AgentCtor = vi.hoisted(() =>
vi.fn(function MockAgent(
this: { options?: Record<string, unknown> },
options?: Record<string, unknown>,
) {
this.options = options;
}),
);
const EnvHttpProxyAgentCtor = vi.hoisted(() =>
vi.fn(function MockEnvHttpProxyAgent(
this: { options?: Record<string, unknown> },
options?: Record<string, unknown>,
) {
this.options = options;
}),
);
const ProxyAgentCtor = vi.hoisted(() =>
vi.fn(function MockProxyAgent(
this: { options?: Record<string, unknown> | string },
options?: Record<string, unknown> | string,
) {
this.options = options;
}),
);
vi.mock("node:net", async () => {
const actual = await vi.importActual<typeof import("node:net")>("node:net");
return {
...actual,
setDefaultAutoSelectFamily,
};
});
vi.mock("node:dns", async () => {
const actual = await vi.importActual<typeof import("node:dns")>("node:dns");
@@ -29,266 +40,655 @@ vi.mock("node:dns", async () => {
};
});
vi.mock("node:net", async () => {
const actual = await vi.importActual<typeof import("node:net")>("node:net");
return {
...actual,
setDefaultAutoSelectFamily,
};
});
vi.mock("undici", () => ({
Agent: AgentCtor,
EnvHttpProxyAgent: EnvHttpProxyAgentCtor,
getGlobalDispatcher,
ProxyAgent: ProxyAgentCtor,
fetch: undiciFetch,
setGlobalDispatcher,
}));
const originalFetch = globalThis.fetch;
function expectEnvProxyAgentConstructorCall(params: { nth: number; autoSelectFamily: boolean }) {
expect(EnvHttpProxyAgentCtor).toHaveBeenNthCalledWith(params.nth, {
connect: {
autoSelectFamily: params.autoSelectFamily,
autoSelectFamilyAttemptTimeout: 300,
},
});
function resolveTelegramFetchOrThrow(
proxyFetch?: typeof fetch,
options?: { network?: { autoSelectFamily?: boolean; dnsResultOrder?: "ipv4first" | "verbatim" } },
) {
return resolveTelegramFetch(proxyFetch, options);
}
function resolveTelegramFetchOrThrow() {
const resolved = resolveTelegramFetch();
if (!resolved) {
throw new Error("expected resolved fetch");
function getDispatcherFromUndiciCall(nth: number) {
const call = undiciFetch.mock.calls[nth - 1] as [RequestInfo | URL, RequestInit?] | undefined;
if (!call) {
throw new Error(`missing undici fetch call #${nth}`);
}
return resolved;
const init = call[1] as (RequestInit & { dispatcher?: unknown }) | undefined;
return init?.dispatcher as
| {
options?: {
connect?: Record<string, unknown>;
proxyTls?: Record<string, unknown>;
};
}
| undefined;
}
function buildFetchFallbackError(code: string) {
const connectErr = Object.assign(new Error(`connect ${code} api.telegram.org:443`), {
code,
});
return Object.assign(new TypeError("fetch failed"), {
cause: connectErr,
});
}
afterEach(() => {
resetTelegramFetchStateForTests();
setDefaultAutoSelectFamily.mockReset();
setDefaultResultOrder.mockReset();
undiciFetch.mockReset();
setGlobalDispatcher.mockReset();
getGlobalDispatcher.mockClear();
getGlobalDispatcherState.value = undefined;
AgentCtor.mockClear();
EnvHttpProxyAgentCtor.mockClear();
ProxyAgentCtor.mockClear();
setDefaultResultOrder.mockReset();
setDefaultAutoSelectFamily.mockReset();
vi.unstubAllEnvs();
vi.clearAllMocks();
if (originalFetch) {
globalThis.fetch = originalFetch;
} else {
delete (globalThis as { fetch?: typeof fetch }).fetch;
}
});
describe("resolveTelegramFetch", () => {
it("returns wrapped global fetch when available", async () => {
const fetchMock = vi.fn(async () => ({}));
globalThis.fetch = fetchMock as unknown as typeof fetch;
it("wraps proxy fetches and leaves retry policy to caller-provided fetch", async () => {
const proxyFetch = vi.fn(async () => ({ ok: true }) as Response) as unknown as typeof fetch;
const resolved = resolveTelegramFetch();
const resolved = resolveTelegramFetchOrThrow(proxyFetch);
expect(resolved).toBeTypeOf("function");
expect(resolved).not.toBe(fetchMock);
});
await resolved("https://api.telegram.org/botx/getMe");
it("wraps proxy fetches and normalizes foreign signals once", async () => {
let seenSignal: AbortSignal | undefined;
const proxyFetch = vi.fn(async (_input: RequestInfo | URL, init?: RequestInit) => {
seenSignal = init?.signal as AbortSignal | undefined;
return {} as Response;
});
const resolved = resolveTelegramFetch(proxyFetch as unknown as typeof fetch);
expect(resolved).toBeTypeOf("function");
let abortHandler: (() => void) | null = null;
const addEventListener = vi.fn((event: string, handler: () => void) => {
if (event === "abort") {
abortHandler = handler;
}
});
const removeEventListener = vi.fn((event: string, handler: () => void) => {
if (event === "abort" && abortHandler === handler) {
abortHandler = null;
}
});
const fakeSignal = {
aborted: false,
addEventListener,
removeEventListener,
} as unknown as AbortSignal;
if (!resolved) {
throw new Error("expected resolved proxy fetch");
}
await resolved("https://example.com", { signal: fakeSignal });
expect(proxyFetch).toHaveBeenCalledOnce();
expect(seenSignal).toBeInstanceOf(AbortSignal);
expect(seenSignal).not.toBe(fakeSignal);
expect(addEventListener).toHaveBeenCalledTimes(1);
expect(removeEventListener).toHaveBeenCalledTimes(1);
expect(proxyFetch).toHaveBeenCalledTimes(1);
expect(undiciFetch).not.toHaveBeenCalled();
});
it("does not double-wrap an already wrapped proxy fetch", async () => {
const proxyFetch = vi.fn(async () => ({ ok: true }) as Response) as unknown as typeof fetch;
const alreadyWrapped = resolveFetch(proxyFetch);
const wrapped = resolveFetch(proxyFetch);
const resolved = resolveTelegramFetch(alreadyWrapped);
const resolved = resolveTelegramFetch(wrapped);
expect(resolved).toBe(alreadyWrapped);
expect(resolved).toBe(wrapped);
});
it("honors env enable override", async () => {
vi.stubEnv("OPENCLAW_TELEGRAM_ENABLE_AUTO_SELECT_FAMILY", "1");
globalThis.fetch = vi.fn(async () => ({})) as unknown as typeof fetch;
resolveTelegramFetch();
expect(setDefaultAutoSelectFamily).toHaveBeenCalledWith(true);
});
it("uses resolver-scoped Agent dispatcher with configured transport policy", async () => {
undiciFetch.mockResolvedValue({ ok: true } as Response);
it("uses config override when provided", async () => {
globalThis.fetch = vi.fn(async () => ({})) as unknown as typeof fetch;
resolveTelegramFetch(undefined, { network: { autoSelectFamily: true } });
expect(setDefaultAutoSelectFamily).toHaveBeenCalledWith(true);
});
it("env disable override wins over config", async () => {
vi.stubEnv("OPENCLAW_TELEGRAM_ENABLE_AUTO_SELECT_FAMILY", "0");
vi.stubEnv("OPENCLAW_TELEGRAM_DISABLE_AUTO_SELECT_FAMILY", "1");
globalThis.fetch = vi.fn(async () => ({})) as unknown as typeof fetch;
resolveTelegramFetch(undefined, { network: { autoSelectFamily: true } });
expect(setDefaultAutoSelectFamily).toHaveBeenCalledWith(false);
});
it("applies dns result order from config", async () => {
globalThis.fetch = vi.fn(async () => ({})) as unknown as typeof fetch;
resolveTelegramFetch(undefined, { network: { dnsResultOrder: "verbatim" } });
expect(setDefaultResultOrder).toHaveBeenCalledWith("verbatim");
});
it("retries dns setter on next call when previous attempt threw", async () => {
setDefaultResultOrder.mockImplementationOnce(() => {
throw new Error("dns setter failed once");
});
globalThis.fetch = vi.fn(async () => ({})) as unknown as typeof fetch;
resolveTelegramFetch(undefined, { network: { dnsResultOrder: "ipv4first" } });
resolveTelegramFetch(undefined, { network: { dnsResultOrder: "ipv4first" } });
expect(setDefaultResultOrder).toHaveBeenCalledTimes(2);
});
it("replaces global undici dispatcher with proxy-aware EnvHttpProxyAgent", async () => {
globalThis.fetch = vi.fn(async () => ({})) as unknown as typeof fetch;
resolveTelegramFetch(undefined, { network: { autoSelectFamily: true } });
expect(setGlobalDispatcher).toHaveBeenCalledTimes(1);
expectEnvProxyAgentConstructorCall({ nth: 1, autoSelectFamily: true });
});
it("keeps an existing proxy-like global dispatcher", async () => {
getGlobalDispatcherState.value = {
constructor: { name: "ProxyAgent" },
};
globalThis.fetch = vi.fn(async () => ({})) as unknown as typeof fetch;
resolveTelegramFetch(undefined, { network: { autoSelectFamily: true } });
expect(setGlobalDispatcher).not.toHaveBeenCalled();
expect(EnvHttpProxyAgentCtor).not.toHaveBeenCalled();
});
it("updates proxy-like dispatcher when proxy env is configured", async () => {
vi.stubEnv("HTTPS_PROXY", "http://127.0.0.1:7890");
getGlobalDispatcherState.value = {
constructor: { name: "ProxyAgent" },
};
globalThis.fetch = vi.fn(async () => ({})) as unknown as typeof fetch;
resolveTelegramFetch(undefined, { network: { autoSelectFamily: true } });
expect(setGlobalDispatcher).toHaveBeenCalledTimes(1);
expect(EnvHttpProxyAgentCtor).toHaveBeenCalledTimes(1);
});
it("sets global dispatcher only once across repeated equal decisions", async () => {
globalThis.fetch = vi.fn(async () => ({})) as unknown as typeof fetch;
resolveTelegramFetch(undefined, { network: { autoSelectFamily: true } });
resolveTelegramFetch(undefined, { network: { autoSelectFamily: true } });
expect(setGlobalDispatcher).toHaveBeenCalledTimes(1);
});
it("updates global dispatcher when autoSelectFamily decision changes", async () => {
globalThis.fetch = vi.fn(async () => ({})) as unknown as typeof fetch;
resolveTelegramFetch(undefined, { network: { autoSelectFamily: true } });
resolveTelegramFetch(undefined, { network: { autoSelectFamily: false } });
expect(setGlobalDispatcher).toHaveBeenCalledTimes(2);
expectEnvProxyAgentConstructorCall({ nth: 1, autoSelectFamily: true });
expectEnvProxyAgentConstructorCall({ nth: 2, autoSelectFamily: false });
});
it("retries once with ipv4 fallback when fetch fails with network timeout/unreachable", async () => {
const timeoutErr = Object.assign(new Error("connect ETIMEDOUT 149.154.166.110:443"), {
code: "ETIMEDOUT",
});
const unreachableErr = Object.assign(
new Error("connect ENETUNREACH 2001:67c:4e8:f004::9:443"),
{
code: "ENETUNREACH",
const resolved = resolveTelegramFetchOrThrow(undefined, {
network: {
autoSelectFamily: true,
dnsResultOrder: "verbatim",
},
);
const fetchError = Object.assign(new TypeError("fetch failed"), {
cause: Object.assign(new Error("aggregate"), {
errors: [timeoutErr, unreachableErr],
}),
});
const fetchMock = vi
.fn()
.mockRejectedValueOnce(fetchError)
.mockResolvedValueOnce({ ok: true } as Response);
globalThis.fetch = fetchMock as unknown as typeof fetch;
const resolved = resolveTelegramFetchOrThrow();
await resolved("https://api.telegram.org/botx/getMe");
await resolved("https://api.telegram.org/file/botx/photos/file_1.jpg");
expect(AgentCtor).toHaveBeenCalledTimes(1);
expect(EnvHttpProxyAgentCtor).not.toHaveBeenCalled();
expect(fetchMock).toHaveBeenCalledTimes(2);
expect(setGlobalDispatcher).toHaveBeenCalledTimes(2);
expectEnvProxyAgentConstructorCall({ nth: 1, autoSelectFamily: true });
expectEnvProxyAgentConstructorCall({ nth: 2, autoSelectFamily: false });
const dispatcher = getDispatcherFromUndiciCall(1);
expect(dispatcher).toBeDefined();
expect(dispatcher?.options?.connect).toEqual(
expect.objectContaining({
autoSelectFamily: true,
autoSelectFamilyAttemptTimeout: 300,
}),
);
expect(typeof dispatcher?.options?.connect?.lookup).toBe("function");
});
it("retries with ipv4 fallback once per request, not once per process", async () => {
const timeoutErr = Object.assign(new Error("connect ETIMEDOUT 149.154.166.110:443"), {
code: "ETIMEDOUT",
it("uses EnvHttpProxyAgent dispatcher when proxy env is configured", async () => {
vi.stubEnv("HTTPS_PROXY", "http://127.0.0.1:7890");
undiciFetch.mockResolvedValue({ ok: true } as Response);
const resolved = resolveTelegramFetchOrThrow(undefined, {
network: {
autoSelectFamily: false,
dnsResultOrder: "ipv4first",
},
});
const fetchError = Object.assign(new TypeError("fetch failed"), {
cause: timeoutErr,
await resolved("https://api.telegram.org/botx/getMe");
expect(EnvHttpProxyAgentCtor).toHaveBeenCalledTimes(1);
expect(AgentCtor).not.toHaveBeenCalled();
const dispatcher = getDispatcherFromUndiciCall(1);
expect(dispatcher?.options?.connect).toEqual(
expect.objectContaining({
autoSelectFamily: false,
autoSelectFamilyAttemptTimeout: 300,
}),
);
expect(dispatcher?.options?.proxyTls).toEqual(
expect.objectContaining({
autoSelectFamily: false,
autoSelectFamilyAttemptTimeout: 300,
}),
);
});
it("pins env-proxy transport policy onto proxyTls for proxied HTTPS requests", async () => {
vi.stubEnv("HTTPS_PROXY", "http://127.0.0.1:7890");
undiciFetch.mockResolvedValue({ ok: true } as Response);
const resolved = resolveTelegramFetchOrThrow(undefined, {
network: {
autoSelectFamily: true,
dnsResultOrder: "ipv4first",
},
});
const fetchMock = vi
.fn()
await resolved("https://api.telegram.org/botx/getMe");
const dispatcher = getDispatcherFromUndiciCall(1);
expect(dispatcher?.options?.connect).toEqual(
expect.objectContaining({
autoSelectFamily: true,
autoSelectFamilyAttemptTimeout: 300,
}),
);
expect(dispatcher?.options?.proxyTls).toEqual(
expect.objectContaining({
autoSelectFamily: true,
autoSelectFamilyAttemptTimeout: 300,
}),
);
});
it("keeps resolver-scoped transport policy for OpenClaw proxy fetches", async () => {
const { makeProxyFetch } = await import("./proxy.js");
const proxyFetch = makeProxyFetch("http://127.0.0.1:7890");
ProxyAgentCtor.mockClear();
undiciFetch.mockResolvedValue({ ok: true } as Response);
const resolved = resolveTelegramFetchOrThrow(proxyFetch, {
network: {
autoSelectFamily: false,
dnsResultOrder: "ipv4first",
},
});
await resolved("https://api.telegram.org/botx/getMe");
expect(ProxyAgentCtor).toHaveBeenCalledTimes(1);
expect(EnvHttpProxyAgentCtor).not.toHaveBeenCalled();
expect(AgentCtor).not.toHaveBeenCalled();
const dispatcher = getDispatcherFromUndiciCall(1);
expect(dispatcher?.options).toEqual(
expect.objectContaining({
uri: "http://127.0.0.1:7890",
}),
);
expect(dispatcher?.options?.proxyTls).toEqual(
expect.objectContaining({
autoSelectFamily: false,
}),
);
});
it("does not blind-retry when sticky IPv4 fallback is disallowed for explicit proxy paths", async () => {
const { makeProxyFetch } = await import("./proxy.js");
const proxyFetch = makeProxyFetch("http://127.0.0.1:7890");
ProxyAgentCtor.mockClear();
const fetchError = buildFetchFallbackError("EHOSTUNREACH");
undiciFetch.mockRejectedValueOnce(fetchError).mockResolvedValueOnce({ ok: true } as Response);
const resolved = resolveTelegramFetchOrThrow(proxyFetch, {
network: {
autoSelectFamily: true,
dnsResultOrder: "ipv4first",
},
});
await expect(resolved("https://api.telegram.org/botx/sendMessage")).rejects.toThrow(
"fetch failed",
);
await resolved("https://api.telegram.org/botx/sendChatAction");
expect(undiciFetch).toHaveBeenCalledTimes(2);
expect(ProxyAgentCtor).toHaveBeenCalledTimes(1);
const firstDispatcher = getDispatcherFromUndiciCall(1);
const secondDispatcher = getDispatcherFromUndiciCall(2);
expect(firstDispatcher).toBe(secondDispatcher);
expect(firstDispatcher?.options?.proxyTls).toEqual(
expect.objectContaining({
autoSelectFamily: true,
autoSelectFamilyAttemptTimeout: 300,
}),
);
expect(firstDispatcher?.options?.proxyTls?.family).not.toBe(4);
});
it("does not blind-retry when sticky IPv4 fallback is disallowed for env proxy paths", async () => {
vi.stubEnv("HTTPS_PROXY", "http://127.0.0.1:7890");
const fetchError = buildFetchFallbackError("EHOSTUNREACH");
undiciFetch.mockRejectedValueOnce(fetchError).mockResolvedValueOnce({ ok: true } as Response);
const resolved = resolveTelegramFetchOrThrow(undefined, {
network: {
autoSelectFamily: true,
dnsResultOrder: "ipv4first",
},
});
await expect(resolved("https://api.telegram.org/botx/sendMessage")).rejects.toThrow(
"fetch failed",
);
await resolved("https://api.telegram.org/botx/sendChatAction");
expect(undiciFetch).toHaveBeenCalledTimes(2);
expect(EnvHttpProxyAgentCtor).toHaveBeenCalledTimes(1);
const firstDispatcher = getDispatcherFromUndiciCall(1);
const secondDispatcher = getDispatcherFromUndiciCall(2);
expect(firstDispatcher).toBe(secondDispatcher);
expect(firstDispatcher?.options?.connect).toEqual(
expect.objectContaining({
autoSelectFamily: true,
autoSelectFamilyAttemptTimeout: 300,
}),
);
expect(firstDispatcher?.options?.connect?.family).not.toBe(4);
});
it("treats ALL_PROXY-only env as direct transport and arms sticky IPv4 fallback", async () => {
vi.stubEnv("ALL_PROXY", "socks5://127.0.0.1:1080");
const fetchError = buildFetchFallbackError("EHOSTUNREACH");
undiciFetch
.mockRejectedValueOnce(fetchError)
.mockResolvedValueOnce({ ok: true } as Response)
.mockRejectedValueOnce(fetchError)
.mockResolvedValueOnce({ ok: true } as Response);
globalThis.fetch = fetchMock as unknown as typeof fetch;
const resolved = resolveTelegramFetchOrThrow();
const resolved = resolveTelegramFetchOrThrow(undefined, {
network: {
autoSelectFamily: true,
dnsResultOrder: "ipv4first",
},
});
await resolved("https://api.telegram.org/file/botx/photos/file_1.jpg");
await resolved("https://api.telegram.org/file/botx/photos/file_2.jpg");
await resolved("https://api.telegram.org/botx/sendMessage");
await resolved("https://api.telegram.org/botx/sendChatAction");
expect(fetchMock).toHaveBeenCalledTimes(4);
expect(EnvHttpProxyAgentCtor).not.toHaveBeenCalled();
expect(AgentCtor).toHaveBeenCalledTimes(2);
const firstDispatcher = getDispatcherFromUndiciCall(1);
const secondDispatcher = getDispatcherFromUndiciCall(2);
const thirdDispatcher = getDispatcherFromUndiciCall(3);
expect(firstDispatcher).not.toBe(secondDispatcher);
expect(secondDispatcher).toBe(thirdDispatcher);
expect(secondDispatcher?.options?.connect).toEqual(
expect.objectContaining({
family: 4,
autoSelectFamily: false,
}),
);
});
it("does not retry when fetch fails without fallback network error codes", async () => {
const fetchError = Object.assign(new TypeError("fetch failed"), {
cause: Object.assign(new Error("connect ECONNRESET"), {
code: "ECONNRESET",
}),
it("arms sticky IPv4 fallback when env proxy init falls back to direct Agent", async () => {
vi.stubEnv("HTTPS_PROXY", "http://127.0.0.1:7890");
EnvHttpProxyAgentCtor.mockImplementationOnce(function ThrowingEnvProxyAgent() {
throw new Error("invalid proxy config");
});
const fetchMock = vi.fn().mockRejectedValue(fetchError);
globalThis.fetch = fetchMock as unknown as typeof fetch;
const fetchError = buildFetchFallbackError("EHOSTUNREACH");
undiciFetch
.mockRejectedValueOnce(fetchError)
.mockResolvedValueOnce({ ok: true } as Response)
.mockResolvedValueOnce({ ok: true } as Response);
const resolved = resolveTelegramFetchOrThrow();
const resolved = resolveTelegramFetchOrThrow(undefined, {
network: {
autoSelectFamily: true,
dnsResultOrder: "ipv4first",
},
});
await expect(resolved("https://api.telegram.org/file/botx/photos/file_3.jpg")).rejects.toThrow(
await resolved("https://api.telegram.org/botx/sendMessage");
await resolved("https://api.telegram.org/botx/sendChatAction");
expect(undiciFetch).toHaveBeenCalledTimes(3);
expect(EnvHttpProxyAgentCtor).toHaveBeenCalledTimes(1);
expect(AgentCtor).toHaveBeenCalledTimes(2);
const firstDispatcher = getDispatcherFromUndiciCall(1);
const secondDispatcher = getDispatcherFromUndiciCall(2);
const thirdDispatcher = getDispatcherFromUndiciCall(3);
expect(firstDispatcher).not.toBe(secondDispatcher);
expect(secondDispatcher).toBe(thirdDispatcher);
expect(secondDispatcher?.options?.connect).toEqual(
expect.objectContaining({
family: 4,
autoSelectFamily: false,
}),
);
});
it("arms sticky IPv4 fallback when NO_PROXY bypasses telegram under env proxy", async () => {
vi.stubEnv("HTTPS_PROXY", "http://127.0.0.1:7890");
vi.stubEnv("NO_PROXY", "api.telegram.org");
const fetchError = buildFetchFallbackError("EHOSTUNREACH");
undiciFetch
.mockRejectedValueOnce(fetchError)
.mockResolvedValueOnce({ ok: true } as Response)
.mockResolvedValueOnce({ ok: true } as Response);
const resolved = resolveTelegramFetchOrThrow(undefined, {
network: {
autoSelectFamily: true,
dnsResultOrder: "ipv4first",
},
});
await resolved("https://api.telegram.org/botx/sendMessage");
await resolved("https://api.telegram.org/botx/sendChatAction");
expect(undiciFetch).toHaveBeenCalledTimes(3);
expect(EnvHttpProxyAgentCtor).toHaveBeenCalledTimes(2);
expect(AgentCtor).not.toHaveBeenCalled();
const firstDispatcher = getDispatcherFromUndiciCall(1);
const secondDispatcher = getDispatcherFromUndiciCall(2);
const thirdDispatcher = getDispatcherFromUndiciCall(3);
expect(firstDispatcher).not.toBe(secondDispatcher);
expect(secondDispatcher).toBe(thirdDispatcher);
expect(secondDispatcher?.options?.connect).toEqual(
expect.objectContaining({
family: 4,
autoSelectFamily: false,
}),
);
});
it("uses no_proxy over NO_PROXY when deciding env-proxy bypass", async () => {
vi.stubEnv("HTTPS_PROXY", "http://127.0.0.1:7890");
vi.stubEnv("NO_PROXY", "");
vi.stubEnv("no_proxy", "api.telegram.org");
const fetchError = buildFetchFallbackError("EHOSTUNREACH");
undiciFetch
.mockRejectedValueOnce(fetchError)
.mockResolvedValueOnce({ ok: true } as Response)
.mockResolvedValueOnce({ ok: true } as Response);
const resolved = resolveTelegramFetchOrThrow(undefined, {
network: {
autoSelectFamily: true,
dnsResultOrder: "ipv4first",
},
});
await resolved("https://api.telegram.org/botx/sendMessage");
await resolved("https://api.telegram.org/botx/sendChatAction");
expect(EnvHttpProxyAgentCtor).toHaveBeenCalledTimes(2);
const secondDispatcher = getDispatcherFromUndiciCall(2);
expect(secondDispatcher?.options?.connect).toEqual(
expect.objectContaining({
family: 4,
autoSelectFamily: false,
}),
);
});
it("matches whitespace and wildcard no_proxy entries like EnvHttpProxyAgent", async () => {
vi.stubEnv("HTTPS_PROXY", "http://127.0.0.1:7890");
vi.stubEnv("no_proxy", "localhost *.telegram.org");
const fetchError = buildFetchFallbackError("EHOSTUNREACH");
undiciFetch
.mockRejectedValueOnce(fetchError)
.mockResolvedValueOnce({ ok: true } as Response)
.mockResolvedValueOnce({ ok: true } as Response);
const resolved = resolveTelegramFetchOrThrow(undefined, {
network: {
autoSelectFamily: true,
dnsResultOrder: "ipv4first",
},
});
await resolved("https://api.telegram.org/botx/sendMessage");
await resolved("https://api.telegram.org/botx/sendChatAction");
expect(EnvHttpProxyAgentCtor).toHaveBeenCalledTimes(2);
const secondDispatcher = getDispatcherFromUndiciCall(2);
expect(secondDispatcher?.options?.connect).toEqual(
expect.objectContaining({
family: 4,
autoSelectFamily: false,
}),
);
});
it("fails closed when explicit proxy dispatcher initialization fails", async () => {
const { makeProxyFetch } = await import("./proxy.js");
const proxyFetch = makeProxyFetch("http://127.0.0.1:7890");
ProxyAgentCtor.mockClear();
ProxyAgentCtor.mockImplementationOnce(function ThrowingProxyAgent() {
throw new Error("invalid proxy config");
});
expect(() =>
resolveTelegramFetchOrThrow(proxyFetch, {
network: {
autoSelectFamily: true,
dnsResultOrder: "ipv4first",
},
}),
).toThrow("explicit proxy dispatcher init failed: invalid proxy config");
});
it("falls back to Agent when env proxy dispatcher initialization fails", async () => {
vi.stubEnv("HTTPS_PROXY", "http://127.0.0.1:7890");
EnvHttpProxyAgentCtor.mockImplementationOnce(function ThrowingEnvProxyAgent() {
throw new Error("invalid proxy config");
});
undiciFetch.mockResolvedValue({ ok: true } as Response);
const resolved = resolveTelegramFetchOrThrow(undefined, {
network: {
autoSelectFamily: false,
},
});
await resolved("https://api.telegram.org/botx/getMe");
expect(EnvHttpProxyAgentCtor).toHaveBeenCalledTimes(1);
expect(AgentCtor).toHaveBeenCalledTimes(1);
const dispatcher = getDispatcherFromUndiciCall(1);
expect(dispatcher?.options?.connect).toEqual(
expect.objectContaining({
autoSelectFamily: false,
}),
);
});
it("retries once and then keeps sticky IPv4 dispatcher for subsequent requests", async () => {
const fetchError = buildFetchFallbackError("ETIMEDOUT");
undiciFetch
.mockRejectedValueOnce(fetchError)
.mockResolvedValueOnce({ ok: true } as Response)
.mockResolvedValueOnce({ ok: true } as Response);
const resolved = resolveTelegramFetchOrThrow(undefined, {
network: {
autoSelectFamily: true,
},
});
await resolved("https://api.telegram.org/botx/sendMessage");
await resolved("https://api.telegram.org/botx/sendChatAction");
expect(undiciFetch).toHaveBeenCalledTimes(3);
const firstDispatcher = getDispatcherFromUndiciCall(1);
const secondDispatcher = getDispatcherFromUndiciCall(2);
const thirdDispatcher = getDispatcherFromUndiciCall(3);
expect(firstDispatcher).toBeDefined();
expect(secondDispatcher).toBeDefined();
expect(thirdDispatcher).toBeDefined();
expect(firstDispatcher).not.toBe(secondDispatcher);
expect(secondDispatcher).toBe(thirdDispatcher);
expect(firstDispatcher?.options?.connect).toEqual(
expect.objectContaining({
autoSelectFamily: true,
autoSelectFamilyAttemptTimeout: 300,
}),
);
expect(secondDispatcher?.options?.connect).toEqual(
expect.objectContaining({
family: 4,
autoSelectFamily: false,
}),
);
});
it("preserves caller-provided dispatcher across fallback retry", async () => {
const fetchError = buildFetchFallbackError("EHOSTUNREACH");
undiciFetch.mockRejectedValueOnce(fetchError).mockResolvedValueOnce({ ok: true } as Response);
const resolved = resolveTelegramFetchOrThrow(undefined, {
network: {
autoSelectFamily: true,
},
});
const callerDispatcher = { name: "caller" };
await resolved("https://api.telegram.org/botx/sendMessage", {
dispatcher: callerDispatcher,
} as RequestInit);
expect(undiciFetch).toHaveBeenCalledTimes(2);
const firstCallInit = undiciFetch.mock.calls[0]?.[1] as
| (RequestInit & { dispatcher?: unknown })
| undefined;
const secondCallInit = undiciFetch.mock.calls[1]?.[1] as
| (RequestInit & { dispatcher?: unknown })
| undefined;
expect(firstCallInit?.dispatcher).toBe(callerDispatcher);
expect(secondCallInit?.dispatcher).toBe(callerDispatcher);
});
it("does not arm sticky fallback from caller-provided dispatcher failures", async () => {
const fetchError = buildFetchFallbackError("EHOSTUNREACH");
undiciFetch
.mockRejectedValueOnce(fetchError)
.mockResolvedValueOnce({ ok: true } as Response)
.mockResolvedValueOnce({ ok: true } as Response);
const resolved = resolveTelegramFetchOrThrow(undefined, {
network: {
autoSelectFamily: true,
},
});
const callerDispatcher = { name: "caller" };
await resolved("https://api.telegram.org/botx/sendMessage", {
dispatcher: callerDispatcher,
} as RequestInit);
await resolved("https://api.telegram.org/botx/sendChatAction");
expect(undiciFetch).toHaveBeenCalledTimes(3);
const firstCallInit = undiciFetch.mock.calls[0]?.[1] as
| (RequestInit & { dispatcher?: unknown })
| undefined;
const secondCallInit = undiciFetch.mock.calls[1]?.[1] as
| (RequestInit & { dispatcher?: unknown })
| undefined;
const thirdDispatcher = getDispatcherFromUndiciCall(3);
expect(firstCallInit?.dispatcher).toBe(callerDispatcher);
expect(secondCallInit?.dispatcher).toBe(callerDispatcher);
expect(thirdDispatcher?.options?.connect).toEqual(
expect.objectContaining({
autoSelectFamily: true,
autoSelectFamilyAttemptTimeout: 300,
}),
);
expect(thirdDispatcher?.options?.connect?.family).not.toBe(4);
});
it("does not retry when error codes do not match fallback rules", async () => {
const fetchError = buildFetchFallbackError("ECONNRESET");
undiciFetch.mockRejectedValue(fetchError);
const resolved = resolveTelegramFetchOrThrow(undefined, {
network: {
autoSelectFamily: true,
},
});
await expect(resolved("https://api.telegram.org/botx/sendMessage")).rejects.toThrow(
"fetch failed",
);
expect(fetchMock).toHaveBeenCalledTimes(1);
expect(undiciFetch).toHaveBeenCalledTimes(1);
});
it("keeps per-resolver transport policy isolated across multiple accounts", async () => {
undiciFetch.mockResolvedValue({ ok: true } as Response);
const resolverA = resolveTelegramFetchOrThrow(undefined, {
network: {
autoSelectFamily: false,
dnsResultOrder: "ipv4first",
},
});
const resolverB = resolveTelegramFetchOrThrow(undefined, {
network: {
autoSelectFamily: true,
dnsResultOrder: "verbatim",
},
});
await resolverA("https://api.telegram.org/botA/getMe");
await resolverB("https://api.telegram.org/botB/getMe");
const dispatcherA = getDispatcherFromUndiciCall(1);
const dispatcherB = getDispatcherFromUndiciCall(2);
expect(dispatcherA).toBeDefined();
expect(dispatcherB).toBeDefined();
expect(dispatcherA).not.toBe(dispatcherB);
expect(dispatcherA?.options?.connect).toEqual(
expect.objectContaining({
autoSelectFamily: false,
}),
);
expect(dispatcherB?.options?.connect).toEqual(
expect.objectContaining({
autoSelectFamily: true,
}),
);
// Core guarantee: Telegram transport no longer mutates process-global defaults.
expect(setGlobalDispatcher).not.toHaveBeenCalled();
expect(setDefaultResultOrder).not.toHaveBeenCalled();
expect(setDefaultAutoSelectFamily).not.toHaveBeenCalled();
});
});