|
| 1 | +import chai from 'chai' |
| 2 | +import chaiAsPromised from 'chai-as-promised' |
| 3 | +import Sinon from 'sinon' |
| 4 | + |
| 5 | +chai.use(chaiAsPromised) |
| 6 | + |
| 7 | +const { expect } = chai |
| 8 | + |
| 9 | +import { DvmJobRequestEventStrategy } from '../../../../src/handlers/event-strategies/dvm-job-request-event-strategy' |
| 10 | +import { Event } from '../../../../src/@types/event' |
| 11 | +import { IDvmJobRepository, IEventRepository } from '../../../../src/@types/repositories' |
| 12 | +import { IEventStrategy } from '../../../../src/@types/message-handlers' |
| 13 | +import { IWebSocketAdapter } from '../../../../src/@types/adapters' |
| 14 | +import { MessageType } from '../../../../src/@types/messages' |
| 15 | +import { WebSocketAdapterEvent } from '../../../../src/constants/adapter' |
| 16 | + |
| 17 | +describe('DvmJobRequestEventStrategy', () => { |
| 18 | + const event: Event = { |
| 19 | + id: 'event-id', |
| 20 | + pubkey: 'requester-pubkey', |
| 21 | + kind: 5000, |
| 22 | + } as any |
| 23 | + |
| 24 | + let webSocket: IWebSocketAdapter |
| 25 | + let eventRepository: IEventRepository |
| 26 | + let dvmJobRepository: IDvmJobRepository |
| 27 | + |
| 28 | + let webSocketEmitStub: Sinon.SinonStub |
| 29 | + let eventRepositoryCreateStub: Sinon.SinonStub |
| 30 | + let dvmJobRepositoryCreateStub: Sinon.SinonStub |
| 31 | + |
| 32 | + let strategy: IEventStrategy<Event, Promise<void>> |
| 33 | + |
| 34 | + let sandbox: Sinon.SinonSandbox |
| 35 | + |
| 36 | + beforeEach(() => { |
| 37 | + sandbox = Sinon.createSandbox() |
| 38 | + |
| 39 | + webSocketEmitStub = sandbox.stub() |
| 40 | + webSocket = { |
| 41 | + emit: webSocketEmitStub, |
| 42 | + } as any |
| 43 | + |
| 44 | + eventRepositoryCreateStub = sandbox.stub() |
| 45 | + eventRepository = { |
| 46 | + create: eventRepositoryCreateStub, |
| 47 | + } as any |
| 48 | + |
| 49 | + dvmJobRepositoryCreateStub = sandbox.stub() |
| 50 | + dvmJobRepository = { |
| 51 | + create: dvmJobRepositoryCreateStub, |
| 52 | + } as any |
| 53 | + |
| 54 | + strategy = new DvmJobRequestEventStrategy(webSocket, eventRepository, dvmJobRepository) |
| 55 | + }) |
| 56 | + |
| 57 | + afterEach(() => { |
| 58 | + sandbox.restore() |
| 59 | + }) |
| 60 | + |
| 61 | + describe('execute', () => { |
| 62 | + it('creates the event', async () => { |
| 63 | + eventRepositoryCreateStub.resolves(1) |
| 64 | + dvmJobRepositoryCreateStub.resolves({}) |
| 65 | + |
| 66 | + await strategy.execute(event) |
| 67 | + |
| 68 | + expect(eventRepositoryCreateStub).to.have.been.calledOnceWithExactly(event) |
| 69 | + }) |
| 70 | + |
| 71 | + it('records a dvm job when the event is newly created', async () => { |
| 72 | + eventRepositoryCreateStub.resolves(1) |
| 73 | + dvmJobRepositoryCreateStub.resolves({}) |
| 74 | + |
| 75 | + await strategy.execute(event) |
| 76 | + |
| 77 | + expect(dvmJobRepositoryCreateStub).to.have.been.calledOnceWithExactly('event-id', 'requester-pubkey', 5000) |
| 78 | + }) |
| 79 | + |
| 80 | + it('broadcasts the event when newly created', async () => { |
| 81 | + eventRepositoryCreateStub.resolves(1) |
| 82 | + dvmJobRepositoryCreateStub.resolves({}) |
| 83 | + |
| 84 | + await strategy.execute(event) |
| 85 | + |
| 86 | + expect(webSocketEmitStub).to.have.been.calledWithExactly(WebSocketAdapterEvent.Message, [ |
| 87 | + MessageType.OK, |
| 88 | + 'event-id', |
| 89 | + true, |
| 90 | + '', |
| 91 | + ]) |
| 92 | + expect(webSocketEmitStub).to.have.been.calledWithExactly(WebSocketAdapterEvent.Broadcast, event) |
| 93 | + }) |
| 94 | + |
| 95 | + it('does not broadcast or record a job when the event is a duplicate', async () => { |
| 96 | + eventRepositoryCreateStub.resolves(0) |
| 97 | + |
| 98 | + await strategy.execute(event) |
| 99 | + |
| 100 | + expect(webSocketEmitStub).to.have.been.calledOnceWithExactly(WebSocketAdapterEvent.Message, [ |
| 101 | + MessageType.OK, |
| 102 | + 'event-id', |
| 103 | + true, |
| 104 | + 'duplicate:', |
| 105 | + ]) |
| 106 | + expect(dvmJobRepositoryCreateStub).not.to.have.been.called |
| 107 | + }) |
| 108 | + |
| 109 | + it('does not reject the event when job recording fails', async () => { |
| 110 | + eventRepositoryCreateStub.resolves(1) |
| 111 | + dvmJobRepositoryCreateStub.rejects(new Error('db unavailable')) |
| 112 | + |
| 113 | + await expect(strategy.execute(event)).to.eventually.be.fulfilled |
| 114 | + |
| 115 | + expect(webSocketEmitStub).to.have.been.calledWithExactly(WebSocketAdapterEvent.Message, [ |
| 116 | + MessageType.OK, |
| 117 | + 'event-id', |
| 118 | + true, |
| 119 | + '', |
| 120 | + ]) |
| 121 | + }) |
| 122 | + |
| 123 | + it('rejects if unable to create the event', async () => { |
| 124 | + const error = new Error('event creation failed') |
| 125 | + eventRepositoryCreateStub.rejects(error) |
| 126 | + |
| 127 | + await expect(strategy.execute(event)).to.eventually.be.rejectedWith(error) |
| 128 | + |
| 129 | + expect(dvmJobRepositoryCreateStub).not.to.have.been.called |
| 130 | + }) |
| 131 | + }) |
| 132 | +}) |
0 commit comments