UNPKG

@omnichat/middy-lock-redis

Version:
75 lines 2.71 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const index_1 = require("./index"); const request = { event: { body: JSON.stringify({ chatId: 'chatId', }), lock: { expiration: new Date().getTime() + 1000000000, unlock: () => new Promise(() => { }), }, }, }; const requestSqs = { event: { Records: [ { messageId: 'messageId', body: JSON.stringify({ chatId: 'chatId', }), lock: { expiration: new Date().getTime() + 1000000000, unlock: () => new Promise((resolve) => { resolve(); }), }, }, ], RecordsLock: undefined, }, }; const mockUnlock = jest.fn(() => true); const mockLock = jest.fn(() => ({ unlock: mockUnlock, })); jest.mock('redlock', () => jest.fn().mockImplementation(() => ({ lock: mockLock, }))); const client = { eval: () => { }, }; describe('MiddlewareLock test', () => { it('invoke before middleware by http request', async () => { const response = (0, index_1.MiddlewareLock)('MESSAGE', 'chatId', client); await response.before(request); expect(mockLock).toHaveBeenCalledWith('LOCKS:MESSAGE:chatId', 7500); }); it('invoke before middleware by SQS', async () => { const response = (0, index_1.MiddlewareLock)('MESSAGE', 'chatId', client, 10); await response.before(requestSqs); expect(mockLock).toHaveBeenCalledWith('LOCKS:MESSAGE:chatId', 10); }); it('invoke after middleware by http request', async () => { const response = (0, index_1.MiddlewareLock)('MESSAGE', 'chatId', client); const afterResponse = await response.after(request); expect(afterResponse).toBeUndefined(); }); it('invoke after middleware by SQS', async () => { const response = (0, index_1.MiddlewareLock)('MESSAGE', 'chatId', client); const afterResponse = await response.after(requestSqs); expect(afterResponse).toBeUndefined(); }); it('invoke after middleware and return SQS queue', async () => { const response = (0, index_1.MiddlewareLock)('MESSAGE', 'chatId', client); requestSqs.event.RecordsLock = Object.assign([], requestSqs.event.Records); requestSqs.event.Records = []; const afterResponse = await response.after(requestSqs); expect(afterResponse).toEqual({ batchItemFailures: [{ itemIdentifier: 'messageId' }], }); }); }); //# sourceMappingURL=index.spec.js.map