|
| 1 | +// Telegram tests cover stalled diagnostic response body handling. |
| 2 | +import { afterEach, describe, expect, it, vi, type Mock } from "vitest"; |
| 3 | +import { probeTelegram, resetTelegramProbeFetcherCacheForTests } from "./probe.js"; |
| 4 | + |
| 5 | +const resolveTelegramTransport = vi.hoisted(() => vi.fn()); |
| 6 | +const makeProxyFetch = vi.hoisted(() => vi.fn()); |
| 7 | + |
| 8 | +vi.mock("./fetch.js", () => ({ |
| 9 | + resolveTelegramTransport, |
| 10 | + resolveTelegramApiBase: (apiRoot?: string) => |
| 11 | + apiRoot?.trim()?.replace(/\/+$/, "") || "https://api.telegram.org", |
| 12 | +})); |
| 13 | + |
| 14 | +vi.mock("./proxy.js", () => ({ |
| 15 | + makeProxyFetch, |
| 16 | +})); |
| 17 | + |
| 18 | +function installFetchMock(): Mock { |
| 19 | + const fetchMock = vi.fn(); |
| 20 | + resolveTelegramTransport.mockImplementation((proxyFetch?: typeof fetch) => ({ |
| 21 | + fetch: proxyFetch ?? (fetchMock as unknown as typeof fetch), |
| 22 | + sourceFetch: proxyFetch ?? (fetchMock as unknown as typeof fetch), |
| 23 | + forceFallback: vi.fn().mockReturnValue(true), |
| 24 | + close: async () => {}, |
| 25 | + })); |
| 26 | + makeProxyFetch.mockImplementation(() => fetchMock as unknown as typeof fetch); |
| 27 | + return fetchMock; |
| 28 | +} |
| 29 | + |
| 30 | +function makeJsonResponse(payload: unknown): Response { |
| 31 | + return new Response(JSON.stringify(payload), { |
| 32 | + status: 200, |
| 33 | + headers: { "content-type": "application/json" }, |
| 34 | + }); |
| 35 | +} |
| 36 | + |
| 37 | +function makeStallingJsonResponse(payload: unknown, cancel: (reason?: unknown) => void): Response { |
| 38 | + return new Response( |
| 39 | + new ReadableStream<Uint8Array>({ |
| 40 | + start(controller) { |
| 41 | + controller.enqueue(new TextEncoder().encode(JSON.stringify(payload))); |
| 42 | + }, |
| 43 | + cancel, |
| 44 | + }), |
| 45 | + { |
| 46 | + status: 200, |
| 47 | + headers: { "content-type": "application/json" }, |
| 48 | + }, |
| 49 | + ); |
| 50 | +} |
| 51 | + |
| 52 | +function makeTricklingJsonResponse(cancel: (reason?: unknown) => void): Response { |
| 53 | + const encoder = new TextEncoder(); |
| 54 | + let timer: ReturnType<typeof setTimeout> | undefined; |
| 55 | + let cancelled = false; |
| 56 | + const response = new Response( |
| 57 | + new ReadableStream<Uint8Array>({ |
| 58 | + start(controller) { |
| 59 | + let index = 0; |
| 60 | + const chunks = ["{", '"ok"', ":", "true", ","]; |
| 61 | + const enqueue = () => { |
| 62 | + if (cancelled) { |
| 63 | + return; |
| 64 | + } |
| 65 | + controller.enqueue(encoder.encode(chunks[index % chunks.length])); |
| 66 | + index += 1; |
| 67 | + timer = setTimeout(enqueue, 40); |
| 68 | + }; |
| 69 | + enqueue(); |
| 70 | + }, |
| 71 | + cancel(reason) { |
| 72 | + cancelled = true; |
| 73 | + if (timer) { |
| 74 | + clearTimeout(timer); |
| 75 | + } |
| 76 | + cancel(reason); |
| 77 | + }, |
| 78 | + }), |
| 79 | + { |
| 80 | + status: 200, |
| 81 | + headers: { "content-type": "application/json" }, |
| 82 | + }, |
| 83 | + ); |
| 84 | + return response; |
| 85 | +} |
| 86 | + |
| 87 | +describe("probeTelegram response body timeouts", () => { |
| 88 | + afterEach(() => { |
| 89 | + resetTelegramProbeFetcherCacheForTests(); |
| 90 | + resolveTelegramTransport.mockReset(); |
| 91 | + makeProxyFetch.mockReset(); |
| 92 | + vi.useRealTimers(); |
| 93 | + vi.clearAllMocks(); |
| 94 | + }); |
| 95 | + |
| 96 | + it("fails quickly when getMe returns a stalled response body", async () => { |
| 97 | + const fetchMock = installFetchMock(); |
| 98 | + const cancel = vi.fn(); |
| 99 | + fetchMock.mockResolvedValueOnce( |
| 100 | + makeStallingJsonResponse( |
| 101 | + { |
| 102 | + ok: true, |
| 103 | + result: { id: 123, is_bot: true, first_name: "Test", username: "bot" }, |
| 104 | + }, |
| 105 | + cancel, |
| 106 | + ), |
| 107 | + ); |
| 108 | + |
| 109 | + vi.useFakeTimers(); |
| 110 | + const probePromise = probeTelegram("placeholder", 50, { |
| 111 | + includeWebhookInfo: false, |
| 112 | + }); |
| 113 | + await vi.advanceTimersByTimeAsync(0); |
| 114 | + await vi.advanceTimersByTimeAsync(60); |
| 115 | + |
| 116 | + const result = await probePromise; |
| 117 | + expect(result.ok).toBe(false); |
| 118 | + expect(result.error).toBe("Telegram diagnostic response body stalled for 25ms"); |
| 119 | + expect(fetchMock).toHaveBeenCalledTimes(1); |
| 120 | + expect(cancel).toHaveBeenCalledTimes(1); |
| 121 | + expect(cancel.mock.calls[0]?.[0]).toBeInstanceOf(Error); |
| 122 | + }); |
| 123 | + |
| 124 | + it("fails on the body deadline when getMe trickles without becoming complete JSON", async () => { |
| 125 | + const fetchMock = installFetchMock(); |
| 126 | + const cancel = vi.fn(); |
| 127 | + vi.useFakeTimers(); |
| 128 | + fetchMock.mockResolvedValueOnce(makeTricklingJsonResponse(cancel)); |
| 129 | + |
| 130 | + const probePromise = probeTelegram("placeholder", 100, { |
| 131 | + includeWebhookInfo: false, |
| 132 | + }); |
| 133 | + await vi.advanceTimersByTimeAsync(0); |
| 134 | + await vi.advanceTimersByTimeAsync(120); |
| 135 | + |
| 136 | + const result = await probePromise; |
| 137 | + expect(result.ok).toBe(false); |
| 138 | + expect(result.error).toBe("Telegram diagnostic response body timed out after 100ms"); |
| 139 | + expect(fetchMock).toHaveBeenCalledTimes(1); |
| 140 | + expect(cancel).toHaveBeenCalledTimes(1); |
| 141 | + expect(cancel.mock.calls[0]?.[0]).toBeInstanceOf(Error); |
| 142 | + }); |
| 143 | + |
| 144 | + it("keeps webhook diagnostics best-effort when webhookInfo response body stalls", async () => { |
| 145 | + const fetchMock = installFetchMock(); |
| 146 | + const cancel = vi.fn(); |
| 147 | + fetchMock.mockResolvedValueOnce( |
| 148 | + makeJsonResponse({ |
| 149 | + ok: true, |
| 150 | + result: { id: 123, is_bot: true, first_name: "Test", username: "bot" }, |
| 151 | + }), |
| 152 | + ); |
| 153 | + fetchMock.mockResolvedValueOnce( |
| 154 | + makeStallingJsonResponse({ ok: true, result: { url: "https://example.test/hook" } }, cancel), |
| 155 | + ); |
| 156 | + |
| 157 | + vi.useFakeTimers(); |
| 158 | + const probePromise = probeTelegram("placeholder", 50); |
| 159 | + await vi.advanceTimersByTimeAsync(0); |
| 160 | + await vi.advanceTimersByTimeAsync(60); |
| 161 | + |
| 162 | + const result = await probePromise; |
| 163 | + expect(result.ok).toBe(true); |
| 164 | + expect(result.webhook).toBeUndefined(); |
| 165 | + expect(fetchMock).toHaveBeenCalledTimes(2); |
| 166 | + expect(cancel).toHaveBeenCalledTimes(1); |
| 167 | + }); |
| 168 | +}); |
0 commit comments