test: stabilize telegram media timing tests

This commit is contained in:
Peter Steinberger
2026-02-13 02:13:09 +01:00
parent 3421b2ec1e
commit 83662ba5bb

View File

@@ -15,6 +15,10 @@ const resolvePinnedHostname = ssrf.resolvePinnedHostname;
const lookupMock = vi.fn();
let resolvePinnedHostnameSpy: ReturnType<typeof vi.spyOn> = null;
const sleep = async (ms: number) => {
await new Promise<void>((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 };