|
| 1 | +import chai from 'chai' |
| 2 | +import EventEmitter from 'events' |
| 3 | +import sinon from 'sinon' |
| 4 | +import sinonChai from 'sinon-chai' |
| 5 | + |
| 6 | +import { IWebSocketAdapter } from '../../../src/@types/adapters' |
| 7 | +import { MessageType } from '../../../src/@types/messages' |
| 8 | +import { IEventRepository } from '../../../src/@types/repositories' |
| 9 | +import { Settings } from '../../../src/@types/settings' |
| 10 | +import { WebSocketAdapterEvent } from '../../../src/constants/adapter' |
| 11 | +import { CountMessageHandler } from '../../../src/handlers/count-message-handler' |
| 12 | + |
| 13 | +chai.use(sinonChai) |
| 14 | +const { expect } = chai |
| 15 | + |
| 16 | +describe('CountMessageHandler', () => { |
| 17 | + let webSocket: IWebSocketAdapter |
| 18 | + let handler: CountMessageHandler |
| 19 | + let eventRepository: IEventRepository |
| 20 | + let onMessageStub: sinon.SinonStub |
| 21 | + let sandbox: sinon.SinonSandbox |
| 22 | + |
| 23 | + beforeEach(() => { |
| 24 | + sandbox = sinon.createSandbox() |
| 25 | + eventRepository = { |
| 26 | + countByFilters: sandbox.stub().resolves(7), |
| 27 | + } as any |
| 28 | + |
| 29 | + webSocket = new EventEmitter() as any |
| 30 | + onMessageStub = sandbox.stub() |
| 31 | + webSocket.on(WebSocketAdapterEvent.Message, onMessageStub) |
| 32 | + |
| 33 | + handler = new CountMessageHandler(webSocket, eventRepository, () => ({ |
| 34 | + limits: { |
| 35 | + client: { |
| 36 | + subscription: { |
| 37 | + maxFilters: 10, |
| 38 | + maxSubscriptionIdLength: 256, |
| 39 | + }, |
| 40 | + }, |
| 41 | + }, |
| 42 | + }) as Settings) |
| 43 | + }) |
| 44 | + |
| 45 | + afterEach(() => { |
| 46 | + webSocket.removeAllListeners() |
| 47 | + sandbox.restore() |
| 48 | + }) |
| 49 | + |
| 50 | + it('emits COUNT message with count on success', async () => { |
| 51 | + const message = [MessageType.COUNT, 'q1', {}] as any |
| 52 | + |
| 53 | + await handler.handleMessage(message) |
| 54 | + |
| 55 | + expect(eventRepository.countByFilters).to.have.been.calledOnceWithExactly([{}]) |
| 56 | + expect(onMessageStub).to.have.been.calledOnceWithExactly([MessageType.COUNT, 'q1', { count: 7 }]) |
| 57 | + }) |
| 58 | + |
| 59 | + it('emits CLOSED message when request is rejected', async () => { |
| 60 | + handler = new CountMessageHandler(webSocket, eventRepository, () => ({ |
| 61 | + limits: { |
| 62 | + client: { |
| 63 | + subscription: { |
| 64 | + maxFilters: 1, |
| 65 | + maxSubscriptionIdLength: 256, |
| 66 | + }, |
| 67 | + }, |
| 68 | + }, |
| 69 | + }) as Settings) |
| 70 | + |
| 71 | + const message = [MessageType.COUNT, 'q1', { kinds: [1] }, { kinds: [2] }] as any |
| 72 | + |
| 73 | + await handler.handleMessage(message) |
| 74 | + |
| 75 | + expect(eventRepository.countByFilters).to.not.have.been.called |
| 76 | + expect(onMessageStub).to.have.been.calledOnce |
| 77 | + expect(onMessageStub.firstCall.args[0][0]).to.equal(MessageType.CLOSED) |
| 78 | + expect(onMessageStub.firstCall.args[0][1]).to.equal('q1') |
| 79 | + }) |
| 80 | + |
| 81 | + it('emits CLOSED message when repository fails', async () => { |
| 82 | + (eventRepository.countByFilters as sinon.SinonStub).rejects(new Error('boom')) |
| 83 | + const message = [MessageType.COUNT, 'q1', {}] as any |
| 84 | + |
| 85 | + await handler.handleMessage(message) |
| 86 | + |
| 87 | + expect(onMessageStub).to.have.been.calledOnceWithExactly([ |
| 88 | + MessageType.CLOSED, |
| 89 | + 'q1', |
| 90 | + 'error: unable to count events', |
| 91 | + ]) |
| 92 | + }) |
| 93 | +}) |
0 commit comments