import { RedisService } from './redis.service'; function createRedisService(client: Record) { return new RedisService(client as never); } describe('RedisService stream pending recovery', () => { it('destroys a dedicated client when connection startup fails without closing the shared client', async () => { const error = new Error('connection failed'); const dedicated = { on: jest.fn(), connect: jest.fn().mockRejectedValue(error), isOpen: true, destroy: jest.fn(), }; const shared = { duplicate: jest.fn().mockReturnValue(dedicated), destroy: jest.fn(), }; const service = createRedisService(shared); await expect(service.createDedicatedConnection()).rejects.toBe(error); expect(dedicated.destroy).toHaveBeenCalledTimes(1); expect(shared.destroy).not.toHaveBeenCalled(); }); it('normalizes XAUTOCLAIM array replies into stream entries', async () => { const sendCommand = jest .fn() .mockResolvedValue([ '0-0', [ [ '1700000000000-0', ['job', '{"kind":"poll-tournament-result"}', 'source', 'test'], ], ], ]); const service = createRedisService({ sendCommand }); await expect( service.claimStalePendingStreamMessages({ key: 'riot:queue:high', group: 'riot-refresh-worker', consumer: 'worker-1', stalePendingMessageMinimumIdleMilliseconds: 1_800_000, count: 1, }), ).resolves.toEqual({ nextStartId: '0-0', messages: [ { id: '1700000000000-0', message: { job: '{"kind":"poll-tournament-result"}', source: 'test', }, }, ], }); expect(sendCommand).toHaveBeenCalledWith([ 'XAUTOCLAIM', 'riot:queue:high', 'riot-refresh-worker', 'worker-1', '1800000', '0-0', 'COUNT', '1', ]); }); });