@bernierllc/message-queue 1.2.0 → 1.5.0
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.
- package/dist/core/message-queue.js +8 -3
- package/dist/core/message-queue.js.map +1 -1
- package/dist/errors.d.ts +12 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +25 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -1
- package/dist/index.js.map +1 -1
- package/package.json +6 -2
- package/CHANGELOG.md +0 -107
- package/examples/basic-usage.ts +0 -197
- package/jest.config.cjs +0 -18
- package/src/core/message-queue.ts +0 -444
- package/src/index.ts +0 -16
- package/src/types/index.ts +0 -95
- package/src/utils/validation.ts +0 -231
- package/tests/message-queue.test.ts +0 -399
- package/tests/setup.ts +0 -16
- package/tests/validation.test.ts +0 -302
- package/tsconfig.json +0 -29
package/src/utils/validation.ts
DELETED
|
@@ -1,231 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
Copyright (c) 2025 Bernier LLC
|
|
3
|
-
|
|
4
|
-
This file is licensed to the client under a limited-use license.
|
|
5
|
-
The client may use and modify this code *only within the scope of the project it was delivered for*.
|
|
6
|
-
Redistribution or use in other products or commercial offerings is not permitted without written consent from Bernier LLC.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import { Message, QueueConfig, MessagePriority } from '../types';
|
|
10
|
-
|
|
11
|
-
export interface ValidationResult {
|
|
12
|
-
isValid: boolean;
|
|
13
|
-
error?: string;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Generate a unique ID for messages
|
|
18
|
-
*/
|
|
19
|
-
export function generateId(): string {
|
|
20
|
-
return `msg_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Validate a message object
|
|
25
|
-
*/
|
|
26
|
-
export function validateMessage(message: Message): ValidationResult {
|
|
27
|
-
if (!message.id || typeof message.id !== 'string') {
|
|
28
|
-
return {
|
|
29
|
-
isValid: false,
|
|
30
|
-
error: 'Message ID is required and must be a string'
|
|
31
|
-
};
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
if (message.content === undefined || message.content === null) {
|
|
35
|
-
return {
|
|
36
|
-
isValid: false,
|
|
37
|
-
error: 'Message content is required'
|
|
38
|
-
};
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
if (!Object.values(MessagePriority).includes(message.priority)) {
|
|
42
|
-
return {
|
|
43
|
-
isValid: false,
|
|
44
|
-
error: 'Invalid message priority'
|
|
45
|
-
};
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
if (!(message.timestamp instanceof Date) || isNaN(message.timestamp.getTime())) {
|
|
49
|
-
return {
|
|
50
|
-
isValid: false,
|
|
51
|
-
error: 'Message timestamp must be a valid Date'
|
|
52
|
-
};
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
if (message.expiresAt && (!(message.expiresAt instanceof Date) || isNaN(message.expiresAt.getTime()))) {
|
|
56
|
-
return {
|
|
57
|
-
isValid: false,
|
|
58
|
-
error: 'Message expiration must be a valid Date'
|
|
59
|
-
};
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
if (message.retryCount !== undefined && (typeof message.retryCount !== 'number' || message.retryCount < 0)) {
|
|
63
|
-
return {
|
|
64
|
-
isValid: false,
|
|
65
|
-
error: 'Retry count must be a non-negative number'
|
|
66
|
-
};
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
if (message.maxRetries !== undefined && (typeof message.maxRetries !== 'number' || message.maxRetries < 0)) {
|
|
70
|
-
return {
|
|
71
|
-
isValid: false,
|
|
72
|
-
error: 'Max retries must be a non-negative number'
|
|
73
|
-
};
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
if (message.metadata && typeof message.metadata !== 'object') {
|
|
77
|
-
return {
|
|
78
|
-
isValid: false,
|
|
79
|
-
error: 'Message metadata must be an object'
|
|
80
|
-
};
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
return { isValid: true };
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* Validate queue configuration
|
|
88
|
-
*/
|
|
89
|
-
export function validateConfig(config: QueueConfig): QueueConfig {
|
|
90
|
-
if (!config.name || typeof config.name !== 'string') {
|
|
91
|
-
throw new Error('Queue name is required and must be a string');
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
if (config.maxSize !== undefined && (typeof config.maxSize !== 'number' || config.maxSize <= 0)) {
|
|
95
|
-
throw new Error('Max size must be a positive number');
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
if (config.defaultPriority !== undefined && !Object.values(MessagePriority).includes(config.defaultPriority)) {
|
|
99
|
-
throw new Error('Invalid default priority');
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
if (config.defaultTtl !== undefined && (typeof config.defaultTtl !== 'number' || config.defaultTtl <= 0)) {
|
|
103
|
-
throw new Error('Default TTL must be a positive number');
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
if (config.maxRetries !== undefined && (typeof config.maxRetries !== 'number' || config.maxRetries < 0)) {
|
|
107
|
-
throw new Error('Max retries must be a non-negative number');
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
if (config.retryDelay !== undefined && (typeof config.retryDelay !== 'number' || config.retryDelay < 0)) {
|
|
111
|
-
throw new Error('Retry delay must be a non-negative number');
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
if (config.retryMaxDelay !== undefined && (typeof config.retryMaxDelay !== 'number' || config.retryMaxDelay < 0)) {
|
|
115
|
-
throw new Error('Retry max delay must be a non-negative number');
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
if (config.retryMaxDelay !== undefined && config.retryDelay !== undefined && config.retryMaxDelay < config.retryDelay) {
|
|
119
|
-
throw new Error('Retry max delay must be greater than or equal to retry delay');
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
return {
|
|
123
|
-
name: config.name,
|
|
124
|
-
maxSize: config.maxSize,
|
|
125
|
-
defaultPriority: config.defaultPriority || MessagePriority.NORMAL,
|
|
126
|
-
defaultTtl: config.defaultTtl,
|
|
127
|
-
enableRetries: config.enableRetries || false,
|
|
128
|
-
maxRetries: config.maxRetries || 0,
|
|
129
|
-
retryDelay: config.retryDelay || 1000,
|
|
130
|
-
retryMaxDelay: config.retryMaxDelay || (config.retryDelay || 1000) * 10,
|
|
131
|
-
retryJitter: config.retryJitter !== false // Default to true
|
|
132
|
-
} as QueueConfig;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
/**
|
|
136
|
-
* Validate message filter
|
|
137
|
-
*/
|
|
138
|
-
export function validateFilter(filter: any): ValidationResult {
|
|
139
|
-
if (filter.priority !== undefined && !Object.values(MessagePriority).includes(filter.priority)) {
|
|
140
|
-
return {
|
|
141
|
-
isValid: false,
|
|
142
|
-
error: 'Invalid priority filter'
|
|
143
|
-
};
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
if (filter.fromTimestamp && (!(filter.fromTimestamp instanceof Date) || isNaN(filter.fromTimestamp.getTime()))) {
|
|
147
|
-
return {
|
|
148
|
-
isValid: false,
|
|
149
|
-
error: 'From timestamp must be a valid Date'
|
|
150
|
-
};
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
if (filter.toTimestamp && (!(filter.toTimestamp instanceof Date) || isNaN(filter.toTimestamp.getTime()))) {
|
|
154
|
-
return {
|
|
155
|
-
isValid: false,
|
|
156
|
-
error: 'To timestamp must be a valid Date'
|
|
157
|
-
};
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
if (filter.limit !== undefined && (typeof filter.limit !== 'number' || filter.limit <= 0)) {
|
|
161
|
-
return {
|
|
162
|
-
isValid: false,
|
|
163
|
-
error: 'Limit must be a positive number'
|
|
164
|
-
};
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
if (filter.offset !== undefined && (typeof filter.offset !== 'number' || filter.offset < 0)) {
|
|
168
|
-
return {
|
|
169
|
-
isValid: false,
|
|
170
|
-
error: 'Offset must be a non-negative number'
|
|
171
|
-
};
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
if (filter.metadata && typeof filter.metadata !== 'object') {
|
|
175
|
-
return {
|
|
176
|
-
isValid: false,
|
|
177
|
-
error: 'Metadata filter must be an object'
|
|
178
|
-
};
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
return { isValid: true };
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
/**
|
|
185
|
-
* Validate message ID format
|
|
186
|
-
*/
|
|
187
|
-
export function validateMessageId(messageId: string): ValidationResult {
|
|
188
|
-
if (!messageId || typeof messageId !== 'string') {
|
|
189
|
-
return {
|
|
190
|
-
isValid: false,
|
|
191
|
-
error: 'Message ID is required and must be a string'
|
|
192
|
-
};
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
if (messageId.length < 10) {
|
|
196
|
-
return {
|
|
197
|
-
isValid: false,
|
|
198
|
-
error: 'Message ID must be at least 10 characters long'
|
|
199
|
-
};
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
return { isValid: true };
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
/**
|
|
206
|
-
* Validate queue name
|
|
207
|
-
*/
|
|
208
|
-
export function validateQueueName(name: string): ValidationResult {
|
|
209
|
-
if (!name || typeof name !== 'string') {
|
|
210
|
-
return {
|
|
211
|
-
isValid: false,
|
|
212
|
-
error: 'Queue name is required and must be a string'
|
|
213
|
-
};
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
if (name.length < 1 || name.length > 100) {
|
|
217
|
-
return {
|
|
218
|
-
isValid: false,
|
|
219
|
-
error: 'Queue name must be between 1 and 100 characters'
|
|
220
|
-
};
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
if (!/^[a-zA-Z0-9_-]+$/.test(name)) {
|
|
224
|
-
return {
|
|
225
|
-
isValid: false,
|
|
226
|
-
error: 'Queue name can only contain letters, numbers, underscores, and hyphens'
|
|
227
|
-
};
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
return { isValid: true };
|
|
231
|
-
}
|
|
@@ -1,399 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
Copyright (c) 2025 Bernier LLC
|
|
3
|
-
|
|
4
|
-
This file is licensed to the client under a limited-use license.
|
|
5
|
-
The client may use and modify this code *only within the scope of the project it was delivered for*.
|
|
6
|
-
Redistribution or use in other products or commercial offerings is not permitted without written consent from Bernier LLC.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import { MessageQueue } from '../src/core/message-queue';
|
|
10
|
-
import { MessagePriority, QueueConfig, QueueOptions, QueueEvent } from '../src/types';
|
|
11
|
-
|
|
12
|
-
describe('MessageQueue', () => {
|
|
13
|
-
let queue: MessageQueue;
|
|
14
|
-
let config: QueueConfig;
|
|
15
|
-
|
|
16
|
-
beforeEach(() => {
|
|
17
|
-
config = {
|
|
18
|
-
name: 'test-queue',
|
|
19
|
-
maxSize: 10,
|
|
20
|
-
defaultPriority: MessagePriority.NORMAL,
|
|
21
|
-
enableRetries: true,
|
|
22
|
-
maxRetries: 3,
|
|
23
|
-
retryDelay: 100
|
|
24
|
-
};
|
|
25
|
-
queue = new MessageQueue(config);
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
describe('constructor', () => {
|
|
29
|
-
it('should create a queue with default options', () => {
|
|
30
|
-
const simpleQueue = new MessageQueue({ name: 'simple-queue' });
|
|
31
|
-
expect(simpleQueue.size).toBe(0);
|
|
32
|
-
expect(simpleQueue.isEmpty).toBe(true);
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
it('should create a queue with custom options', () => {
|
|
36
|
-
const options: QueueOptions = {
|
|
37
|
-
enableEvents: false,
|
|
38
|
-
enableStats: false,
|
|
39
|
-
enablePersistence: false
|
|
40
|
-
};
|
|
41
|
-
const customQueue = new MessageQueue(config, options);
|
|
42
|
-
expect(customQueue.size).toBe(0);
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
it('should throw error for invalid config', () => {
|
|
46
|
-
expect(() => new MessageQueue({ name: '' })).toThrow('Queue name is required');
|
|
47
|
-
expect(() => new MessageQueue({ name: 'test', maxSize: -1 })).toThrow('Max size must be a positive number');
|
|
48
|
-
});
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
describe('addMessage', () => {
|
|
52
|
-
it('should add a message successfully', () => {
|
|
53
|
-
const result = queue.addMessage('test content');
|
|
54
|
-
expect(result.success).toBe(true);
|
|
55
|
-
expect(result.messageId).toBeDefined();
|
|
56
|
-
expect(queue.size).toBe(1);
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
it('should add message with custom priority', () => {
|
|
60
|
-
const result = queue.addMessage('high priority', MessagePriority.HIGH);
|
|
61
|
-
expect(result.success).toBe(true);
|
|
62
|
-
expect(queue.size).toBe(1);
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
it('should add message with metadata', () => {
|
|
66
|
-
const metadata = { userId: '123', type: 'notification' };
|
|
67
|
-
const result = queue.addMessage('test content', MessagePriority.NORMAL, metadata);
|
|
68
|
-
expect(result.success).toBe(true);
|
|
69
|
-
expect(queue.size).toBe(1);
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
it('should respect max size limit', () => {
|
|
73
|
-
// Fill the queue
|
|
74
|
-
for (let i = 0; i < 10; i++) {
|
|
75
|
-
queue.addMessage(`message ${i}`);
|
|
76
|
-
}
|
|
77
|
-
expect(queue.size).toBe(10);
|
|
78
|
-
expect(queue.isFull).toBe(true);
|
|
79
|
-
|
|
80
|
-
// Try to add one more
|
|
81
|
-
const result = queue.addMessage('overflow message');
|
|
82
|
-
expect(result.success).toBe(false);
|
|
83
|
-
expect(result.error).toBe('Queue is full');
|
|
84
|
-
expect(queue.size).toBe(10);
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
it('should not add message when queue is paused', () => {
|
|
88
|
-
queue.pause();
|
|
89
|
-
const result = queue.addMessage('test content');
|
|
90
|
-
expect(result.success).toBe(false);
|
|
91
|
-
expect(result.error).toBe('Queue is not active');
|
|
92
|
-
});
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
describe('getNextMessage', () => {
|
|
96
|
-
it('should return null for empty queue', () => {
|
|
97
|
-
expect(queue.getNextMessage()).toBeNull();
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
it('should return messages in priority order', () => {
|
|
101
|
-
queue.addMessage('low priority', MessagePriority.LOW);
|
|
102
|
-
queue.addMessage('high priority', MessagePriority.HIGH);
|
|
103
|
-
queue.addMessage('normal priority', MessagePriority.NORMAL);
|
|
104
|
-
|
|
105
|
-
const first = queue.getNextMessage();
|
|
106
|
-
const second = queue.getNextMessage();
|
|
107
|
-
const third = queue.getNextMessage();
|
|
108
|
-
|
|
109
|
-
expect(first?.content).toBe('high priority');
|
|
110
|
-
expect(second?.content).toBe('normal priority');
|
|
111
|
-
expect(third?.content).toBe('low priority');
|
|
112
|
-
});
|
|
113
|
-
|
|
114
|
-
it('should remove expired messages', () => {
|
|
115
|
-
const message = queue.addMessage('expired message');
|
|
116
|
-
expect(message.success).toBe(true);
|
|
117
|
-
|
|
118
|
-
// Manually expire the message by setting expiresAt to past
|
|
119
|
-
const messages = queue.getMessages();
|
|
120
|
-
if (messages.length > 0) {
|
|
121
|
-
messages[0].expiresAt = new Date(Date.now() - 1000);
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
expect(queue.getNextMessage()).toBeNull();
|
|
125
|
-
});
|
|
126
|
-
});
|
|
127
|
-
|
|
128
|
-
describe('processMessage', () => {
|
|
129
|
-
it('should process message successfully', async () => {
|
|
130
|
-
queue.addMessage('test content');
|
|
131
|
-
|
|
132
|
-
const processor = jest.fn().mockResolvedValue(true);
|
|
133
|
-
const result = await queue.processMessage(processor);
|
|
134
|
-
|
|
135
|
-
expect(result.success).toBe(true);
|
|
136
|
-
expect(processor).toHaveBeenCalledTimes(1);
|
|
137
|
-
expect(queue.size).toBe(0);
|
|
138
|
-
});
|
|
139
|
-
|
|
140
|
-
it('should handle processing failure with retries', async () => {
|
|
141
|
-
queue.addMessage('test content');
|
|
142
|
-
expect(queue.size).toBe(1);
|
|
143
|
-
|
|
144
|
-
const processor = jest.fn().mockResolvedValue(false); // Always fail
|
|
145
|
-
|
|
146
|
-
// Original processing attempt - this should succeed (returns success: true)
|
|
147
|
-
// but the processor itself fails, so the message will be scheduled for retry
|
|
148
|
-
const result = await queue.processMessage(processor);
|
|
149
|
-
expect(result.success).toBe(true);
|
|
150
|
-
expect(processor).toHaveBeenCalledTimes(1);
|
|
151
|
-
expect(queue.size).toBe(0); // Message removed, retry scheduled
|
|
152
|
-
|
|
153
|
-
// Wait a bit for the first retry to be scheduled and processed
|
|
154
|
-
await new Promise(resolve => setTimeout(resolve, 200));
|
|
155
|
-
|
|
156
|
-
// The retry logic should have completed by now
|
|
157
|
-
// We mainly just want to test that the retry mechanism doesn't crash
|
|
158
|
-
expect(processor).toHaveBeenCalledTimes(1); // Only initial call in this test
|
|
159
|
-
});
|
|
160
|
-
|
|
161
|
-
it('should handle processing failure without retries', async () => {
|
|
162
|
-
const noRetryConfig = { ...config, enableRetries: false };
|
|
163
|
-
const noRetryQueue = new MessageQueue(noRetryConfig);
|
|
164
|
-
noRetryQueue.addMessage('test content');
|
|
165
|
-
|
|
166
|
-
const processor = jest.fn().mockResolvedValue(false);
|
|
167
|
-
const result = await noRetryQueue.processMessage(processor);
|
|
168
|
-
|
|
169
|
-
expect(result.success).toBe(true);
|
|
170
|
-
expect(noRetryQueue.size).toBe(0); // Message removed after failure
|
|
171
|
-
});
|
|
172
|
-
|
|
173
|
-
it('should handle processor exceptions', async () => {
|
|
174
|
-
queue.addMessage('test content');
|
|
175
|
-
|
|
176
|
-
const processor = jest.fn().mockRejectedValue(new Error('Processing error'));
|
|
177
|
-
const result = await queue.processMessage(processor);
|
|
178
|
-
|
|
179
|
-
expect(result.success).toBe(true);
|
|
180
|
-
expect(processor).toHaveBeenCalledTimes(1);
|
|
181
|
-
});
|
|
182
|
-
|
|
183
|
-
it('should use custom retry configuration', async () => {
|
|
184
|
-
const customRetryConfig = {
|
|
185
|
-
...config,
|
|
186
|
-
retryDelay: 50,
|
|
187
|
-
retryMaxDelay: 200,
|
|
188
|
-
retryJitter: false
|
|
189
|
-
};
|
|
190
|
-
const customQueue = new MessageQueue(customRetryConfig);
|
|
191
|
-
customQueue.addMessage('test content');
|
|
192
|
-
|
|
193
|
-
const processor = jest.fn().mockResolvedValue(false);
|
|
194
|
-
const result = await customQueue.processMessage(processor);
|
|
195
|
-
|
|
196
|
-
expect(result.success).toBe(true);
|
|
197
|
-
expect(processor).toHaveBeenCalledTimes(1);
|
|
198
|
-
expect(customQueue.size).toBe(0); // Message removed, retry scheduled
|
|
199
|
-
|
|
200
|
-
// Wait a bit for retries with custom timing
|
|
201
|
-
await new Promise(resolve => setTimeout(resolve, 100));
|
|
202
|
-
|
|
203
|
-
// Test that custom retry config is being used
|
|
204
|
-
// We mainly just want to test that the retry mechanism works with custom config
|
|
205
|
-
expect(processor).toHaveBeenCalledTimes(1); // Only initial call in this test
|
|
206
|
-
});
|
|
207
|
-
});
|
|
208
|
-
|
|
209
|
-
describe('getMessages', () => {
|
|
210
|
-
beforeEach(() => {
|
|
211
|
-
queue.addMessage('message1', MessagePriority.LOW);
|
|
212
|
-
queue.addMessage('message2', MessagePriority.HIGH);
|
|
213
|
-
queue.addMessage('message3', MessagePriority.NORMAL);
|
|
214
|
-
});
|
|
215
|
-
|
|
216
|
-
it('should return all messages by default', () => {
|
|
217
|
-
const messages = queue.getMessages();
|
|
218
|
-
expect(messages).toHaveLength(3);
|
|
219
|
-
});
|
|
220
|
-
|
|
221
|
-
it('should filter by priority', () => {
|
|
222
|
-
const highPriorityMessages = queue.getMessages({ priority: MessagePriority.HIGH });
|
|
223
|
-
expect(highPriorityMessages).toHaveLength(1);
|
|
224
|
-
expect(highPriorityMessages[0].content).toBe('message2');
|
|
225
|
-
});
|
|
226
|
-
|
|
227
|
-
it('should filter by timestamp range', () => {
|
|
228
|
-
const now = new Date();
|
|
229
|
-
const future = new Date(now.getTime() + 1000);
|
|
230
|
-
const past = new Date(now.getTime() - 1000);
|
|
231
|
-
|
|
232
|
-
const messages = queue.getMessages({ fromTimestamp: past, toTimestamp: future });
|
|
233
|
-
expect(messages).toHaveLength(3);
|
|
234
|
-
});
|
|
235
|
-
|
|
236
|
-
it('should filter by metadata', () => {
|
|
237
|
-
queue.addMessage('metadata message', MessagePriority.NORMAL, { type: 'test' });
|
|
238
|
-
|
|
239
|
-
const messages = queue.getMessages({ metadata: { type: 'test' } });
|
|
240
|
-
expect(messages).toHaveLength(1);
|
|
241
|
-
expect(messages[0].content).toBe('metadata message');
|
|
242
|
-
});
|
|
243
|
-
|
|
244
|
-
it('should apply pagination', () => {
|
|
245
|
-
const messages = queue.getMessages({ limit: 2, offset: 1 });
|
|
246
|
-
expect(messages).toHaveLength(2);
|
|
247
|
-
});
|
|
248
|
-
});
|
|
249
|
-
|
|
250
|
-
describe('removeMessage', () => {
|
|
251
|
-
it('should remove specific message', () => {
|
|
252
|
-
const result = queue.addMessage('test content');
|
|
253
|
-
expect(result.success).toBe(true);
|
|
254
|
-
expect(queue.size).toBe(1);
|
|
255
|
-
|
|
256
|
-
const removeResult = queue.removeMessage(result.messageId!);
|
|
257
|
-
expect(removeResult.success).toBe(true);
|
|
258
|
-
expect(queue.size).toBe(0);
|
|
259
|
-
});
|
|
260
|
-
|
|
261
|
-
it('should return error for non-existent message', () => {
|
|
262
|
-
const result = queue.removeMessage('non-existent-id');
|
|
263
|
-
expect(result.success).toBe(false);
|
|
264
|
-
expect(result.error).toBe('Message not found');
|
|
265
|
-
});
|
|
266
|
-
});
|
|
267
|
-
|
|
268
|
-
describe('clear', () => {
|
|
269
|
-
it('should clear all messages', () => {
|
|
270
|
-
queue.addMessage('message1');
|
|
271
|
-
queue.addMessage('message2');
|
|
272
|
-
expect(queue.size).toBe(2);
|
|
273
|
-
|
|
274
|
-
const result = queue.clear();
|
|
275
|
-
expect(result.success).toBe(true);
|
|
276
|
-
expect(queue.size).toBe(0);
|
|
277
|
-
expect(queue.isEmpty).toBe(true);
|
|
278
|
-
});
|
|
279
|
-
});
|
|
280
|
-
|
|
281
|
-
describe('getStats', () => {
|
|
282
|
-
it('should return queue statistics', () => {
|
|
283
|
-
queue.addMessage('message1');
|
|
284
|
-
queue.addMessage('message2');
|
|
285
|
-
|
|
286
|
-
const stats = queue.getStats();
|
|
287
|
-
expect(stats.totalMessages).toBe(2);
|
|
288
|
-
expect(stats.pendingMessages).toBe(2);
|
|
289
|
-
expect(stats.processedMessages).toBe(0);
|
|
290
|
-
expect(stats.failedMessages).toBe(0);
|
|
291
|
-
});
|
|
292
|
-
|
|
293
|
-
it('should update stats after processing', async () => {
|
|
294
|
-
queue.addMessage('message1');
|
|
295
|
-
queue.addMessage('message2');
|
|
296
|
-
|
|
297
|
-
const processor = jest.fn().mockResolvedValue(true);
|
|
298
|
-
await queue.processMessage(processor);
|
|
299
|
-
|
|
300
|
-
const stats = queue.getStats();
|
|
301
|
-
expect(stats.processedMessages).toBe(1);
|
|
302
|
-
expect(stats.pendingMessages).toBe(1);
|
|
303
|
-
});
|
|
304
|
-
});
|
|
305
|
-
|
|
306
|
-
describe('getInfo', () => {
|
|
307
|
-
it('should return queue information', () => {
|
|
308
|
-
const info = queue.getInfo();
|
|
309
|
-
expect(info.name).toBe('test-queue');
|
|
310
|
-
expect(info.config.name).toBe('test-queue');
|
|
311
|
-
expect(info.isActive).toBe(true);
|
|
312
|
-
expect(info.createdAt).toBeInstanceOf(Date);
|
|
313
|
-
expect(info.lastActivity).toBeInstanceOf(Date);
|
|
314
|
-
});
|
|
315
|
-
});
|
|
316
|
-
|
|
317
|
-
describe('events', () => {
|
|
318
|
-
it('should emit events when enabled', () => {
|
|
319
|
-
const events: QueueEvent[] = [];
|
|
320
|
-
queue.onEvent((event) => events.push(event));
|
|
321
|
-
|
|
322
|
-
queue.addMessage('test content');
|
|
323
|
-
expect(events).toHaveLength(1);
|
|
324
|
-
expect(events[0].type).toBe('message_added');
|
|
325
|
-
});
|
|
326
|
-
|
|
327
|
-
it('should not emit events when disabled', () => {
|
|
328
|
-
const options: QueueOptions = { enableEvents: false };
|
|
329
|
-
const noEventQueue = new MessageQueue(config, options);
|
|
330
|
-
|
|
331
|
-
const events: QueueEvent[] = [];
|
|
332
|
-
noEventQueue.onEvent((event) => events.push(event));
|
|
333
|
-
|
|
334
|
-
noEventQueue.addMessage('test content');
|
|
335
|
-
expect(events).toHaveLength(0);
|
|
336
|
-
});
|
|
337
|
-
|
|
338
|
-
it('should handle event handler errors gracefully', () => {
|
|
339
|
-
const errorHandler = jest.fn().mockImplementation(() => {
|
|
340
|
-
throw new Error('Handler error');
|
|
341
|
-
});
|
|
342
|
-
|
|
343
|
-
queue.onEvent(errorHandler);
|
|
344
|
-
|
|
345
|
-
// Should not throw and should still add the message
|
|
346
|
-
const result = queue.addMessage('test content');
|
|
347
|
-
expect(result.success).toBe(true);
|
|
348
|
-
expect(queue.size).toBe(1);
|
|
349
|
-
});
|
|
350
|
-
});
|
|
351
|
-
|
|
352
|
-
describe('pause and resume', () => {
|
|
353
|
-
it('should pause and resume queue', () => {
|
|
354
|
-
expect(queue.getInfo().isActive).toBe(true);
|
|
355
|
-
|
|
356
|
-
queue.pause();
|
|
357
|
-
expect(queue.getInfo().isActive).toBe(false);
|
|
358
|
-
|
|
359
|
-
queue.resume();
|
|
360
|
-
expect(queue.getInfo().isActive).toBe(true);
|
|
361
|
-
});
|
|
362
|
-
|
|
363
|
-
it('should not process messages when paused', async () => {
|
|
364
|
-
queue.addMessage('test content');
|
|
365
|
-
queue.pause();
|
|
366
|
-
|
|
367
|
-
const processor = jest.fn();
|
|
368
|
-
const result = await queue.processMessage(processor);
|
|
369
|
-
|
|
370
|
-
expect(result.success).toBe(false);
|
|
371
|
-
expect(result.error).toBe('No messages available');
|
|
372
|
-
expect(processor).not.toHaveBeenCalled();
|
|
373
|
-
});
|
|
374
|
-
});
|
|
375
|
-
|
|
376
|
-
describe('properties', () => {
|
|
377
|
-
it('should return correct size', () => {
|
|
378
|
-
expect(queue.size).toBe(0);
|
|
379
|
-
queue.addMessage('message1');
|
|
380
|
-
expect(queue.size).toBe(1);
|
|
381
|
-
});
|
|
382
|
-
|
|
383
|
-
it('should return correct empty state', () => {
|
|
384
|
-
expect(queue.isEmpty).toBe(true);
|
|
385
|
-
queue.addMessage('message1');
|
|
386
|
-
expect(queue.isEmpty).toBe(false);
|
|
387
|
-
});
|
|
388
|
-
|
|
389
|
-
it('should return correct full state', () => {
|
|
390
|
-
expect(queue.isFull).toBe(false);
|
|
391
|
-
|
|
392
|
-
// Fill the queue
|
|
393
|
-
for (let i = 0; i < 10; i++) {
|
|
394
|
-
queue.addMessage(`message ${i}`);
|
|
395
|
-
}
|
|
396
|
-
expect(queue.isFull).toBe(true);
|
|
397
|
-
});
|
|
398
|
-
});
|
|
399
|
-
});
|
package/tests/setup.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
Copyright (c) 2025 Bernier LLC
|
|
3
|
-
|
|
4
|
-
This file is licensed to the client under a limited-use license.
|
|
5
|
-
The client may use and modify this code *only within the scope of the project it was delivered for*.
|
|
6
|
-
Redistribution or use in other products or commercial offerings is not permitted without written consent from Bernier LLC.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
// Global test setup
|
|
10
|
-
beforeEach(() => {
|
|
11
|
-
// Reset any global state if needed
|
|
12
|
-
});
|
|
13
|
-
|
|
14
|
-
afterEach(() => {
|
|
15
|
-
// Clean up after each test
|
|
16
|
-
});
|