|
| 1 | +import * as chai from 'chai' |
| 2 | +import * as sinon from 'sinon' |
| 3 | +import sinonChai from 'sinon-chai' |
| 4 | + |
| 5 | +chai.use(sinonChai) |
| 6 | + |
| 7 | +const { expect } = chai |
| 8 | + |
| 9 | +import { IEventRepository } from '../../../src/@types/repositories' |
| 10 | +import { IPaymentsService } from '../../../src/@types/services' |
| 11 | +import { MaintenanceWorker } from '../../../src/app/maintenance-worker' |
| 12 | +import { Settings } from '../../../src/@types/settings' |
| 13 | + |
| 14 | +describe('MaintenanceWorker', () => { |
| 15 | + let worker: MaintenanceWorker |
| 16 | + let sandbox: sinon.SinonSandbox |
| 17 | + let paymentsService: sinon.SinonStubbedInstance<IPaymentsService> |
| 18 | + let eventRepository: sinon.SinonStubbedInstance<IEventRepository> |
| 19 | + let settings: sinon.SinonStub |
| 20 | + let processMock: any |
| 21 | + |
| 22 | + beforeEach(() => { |
| 23 | + sandbox = sinon.createSandbox() |
| 24 | + paymentsService = { |
| 25 | + getPendingInvoices: sandbox.stub(), |
| 26 | + getInvoiceFromPaymentsProcessor: sandbox.stub(), |
| 27 | + updateInvoiceStatus: sandbox.stub(), |
| 28 | + confirmInvoice: sandbox.stub(), |
| 29 | + sendInvoiceUpdateNotification: sandbox.stub(), |
| 30 | + } as any |
| 31 | + eventRepository = { |
| 32 | + deleteExpiredAndRetained: sandbox.stub(), |
| 33 | + } as any |
| 34 | + settings = sandbox.stub() |
| 35 | + processMock = { |
| 36 | + on: sandbox.stub().returnsThis(), |
| 37 | + } |
| 38 | + |
| 39 | + worker = new MaintenanceWorker( |
| 40 | + processMock as any, |
| 41 | + paymentsService as any, |
| 42 | + eventRepository as any, |
| 43 | + settings as any, |
| 44 | + ) |
| 45 | + }) |
| 46 | + |
| 47 | + afterEach(() => { |
| 48 | + sandbox.restore() |
| 49 | + }) |
| 50 | + |
| 51 | + describe('onSchedule', () => { |
| 52 | + it('purges events and processes invoices', async () => { |
| 53 | + const currentSettings: Settings = { |
| 54 | + info: {} as any, |
| 55 | + network: {} as any, |
| 56 | + payments: { |
| 57 | + enabled: true, |
| 58 | + } as any, |
| 59 | + limits: { |
| 60 | + event: { |
| 61 | + retentionDays: 30, |
| 62 | + }, |
| 63 | + } as any, |
| 64 | + } |
| 65 | + settings.returns(currentSettings) |
| 66 | + eventRepository.deleteExpiredAndRetained.resolves(10) |
| 67 | + paymentsService.getPendingInvoices.resolves([]) |
| 68 | + |
| 69 | + await (worker as any).onSchedule() |
| 70 | + |
| 71 | + expect(eventRepository.deleteExpiredAndRetained).to.have.been.calledOnceWithExactly(30) |
| 72 | + expect(paymentsService.getPendingInvoices).to.have.been.calledOnce |
| 73 | + }) |
| 74 | + |
| 75 | + it('purges events even if payments are disabled', async () => { |
| 76 | + const currentSettings: Settings = { |
| 77 | + info: {} as any, |
| 78 | + network: {} as any, |
| 79 | + payments: { |
| 80 | + enabled: false, |
| 81 | + } as any, |
| 82 | + limits: { |
| 83 | + event: { |
| 84 | + retentionDays: 30, |
| 85 | + }, |
| 86 | + } as any, |
| 87 | + } |
| 88 | + settings.returns(currentSettings) |
| 89 | + eventRepository.deleteExpiredAndRetained.resolves(10) |
| 90 | + |
| 91 | + await (worker as any).onSchedule() |
| 92 | + |
| 93 | + expect(eventRepository.deleteExpiredAndRetained).to.have.been.calledOnceWithExactly(30) |
| 94 | + expect(paymentsService.getPendingInvoices).not.to.have.been.called |
| 95 | + }) |
| 96 | + |
| 97 | + it('does not purge when retentionDays is not configured', async () => { |
| 98 | + const currentSettings: Settings = { |
| 99 | + info: {} as any, |
| 100 | + network: {} as any, |
| 101 | + payments: { |
| 102 | + enabled: false, |
| 103 | + } as any, |
| 104 | + } |
| 105 | + settings.returns(currentSettings) |
| 106 | + |
| 107 | + await (worker as any).onSchedule() |
| 108 | + |
| 109 | + expect(eventRepository.deleteExpiredAndRetained).not.to.have.been.called |
| 110 | + }) |
| 111 | + |
| 112 | + it('handles error during purge when retentionDays is configured', async () => { |
| 113 | + const currentSettings: Settings = { |
| 114 | + info: {} as any, |
| 115 | + network: {} as any, |
| 116 | + payments: { |
| 117 | + enabled: false, |
| 118 | + } as any, |
| 119 | + limits: { |
| 120 | + event: { |
| 121 | + retentionDays: 30, |
| 122 | + }, |
| 123 | + } as any, |
| 124 | + } |
| 125 | + settings.returns(currentSettings) |
| 126 | + eventRepository.deleteExpiredAndRetained.rejects(new Error('DB Error')) |
| 127 | + |
| 128 | + // Should not throw |
| 129 | + await (worker as any).onSchedule() |
| 130 | + |
| 131 | + expect(eventRepository.deleteExpiredAndRetained).to.have.been.calledOnceWithExactly(30) |
| 132 | + }) |
| 133 | + }) |
| 134 | +}) |
0 commit comments