perf(test): replace telegram media flush sleeps

This commit is contained in:
Peter Steinberger
2026-02-18 18:10:32 +00:00
parent e47df9ed76
commit 84841aebe5

View File

@@ -13,10 +13,6 @@ const TELEGRAM_TEST_TIMINGS = {
textFragmentGapMs: 30,
} as const;
const sleep = async (ms: number) => {
await new Promise<void>((resolve) => setTimeout(resolve, ms));
};
async function createBotHandler(): Promise<{
handler: (ctx: Record<string, unknown>) => Promise<void>;
replySpy: ReturnType<typeof vi.fn>;
@@ -257,10 +253,14 @@ describe("telegram media groups", () => {
await second;
expect(replySpy).not.toHaveBeenCalled();
await sleep(MEDIA_GROUP_FLUSH_MS);
await vi.waitFor(
() => {
expect(replySpy).toHaveBeenCalledTimes(1);
},
{ timeout: MEDIA_GROUP_FLUSH_MS * 2, interval: 10 },
);
expect(runtimeError).not.toHaveBeenCalled();
expect(replySpy).toHaveBeenCalledTimes(1);
const payload = replySpy.mock.calls[0][0];
expect(payload.Body).toContain("Here are my photos");
expect(payload.MediaPaths).toHaveLength(2);
@@ -305,9 +305,12 @@ describe("telegram media groups", () => {
await Promise.all([first, second]);
expect(replySpy).not.toHaveBeenCalled();
await sleep(MEDIA_GROUP_FLUSH_MS);
expect(replySpy).toHaveBeenCalledTimes(2);
await vi.waitFor(
() => {
expect(replySpy).toHaveBeenCalledTimes(2);
},
{ timeout: MEDIA_GROUP_FLUSH_MS * 2, interval: 10 },
);
fetchSpy.mockRestore();
},
@@ -558,9 +561,13 @@ describe("telegram text fragments", () => {
});
expect(replySpy).not.toHaveBeenCalled();
await sleep(TEXT_FRAGMENT_FLUSH_MS);
await vi.waitFor(
() => {
expect(replySpy).toHaveBeenCalledTimes(1);
},
{ timeout: TEXT_FRAGMENT_FLUSH_MS * 2, interval: 10 },
);
expect(replySpy).toHaveBeenCalledTimes(1);
const payload = replySpy.mock.calls[0][0] as { RawBody?: string; Body?: string };
expect(payload.RawBody).toContain(part1.slice(0, 32));
expect(payload.RawBody).toContain(part2.slice(0, 32));