|
1 |
| -import dotenv from 'dotenv'; |
2 |
| -dotenv.config({ path: '../../.env.sample' }); |
3 |
| -import { expect } from 'chai'; |
4 |
| -import sinon, { SinonStub } from 'sinon'; |
5 |
| -import client, { Channel, Connection } from 'amqplib'; |
6 |
| -import messageBroker from '../../src/events/broker'; |
7 |
| -import config from '../../src/config'; |
8 |
| - |
9 |
| -describe('MessageBroker', () => { |
10 |
| - let connectStub: SinonStub; |
11 |
| - let assertQueueStub: SinonStub; |
12 |
| - let sendToQueueStub: SinonStub; |
13 |
| - let consumeStub: SinonStub; |
14 |
| - let ackStub: SinonStub; |
15 |
| - let connection: Connection; |
16 |
| - let channel: Channel; |
17 |
| - |
18 |
| - beforeEach(() => { |
19 |
| - connection = { |
20 |
| - createChannel: sinon.stub(), |
21 |
| - close: sinon.stub(), |
22 |
| - } as unknown as Connection; |
23 |
| - |
24 |
| - channel = { |
25 |
| - assertQueue: sinon.stub(), |
26 |
| - sendToQueue: sinon.stub(), |
27 |
| - consume: sinon.stub(), |
28 |
| - ack: sinon.stub(), |
29 |
| - } as unknown as Channel; |
30 |
| - |
31 |
| - connectStub = sinon.stub(client, 'connect').resolves(connection); |
32 |
| - (connection.createChannel as SinonStub).resolves(channel); |
33 |
| - assertQueueStub = channel.assertQueue as SinonStub; |
34 |
| - sendToQueueStub = channel.sendToQueue as SinonStub; |
35 |
| - consumeStub = channel.consume as SinonStub; |
36 |
| - ackStub = channel.ack as SinonStub; |
37 |
| - |
38 |
| - messageBroker['connected'] = false; |
39 |
| - messageBroker['connection'] = undefined; |
40 |
| - messageBroker['channel'] = undefined; |
41 |
| - }); |
42 |
| - |
43 |
| - afterEach(() => { |
44 |
| - sinon.restore(); |
45 |
| - }); |
46 |
| - |
47 |
| - describe('connect', () => { |
48 |
| - it('should connect to RabbitMQ', async () => { |
49 |
| - await messageBroker.connect(); |
50 |
| - |
51 |
| - expect(connectStub).to.have.been.calledWith(config.BROKER_URL); |
52 |
| - // eslint-disable-next-line @typescript-eslint/no-unused-expressions |
53 |
| - expect(connection.createChannel).to.have.been.called; |
54 |
| - // eslint-disable-next-line @typescript-eslint/no-unused-expressions |
55 |
| - expect(messageBroker['connected']).to.be.true; |
56 |
| - }); |
57 |
| - |
58 |
| - it('should not reconnect if already connected', async () => { |
59 |
| - messageBroker['connection'] = connection; |
60 |
| - messageBroker['channel'] = channel; |
61 |
| - messageBroker['connected'] = true; |
62 |
| - |
63 |
| - await messageBroker.connect(); |
64 |
| - |
65 |
| - // eslint-disable-next-line @typescript-eslint/no-unused-expressions |
66 |
| - expect(connectStub).not.to.have.been.called; |
67 |
| - // eslint-disable-next-line @typescript-eslint/no-unused-expressions |
68 |
| - expect(connection.createChannel).not.to.have.been.called; |
69 |
| - }); |
70 |
| - |
71 |
| - it('should throw an error if connection fails', async () => { |
72 |
| - connectStub.rejects(new Error('Connection error')); |
73 |
| - |
74 |
| - try { |
75 |
| - await messageBroker.connect(); |
76 |
| - } catch (error) { |
77 |
| - if (error instanceof Error) { |
78 |
| - expect(error.message).to.equal('Connection error'); |
79 |
| - } |
80 |
| - } |
81 |
| - }); |
82 |
| - }); |
83 |
| - |
84 |
| - describe('produce', () => { |
85 |
| - it('should produce a message to the queue', async () => { |
86 |
| - await messageBroker.produce('testQueue', { test: 'message' }); |
87 |
| - |
88 |
| - // eslint-disable-next-line @typescript-eslint/no-unused-expressions |
89 |
| - expect(connectStub).to.have.been.called; |
90 |
| - expect(sendToQueueStub).to.have.been.calledWith( |
91 |
| - 'testQueue', |
92 |
| - Buffer.from(JSON.stringify({ test: 'message' })), |
93 |
| - ); |
94 |
| - }); |
95 |
| - |
96 |
| - it('should throw an error if producing message fails', async () => { |
97 |
| - sendToQueueStub.rejects(new Error('Produce error')); |
98 |
| - |
99 |
| - try { |
100 |
| - await messageBroker.produce('testQueue', { test: 'message' }); |
101 |
| - } catch (error) { |
102 |
| - if (error instanceof Error) { |
103 |
| - expect(error.message).to.equal('Produce error'); |
104 |
| - } |
105 |
| - } |
106 |
| - }); |
107 |
| - }); |
108 |
| - |
109 |
| - describe('consume', () => { |
110 |
| - it('should consume messages from the queue', async () => { |
111 |
| - const onMessage = sinon.stub(); |
112 |
| - const msg = { content: Buffer.from(JSON.stringify({ test: 'message' })) }; |
113 |
| - |
114 |
| - consumeStub.yields(msg); |
115 |
| - |
116 |
| - await messageBroker.consume('testQueue', onMessage); |
117 |
| - |
118 |
| - // eslint-disable-next-line @typescript-eslint/no-unused-expressions |
119 |
| - expect(connectStub).to.have.been.called; |
120 |
| - expect(assertQueueStub).to.have.been.calledWith('testQueue', { durable: true }); |
121 |
| - expect(consumeStub).to.have.been.calledWith('testQueue', sinon.match.func, { noAck: false }); |
122 |
| - expect(onMessage).to.have.been.calledWith({ test: 'message' }); |
123 |
| - expect(ackStub).to.have.been.calledWith(msg); |
124 |
| - }); |
125 |
| - |
126 |
| - it('should throw an error if consuming message fails', async () => { |
127 |
| - consumeStub.rejects(new Error('Consume error')); |
128 |
| - |
129 |
| - try { |
130 |
| - await messageBroker.consume('testQueue', sinon.stub()); |
131 |
| - } catch (error) { |
132 |
| - if (error instanceof Error) { |
133 |
| - expect(error.message).to.equal('Consume error'); |
134 |
| - } |
135 |
| - } |
136 |
| - }); |
137 |
| - }); |
138 |
| -}); |
| 1 | +// import dotenv from 'dotenv'; |
| 2 | +// dotenv.config({ path: '../../.env.sample' }); |
| 3 | +// import { expect } from 'chai'; |
| 4 | +// import sinon, { SinonStub } from 'sinon'; |
| 5 | +// import client, { Channel, Connection } from 'amqplib'; |
| 6 | +// import messageBroker from '../../src/events/broker'; |
| 7 | +// import config from '../../src/config'; |
| 8 | + |
| 9 | +// describe('MessageBroker', () => { |
| 10 | +// let connectStub: SinonStub; |
| 11 | +// let assertQueueStub: SinonStub; |
| 12 | +// let sendToQueueStub: SinonStub; |
| 13 | +// let consumeStub: SinonStub; |
| 14 | +// let ackStub: SinonStub; |
| 15 | +// let connection: Connection; |
| 16 | +// let channel: Channel; |
| 17 | + |
| 18 | +// beforeEach(() => { |
| 19 | +// connection = { |
| 20 | +// createChannel: sinon.stub(), |
| 21 | +// close: sinon.stub(), |
| 22 | +// } as unknown as Connection; |
| 23 | + |
| 24 | +// channel = { |
| 25 | +// assertQueue: sinon.stub(), |
| 26 | +// sendToQueue: sinon.stub(), |
| 27 | +// consume: sinon.stub(), |
| 28 | +// ack: sinon.stub(), |
| 29 | +// } as unknown as Channel; |
| 30 | + |
| 31 | +// connectStub = sinon.stub(client, 'connect').resolves(connection); |
| 32 | +// (connection.createChannel as SinonStub).resolves(channel); |
| 33 | +// assertQueueStub = channel.assertQueue as SinonStub; |
| 34 | +// sendToQueueStub = channel.sendToQueue as SinonStub; |
| 35 | +// consumeStub = channel.consume as SinonStub; |
| 36 | +// ackStub = channel.ack as SinonStub; |
| 37 | + |
| 38 | +// messageBroker['connected'] = false; |
| 39 | +// messageBroker['connection'] = undefined; |
| 40 | +// messageBroker['channel'] = undefined; |
| 41 | +// }); |
| 42 | + |
| 43 | +// afterEach(() => { |
| 44 | +// sinon.restore(); |
| 45 | +// }); |
| 46 | + |
| 47 | +// describe('connect', () => { |
| 48 | +// it('should connect to RabbitMQ', async () => { |
| 49 | +// await messageBroker.connect(); |
| 50 | + |
| 51 | +// expect(connectStub).to.have.been.calledWith(config.BROKER_URL); |
| 52 | +// // eslint-disable-next-line @typescript-eslint/no-unused-expressions |
| 53 | +// expect(connection.createChannel).to.have.been.called; |
| 54 | +// // eslint-disable-next-line @typescript-eslint/no-unused-expressions |
| 55 | +// expect(messageBroker['connected']).to.be.true; |
| 56 | +// }); |
| 57 | + |
| 58 | +// it('should not reconnect if already connected', async () => { |
| 59 | +// messageBroker['connection'] = connection; |
| 60 | +// messageBroker['channel'] = channel; |
| 61 | +// messageBroker['connected'] = true; |
| 62 | + |
| 63 | +// await messageBroker.connect(); |
| 64 | + |
| 65 | +// // eslint-disable-next-line @typescript-eslint/no-unused-expressions |
| 66 | +// expect(connectStub).not.to.have.been.called; |
| 67 | +// // eslint-disable-next-line @typescript-eslint/no-unused-expressions |
| 68 | +// expect(connection.createChannel).not.to.have.been.called; |
| 69 | +// }); |
| 70 | + |
| 71 | +// it('should throw an error if connection fails', async () => { |
| 72 | +// connectStub.rejects(new Error('Connection error')); |
| 73 | + |
| 74 | +// try { |
| 75 | +// await messageBroker.connect(); |
| 76 | +// } catch (error) { |
| 77 | +// if (error instanceof Error) { |
| 78 | +// expect(error.message).to.equal('Connection error'); |
| 79 | +// } |
| 80 | +// } |
| 81 | +// }); |
| 82 | +// }); |
| 83 | + |
| 84 | +// describe('produce', () => { |
| 85 | +// it('should produce a message to the queue', async () => { |
| 86 | +// await messageBroker.produce('testQueue', { test: 'message' }); |
| 87 | + |
| 88 | +// // eslint-disable-next-line @typescript-eslint/no-unused-expressions |
| 89 | +// expect(connectStub).to.have.been.called; |
| 90 | +// expect(sendToQueueStub).to.have.been.calledWith( |
| 91 | +// 'testQueue', |
| 92 | +// Buffer.from(JSON.stringify({ test: 'message' })), |
| 93 | +// ); |
| 94 | +// }); |
| 95 | + |
| 96 | +// it('should throw an error if producing message fails', async () => { |
| 97 | +// sendToQueueStub.rejects(new Error('Produce error')); |
| 98 | + |
| 99 | +// try { |
| 100 | +// await messageBroker.produce('testQueue', { test: 'message' }); |
| 101 | +// } catch (error) { |
| 102 | +// if (error instanceof Error) { |
| 103 | +// expect(error.message).to.equal('Produce error'); |
| 104 | +// } |
| 105 | +// } |
| 106 | +// }); |
| 107 | +// }); |
| 108 | + |
| 109 | +// describe('consume', () => { |
| 110 | +// it('should consume messages from the queue', async () => { |
| 111 | +// const onMessage = sinon.stub(); |
| 112 | +// const msg = { content: Buffer.from(JSON.stringify({ test: 'message' })) }; |
| 113 | + |
| 114 | +// consumeStub.yields(msg); |
| 115 | + |
| 116 | +// await messageBroker.consume('testQueue', onMessage); |
| 117 | + |
| 118 | +// // eslint-disable-next-line @typescript-eslint/no-unused-expressions |
| 119 | +// expect(connectStub).to.have.been.called; |
| 120 | +// expect(assertQueueStub).to.have.been.calledWith('testQueue', { durable: true }); |
| 121 | +// expect(consumeStub).to.have.been.calledWith('testQueue', sinon.match.func, { noAck: false }); |
| 122 | +// expect(onMessage).to.have.been.calledWith({ test: 'message' }); |
| 123 | +// expect(ackStub).to.have.been.calledWith(msg); |
| 124 | +// }); |
| 125 | + |
| 126 | +// it('should throw an error if consuming message fails', async () => { |
| 127 | +// consumeStub.rejects(new Error('Consume error')); |
| 128 | + |
| 129 | +// try { |
| 130 | +// await messageBroker.consume('testQueue', sinon.stub()); |
| 131 | +// } catch (error) { |
| 132 | +// if (error instanceof Error) { |
| 133 | +// expect(error.message).to.equal('Consume error'); |
| 134 | +// } |
| 135 | +// } |
| 136 | +// }); |
| 137 | +// }); |
| 138 | +// }); |
0 commit comments