<commit_before>'use strict';
const BSON = require('bson');
const Readable = require('stream').Readable;
const Writable = require('stream').Writable;
const MessageStream = require('../../../lib/core/cmap/message_stream');
const Msg = require('../../../lib/core/connection/msg').Msg;
const expect = require('chai').expect;

function bufferToStream(buffer) {
  const stream = new Readable();
  stream.push(buffer);
  stream.push(null);
  return stream;
}

describe('Message Stream', function() {
  describe('reading', function() {
    [
      {
        description: 'valid OP_REPLY',
        data: Buffer.from(
          '370000000100000001000000010000000000000000000000000000000000000001000000130000001069736d6173746572000100000000',
          'hex'
        ),
        documents: [{ ismaster: 1 }]
      },
      {
        description: 'valid OP_MSG',
        data: Buffer.from(
          '370000000100000000000000dd0700000000000000220000001069736d6173746572000100000002246462000600000061646d696e0000',
          'hex'
        ),
        documents: [{ $db: 'admin', ismaster: 1 }]
      },
      {
        description: 'Invalid message size (negative)',
        data: Buffer.from('ffffffff', 'hex'),
        error: 'Invalid message size: -1'
      },
      {
        description: 'Invalid message size (exceeds maximum)',
        data: Buffer.from('01000004', 'hex'),
        error: 'Invalid message size: 67108865, max allowed: 67108864'
      }
    ].forEach(test => {
      it(test.description, function(done) {
        const bson = new BSON();
        const error = test.error;
        const inputStream = bufferToStream(test.data);
        const messageStream = new MessageStream({ bson });

        messageStream.on('message', msg => {
          if (error) {
            done(new Error(`expected error: ${error}`));
            return;
          }

          msg.parse();

          if (test.documents) {
            expect(msg).to.have.property('documents');
            expect(msg.documents).to.eql(test.documents);
          }

          done();
        });

        messageStream.on('error', err => {
          if (error == null) {
            done(err);
            return;
          }

          expect(err.message).to.equal(error);
          done();
        });

        inputStream.pipe(messageStream);
      });
    });
  });

  describe('writing', function() {
    it('should write a message to the stream', function(done) {<commit_msg>test: prefer use of chained chai assertions<commit_after>'use strict';
const BSON = require('bson');
const Readable = require('stream').Readable;
const Writable = require('stream').Writable;
const MessageStream = require('../../../lib/core/cmap/message_stream');
const Msg = require('../../../lib/core/connection/msg').Msg;
const expect = require('chai').expect;

function bufferToStream(buffer) {
  const stream = new Readable();
  stream.push(buffer);
  stream.push(null);
  return stream;
}

describe('Message Stream', function() {
  describe('reading', function() {
    [
      {
        description: 'valid OP_REPLY',
        data: Buffer.from(
          '370000000100000001000000010000000000000000000000000000000000000001000000130000001069736d6173746572000100000000',
          'hex'
        ),
        documents: [{ ismaster: 1 }]
      },
      {
        description: 'valid OP_MSG',
        data: Buffer.from(
          '370000000100000000000000dd0700000000000000220000001069736d6173746572000100000002246462000600000061646d696e0000',
          'hex'
        ),
        documents: [{ $db: 'admin', ismaster: 1 }]
      },
      {
        description: 'Invalid message size (negative)',
        data: Buffer.from('ffffffff', 'hex'),
        error: 'Invalid message size: -1'
      },
      {
        description: 'Invalid message size (exceeds maximum)',
        data: Buffer.from('01000004', 'hex'),
        error: 'Invalid message size: 67108865, max allowed: 67108864'
      }
    ].forEach(test => {
      it(test.description, function(done) {
        const bson = new BSON();
        const error = test.error;
        const inputStream = bufferToStream(test.data);
        const messageStream = new MessageStream({ bson });

        messageStream.on('message', msg => {
          if (error) {
            done(new Error(`expected error: ${error}`));
            return;
          }

          msg.parse();

          if (test.documents) {
            expect(msg)
              .to.have.property('documents')
              .that.deep.equals(test.documents);
          }

          done();
        });

        messageStream.on('error', err => {
          if (error == null) {
            done(err);
            return;
          }

          expect(err)
            .to.have.property('message')
            .that.equals(error);

          done();
        });

        inputStream.pipe(messageStream);
      });
    });
  });

  describe('writing', function() {
    it('should write a message to the stream', function(done) {
{'fraction_non_alphanumeric': 0.14042126379137412, 'fraction_numerical': 0.09669007021063189, 'mean_word_length': 2.2482084690553745, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 20, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6424465', 'n_tokens_mistral': 1768, 'n_tokens_neox': 1372, 'n_words': 289}