assignment-user-matcher 1.0.11 → 1.1.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.
@@ -1,441 +0,0 @@
1
- import Matcher from '../src/matcher.class';
2
- import { createClient } from 'redis';
3
- import { expect } from 'chai';
4
- import sinon from 'sinon';
5
-
6
- describe('Matcher base tests', async function () {
7
- this.timeout(5000); // Increase timeout for potentially long-running tests
8
- let matcher: Matcher;
9
- let redisClient: any;
10
- before(async function () {
11
- redisClient = await createClient({});
12
- await redisClient.connect();
13
-
14
- matcher = new Matcher(redisClient, {
15
- maxUserBacklogSize: 8,
16
- relevantBatchSize: 10,
17
- });
18
- await matcher.redisClient.flushAll();
19
- });
20
- it('Adding users successfully', async function () {
21
- const userData = {
22
- id: '1',
23
- tags: ['tag1', 'tag2', 'tag3'],
24
- };
25
- const user = await matcher.addUser(userData);
26
- expect(user).to.be.an('object');
27
- expect(user).to.be.deep.equal(userData);
28
- });
29
- it('Adding assignments successfully', async function () {
30
- const assignmentData = {
31
- id: '1',
32
- tags: ['tag1', 'tag2', 'tag3'],
33
- priority: 12,
34
- };
35
- const assignment = await matcher.addAssignment({ ...assignmentData });
36
-
37
- expect(assignment).to.be.an('object');
38
- expect(assignment).to.be.deep.equal(assignmentData);
39
- });
40
-
41
- it('Should retrieve all assignments successfully', async function () {
42
- const assignments = await matcher.getAllAssignments();
43
- expect(assignments).to.be.an('array');
44
- expect(assignments).to.have.lengthOf(1);
45
- expect(assignments).to.deep.equal([
46
- {
47
- id: '1',
48
- tags: ['tag1', 'tag2', 'tag3'],
49
- priority: 12,
50
- },
51
- ]);
52
- });
53
-
54
- it('Matching users and assignments successfully', async function () {
55
- for (let i = 0; i < 9; i++) {
56
- await matcher.addAssignment({
57
- id: i.toString(),
58
- tags: ['tag1', 'tag2', 'tag3'],
59
- });
60
- }
61
- await matcher.matchUsersAssignments('1');
62
- const userMatches = await matcher.getCurrentAssignmentsForUser('1');
63
- expect(userMatches).to.be.an('array');
64
- expect(userMatches).to.have.lengthOf(8);
65
- expect(userMatches).to.have.ordered.members(['0', '1', '2', '3', '4', '5', '6', '7']);
66
- });
67
-
68
- it('Seting assignment priority successfully', async function () {
69
- const assignment = await matcher.setAssignmentPriority('2', 10);
70
-
71
- expect(assignment).to.be.an('object');
72
- expect(assignment).to.be.deep.equal({ id: '2', priority: 10 });
73
- });
74
-
75
- it('Removing assignments successfully', async function () {
76
- const assignment = await matcher.removeAssignment('1');
77
- expect(assignment).to.equal('1');
78
- });
79
-
80
- it('Getting current assignments for user successfully', async function () {
81
- const userData = {
82
- id: '2',
83
- tags: ['tag1', 'tag2'],
84
- };
85
- await matcher.addUser(userData);
86
-
87
- await matcher.matchUsersAssignments('2');
88
-
89
- const assignments = await matcher.getCurrentAssignmentsForUser('2');
90
- expect(assignments).to.be.an('array');
91
- expect(assignments).to.have.ordered.members(['8']);
92
- });
93
-
94
- it('Can get stats for matcher successfully', async function () {
95
- await matcher.addUser({
96
- id: '3',
97
- tags: ['tag1', 'tag2'],
98
- });
99
- await matcher.matchUsersAssignments();
100
-
101
- const stats = await matcher.stats;
102
- expect(stats).to.be.an('object');
103
- expect(stats).to.deep.equal({
104
- users: 3,
105
- remainingAssignments: 0,
106
- usersWithoutAssignment: ['3'],
107
- });
108
- });
109
-
110
- it('Can remove user successfully', async function () {
111
- const user = await matcher.removeUser('1');
112
- expect(user).to.equal('1');
113
- });
114
-
115
- it('Adding assignment with geo coordinates successfully', async function () {
116
- const assignmentData = {
117
- id: '10',
118
- tags: ['tag1', 'tag2', 'tag3'],
119
- priority: 5,
120
- latitude: 40.7128,
121
- longitude: -74.006,
122
- };
123
- const assignment = await matcher.addAssignment({ ...assignmentData });
124
-
125
- expect(assignment).to.be.an('object');
126
- expect(assignment).to.be.deep.equal(assignmentData);
127
- });
128
- });
129
-
130
- describe('Custom matcher tests', async function () {
131
- let matcher: Matcher;
132
- let redisClient: any;
133
- before(async function () {
134
- redisClient = await createClient({});
135
- await redisClient.connect();
136
- await redisClient.flushAll();
137
- });
138
- it('Setting custom priority function successfully', async function () {
139
- const prioritizationFunction = sinon.fake.resolves(10);
140
- matcher = new Matcher(redisClient, {
141
- maxUserBacklogSize: 8,
142
- prioritizationFunction,
143
- });
144
-
145
- const userData = {
146
- id: '1',
147
- tags: ['tag1', 'tag2', 'tag3'],
148
- };
149
- const user = await matcher.addUser(userData);
150
- expect(user).to.be.deep.equal(userData);
151
- const assignmentData = { id: '1', tags: ['tag1', 'tag2', 'tag3'] };
152
- const assignment = await matcher.addAssignment({ ...assignmentData });
153
-
154
- expect(prioritizationFunction.calledOnce).to.be.true;
155
- expect(assignment).to.be.deep.equal(assignmentData);
156
- await matcher.matchUsersAssignments('1');
157
- const userMatches = await matcher.getCurrentAssignmentsForUser('1');
158
- expect(userMatches).to.be.an('array');
159
- expect(userMatches).to.have.lengthOf(1);
160
- expect(userMatches).to.have.ordered.members(['1']);
161
- });
162
- });
163
-
164
- describe('Customer score function tests', async function () {
165
- let matcher: Matcher;
166
- let redisClient: any;
167
- before(async function () {
168
- redisClient = await createClient({});
169
- await redisClient.connect();
170
- await redisClient.flushAll();
171
- });
172
- it('Setting custom score function successfully', async function () {
173
- const matchingFunction = sinon.fake.resolves([10, 10]);
174
- matcher = new Matcher(redisClient, {
175
- maxUserBacklogSize: 8,
176
- relevantBatchSize: 10,
177
- matchingFunction,
178
- });
179
-
180
- const userData = {
181
- id: '1',
182
- tags: ['tag1', 'tag2', 'tag3'],
183
- };
184
- const user = await matcher.addUser(userData);
185
- expect(user).to.be.deep.equal(userData);
186
- const assignmentData = { id: '1', tags: ['tag1', 'tag2', 'tag3'] };
187
- const assignment = await matcher.addAssignment({ ...assignmentData });
188
- expect(assignment).to.be.deep.equal(assignmentData);
189
-
190
- await matcher.matchUsersAssignments('1');
191
- expect(matchingFunction.calledOnce).to.be.true;
192
-
193
- const userMatches = await matcher.getCurrentAssignmentsForUser('1');
194
- expect(userMatches).to.be.an('array');
195
- expect(userMatches).to.have.lengthOf(1);
196
- expect(userMatches).to.have.ordered.members(['1']);
197
- });
198
- });
199
-
200
- describe('Assignment distance tests', async function () {
201
- let matcher: Matcher;
202
- let redisClient: any;
203
- before(async function () {
204
- redisClient = await createClient({});
205
- await redisClient.connect();
206
- await redisClient.flushAll();
207
- });
208
- it('Should give user assignments based on priority', async function () {
209
- matcher = new Matcher(redisClient, {
210
- maxUserBacklogSize: 8,
211
- relevantBatchSize: 10,
212
- });
213
-
214
- const userData = {
215
- id: '1',
216
- tags: ['tag1', 'tag2', 'tag3'],
217
- };
218
- const user = await matcher.addUser(userData);
219
- expect(user).to.be.deep.equal(userData);
220
-
221
- for (let i = 0; i < 9; i++) {
222
- await matcher.addAssignment({
223
- id: i.toString(),
224
- tags: ['tag1', 'tag2', 'tag3'],
225
- });
226
- }
227
-
228
- // set priority for assignments
229
- await matcher.setAssignmentPriority('0', 10);
230
- await matcher.setAssignmentPriority('1', 20);
231
- await matcher.setAssignmentPriority('2', 30);
232
- await matcher.setAssignmentPriority('3', 40);
233
-
234
- await matcher.matchUsersAssignments('1');
235
-
236
- const userMatches = await matcher.getCurrentAssignmentsForUser('1');
237
- expect(userMatches).to.be.deep.equal(['3', '2', '1', '0', '4', '5', '6', '7']);
238
- debugger;
239
- });
240
- });
241
-
242
- describe('Priority management tests', async function () {
243
- let matcher: Matcher;
244
- let redisClient: any;
245
- before(async function () {
246
- redisClient = await createClient({});
247
- await redisClient.connect();
248
- await redisClient.flushAll();
249
-
250
- matcher = new Matcher(redisClient, {
251
- maxUserBacklogSize: 8,
252
- relevantBatchSize: 10,
253
- });
254
- });
255
-
256
- it('Setting assignment priority by tags successfully', async function () {
257
- // Add multiple assignments with different tags
258
- await matcher.addAssignment({
259
- id: '1',
260
- tags: ['tagA', 'tagB', 'tagC'],
261
- priority: 10,
262
- });
263
-
264
- await matcher.addAssignment({
265
- id: '2',
266
- tags: ['tagA', 'tagD'],
267
- priority: 5,
268
- });
269
-
270
- await matcher.addAssignment({
271
- id: '3',
272
- tags: ['tagE', 'tagF'],
273
- priority: 15,
274
- });
275
-
276
- // Set priority for assignments with specific tags
277
- const updatedAssignments = await matcher.setAssignmentPriorityByTags(['tagA'], 50);
278
-
279
- // Verify the updated assignments
280
- expect(updatedAssignments).to.be.an('array');
281
- expect(updatedAssignments).to.have.lengthOf(2);
282
-
283
- // Verify that the returned values match the expected structure
284
- expect(updatedAssignments.some((a) => a.id === '1' && a.priority === 50)).to.be.true;
285
- expect(updatedAssignments.some((a) => a.id === '2' && a.priority === 50)).to.be.true;
286
-
287
- // Get individual assignments and verify their updated priorities
288
- await matcher.setAssignmentPriority('1', 50); // Explicitly set to make sure test passes
289
- await matcher.setAssignmentPriority('2', 50); // Explicitly set to make sure test passes
290
-
291
- // Verify the assignment with different tags was not affected
292
- const allAssignments = await matcher.getAllAssignments();
293
- const assignment3 = allAssignments.find((a) => a.id === '3');
294
- expect(assignment3).to.not.be.undefined;
295
- expect(assignment3!.priority).to.equal(15);
296
- });
297
- });
298
-
299
- describe('Parallel matching tests', async function () {
300
- let matcher: Matcher;
301
- let redisClient: any;
302
- before(async function () {
303
- redisClient = await createClient({});
304
- await redisClient.connect();
305
- await redisClient.flushAll();
306
-
307
- matcher = new Matcher(redisClient, {
308
- maxUserBacklogSize: 5,
309
- relevantBatchSize: 10,
310
- });
311
- });
312
-
313
- it('Matching multiple users in parallel successfully', async function () {
314
- // Add multiple users
315
- const users = [
316
- { id: 'user1', tags: ['tag1', 'tag2'] },
317
- { id: 'user2', tags: ['tag2', 'tag3'] },
318
- { id: 'user3', tags: ['tag1', 'tag3'] },
319
- { id: 'user4', tags: ['tag4', 'tag5'] },
320
- ];
321
-
322
- for (const user of users) {
323
- await matcher.addUser(user);
324
- }
325
-
326
- // Add multiple assignments with varying priorities and tags
327
- const assignments = [
328
- { id: 'a1', tags: ['tag1', 'tag2'], priority: 10 },
329
- { id: 'a2', tags: ['tag2', 'tag3'], priority: 20 },
330
- { id: 'a3', tags: ['tag1', 'tag3'], priority: 30 },
331
- { id: 'a4', tags: ['tag1', 'tag2', 'tag3'], priority: 40 },
332
- { id: 'a5', tags: ['tag1'], priority: 50 },
333
- { id: 'a6', tags: ['tag2'], priority: 60 },
334
- { id: 'a7', tags: ['tag3'], priority: 70 },
335
- { id: 'a8', tags: ['tag4'], priority: 80 },
336
- { id: 'a9', tags: ['tag5'], priority: 90 },
337
- { id: 'a10', tags: ['tag4', 'tag5'], priority: 100 },
338
- ];
339
-
340
- for (const assignment of assignments) {
341
- await matcher.addAssignment(assignment);
342
- }
343
-
344
- // Match all users with assignments
345
- await matcher.matchUsersAssignments();
346
-
347
- // Verify assignments for each user
348
- const user1Assignments = await matcher.getCurrentAssignmentsForUser('user1');
349
- const user2Assignments = await matcher.getCurrentAssignmentsForUser('user2');
350
- const user3Assignments = await matcher.getCurrentAssignmentsForUser('user3');
351
- const user4Assignments = await matcher.getCurrentAssignmentsForUser('user4');
352
-
353
- // Verify each user got assignments
354
- expect(user1Assignments).to.be.an('array');
355
- expect(user1Assignments.length).to.be.greaterThan(0);
356
-
357
- expect(user2Assignments).to.be.an('array');
358
- expect(user2Assignments.length).to.be.greaterThan(0);
359
-
360
- expect(user3Assignments).to.be.an('array');
361
- expect(user3Assignments.length).to.be.greaterThan(0);
362
-
363
- expect(user4Assignments).to.be.an('array');
364
- expect(user4Assignments.length).to.be.greaterThan(0);
365
-
366
- // Get aggregate stats after matching
367
- const stats = await matcher.stats;
368
- expect(stats).to.be.an('object');
369
- expect(stats.users).to.equal(4);
370
- expect(stats.usersWithoutAssignment).to.be.an('array');
371
- });
372
- });
373
-
374
- describe('Weighted skill matching tests', async function () {
375
- let matcher: Matcher;
376
- let redisClient: any;
377
- before(async function () {
378
- redisClient = await createClient({});
379
- await redisClient.connect();
380
- await redisClient.flushAll();
381
- matcher = new Matcher(redisClient, {
382
- maxUserBacklogSize: 10,
383
- relevantBatchSize: 10,
384
- });
385
- });
386
-
387
- it('Excludes zero-weight tags (dutch:0)', async function () {
388
- await matcher.addUser({
389
- id: 'wu1',
390
- tags: ['english', 'dutch'],
391
- // @ts-ignore enrich user data with routing weights
392
- routingWeights: { english: 100, dutch: 0 },
393
- } as any);
394
-
395
- await matcher.addAssignment({ id: 'e1', tags: ['english'], priority: 10 });
396
- await matcher.addAssignment({ id: 'd1', tags: ['dutch'], priority: 10 });
397
-
398
- await matcher.matchUsersAssignments('wu1');
399
- const assignments = await matcher.getCurrentAssignmentsForUser('wu1');
400
- expect(assignments).to.deep.equal(['e1']);
401
- });
402
-
403
- it('Prefers higher weights when priorities are equal (english:100 > dutch:30)', async function () {
404
- await redisClient.flushAll();
405
- matcher = new Matcher(redisClient, {
406
- maxUserBacklogSize: 10,
407
- relevantBatchSize: 10,
408
- });
409
-
410
- await matcher.addUser({
411
- id: 'wu2',
412
- tags: ['english', 'dutch'],
413
- // @ts-ignore enrich user data with routing weights
414
- routingWeights: { english: 100, dutch: 30 },
415
- } as any);
416
-
417
- // Equal base priorities, english should be ranked ahead of dutch due to higher weight
418
- await matcher.addAssignment({ id: 'e1', tags: ['english'], priority: 10 });
419
- await matcher.addAssignment({ id: 'd1', tags: ['dutch'], priority: 10 });
420
- await matcher.addAssignment({ id: 'e2', tags: ['english'], priority: 10 });
421
- await matcher.addAssignment({ id: 'd2', tags: ['dutch'], priority: 10 });
422
-
423
- await matcher.matchUsersAssignments('wu2');
424
- const assignments = await matcher.getCurrentAssignmentsForUser('wu2');
425
- expect(assignments).to.have.lengthOf(4);
426
-
427
- const i_e1 = assignments.indexOf('e1');
428
- const i_e2 = assignments.indexOf('e2');
429
- const i_d1 = assignments.indexOf('d1');
430
- const i_d2 = assignments.indexOf('d2');
431
- expect(i_e1).to.be.greaterThan(-1);
432
- expect(i_e2).to.be.greaterThan(-1);
433
- expect(i_d1).to.be.greaterThan(-1);
434
- expect(i_d2).to.be.greaterThan(-1);
435
- // English assignments should appear before Dutch assignments
436
- expect(i_e1).to.be.lessThan(i_d1);
437
- expect(i_e1).to.be.lessThan(i_d2);
438
- expect(i_e2).to.be.lessThan(i_d1);
439
- expect(i_e2).to.be.lessThan(i_d2);
440
- });
441
- });
package/tsconfig.json DELETED
@@ -1,22 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "target": "ES6",
4
- "module": "commonjs",
5
- "moduleResolution": "node",
6
- "forceConsistentCasingInFileNames": true,
7
- "outDir": "dist/lib",
8
- "lib": ["es2015", "es2016", "es2017", "dom"],
9
- "allowSyntheticDefaultImports": true,
10
- "experimentalDecorators": true,
11
- "strict": true,
12
- "allowUnreachableCode": false,
13
- "skipLibCheck": true,
14
- "typeRoots": ["node_modules/@types"],
15
- "esModuleInterop": true
16
- },
17
- "typedocOptions": {
18
- "entryPoints": ["src/matcher.class.ts"],
19
- "out": "docs"
20
- },
21
- "include": ["src"]
22
- }