import { GarnetService } from '../global/garnet/garnet.service'; import { RiotRateLimiterService } from './riot-rate-limiter.service'; function makeService(sendCommand = jest.fn().mockResolvedValue(0)) { const garnet = { getString: jest.fn().mockResolvedValue(null), raw: jest.fn().mockReturnValue({ sendCommand }), setString: jest.fn().mockResolvedValue(undefined), } as unknown as jest.Mocked; return { service: new RiotRateLimiterService(garnet), garnet, sendCommand, }; } function observedState(params: { count120: number; limit120?: number; observedAt: number; windowStartAt: number; }) { return JSON.stringify({ count120: params.count120, limit120: params.limit120 ?? 100, observedAt: params.observedAt, windowStartAt: params.windowStartAt, }); } describe('RiotRateLimiterService', () => { afterEach(() => { jest.restoreAllMocks(); }); it('uses global app rate keys and passes cost to the Lua script', async () => { jest.spyOn(Date, 'now').mockReturnValue(1_000); const { service, sendCommand } = makeService(); await service.acquire('asia', 3); 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:rl:app:1000'); expect(command[4]).toBe('riot:rl:app:120000'); expect(command[5]).toBe('1000'); expect(command[7]).toBe('3'); }); it('records the 120 second app rate observation from Riot headers', async () => { jest.spyOn(Date, 'now').mockReturnValue(1_000_000); const { service, garnet } = makeService(); await service.recordAppRateLimitObservation('47:120,3:1', '100:120,20:1'); expect(garnet.setString).toHaveBeenCalledTimes(1); const [key, raw, ttl] = garnet.setString.mock.calls[0]; expect(key).toBe('riot:app:rl:observed'); expect(ttl).toBe(300); expect(JSON.parse(raw)).toEqual({ count120: 47, limit120: 100, observedAt: 1_000_000, windowStartAt: 1_000_000, }); }); it('resets the inferred window when Riot count decreases or observation is stale', async () => { const { service, garnet } = makeService(); jest.spyOn(Date, 'now').mockReturnValue(1_060_000); garnet.getString.mockResolvedValueOnce( observedState({ count120: 80, observedAt: 1_050_000, windowStartAt: 1_000_000, }), ); await service.recordAppRateLimitObservation('2:120', null); expect(JSON.parse(garnet.setString.mock.calls[0][1])).toMatchObject({ count120: 2, windowStartAt: 1_060_000, }); jest.spyOn(Date, 'now').mockReturnValue(1_240_000); garnet.getString.mockResolvedValueOnce( observedState({ count120: 20, observedAt: 1_120_000, windowStartAt: 1_000_000, }), ); await service.recordAppRateLimitObservation('25:120', null); expect(JSON.parse(garnet.setString.mock.calls[1][1])).toMatchObject({ count120: 25, windowStartAt: 1_240_000, }); }); it('opens the refresh gate when there is no active observation', async () => { const { service, garnet } = makeService(); await expect(service.isRefreshWindowOpen(0.5)).resolves.toBe(true); garnet.getString.mockResolvedValueOnce( observedState({ count120: 90, observedAt: 1_000_000, windowStartAt: 900_000, }), ); jest.spyOn(Date, 'now').mockReturnValue(1_120_000); await expect(service.isRefreshWindowOpen(0.5)).resolves.toBe(true); }); it.each([ { ratio: 0.5, count120: 49, expected: true }, { ratio: 0.5, count120: 50, expected: false }, { ratio: 0.8, count120: 79, expected: true }, { ratio: 0.8, count120: 80, expected: false }, ])( 'checks tail headroom for refresh gate ratio=$ratio count=$count120', async ({ ratio, count120, expected }) => { jest.spyOn(Date, 'now').mockReturnValue(1_070_000); const { service, garnet } = makeService(); garnet.getString.mockResolvedValue( observedState({ count120, observedAt: 1_069_000, windowStartAt: 1_000_000, }), ); await expect(service.isRefreshWindowOpen(ratio)).resolves.toBe(expected); }, ); it('keeps the refresh gate closed before the 120 second window tail', async () => { jest.spyOn(Date, 'now').mockReturnValue(1_050_000); const { service, garnet } = makeService(); garnet.getString.mockResolvedValue( observedState({ count120: 10, observedAt: 1_049_000, windowStartAt: 1_000_000, }), ); await expect(service.isRefreshWindowOpen(0.8)).resolves.toBe(false); }); });