test(telegram): cancel real timer in media group tests to prevent double invocation

This commit is contained in:
Marcus Castro
2026-02-19 10:06:14 -03:00
committed by Ayaan Zaidi
parent 81fe06c058
commit 1d145ff71d

View File

@@ -1760,10 +1760,19 @@ describe("createTelegramBot", () => {
await Promise.all([first, second]);
expect(replySpy).not.toHaveBeenCalled();
const flushTimerCall = [...setTimeoutSpy.mock.calls]
.toReversed()
.find((call) => call[1] === TELEGRAM_TEST_TIMINGS.mediaGroupFlushMs);
const flushTimer = flushTimerCall?.[0] as (() => unknown) | undefined;
const flushTimerCallIndex = setTimeoutSpy.mock.calls.findLastIndex(
(call) => call[1] === TELEGRAM_TEST_TIMINGS.mediaGroupFlushMs,
);
const flushTimer =
flushTimerCallIndex >= 0
? (setTimeoutSpy.mock.calls[flushTimerCallIndex]?.[0] as (() => unknown) | undefined)
: undefined;
// Cancel the real timer so it cannot fire a second time after we manually invoke it.
if (flushTimerCallIndex >= 0) {
clearTimeout(
setTimeoutSpy.mock.results[flushTimerCallIndex]?.value as ReturnType<typeof setTimeout>,
);
}
expect(flushTimer).toBeTypeOf("function");
await flushTimer?.();
@@ -1939,10 +1948,19 @@ describe("createTelegramBot", () => {
await Promise.all([first, second]);
expect(replySpy).not.toHaveBeenCalled();
const flushTimerCall = [...setTimeoutSpy.mock.calls]
.toReversed()
.find((call) => call[1] === TELEGRAM_TEST_TIMINGS.mediaGroupFlushMs);
const flushTimer = flushTimerCall?.[0] as (() => unknown) | undefined;
const flushTimerCallIndex = setTimeoutSpy.mock.calls.findLastIndex(
(call) => call[1] === TELEGRAM_TEST_TIMINGS.mediaGroupFlushMs,
);
const flushTimer =
flushTimerCallIndex >= 0
? (setTimeoutSpy.mock.calls[flushTimerCallIndex]?.[0] as (() => unknown) | undefined)
: undefined;
// Cancel the real timer so it cannot fire a second time after we manually invoke it.
if (flushTimerCallIndex >= 0) {
clearTimeout(
setTimeoutSpy.mock.results[flushTimerCallIndex]?.value as ReturnType<typeof setTimeout>,
);
}
expect(flushTimer).toBeTypeOf("function");
await flushTimer?.();