@quatrain/queue-amqp 1.1.19 → 1.1.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -16,7 +16,7 @@ class AmqpQueueAdapter extends queue_1.AbstractQueueAdapter {
16
16
  constructor(params) {
17
17
  super(params);
18
18
  const { host = 'localhost', user = 'guest', password = 'guest', port = 5672, } = params.config || {};
19
- this._client = new amqp_client_1.AMQPClient(`amqp://${user}:${password}@${host}:${port}?frameMax=0`);
19
+ this._client = new amqp_client_1.AMQPClient(`amqp://${user}:${password}@${host}:${port}?frameMax=0x80000000`);
20
20
  }
21
21
  send(data, topic) {
22
22
  return __awaiter(this, void 0, void 0, function* () {
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,118 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ const AmqpQueueAdapter_1 = require("./AmqpQueueAdapter");
13
+ const amqp_client_1 = require("@cloudamqp/amqp-client");
14
+ const queue_1 = require("@quatrain/queue");
15
+ // Mock the @cloudamqp/amqp-client library
16
+ jest.mock('@cloudamqp/amqp-client');
17
+ // Mock @quatrain/queue to spy on Queue.info
18
+ jest.mock('@quatrain/queue', () => (Object.assign(Object.assign({}, jest.requireActual('@quatrain/queue')), { Queue: {
19
+ info: jest.fn(),
20
+ } })));
21
+ const mockPublish = jest
22
+ .fn()
23
+ .mockResolvedValue({ confirmId: 'mock-confirm-id' });
24
+ const mockSubscribe = jest.fn();
25
+ const mockQueue = jest.fn().mockResolvedValue({
26
+ publish: mockPublish,
27
+ subscribe: mockSubscribe,
28
+ });
29
+ const mockChannel = jest.fn().mockResolvedValue({
30
+ queue: mockQueue,
31
+ });
32
+ const mockConnect = jest.fn().mockResolvedValue({
33
+ channel: mockChannel,
34
+ });
35
+ const AMQPClientMock = amqp_client_1.AMQPClient;
36
+ AMQPClientMock.mockImplementation(() => {
37
+ return {
38
+ connect: mockConnect,
39
+ };
40
+ });
41
+ describe('AmqpQueueAdapter', () => {
42
+ const params = {
43
+ config: {
44
+ host: 'test-host',
45
+ user: 'test-user',
46
+ password: 'test-password',
47
+ port: 1234,
48
+ },
49
+ topic: 'default-topic',
50
+ };
51
+ beforeEach(() => {
52
+ // Clear all instances and calls to constructor and all methods:
53
+ AMQPClientMock.mockClear();
54
+ mockConnect.mockClear();
55
+ mockChannel.mockClear();
56
+ mockQueue.mockClear();
57
+ mockPublish.mockClear();
58
+ mockSubscribe.mockClear();
59
+ queue_1.Queue.info.mockClear();
60
+ });
61
+ it('should construct with default and custom parameters', () => {
62
+ new AmqpQueueAdapter_1.AmqpQueueAdapter({ config: {} });
63
+ expect(AMQPClientMock).toHaveBeenCalledWith('amqp://guest:guest@localhost:5672?frameMax=0');
64
+ new AmqpQueueAdapter_1.AmqpQueueAdapter(params);
65
+ expect(AMQPClientMock).toHaveBeenCalledWith('amqp://test-user:test-password@test-host:1234?frameMax=0');
66
+ });
67
+ describe('send', () => {
68
+ it('should send a message to the specified topic', () => __awaiter(void 0, void 0, void 0, function* () {
69
+ const adapter = new AmqpQueueAdapter_1.AmqpQueueAdapter(params);
70
+ const data = { key: 'value' };
71
+ const topic = 'test-topic';
72
+ const confirmId = yield adapter.send(data, topic);
73
+ expect(mockConnect).toHaveBeenCalledTimes(1);
74
+ expect(mockChannel).toHaveBeenCalledTimes(1);
75
+ expect(mockQueue).toHaveBeenCalledWith(topic);
76
+ expect(mockPublish).toHaveBeenCalledWith(Buffer.from(JSON.stringify(data)), { deliveryMode: 2 });
77
+ expect(queue_1.Queue.info).toHaveBeenCalledWith(`[AMQP] Sending message to ${topic}`);
78
+ expect(queue_1.Queue.info).toHaveBeenCalledWith(`[AMQP] Message send with id mock-confirm-id`);
79
+ expect(confirmId).toBe('mock-confirm-id');
80
+ }));
81
+ });
82
+ describe('listen', () => {
83
+ it('should listen to the default topic if none is provided', () => __awaiter(void 0, void 0, void 0, function* () {
84
+ const adapter = new AmqpQueueAdapter_1.AmqpQueueAdapter(params);
85
+ const handler = jest.fn();
86
+ yield adapter.listen(undefined, handler);
87
+ expect(mockConnect).toHaveBeenCalledTimes(1);
88
+ expect(mockChannel).toHaveBeenCalledTimes(1);
89
+ expect(mockQueue).toHaveBeenCalledWith(params.topic);
90
+ expect(mockSubscribe).toHaveBeenCalledWith({ noAck: true }, expect.any(Function));
91
+ }));
92
+ it('should listen to a specific topic', () => __awaiter(void 0, void 0, void 0, function* () {
93
+ const adapter = new AmqpQueueAdapter_1.AmqpQueueAdapter(params);
94
+ const handler = jest.fn();
95
+ const topic = 'specific-topic';
96
+ yield adapter.listen(topic, handler);
97
+ expect(mockQueue).toHaveBeenCalledWith(topic);
98
+ }));
99
+ it('should throw an error if no topic is available', () => __awaiter(void 0, void 0, void 0, function* () {
100
+ const adapter = new AmqpQueueAdapter_1.AmqpQueueAdapter({ config: {} });
101
+ const handler = jest.fn();
102
+ yield expect(adapter.listen(undefined, handler)).rejects.toThrow('No topic provided for listening.');
103
+ }));
104
+ it('should call the message handler with the message body', () => __awaiter(void 0, void 0, void 0, function* () {
105
+ const adapter = new AmqpQueueAdapter_1.AmqpQueueAdapter(params);
106
+ const handler = jest.fn();
107
+ const messageContent = { data: 'test message' };
108
+ const message = {
109
+ bodyToString: () => JSON.stringify(messageContent),
110
+ };
111
+ mockSubscribe.mockImplementation((options, callback) => __awaiter(void 0, void 0, void 0, function* () {
112
+ yield callback(message);
113
+ }));
114
+ yield adapter.listen('any-topic', handler);
115
+ expect(handler).toHaveBeenCalledWith(JSON.stringify(messageContent));
116
+ }));
117
+ });
118
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quatrain/queue-amqp",
3
- "version": "1.1.19",
3
+ "version": "1.1.20",
4
4
  "description": "Queue adapter for AMQP compatible queue",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",