mancala/apps/backend/tests/game/GameClockScheduler.test.ts
Halit Aksoy 3d4f5d7d59 feat(backend): end games on time with a scheduled service
GameClockScheduler checks every live game once a second and ends those whose running clock has reached zero, whether or not anyone is connected. The clock replaces the offline turn timeout and the inactivity rule, so both are removed with the offline times they used.
2026-10-16 01:29:22 +03:00

23 lines
875 B
TypeScript

import { GameClockScheduler } from "../../src/game/GameClockScheduler";
describe("GameClockScheduler", () => {
beforeEach(() => jest.useFakeTimers());
afterEach(() => jest.useRealTimers());
it("ends games out of time on every interval until stopped", () => {
const gameManager = { endGamesOutOfTime: jest.fn() };
const scheduler = new GameClockScheduler(gameManager, 1_000);
scheduler.start();
scheduler.start(); // starting twice does not run it twice
jest.advanceTimersByTime(999);
expect(gameManager.endGamesOutOfTime).not.toHaveBeenCalled();
jest.advanceTimersByTime(2_001);
expect(gameManager.endGamesOutOfTime).toHaveBeenCalledTimes(3);
scheduler.stop();
jest.advanceTimersByTime(10_000);
expect(gameManager.endGamesOutOfTime).toHaveBeenCalledTimes(3);
});
});