From 83662ba5bbe974da16f110fd7a0e5fc2e284a531 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Fri, 13 Feb 2026 02:13:09 +0100 Subject: [PATCH] test: stabilize telegram media timing tests --- ...s-media-file-path-no-file-download.test.ts | 22 ++++++++----------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/src/telegram/bot.media.downloads-media-file-path-no-file-download.test.ts b/src/telegram/bot.media.downloads-media-file-path-no-file-download.test.ts index 5d5a05c8ba9..6e2416c4f4b 100644 --- a/src/telegram/bot.media.downloads-media-file-path-no-file-download.test.ts +++ b/src/telegram/bot.media.downloads-media-file-path-no-file-download.test.ts @@ -15,6 +15,10 @@ const resolvePinnedHostname = ssrf.resolvePinnedHostname; const lookupMock = vi.fn(); let resolvePinnedHostnameSpy: ReturnType = null; +const sleep = async (ms: number) => { + await new Promise((resolve) => setTimeout(resolve, ms)); +}; + type ApiStub = { config: { use: (arg: unknown) => void }; sendChatAction: typeof sendChatActionSpy; @@ -285,12 +289,8 @@ describe("telegram inbound media", () => { }); describe("telegram media groups", () => { - beforeEach(() => { - vi.useFakeTimers(); - }); - afterEach(() => { - vi.useRealTimers(); + vi.clearAllTimers(); }); const MEDIA_GROUP_TEST_TIMEOUT_MS = process.platform === "win32" ? 45_000 : 20_000; @@ -359,7 +359,7 @@ describe("telegram media groups", () => { await second; expect(replySpy).not.toHaveBeenCalled(); - await vi.advanceTimersByTimeAsync(MEDIA_GROUP_FLUSH_MS); + await sleep(MEDIA_GROUP_FLUSH_MS); expect(runtimeError).not.toHaveBeenCalled(); expect(replySpy).toHaveBeenCalledTimes(1); @@ -425,7 +425,7 @@ describe("telegram media groups", () => { await Promise.all([first, second]); expect(replySpy).not.toHaveBeenCalled(); - await vi.advanceTimersByTimeAsync(MEDIA_GROUP_FLUSH_MS); + await sleep(MEDIA_GROUP_FLUSH_MS); expect(replySpy).toHaveBeenCalledTimes(2); @@ -721,12 +721,8 @@ describe("telegram stickers", () => { }); describe("telegram text fragments", () => { - beforeEach(() => { - vi.useFakeTimers(); - }); - afterEach(() => { - vi.useRealTimers(); + vi.clearAllTimers(); }); const TEXT_FRAGMENT_TEST_TIMEOUT_MS = process.platform === "win32" ? 45_000 : 20_000; @@ -774,7 +770,7 @@ describe("telegram text fragments", () => { }); expect(replySpy).not.toHaveBeenCalled(); - await vi.advanceTimersByTimeAsync(TEXT_FRAGMENT_FLUSH_MS); + await sleep(TEXT_FRAGMENT_FLUSH_MS); expect(replySpy).toHaveBeenCalledTimes(1); const payload = replySpy.mock.calls[0][0] as { RawBody?: string; Body?: string };