|
1 | | -import { afterEach, describe, expect, it, vi } from "vitest"; |
2 | | -import { resolveFetch } from "../../../src/infra/fetch.js"; |
3 | | -import { resolveTelegramFetch, resolveTelegramTransport } from "./fetch.js"; |
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
4 | 2 |
|
5 | 3 | const setDefaultResultOrder = vi.hoisted(() => vi.fn()); |
6 | 4 | const setDefaultAutoSelectFamily = vi.hoisted(() => vi.fn()); |
@@ -56,6 +54,16 @@ vi.mock("undici", () => ({ |
56 | 54 | setGlobalDispatcher, |
57 | 55 | })); |
58 | 56 |
|
| 57 | +let resolveFetch: typeof import("../../../src/infra/fetch.js").resolveFetch; |
| 58 | +let resolveTelegramFetch: typeof import("./fetch.js").resolveTelegramFetch; |
| 59 | +let resolveTelegramTransport: typeof import("./fetch.js").resolveTelegramTransport; |
| 60 | + |
| 61 | +beforeEach(async () => { |
| 62 | + vi.resetModules(); |
| 63 | + ({ resolveFetch } = await import("../../../src/infra/fetch.js")); |
| 64 | + ({ resolveTelegramFetch, resolveTelegramTransport } = await import("./fetch.js")); |
| 65 | +}); |
| 66 | + |
59 | 67 | function resolveTelegramFetchOrThrow( |
60 | 68 | proxyFetch?: typeof fetch, |
61 | 69 | options?: { network?: { autoSelectFamily?: boolean; dnsResultOrder?: "ipv4first" | "verbatim" } }, |
@@ -152,6 +160,24 @@ function expectPinnedIpv4ConnectDispatcher(args: { |
152 | 160 | } |
153 | 161 | } |
154 | 162 |
|
| 163 | +function expectPinnedFallbackIpDispatcher(callIndex: number) { |
| 164 | + const dispatcher = getDispatcherFromUndiciCall(callIndex); |
| 165 | + expect(dispatcher?.options?.connect).toEqual( |
| 166 | + expect.objectContaining({ |
| 167 | + family: 4, |
| 168 | + autoSelectFamily: false, |
| 169 | + lookup: expect.any(Function), |
| 170 | + }), |
| 171 | + ); |
| 172 | + const callback = vi.fn(); |
| 173 | + ( |
| 174 | + dispatcher?.options?.connect?.lookup as |
| 175 | + | ((hostname: string, callback: (err: null, address: string, family: number) => void) => void) |
| 176 | + | undefined |
| 177 | + )?.("api.telegram.org", callback); |
| 178 | + expect(callback).toHaveBeenCalledWith(null, "149.154.167.220", 4); |
| 179 | +} |
| 180 | + |
155 | 181 | function expectCallerDispatcherPreserved(callIndexes: number[], dispatcher: unknown) { |
156 | 182 | for (const callIndex of callIndexes) { |
157 | 183 | const callInit = undiciFetch.mock.calls[callIndex - 1]?.[1] as |
@@ -395,7 +421,7 @@ describe("resolveTelegramFetch", () => { |
395 | 421 | pinnedCall: 2, |
396 | 422 | followupCall: 3, |
397 | 423 | }); |
398 | | - expect(transport.pinnedDispatcherPolicy).toEqual( |
| 424 | + expect(transport.dispatcherAttempts?.[0]?.dispatcherPolicy).toEqual( |
399 | 425 | expect.objectContaining({ |
400 | 426 | mode: "direct", |
401 | 427 | }), |
@@ -533,6 +559,34 @@ describe("resolveTelegramFetch", () => { |
533 | 559 | ); |
534 | 560 | }); |
535 | 561 |
|
| 562 | + it("escalates from IPv4 fallback to pinned Telegram IP and keeps it sticky", async () => { |
| 563 | + undiciFetch |
| 564 | + .mockRejectedValueOnce(buildFetchFallbackError("ETIMEDOUT")) |
| 565 | + .mockRejectedValueOnce(buildFetchFallbackError("EHOSTUNREACH")) |
| 566 | + .mockResolvedValueOnce({ ok: true } as Response) |
| 567 | + .mockResolvedValueOnce({ ok: true } as Response); |
| 568 | + |
| 569 | + const resolved = resolveTelegramFetchOrThrow(undefined, { |
| 570 | + network: { |
| 571 | + autoSelectFamily: true, |
| 572 | + dnsResultOrder: "ipv4first", |
| 573 | + }, |
| 574 | + }); |
| 575 | + |
| 576 | + await resolved("https://api.telegram.org/botx/sendMessage"); |
| 577 | + await resolved("https://api.telegram.org/botx/sendChatAction"); |
| 578 | + |
| 579 | + expect(undiciFetch).toHaveBeenCalledTimes(4); |
| 580 | + |
| 581 | + const secondDispatcher = getDispatcherFromUndiciCall(2); |
| 582 | + const thirdDispatcher = getDispatcherFromUndiciCall(3); |
| 583 | + const fourthDispatcher = getDispatcherFromUndiciCall(4); |
| 584 | + |
| 585 | + expect(secondDispatcher).not.toBe(thirdDispatcher); |
| 586 | + expect(thirdDispatcher).toBe(fourthDispatcher); |
| 587 | + expectPinnedFallbackIpDispatcher(3); |
| 588 | + }); |
| 589 | + |
536 | 590 | it("preserves caller-provided dispatcher across fallback retry", async () => { |
537 | 591 | const fetchError = buildFetchFallbackError("EHOSTUNREACH"); |
538 | 592 | undiciFetch.mockRejectedValueOnce(fetchError).mockResolvedValueOnce({ ok: true } as Response); |
|
0 commit comments