import { GarnetService } from '../global/garnet/garnet.service'; import { RiotRateLimiterService } from './riot-rate-limiter.service'; function makeService(sendCommand = jest.fn().mockResolvedValue(0)) { const garnet = { raw: jest.fn().mockReturnValue({ sendCommand }), } as unknown as GarnetService; return { service: new RiotRateLimiterService(garnet), sendCommand, }; } describe('RiotRateLimiterService background limiter', () => { afterEach(() => { jest.restoreAllMocks(); }); it('uses the background key namespace and passes cost to the Lua script', async () => { jest.spyOn(Date, 'now').mockReturnValue(1_000); const { service, sendCommand } = makeService(); await service.acquireBackground(8); expect(sendCommand).toHaveBeenCalledTimes(1); const command = sendCommand.mock.calls[0][0] as string[]; expect(command[0]).toBe('EVAL'); expect(command[2]).toBe('2'); expect(command[3]).toBe('riot:bg:rl:1000'); expect(command[4]).toBe('riot:bg:rl:120000'); expect(command[5]).toBe('1000'); expect(command[7]).toBe('8'); }); it('reads cleaned background usage for both windows', async () => { jest.spyOn(Date, 'now').mockReturnValue(200_000); const sendCommand = jest .fn() .mockImplementation(async (command: string[]) => { if (command[0] === 'ZREMRANGEBYSCORE') return 0; if (command[1] === 'riot:bg:rl:1000') return 3; if (command[1] === 'riot:bg:rl:120000') return 24; return 0; }); const { service } = makeService(sendCommand); await expect(service.getBackgroundUsage()).resolves.toEqual({ used1s: 3, used2m: 24, }); expect(sendCommand).toHaveBeenCalledWith([ 'ZREMRANGEBYSCORE', 'riot:bg:rl:1000', '-inf', '199000', ]); expect(sendCommand).toHaveBeenCalledWith([ 'ZREMRANGEBYSCORE', 'riot:bg:rl:120000', '-inf', '80000', ]); }); });