-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathmatch.ts
780 lines (724 loc) · 28.7 KB
/
match.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import { EJSON } from 'bson';
import { expect } from 'chai';
import { inspect } from 'util';
import {
Binary,
type BSONTypeAlias,
CommandFailedEvent,
CommandStartedEvent,
CommandSucceededEvent,
ConnectionCheckedInEvent,
ConnectionCheckedOutEvent,
ConnectionCheckOutFailedEvent,
ConnectionCheckOutStartedEvent,
ConnectionClosedEvent,
ConnectionCreatedEvent,
ConnectionPoolClearedEvent,
ConnectionPoolClosedEvent,
ConnectionPoolCreatedEvent,
ConnectionPoolReadyEvent,
ConnectionReadyEvent,
type Document,
Long,
MongoError,
MongoServerError,
ObjectId,
type OneOrMore,
ServerClosedEvent,
ServerDescriptionChangedEvent,
ServerHeartbeatFailedEvent,
ServerHeartbeatStartedEvent,
ServerHeartbeatSucceededEvent,
ServerOpeningEvent,
TopologyClosedEvent,
TopologyDescriptionChangedEvent,
TopologyOpeningEvent
} from '../../mongodb';
import { ejson } from '../utils';
import { type CmapEvent, type CommandEvent, type EntitiesMap, type SdamEvent } from './entities';
import {
type ExpectedCmapEvent,
type ExpectedCommandEvent,
type ExpectedError,
type ExpectedEventsForClient,
type ExpectedLogMessage,
type ExpectedSdamEvent
} from './schema';
export interface ExistsOperator {
$$exists: boolean;
}
export function isExistsOperator(value: unknown): value is ExistsOperator {
return typeof value === 'object' && value != null && '$$exists' in value;
}
export interface TypeOperator {
$$type: OneOrMore<BSONTypeAlias>;
}
export function isTypeOperator(value: unknown): value is TypeOperator {
return typeof value === 'object' && value != null && '$$type' in value;
}
export interface MatchesEntityOperator {
$$matchesEntity: string;
}
export function isMatchesEntityOperator(value: unknown): value is MatchesEntityOperator {
return typeof value === 'object' && value != null && '$$matchesEntity' in value;
}
export interface MatchesHexBytesOperator {
$$matchesHexBytes: string;
}
export function isMatchesHexBytesOperator(value: unknown): value is MatchesHexBytesOperator {
return typeof value === 'object' && value != null && '$$matchesHexBytes' in value;
}
export interface UnsetOrMatchesOperator {
$$unsetOrMatches: unknown;
}
export function isUnsetOrMatchesOperator(value: unknown): value is UnsetOrMatchesOperator {
return typeof value === 'object' && value != null && '$$unsetOrMatches' in value;
}
export interface SessionLsidOperator {
$$sessionLsid: string;
}
export function isSessionLsidOperator(value: unknown): value is SessionLsidOperator {
return typeof value === 'object' && value != null && '$$sessionLsid' in value;
}
export interface MatchAsDocumentOperator {
$$matchAsDocument: unknown;
}
export function isMatchAsDocumentOperator(value: unknown): value is MatchAsDocumentOperator {
return typeof value === 'object' && value != null && '$$matchAsDocument' in value;
}
export interface MatchAsRootOperator {
$$matchAsRoot: unknown;
}
export function isMatchAsRootOperator(value: unknown): value is MatchAsRootOperator {
return typeof value === 'object' && value != null && '$$matchAsRoot' in value;
}
export const SpecialOperatorKeys = [
'$$exists',
'$$type',
'$$matchesEntity',
'$$matchesHexBytes',
'$$matchAsRoot',
'$$matchAsDocument',
'$$unsetOrMatches',
'$$sessionLsid'
];
export type SpecialOperator =
| ExistsOperator
| TypeOperator
| MatchesEntityOperator
| MatchesHexBytesOperator
| UnsetOrMatchesOperator
| SessionLsidOperator
| MatchAsDocumentOperator
| MatchAsRootOperator;
type KeysOfUnion<T> = T extends object ? keyof T : never;
export type SpecialOperatorKey = KeysOfUnion<SpecialOperator>;
export function isSpecialOperator(value: unknown): value is SpecialOperator {
return (
isExistsOperator(value) ||
isTypeOperator(value) ||
isMatchesEntityOperator(value) ||
isMatchesHexBytesOperator(value) ||
isUnsetOrMatchesOperator(value) ||
isSessionLsidOperator(value) ||
isMatchAsRootOperator(value) ||
isMatchAsDocumentOperator(value)
);
}
const TYPE_MAP = new Map();
TYPE_MAP.set('double', actual => typeof actual === 'number' || actual._bsontype === 'Double');
TYPE_MAP.set('string', actual => typeof actual === 'string');
TYPE_MAP.set('object', actual => typeof actual === 'object' && actual !== null);
TYPE_MAP.set('array', actual => Array.isArray(actual));
TYPE_MAP.set('binData', actual => actual instanceof Binary);
TYPE_MAP.set('undefined', actual => actual === undefined);
TYPE_MAP.set('objectId', actual => actual instanceof ObjectId);
TYPE_MAP.set('bool', actual => typeof actual === 'boolean');
TYPE_MAP.set('date', actual => actual instanceof Date);
TYPE_MAP.set('null', actual => actual === null);
TYPE_MAP.set('regex', actual => actual instanceof RegExp || actual._bsontype === 'BSONRegExp');
TYPE_MAP.set('dbPointer', actual => actual._bsontype === 'DBRef');
TYPE_MAP.set('javascript', actual => actual._bsontype === 'Code');
TYPE_MAP.set('symbol', actual => actual._bsontype === 'Symbol');
TYPE_MAP.set('javascriptWithScope', actual => actual._bsontype === 'Code' && actual.scope);
TYPE_MAP.set('timestamp', actual => actual._bsontype === 'Timestamp');
TYPE_MAP.set('decimal', actual => actual._bsontype === 'Decimal128');
TYPE_MAP.set('minKey', actual => actual._bsontype === 'MinKey');
TYPE_MAP.set('maxKey', actual => actual._bsontype === 'MaxKey');
TYPE_MAP.set(
'int',
actual => (typeof actual === 'number' && Number.isInteger(actual)) || actual._bsontype === 'Int32'
);
TYPE_MAP.set(
'long',
actual =>
(typeof actual === 'number' && Number.isInteger(actual)) ||
Long.isLong(actual) ||
typeof actual === 'bigint'
);
/**
* resultCheck
*
* @param actual - the actual result
* @param expected - the expected result
* @param entities - the EntitiesMap associated with the test
* @param path - an array of strings representing the 'path' in the document down to the current
* value. For example, given `{ a: { b: { c: 4 } } }`, when evaluating `{ c: 4 }`, the path
* will look like: `['a', 'b']`. Used to print useful error messages when assertions fail.
* @param checkExtraKeys - a boolean value that determines how keys present on the `actual` object but
* not on the `expected` object are treated. When set to `true`, any extra keys on the
* `actual` object will throw an error
*/
export function resultCheck(
actual: Document,
expected: Document | number | string | boolean,
entities: EntitiesMap,
path: string[] = [],
checkExtraKeys = false
): void {
function checkNestedDocuments(key: string, value: any, checkExtraKeys: boolean) {
if (key === 'sort') {
// TODO: This is a workaround that works because all sorts in the specs
// are objects with one key; ideally we'd want to adjust the spec definitions
// to indicate whether order matters for any given key and set general
// expectations accordingly (see NODE-3235)
expect(Object.keys(value)).to.have.lengthOf(1);
expect(actual[key]).to.be.instanceOf(Map);
expect(actual[key].size).to.equal(1);
const expectedSortKey = Object.keys(value)[0];
expect(actual[key]).to.have.all.keys(expectedSortKey);
const objFromActual = { [expectedSortKey]: actual[key].get(expectedSortKey) };
resultCheck(objFromActual, value, entities, path, checkExtraKeys);
} else if (key === 'createIndexes') {
for (const [i, userIndex] of actual.indexes.entries()) {
expect(expected).to.have.nested.property(`.indexes[${i}].key`).to.be.a('object');
// @ts-expect-error: Not worth narrowing to a document
expect(Object.keys(expected.indexes[i].key)).to.have.lengthOf(1);
expect(userIndex).to.have.property('key').that.is.instanceOf(Map);
expect(
userIndex.key.size,
'Test input is JSON and cannot correctly test more than 1 key'
).to.equal(1);
userIndex.key = Object.fromEntries(userIndex.key);
}
resultCheck(actual[key], value, entities, path, checkExtraKeys);
} else {
resultCheck(actual[key], value, entities, path, checkExtraKeys);
}
}
if (typeof expected === 'object' && expected) {
// Expected is an object
// either its a special operator or just an object to check equality against
if (isSpecialOperator(expected)) {
// Special operation check is a base condition
// specialCheck may recurse depending upon the check ($$unsetOrMatches)
specialCheck(actual, expected, entities, path, checkExtraKeys);
return;
}
if (typeof actual !== 'object') {
expect.fail('Expected actual value to be an object');
}
const expectedEntries = Object.entries(expected);
if (Array.isArray(expected)) {
if (!Array.isArray(actual)) {
expect.fail(
`expected value at ${path.join('.')} to be an array, but received ${inspect(actual)}`
);
}
for (const [index, value] of expectedEntries) {
path.push(`[${index}]`);
checkNestedDocuments(index, value, false);
path.pop();
}
} else {
for (const [key, value] of expectedEntries) {
path.push(`.${key}`);
checkNestedDocuments(key, value, true);
path.pop();
}
if (checkExtraKeys) {
expect(actual, `Expected actual to exist at ${path.join('')}`).to.exist;
// by using `Object.keys`, we ignore non-enumerable properties. This is intentional.
const actualKeys = Object.keys(actual);
const expectedKeys = Object.keys(expected);
// Don't check for full key set equality because some of the actual keys
// might be e.g. $$unsetOrMatches, which can be omitted.
const extraKeys = actualKeys.filter(key => !expectedKeys.includes(key));
if (extraKeys.length > 0) {
expect.fail(
`object has more keys than expected. \n\tactual: [${actualKeys}] \n\texpected: [${expectedKeys}]`
);
}
}
}
return;
}
// Here's our recursion base case
// expected is: number | Long | string | boolean | null
if (Long.isLong(actual) && typeof expected === 'number') {
// Long requires special equality check
expect(actual.equals(expected)).to.be.true;
} else if (Long.isLong(expected) && typeof actual === 'number') {
// Long requires special equality check
expect(expected.equals(actual)).to.be.true;
} else if (Number.isNaN(actual) && Number.isNaN(expected)) {
// in JS, NaN isn't equal to NaN but we want to not fail if we have two NaN
} else if (
typeof expected === 'number' &&
typeof actual === 'number' &&
expected === 0 &&
actual === 0
) {
// case to handle +0 and -0
expect(Object.is(expected, actual)).to.be.true;
} else if (actual && actual._bsontype === 'Int32' && typeof expected === 'number') {
expect(actual.value).to.equal(expected);
} else {
expect(actual).to.equal(expected);
}
}
export function specialCheck(
actual: Document,
expected: SpecialOperator,
entities: EntitiesMap,
path: string[] = [],
checkExtraKeys: boolean
): void {
if (isUnsetOrMatchesOperator(expected)) {
if (actual === null || actual === undefined) return;
resultCheck(actual, expected.$$unsetOrMatches as any, entities, path, checkExtraKeys);
} else if (isMatchesEntityOperator(expected)) {
// $$matchesEntity
const entity = entities.get(expected.$$matchesEntity);
if (
typeof actual === 'object' && // an object
actual && // that isn't null
'equals' in actual && // with an equals
typeof actual.equals === 'function' // method
) {
expect(actual.equals(entity)).to.be.true;
} else {
expect(actual).to.equal(entity);
}
} else if (isMatchesHexBytesOperator(expected)) {
// $$matchesHexBytes
const expectedBuffer = Buffer.from(expected.$$matchesHexBytes, 'hex');
expect(expectedBuffer.every((byte, index) => byte === actual[index])).to.be.true;
} else if (isSessionLsidOperator(expected)) {
// $$sessionLsid
const session = entities.getEntity('session', expected.$$sessionLsid, false);
expect(session, `Session ${expected.$$sessionLsid} does not exist in entities`).to.exist;
const entitySessionHex = session.id!.id.toString('hex').toUpperCase();
const actualSessionHex = actual.id!.toString('hex').toUpperCase();
expect(
entitySessionHex,
`Session entity ${expected.$$sessionLsid} does not match lsid`
).to.equal(actualSessionHex);
} else if (isTypeOperator(expected)) {
// $$type
let ok = false;
const types = Array.isArray(expected.$$type) ? expected.$$type : [expected.$$type];
for (const type of types) {
ok ||= TYPE_MAP.get(type)(actual);
}
expect(ok, `Expected [${actual}] to be one of [${types}]`).to.be.true;
} else if (isExistsOperator(expected)) {
// $$exists
const actualExists = actual !== undefined && actual !== null;
if (expected.$$exists) {
expect(
actualExists,
ejson`expected value at path ${path.join('')} to exist, but received ${actual}`
).to.be.true;
} else {
expect(
actualExists,
ejson`expected value at path ${path.join('')} NOT to exist, but received ${actual}`
).to.be.false;
}
} else if (isMatchAsDocumentOperator(expected)) {
if (typeof actual === 'string') {
const actualDoc = EJSON.parse(actual, { relaxed: false });
resultCheck(actualDoc, expected.$$matchAsDocument as any, entities, path, true);
} else {
expect.fail(
`Expected value at path '${path.join('')}' to be string, but received ${inspect(actual)}`
);
}
} else if (isMatchAsRootOperator(expected)) {
expect(
typeof actual,
`Expected value at path '${path.join('')}' to be object, but received ${inspect(actual)}`
).to.equal('object');
expect(typeof expected.$$matchAsRoot, 'Value of $$matchAsRoot must be an object').to.equal(
'object'
);
resultCheck(actual, expected.$$matchAsRoot as any, entities, path, false);
} else {
expect.fail(`Unknown special operator: ${JSON.stringify(expected)}`);
}
}
// CMAP events where the payload does not matter.
const EMPTY_CMAP_EVENTS = {
poolCreatedEvent: ConnectionPoolCreatedEvent,
poolReadyEvent: ConnectionPoolReadyEvent,
poolClosedEvent: ConnectionPoolClosedEvent,
connectionCreatedEvent: ConnectionCreatedEvent,
connectionReadyEvent: ConnectionReadyEvent,
connectionCheckOutStartedEvent: ConnectionCheckOutStartedEvent,
connectionCheckOutFailedEvent: ConnectionCheckOutFailedEvent,
connectionCheckedOutEvent: ConnectionCheckedOutEvent,
connectionCheckedInEvent: ConnectionCheckedInEvent
};
function validEmptyCmapEvent(expected: ExpectedCommandEvent | ExpectedCmapEvent) {
const expectedEventName = Object.keys(expected)[0];
return !!EMPTY_CMAP_EVENTS[expectedEventName];
}
function failOnMismatchedCount(
actual: CommandEvent[] | CmapEvent[] | SdamEvent[],
expected: (ExpectedCommandEvent & ExpectedCmapEvent & ExpectedSdamEvent)[]
) {
const actualNames = actual.map(a => a.constructor.name);
const expectedNames = expected.map(e => Object.keys(e)[0]);
expect.fail(
`Expected event count mismatch, expected ${inspect(expectedNames)} but got ${inspect(
actualNames
)}`
);
}
function compareCommandStartedEvents(
actual: CommandStartedEvent,
expected: ExpectedCommandEvent['commandStartedEvent'],
entities: EntitiesMap,
prefix: string
) {
if (expected!.command) {
resultCheck(actual.command, expected!.command, entities, [`${prefix}.command`]);
}
if (expected!.commandName) {
expect(
expected!.commandName,
`expected ${prefix}.commandName to equal ${expected!.commandName} but received ${
actual.commandName
}`
).to.equal(actual.commandName);
}
if (expected!.databaseName) {
expect(
expected!.databaseName,
`expected ${prefix}.databaseName to equal ${expected!.databaseName} but received ${
actual.databaseName
}`
).to.equal(actual.databaseName);
}
}
function compareCommandSucceededEvents(
actual: CommandSucceededEvent,
expected: ExpectedCommandEvent['commandSucceededEvent'],
entities: EntitiesMap,
prefix: string
) {
if (expected!.reply) {
resultCheck(actual.reply as Document, expected!.reply, entities, [prefix]);
}
if (expected!.commandName) {
expect(
expected!.commandName,
`expected ${prefix}.commandName to equal ${expected!.commandName} but received ${
actual.commandName
}`
).to.equal(actual.commandName);
}
}
function compareCommandFailedEvents(
actual: CommandFailedEvent,
expected: ExpectedCommandEvent['commandFailedEvent'],
entities: EntitiesMap,
prefix: string
) {
if (expected!.commandName) {
expect(
expected!.commandName,
`expected ${prefix}.commandName to equal ${expected!.commandName} but received ${
actual.commandName
}`
).to.equal(actual.commandName);
}
}
function compareEvents(
actual: CommandEvent[] | CmapEvent[] | SdamEvent[],
expected: (ExpectedCommandEvent & ExpectedCmapEvent & ExpectedSdamEvent)[],
entities: EntitiesMap
) {
if (actual.length !== expected.length) {
failOnMismatchedCount(actual, expected);
}
for (const [index, actualEvent] of actual.entries()) {
const expectedEvent = expected[index];
const rootPrefix = `events[${index}]`;
if (expectedEvent.commandStartedEvent) {
const path = `${rootPrefix}.commandStartedEvent`;
if (!(actualEvent instanceof CommandStartedEvent)) {
expect.fail(`expected ${path} to be instanceof CommandStartedEvent`);
}
compareCommandStartedEvents(actualEvent, expectedEvent.commandStartedEvent, entities, path);
if (expectedEvent.commandStartedEvent.hasServerConnectionId) {
expect(actualEvent).property('serverConnectionId').to.be.a('bigint');
} else if (expectedEvent.commandStartedEvent.hasServerConnectionId === false) {
expect(actualEvent).property('serverConnectionId').to.be.null;
}
} else if (expectedEvent.commandSucceededEvent) {
const path = `${rootPrefix}.commandSucceededEvent`;
if (!(actualEvent instanceof CommandSucceededEvent)) {
expect.fail(`expected ${path} to be instanceof CommandSucceededEvent`);
}
compareCommandSucceededEvents(
actualEvent,
expectedEvent.commandSucceededEvent,
entities,
path
);
if (expectedEvent.commandSucceededEvent.hasServerConnectionId) {
expect(actualEvent).property('serverConnectionId').to.be.a('bigint');
} else if (expectedEvent.commandSucceededEvent.hasServerConnectionId === false) {
expect(actualEvent).property('serverConnectionId').to.be.null;
}
} else if (expectedEvent.commandFailedEvent) {
const path = `${rootPrefix}.commandFailedEvent`;
if (!(actualEvent instanceof CommandFailedEvent)) {
expect.fail(`expected ${path} to be instanceof CommandFailedEvent`);
}
compareCommandFailedEvents(actualEvent, expectedEvent.commandFailedEvent, entities, path);
if (expectedEvent.commandFailedEvent.hasServerConnectionId) {
expect(actualEvent).property('serverConnectionId').to.be.a('bigint');
} else if (expectedEvent.commandFailedEvent.hasServerConnectionId === false) {
expect(actualEvent).property('serverConnectionId').to.be.null;
}
} else if (expectedEvent.connectionClosedEvent) {
expect(actualEvent).to.be.instanceOf(ConnectionClosedEvent);
if (expectedEvent.connectionClosedEvent.hasServiceId) {
expect(actualEvent).property('serviceId').to.exist;
}
} else if (expectedEvent.poolClearedEvent) {
expect(actualEvent).to.be.instanceOf(ConnectionPoolClearedEvent);
if (expectedEvent.poolClearedEvent.hasServiceId) {
expect(actualEvent).property('serviceId').to.exist;
}
if (expectedEvent.poolClearedEvent.interruptInUseConnections != null) {
expect(actualEvent)
.property('interruptInUseConnections')
.to.equal(expectedEvent.poolClearedEvent.interruptInUseConnections);
}
} else if (validEmptyCmapEvent(expectedEvent as ExpectedCmapEvent)) {
const expectedEventName = Object.keys(expectedEvent)[0];
const expectedEventInstance = EMPTY_CMAP_EVENTS[expectedEventName];
expect(actualEvent).to.be.instanceOf(expectedEventInstance);
} else if (expectedEvent.serverDescriptionChangedEvent) {
expect(actualEvent).to.be.instanceOf(ServerDescriptionChangedEvent);
const expectedServerDescriptionKeys = ['previousDescription', 'newDescription'];
expect(expectedServerDescriptionKeys).to.include.all.members(
Object.keys(expectedEvent.serverDescriptionChangedEvent)
);
for (const descriptionKey of expectedServerDescriptionKeys) {
expect(actualEvent).to.have.property(descriptionKey);
const expectedDescription =
expectedEvent.serverDescriptionChangedEvent[descriptionKey] ?? {};
for (const nestedKey of Object.keys(expectedDescription)) {
expect(actualEvent[descriptionKey]).to.have.property(
nestedKey,
expectedDescription[nestedKey]
);
}
}
} else if (expectedEvent.serverHeartbeatStartedEvent) {
expect(actualEvent).to.be.instanceOf(ServerHeartbeatStartedEvent);
const expectedSdamEvent = expectedEvent.serverHeartbeatStartedEvent;
for (const property of Object.keys(expectedSdamEvent)) {
expect(actualEvent[property]).to.equal(expectedSdamEvent[property]);
}
} else if (expectedEvent.serverHeartbeatFailedEvent) {
expect(actualEvent).to.be.instanceOf(ServerHeartbeatFailedEvent);
const expectedSdamEvent = expectedEvent.serverHeartbeatFailedEvent;
for (const property of Object.keys(expectedSdamEvent)) {
expect(actualEvent[property]).to.equal(expectedSdamEvent[property]);
}
} else if (expectedEvent.serverHeartbeatSucceededEvent) {
expect(actualEvent).to.be.instanceOf(ServerHeartbeatSucceededEvent);
const expectedSdamEvent = expectedEvent.serverHeartbeatSucceededEvent;
for (const property of Object.keys(expectedSdamEvent)) {
expect(actualEvent[property]).to.equal(expectedSdamEvent[property]);
}
} else if (expectedEvent.serverOpeningEvent) {
expect(actualEvent).to.be.instanceOf(ServerOpeningEvent);
const expectedSdamEvent = expectedEvent.serverOpeningEvent;
for (const property of Object.keys(expectedSdamEvent)) {
expect(actualEvent[property]).to.equal(expectedSdamEvent[property]);
}
} else if (expectedEvent.serverClosedEvent) {
expect(actualEvent).to.be.instanceOf(ServerClosedEvent);
const expectedSdamEvent = expectedEvent.serverClosedEvent;
for (const property of Object.keys(expectedSdamEvent)) {
expect(actualEvent[property]).to.equal(expectedSdamEvent[property]);
}
} else if (expectedEvent.topologyOpeningEvent) {
expect(actualEvent).to.be.instanceOf(TopologyOpeningEvent);
const expectedSdamEvent = expectedEvent.topologyOpeningEvent;
for (const property of Object.keys(expectedSdamEvent)) {
expect(actualEvent[property]).to.equal(expectedSdamEvent[property]);
}
} else if (expectedEvent.topologyClosedEvent) {
expect(actualEvent).to.be.instanceOf(TopologyClosedEvent);
const expectedSdamEvent = expectedEvent.topologyClosedEvent;
for (const property of Object.keys(expectedSdamEvent)) {
expect(actualEvent[property]).to.equal(expectedSdamEvent[property]);
}
} else if (expectedEvent.topologyDescriptionChangedEvent) {
expect(actualEvent).to.be.instanceOf(TopologyDescriptionChangedEvent);
const actualTopChangedEvent = actualEvent as TopologyDescriptionChangedEvent;
const expectedSdamEvent = expectedEvent.topologyDescriptionChangedEvent;
if (expectedSdamEvent.previousDescription?.type) {
expect(actualTopChangedEvent.previousDescription.type).to.equal(
expectedSdamEvent.previousDescription.type
);
}
if (expectedSdamEvent.newDescription?.type) {
expect(actualTopChangedEvent.newDescription.type).to.equal(
expectedSdamEvent.newDescription.type
);
}
} else {
expect.fail(`Encountered unexpected event - ${inspect(actualEvent)}`);
}
}
}
export function matchesEvents(
{ events: expected, ignoreExtraEvents }: ExpectedEventsForClient,
actual: CommandEvent[] | CmapEvent[] | SdamEvent[],
entities: EntitiesMap
): void {
ignoreExtraEvents = ignoreExtraEvents ?? false;
if (ignoreExtraEvents) {
if (actual.length < expected.length) {
failOnMismatchedCount(actual, expected);
}
const slicedActualEvents = actual.slice(0, expected.length);
compareEvents(slicedActualEvents, expected, entities);
} else {
if (actual.length !== expected.length) {
failOnMismatchedCount(actual, expected);
}
compareEvents(actual, expected, entities);
}
}
export function filterExtraLogs(
logsToIgnore: ExpectedLogMessage[],
actual: ExpectedLogMessage[],
entities: EntitiesMap
): ExpectedLogMessage[] {
function isLogRelevant(log: ExpectedLogMessage) {
for (const logToIgnore of logsToIgnore) {
try {
// see if log matches a log to ignore, it is not relevant
resultCheck(log.data, logToIgnore.data, entities, undefined, false);
return false;
} catch {
continue;
}
}
// if log does not match any logs to ignore, it is relevant
return true;
}
const filteredMessages: ExpectedLogMessage[] = actual.filter(isLogRelevant);
return filteredMessages;
}
export function compareLogs(
expected: ExpectedLogMessage[],
actual: ExpectedLogMessage[],
entities: EntitiesMap
): void {
expect(actual).to.have.lengthOf(expected.length);
for (const [index, actualLog] of actual.entries()) {
const rootPrefix = `expectLogMessages[${index}]`;
const expectedLog = expected[index];
// Check that log levels match
expect(actualLog).to.have.property('level', expectedLog.level);
// Check that components match
expect(actualLog).to.have.property('component', expectedLog.component);
// NOTE: The spec states that if the failureIsRedacted flag is present, we
// must assert that a failure occurred.
if (expectedLog.failureIsRedacted !== undefined) {
expect(expectedLog.failureIsRedacted).to.be.a('boolean');
expect(actualLog.data.failure, 'Expected failure to exist').to.exist;
if (expectedLog.failureIsRedacted) {
// Assert that failure has been redacted
expect(actualLog.data.failure, 'Expected failure to have been redacted').to.equal(
'(redacted)'
);
} else {
// Assert that failure has not been redacted
expect(
actualLog.data.failure,
'Expected failure to have not been redacted'
).to.not.deep.equal({});
}
}
resultCheck(actualLog.data, expectedLog.data, entities, [rootPrefix], false);
}
}
function isMongoCryptError(err): boolean {
if (err.constructor.name === 'MongoCryptError') {
return true;
}
return err.stack.includes('at ClientEncryption');
}
export function expectErrorCheck(
error: Error | MongoError,
expected: ExpectedError,
entities: EntitiesMap
): void {
const expectMessage = `\n\nOriginal Error Stack:\n${error.stack}\n\n`;
if (!isMongoCryptError(error)) {
expect(error, expectMessage).to.be.instanceOf(MongoError);
}
if (expected.isClientError === false) {
expect(error).to.be.instanceOf(MongoServerError);
} else if (expected.isClientError === true) {
expect(error).not.to.be.instanceOf(MongoServerError);
}
if (expected.errorContains != null) {
expect(error.message.toLowerCase(), expectMessage.toLowerCase()).to.include(
expected.errorContains.toLowerCase()
);
}
if (expected.errorCode != null) {
expect(error, expectMessage).to.have.property('code', expected.errorCode);
}
if (expected.errorCodeName != null) {
expect(error, expectMessage).to.have.property('codeName', expected.errorCodeName);
}
if (expected.errorLabelsContain != null) {
const mongoError = error as MongoError;
for (const errorLabel of expected.errorLabelsContain) {
expect(
mongoError.hasErrorLabel(errorLabel),
`Error was supposed to have label ${errorLabel}, has [${mongoError.errorLabels}] -- ${expectMessage}`
).to.be.true;
}
}
if (expected.errorLabelsOmit != null) {
const mongoError = error as MongoError;
for (const errorLabel of expected.errorLabelsOmit) {
expect(
mongoError.hasErrorLabel(errorLabel),
`Error was not supposed to have label ${errorLabel}, has [${mongoError.errorLabels}] -- ${expectMessage}`
).to.be.false;
}
}
if (expected.expectResult != null) {
resultCheck(error, expected.expectResult as any, entities);
}
if (expected.errorResponse != null) {
resultCheck(error, expected.errorResponse, entities);
}
}