Files
RedditBot/tests/main-loop.test.ts
T

46 lines
1.5 KiB
TypeScript

import { describe, expect, it, vi } from 'vitest';
import { currentHourlySendCount, startNonOverlappingSchedule } from '../src/main-loop.js';
describe('startNonOverlappingSchedule', () => {
it('does not begin a second response check until the prior check settles', async () => {
vi.useFakeTimers();
let finishFirst: (() => void) | undefined;
const check = vi
.fn<() => Promise<void>>()
.mockImplementationOnce(() => new Promise<void>((resolve) => { finishFirst = resolve; }))
.mockResolvedValue(undefined);
const schedule = startNonOverlappingSchedule(check, 1_000);
expect(check).toHaveBeenCalledTimes(1);
await vi.advanceTimersByTimeAsync(10_000);
expect(check).toHaveBeenCalledTimes(1);
finishFirst?.();
await Promise.resolve();
await vi.advanceTimersByTimeAsync(1_000);
expect(check).toHaveBeenCalledTimes(2);
schedule.stop();
vi.useRealTimers();
});
it('does not schedule another check after stop', async () => {
vi.useFakeTimers();
const check = vi.fn<() => Promise<void>>().mockResolvedValue(undefined);
const schedule = startNonOverlappingSchedule(check, 1_000);
await Promise.resolve();
schedule.stop();
await vi.advanceTimersByTimeAsync(10_000);
expect(check).toHaveBeenCalledTimes(1);
vi.useRealTimers();
});
});
describe('currentHourlySendCount', () => {
it('uses the post-send limiter count without adding an extra message', () => {
expect(currentHourlySendCount({ getStats: () => ({ messagesThisHour: 1 }) })).toBe(1);
});
});