import { AuctionRedisNoscriptError, AuctionRedisService, } from './auction-redis.service'; function makeService(sendCommand: jest.Mock) { const client = { destroy: jest.fn(), quit: jest.fn().mockResolvedValue(undefined), sendCommand, }; return new AuctionRedisService(client as never); } describe('AuctionRedisService', () => { it('uses Redis TIME as the timestamp source', async () => { const service = makeService(jest.fn().mockResolvedValue(['1781700000', '123000'])); await expect(service.getCurrentRedisTimestampMS()).resolves.toBe( 1_781_700_000_123, ); }); it('loads and retries a script once when EVALSHA returns NOSCRIPT', async () => { const sendCommand = jest .fn() .mockRejectedValueOnce(new AuctionRedisNoscriptError()) .mockResolvedValueOnce('sha-from-redis') .mockResolvedValueOnce(['ok']); const service = makeService(sendCommand); await expect( service.evalShaWithReload({ keys: ['auction:state:1'], script: 'return 1', scriptSha: 'old-sha', values: ['v1'], }), ).resolves.toEqual(['ok']); expect(sendCommand.mock.calls).toEqual([ [['EVALSHA', 'old-sha', '1', 'auction:state:1', 'v1']], [['SCRIPT', 'LOAD', 'return 1']], [['EVALSHA', 'sha-from-redis', '1', 'auction:state:1', 'v1']], ]); }); it('does not reload scripts for non-NOSCRIPT errors', async () => { const error = new Error('OOM command not allowed'); const sendCommand = jest.fn().mockRejectedValue(error); const service = makeService(sendCommand); await expect( service.evalShaWithReload({ keys: [], script: 'return 1', scriptSha: 'sha', values: [], }), ).rejects.toBe(error); expect(sendCommand).toHaveBeenCalledTimes(1); }); });