<commit_before>/* @flow */

import cozy from 'cozy-client-js'
import EventEmitter from 'events'
import pick from 'lodash.pick'
import sinon from 'sinon'

import Config from '../../src/config'
import Ignore from '../../src/ignore'
import Local from '../../src/local'
import Merge from '../../src/merge'
import Pouch from '../../src/pouch'
import Prep from '../../src/prep'
import Remote from '../../src/remote'
import Sync from '../../src/sync'

import { LocalTestHelpers } from './local'
import { RemoteTestHelpers } from './remote'

export class IntegrationTestHelpers {
  local: LocalTestHelpers
  remote: RemoteTestHelpers
  prep: Prep

  _pouch: Pouch
  _sync: Sync
  _local: Local
  _remote: Remote

  constructor (config: Config, pouch: Pouch, cozyClient: cozy.Client) {
    const merge = new Merge(pouch)
    const ignore = new Ignore([])
    this.prep = new Prep(merge, ignore, config)
    const events = new EventEmitter()
    this._local = merge.local = new Local(config, this.prep, pouch, events)
    this._remote = merge.remote = new Remote(config, this.prep, pouch, events)
    this._remote.remoteCozy.client = cozyClient
    this._sync = new Sync(pouch, this._local, this._remote, ignore, events)
    this._sync.stopped = false
    this._pouch = pouch
    this.local = new LocalTestHelpers(this._local)
    this.remote = new RemoteTestHelpers(this._remote)
  }

  async syncAll () {
    const seq = await this._pouch.getLocalSeqAsync()
    const changes = await this._pouch.db.changes({
      since: seq,
      include_docs: true,
      filter: '_view',
      view: 'byPath'
    })

    for (let change of changes.results) {
      await this._sync.apply(change)
    }
  }

  spyPouch () {
    sinon.spy(this._pouch, 'put')
    sinon.spy(this._pouch, 'bulkDocs')
  }

  putDocs (...props: string[]) {
    const results = []

    for (const args of this._pouch.bulkDocs.args) {
      for (const doc of args[0]) {
        results.push(pick(doc, props))
      }
    }

    for (const args of this._pouch.put.args) {
      const doc = args[0]
      results.push(pick(doc, props))
    }

    return results
  }

  pullChange (id: string) {
    return this._remote.watcher.pullOne(id)
  }
}<commit_msg>Remove old useless pullChange() test helper<commit_after>/* @flow */

import cozy from 'cozy-client-js'
import EventEmitter from 'events'
import pick from 'lodash.pick'
import sinon from 'sinon'

import Config from '../../src/config'
import Ignore from '../../src/ignore'
import Local from '../../src/local'
import Merge from '../../src/merge'
import Pouch from '../../src/pouch'
import Prep from '../../src/prep'
import Remote from '../../src/remote'
import Sync from '../../src/sync'

import { LocalTestHelpers } from './local'
import { RemoteTestHelpers } from './remote'

export class IntegrationTestHelpers {
  local: LocalTestHelpers
  remote: RemoteTestHelpers
  prep: Prep

  _pouch: Pouch
  _sync: Sync
  _local: Local
  _remote: Remote

  constructor (config: Config, pouch: Pouch, cozyClient: cozy.Client) {
    const merge = new Merge(pouch)
    const ignore = new Ignore([])
    this.prep = new Prep(merge, ignore, config)
    const events = new EventEmitter()
    this._local = merge.local = new Local(config, this.prep, pouch, events)
    this._remote = merge.remote = new Remote(config, this.prep, pouch, events)
    this._remote.remoteCozy.client = cozyClient
    this._sync = new Sync(pouch, this._local, this._remote, ignore, events)
    this._sync.stopped = false
    this._pouch = pouch
    this.local = new LocalTestHelpers(this._local)
    this.remote = new RemoteTestHelpers(this._remote)
  }

  async syncAll () {
    const seq = await this._pouch.getLocalSeqAsync()
    const changes = await this._pouch.db.changes({
      since: seq,
      include_docs: true,
      filter: '_view',
      view: 'byPath'
    })

    for (let change of changes.results) {
      await this._sync.apply(change)
    }
  }

  spyPouch () {
    sinon.spy(this._pouch, 'put')
    sinon.spy(this._pouch, 'bulkDocs')
  }

  putDocs (...props: string[]) {
    const results = []

    for (const args of this._pouch.bulkDocs.args) {
      for (const doc of args[0]) {
        results.push(pick(doc, props))
      }
    }

    for (const args of this._pouch.put.args) {
      const doc = args[0]
      results.push(pick(doc, props))
    }

    return results
  }
}
{'fraction_non_alphanumeric': 0.13589567604667124, 'fraction_numerical': 0.0009151223976206818, 'mean_word_length': 2.8931433659839714, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4204325', 'n_tokens_mistral': 1433, 'n_tokens_neox': 1416, 'n_words': 392}