diff --git a/apps/meteor/tests/unit/server/livechat/lib/requestPdfTranscript.spec.ts b/apps/meteor/tests/unit/server/livechat/lib/requestPdfTranscript.spec.ts new file mode 100644 index 000000000000..c0e3d03dab4f --- /dev/null +++ b/apps/meteor/tests/unit/server/livechat/lib/requestPdfTranscript.spec.ts @@ -0,0 +1,77 @@ +import { expect } from 'chai'; +import { describe, it, beforeEach, after } from 'mocha'; +import proxyquire from 'proxyquire'; +import sinon from 'sinon'; + +const setStub = sinon.stub(); +const workOnPdfStub = sinon.stub(); +const queueWorkStub = sinon.stub(); + +const { requestPdfTranscript } = proxyquire + .noCallThru() + .load('../../../../../ee/app/livechat-enterprise/server/lib/requestPdfTranscript.ts', { + '@rocket.chat/models': { + LivechatRooms: { + setTranscriptRequestedPdfById: setStub, + }, + }, + '@rocket.chat/core-services': { + OmnichannelTranscript: { + workOnPdf: workOnPdfStub, + }, + QueueWorker: { + queueWork: queueWorkStub, + }, + }, + }); + +describe('requestPdfTranscript', () => { + const currentTestModeValue = process.env.TEST_MODE; + + beforeEach(() => { + setStub.reset(); + workOnPdfStub.reset(); + queueWorkStub.reset(); + }); + + after(() => { + process.env.TEST_MODE = currentTestModeValue; + }); + + it('should throw an error if room parameter is null', async () => { + await expect(requestPdfTranscript(null, 'userId')).to.be.rejectedWith('room-not-found'); + }); + it('should throw an error if room is still open', async () => { + await expect(requestPdfTranscript({ open: true }, 'userId')).to.be.rejectedWith('room-still-open'); + }); + it('should throw an error if room doesnt have a v property', async () => { + await expect(requestPdfTranscript({}, 'userId')).to.be.rejectedWith('improper-room-state'); + }); + it('should not request a transcript if it was already requested', async () => { + await requestPdfTranscript({ v: 1, pdfTranscriptRequested: true }, 'userId'); + expect(setStub.callCount).to.equal(0); + expect(workOnPdfStub.callCount).to.equal(0); + expect(queueWorkStub.callCount).to.equal(0); + }); + it('should set pdfTranscriptRequested to true on room', async () => { + await requestPdfTranscript({ _id: 'roomId', v: {}, pdfTranscriptRequested: false }, 'userId'); + expect(setStub.calledWith('roomId')).to.be.true; + }); + it('should call workOnPdf if TEST_MODE is true', async () => { + process.env.TEST_MODE = 'true'; + await requestPdfTranscript({ _id: 'roomId', v: {} }, 'userId'); + expect(workOnPdfStub.getCall(0).calledWithExactly({ details: { rid: 'roomId', userId: 'userId', from: 'omnichannel-transcript' } })).to + .be.true; + expect(queueWorkStub.calledOnce).to.be.false; + }); + it('should queue work if TEST_MODE is not set', async () => { + delete process.env.TEST_MODE; + await requestPdfTranscript({ _id: 'roomId', v: {} }, 'userId'); + expect(workOnPdfStub.calledOnce).to.be.false; + expect( + queueWorkStub.getCall(0).calledWithExactly('work', 'omnichannel-transcript.workOnPdf', { + details: { rid: 'roomId', userId: 'userId', from: 'omnichannel-transcript' }, + }), + ).to.be.true; + }); +}); diff --git a/ee/packages/omnichannel-services/package.json b/ee/packages/omnichannel-services/package.json index 15b57e56881a..31a6578463f0 100644 --- a/ee/packages/omnichannel-services/package.json +++ b/ee/packages/omnichannel-services/package.json @@ -43,5 +43,8 @@ "typings": "./dist/index.d.ts", "files": [ "/dist" - ] + ], + "volta": { + "extends": "../../../package.json" + } } diff --git a/ee/packages/omnichannel-services/src/OmnichannelTranscript.spec.ts b/ee/packages/omnichannel-services/src/OmnichannelTranscript.spec.ts index 569e0b4eefe5..4620fea206d6 100644 --- a/ee/packages/omnichannel-services/src/OmnichannelTranscript.spec.ts +++ b/ee/packages/omnichannel-services/src/OmnichannelTranscript.spec.ts @@ -24,9 +24,6 @@ jest.mock('@rocket.chat/core-services', () => ({ Room: { createDirectMessage: jest.fn().mockResolvedValue({ rid: 'roomId' }), }, - QueueWorker: { - queueWork: jest.fn(), - }, Translation: { translate: jest.fn().mockResolvedValue('translated message'), translateToServerLanguage: jest.fn().mockResolvedValue('translated server message'), @@ -40,7 +37,6 @@ jest.mock('@rocket.chat/core-services', () => ({ jest.mock('@rocket.chat/models', () => ({ LivechatRooms: { findOneById: jest.fn().mockResolvedValue({}), - setTranscriptRequestedPdfById: jest.fn(), unsetTranscriptRequestedPdfById: jest.fn(), setPdfTranscriptFileIdById: jest.fn(), },