Files
openclaw/src/telegram/proxy.test.ts
Eugene 45b74fb56c 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
2026-03-10 11:28:51 +05:30

57 lines
1.6 KiB
TypeScript

import { describe, expect, it, vi } from "vitest";
const mocks = vi.hoisted(() => {
const undiciFetch = vi.fn();
const proxyAgentSpy = vi.fn();
const setGlobalDispatcher = vi.fn();
class ProxyAgent {
static lastCreated: ProxyAgent | undefined;
proxyUrl: string;
constructor(proxyUrl: string) {
this.proxyUrl = proxyUrl;
ProxyAgent.lastCreated = this;
proxyAgentSpy(proxyUrl);
}
}
return {
ProxyAgent,
undiciFetch,
proxyAgentSpy,
setGlobalDispatcher,
getLastAgent: () => ProxyAgent.lastCreated,
};
});
vi.mock("undici", () => ({
ProxyAgent: mocks.ProxyAgent,
fetch: mocks.undiciFetch,
setGlobalDispatcher: mocks.setGlobalDispatcher,
}));
import { getProxyUrlFromFetch, makeProxyFetch } from "./proxy.js";
describe("makeProxyFetch", () => {
it("uses undici fetch with ProxyAgent dispatcher", async () => {
const proxyUrl = "http://proxy.test:8080";
mocks.undiciFetch.mockResolvedValue({ ok: true });
const proxyFetch = makeProxyFetch(proxyUrl);
await proxyFetch("https://api.telegram.org/bot123/getMe");
expect(mocks.proxyAgentSpy).toHaveBeenCalledWith(proxyUrl);
expect(mocks.undiciFetch).toHaveBeenCalledWith(
"https://api.telegram.org/bot123/getMe",
expect.objectContaining({ dispatcher: mocks.getLastAgent() }),
);
expect(mocks.setGlobalDispatcher).not.toHaveBeenCalled();
});
it("attaches proxy metadata for resolver transport handling", () => {
const proxyUrl = "http://proxy.test:8080";
const proxyFetch = makeProxyFetch(proxyUrl);
expect(getProxyUrlFromFetch(proxyFetch)).toBe(proxyUrl);
});
});