|
| 1 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import { resolveTelegramTransport } from "../telegram/fetch.js"; |
| 3 | +import { fetchRemoteMedia } from "./fetch.js"; |
| 4 | + |
| 5 | +const undiciFetch = vi.hoisted(() => vi.fn()); |
| 6 | +const AgentCtor = vi.hoisted(() => |
| 7 | + vi.fn(function MockAgent( |
| 8 | + this: { options?: Record<string, unknown> }, |
| 9 | + options?: Record<string, unknown>, |
| 10 | + ) { |
| 11 | + this.options = options; |
| 12 | + }), |
| 13 | +); |
| 14 | +const EnvHttpProxyAgentCtor = vi.hoisted(() => |
| 15 | + vi.fn(function MockEnvHttpProxyAgent( |
| 16 | + this: { options?: Record<string, unknown> }, |
| 17 | + options?: Record<string, unknown>, |
| 18 | + ) { |
| 19 | + this.options = options; |
| 20 | + }), |
| 21 | +); |
| 22 | +const ProxyAgentCtor = vi.hoisted(() => |
| 23 | + vi.fn(function MockProxyAgent( |
| 24 | + this: { options?: Record<string, unknown> | string }, |
| 25 | + options?: Record<string, unknown> | string, |
| 26 | + ) { |
| 27 | + this.options = options; |
| 28 | + }), |
| 29 | +); |
| 30 | + |
| 31 | +vi.mock("undici", () => ({ |
| 32 | + Agent: AgentCtor, |
| 33 | + EnvHttpProxyAgent: EnvHttpProxyAgentCtor, |
| 34 | + ProxyAgent: ProxyAgentCtor, |
| 35 | + fetch: undiciFetch, |
| 36 | +})); |
| 37 | + |
| 38 | +describe("fetchRemoteMedia telegram network policy", () => { |
| 39 | + type LookupFn = NonNullable<Parameters<typeof fetchRemoteMedia>[0]["lookupFn"]>; |
| 40 | + |
| 41 | + afterEach(() => { |
| 42 | + undiciFetch.mockReset(); |
| 43 | + AgentCtor.mockClear(); |
| 44 | + EnvHttpProxyAgentCtor.mockClear(); |
| 45 | + ProxyAgentCtor.mockClear(); |
| 46 | + vi.unstubAllEnvs(); |
| 47 | + }); |
| 48 | + |
| 49 | + it("preserves Telegram resolver transport policy for file downloads", async () => { |
| 50 | + const lookupFn = vi.fn(async () => [ |
| 51 | + { address: "149.154.167.220", family: 4 }, |
| 52 | + ]) as unknown as LookupFn; |
| 53 | + undiciFetch.mockResolvedValueOnce( |
| 54 | + new Response(new Uint8Array([0xff, 0xd8, 0xff, 0x00]), { |
| 55 | + status: 200, |
| 56 | + headers: { "content-type": "image/jpeg" }, |
| 57 | + }), |
| 58 | + ); |
| 59 | + |
| 60 | + const telegramTransport = resolveTelegramTransport(undefined, { |
| 61 | + network: { |
| 62 | + autoSelectFamily: true, |
| 63 | + dnsResultOrder: "verbatim", |
| 64 | + }, |
| 65 | + }); |
| 66 | + |
| 67 | + await fetchRemoteMedia({ |
| 68 | + url: "https://api.telegram.org/file/bottok/photos/1.jpg", |
| 69 | + fetchImpl: telegramTransport.sourceFetch, |
| 70 | + dispatcherPolicy: telegramTransport.pinnedDispatcherPolicy, |
| 71 | + lookupFn, |
| 72 | + maxBytes: 1024, |
| 73 | + ssrfPolicy: { |
| 74 | + allowedHostnames: ["api.telegram.org"], |
| 75 | + allowRfc2544BenchmarkRange: true, |
| 76 | + }, |
| 77 | + }); |
| 78 | + |
| 79 | + const init = undiciFetch.mock.calls[0]?.[1] as |
| 80 | + | (RequestInit & { |
| 81 | + dispatcher?: { |
| 82 | + options?: { |
| 83 | + connect?: Record<string, unknown>; |
| 84 | + }; |
| 85 | + }; |
| 86 | + }) |
| 87 | + | undefined; |
| 88 | + |
| 89 | + expect(init?.dispatcher?.options?.connect).toEqual( |
| 90 | + expect.objectContaining({ |
| 91 | + autoSelectFamily: true, |
| 92 | + autoSelectFamilyAttemptTimeout: 300, |
| 93 | + lookup: expect.any(Function), |
| 94 | + }), |
| 95 | + ); |
| 96 | + }); |
| 97 | + |
| 98 | + it("keeps explicit proxy routing for file downloads", async () => { |
| 99 | + const { makeProxyFetch } = await import("../telegram/proxy.js"); |
| 100 | + const lookupFn = vi.fn(async () => [ |
| 101 | + { address: "149.154.167.220", family: 4 }, |
| 102 | + ]) as unknown as LookupFn; |
| 103 | + undiciFetch.mockResolvedValueOnce( |
| 104 | + new Response(new Uint8Array([0x25, 0x50, 0x44, 0x46]), { |
| 105 | + status: 200, |
| 106 | + headers: { "content-type": "application/pdf" }, |
| 107 | + }), |
| 108 | + ); |
| 109 | + |
| 110 | + const telegramTransport = resolveTelegramTransport(makeProxyFetch("http://127.0.0.1:7890"), { |
| 111 | + network: { |
| 112 | + autoSelectFamily: false, |
| 113 | + dnsResultOrder: "ipv4first", |
| 114 | + }, |
| 115 | + }); |
| 116 | + |
| 117 | + await fetchRemoteMedia({ |
| 118 | + url: "https://api.telegram.org/file/bottok/files/1.pdf", |
| 119 | + fetchImpl: telegramTransport.sourceFetch, |
| 120 | + dispatcherPolicy: telegramTransport.pinnedDispatcherPolicy, |
| 121 | + lookupFn, |
| 122 | + maxBytes: 1024, |
| 123 | + ssrfPolicy: { |
| 124 | + allowedHostnames: ["api.telegram.org"], |
| 125 | + allowRfc2544BenchmarkRange: true, |
| 126 | + }, |
| 127 | + }); |
| 128 | + |
| 129 | + const init = undiciFetch.mock.calls[0]?.[1] as |
| 130 | + | (RequestInit & { |
| 131 | + dispatcher?: { |
| 132 | + options?: { |
| 133 | + uri?: string; |
| 134 | + }; |
| 135 | + }; |
| 136 | + }) |
| 137 | + | undefined; |
| 138 | + |
| 139 | + expect(init?.dispatcher?.options?.uri).toBe("http://127.0.0.1:7890"); |
| 140 | + expect(ProxyAgentCtor).toHaveBeenCalled(); |
| 141 | + }); |
| 142 | +}); |
0 commit comments