<commit_before>import {Meteor} from 'meteor/meteor'
import {Mongo} from 'meteor/mongo';
import {sinon} from 'meteor/practicalmeteor:sinon';
import {expect} from 'meteor/practicalmeteor:chai'
// Import and rename a variable exported by syncforce.js.
//import {SyncForce} from 'meteor/nicocrm:syncforce'
//import {SyncForce} from './syncforce-server'
import {SyncForce} from 'meteor/nicocrm:syncforce'
import Hooks from './lib/Hooks'
import CollectionSync from './CollectionSync'
import EventEmitter from 'events'

describe('SyncForce', () => {
  const fakeConnection = {}

  beforeEach(() => {
    SyncForce.getConnection = () => fakeConnection
  })

  it('should be defined', () => {
    expect(SyncForce).to.be.ok
  })

  describe('CollectionSync', () => {
    let collection, findEmitter, sobjectMock, connectionMock, sfMock

    beforeEach(() => {
      // setup default versions of the mocks...
      collection = new Mongo.Collection(null)
      findEmitter = new EventEmitter()
      findEmitter.sort = sinon.stub().returns(findEmitter)
      findEmitter.run = sinon.stub()
      sobjectMock = {
        find: sinon.stub().returns(findEmitter),
        deleted: sinon.stub().yields(null, {deletedRecords: []})
      }
      connectionMock = {
        sobject: () => sobjectMock
      }
      sfMock = {
        getConnection: () => connectionMock,
        _notifySynced: sinon.stub()
      }
    })

    it('should sync new records from SF', sinon.test(function() {
      const collectionSync = new CollectionSync(sfMock, collection, 'Account', '', {})
      const callback = sinon.stub()
      // send records when query runs
      findEmitter.run = () => {
        findEmitter.emit('record', {
          Name: 'new account'
        })
        findEmitter.emit('end')
      }
      // run it
      collectionSync.run(callback)

      // check results
      expect(callback).to.have.been.called
      expect(collection.findOne({Name: 'new account'})).to.be.ok
      expect(collectionSync.running).to.equal(false)
    }))

    it('should report error to callback', () => {
      const collectionSync = new CollectionSync(sfMock, collection, 'Account', '', {})
      const callback = sinon.stub()
      // send records when query runs
      findEmitter.run = () => {
        findEmitter.emit('error', 'oh noes')
      }
      // run it
      collectionSync.run(callback)

      // check results
      expect(callback).to.have.been.calledWith('oh noes')
    })

    // note this one can fail when the test does a large number of outputs (eg logs) for each insert call
    it('should sync large number of new records', sinon.test(function() {
      const collectionSync = new CollectionSync(sfMock, collection, 'Account', '', {})
      const callback = sinon.stub()
      // send records when query runs
      findEmitter.run = () => {
        for(let i=0; i < 10000; i++) {
          findEmitter.emit('record', {
            Name: 'new account ' + i
          })
        }
        findEmitter.emit('end')
      }
      // run it
      collectionSync.run(callback)

      // check results
      expect(callback).to.have.been.called
      expect(collection.findOne({Name: 'new account 9999'})).to.be.ok
      expect(collectionSync.running).to.equal(false)
    }))

    it('should run transform on incoming records', () => {<commit_msg>adjust timeout on test<commit_after>import {Meteor} from 'meteor/meteor'
import {Mongo} from 'meteor/mongo';
import {sinon} from 'meteor/practicalmeteor:sinon';
import {expect} from 'meteor/practicalmeteor:chai'
// Import and rename a variable exported by syncforce.js.
//import {SyncForce} from 'meteor/nicocrm:syncforce'
//import {SyncForce} from './syncforce-server'
import {SyncForce} from 'meteor/nicocrm:syncforce'
import Hooks from './lib/Hooks'
import CollectionSync from './CollectionSync'
import EventEmitter from 'events'

describe('SyncForce', () => {
  const fakeConnection = {}

  beforeEach(() => {
    SyncForce.getConnection = () => fakeConnection
  })

  it('should be defined', () => {
    expect(SyncForce).to.be.ok
  })

  describe('CollectionSync', () => {
    let collection, findEmitter, sobjectMock, connectionMock, sfMock

    beforeEach(() => {
      // setup default versions of the mocks...
      collection = new Mongo.Collection(null)
      findEmitter = new EventEmitter()
      findEmitter.sort = sinon.stub().returns(findEmitter)
      findEmitter.run = sinon.stub()
      sobjectMock = {
        find: sinon.stub().returns(findEmitter),
        deleted: sinon.stub().yields(null, {deletedRecords: []})
      }
      connectionMock = {
        sobject: () => sobjectMock
      }
      sfMock = {
        getConnection: () => connectionMock,
        _notifySynced: sinon.stub()
      }
    })

    it('should sync new records from SF', sinon.test(function() {
      const collectionSync = new CollectionSync(sfMock, collection, 'Account', '', {})
      const callback = sinon.stub()
      // send records when query runs
      findEmitter.run = () => {
        findEmitter.emit('record', {
          Name: 'new account'
        })
        findEmitter.emit('end')
      }
      // run it
      collectionSync.run(callback)

      // check results
      expect(callback).to.have.been.called
      expect(collection.findOne({Name: 'new account'})).to.be.ok
      expect(collectionSync.running).to.equal(false)
    }))

    it('should report error to callback', () => {
      const collectionSync = new CollectionSync(sfMock, collection, 'Account', '', {})
      const callback = sinon.stub()
      // send records when query runs
      findEmitter.run = () => {
        findEmitter.emit('error', 'oh noes')
      }
      // run it
      collectionSync.run(callback)

      // check results
      expect(callback).to.have.been.calledWith('oh noes')
    })

    // note this one can fail when the test does a large number of outputs (eg logs) for each insert call
    it('should sync large number of new records', sinon.test(function() {
      this.timeout(10000); // set timeout to 20 sec.
      const collectionSync = new CollectionSync(sfMock, collection, 'Account', '', {})
      const callback = sinon.stub()
      // send records when query runs
      findEmitter.run = () => {
        for(let i=0; i < 10000; i++) {
          findEmitter.emit('record', {
            Name: 'new account ' + i
          })
        }
        findEmitter.emit('end')
      }
      // run it
      collectionSync.run(callback)

      // check results
      expect(callback).to.have.been.called
      expect(collection.findOne({Name: 'new account 9999'})).to.be.ok
      expect(collectionSync.running).to.equal(false)
    }))

    it('should run transform on incoming records', () => {
{'fraction_non_alphanumeric': 0.13795663795663796, 'fraction_numerical': 0.004009504009504009, 'mean_word_length': 2.694459681843116, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, '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': '5254826', 'n_tokens_mistral': 2005, 'n_tokens_neox': 1970, 'n_words': 477}