<commit_before>/* @flow */
/* eslint-env mocha */

const async = require('async')
const EventEmitter = require('events')
const _ = require('lodash')
const path = require('path')
const sinon = require('sinon')
const should = require('should')
const CozyClient = require('cozy-client-js').Client

const pouchdbBuilders = require('../../support/builders/pouchdb')
const configHelpers = require('../../support/helpers/config')
const { onPlatform } = require('../../support/helpers/platform')
const pouchHelpers = require('../../support/helpers/pouch')
const { builders } = require('../../support/helpers/cozy')

const { createMetadata } = require('../../../core/conversion')
const metadata = require('../../../core/metadata')
const { FILES_DOCTYPE, ROOT_DIR_ID, TRASH_DIR_ID } = require('../../../core/remote/constants')
const Prep = require('../../../core/prep')
const { RemoteCozy } = require('../../../core/remote/cozy')
const { RemoteWatcher } = require('../../../core/remote/watcher')

const { assignId, ensureValidPath } = metadata

/*::
import type { RemoteChange } from '../../../core/remote/change'
import type { RemoteDoc, RemoteDeletion } from '../../../core/remote/document'
import type { Metadata } from '../../../core/metadata'
*/

describe('RemoteWatcher', function () {
  before('instanciate config', configHelpers.createConfig)
  before('register OAuth client', configHelpers.registerClient)
  before(pouchHelpers.createDatabase)
  before(function instanciateRemoteWatcher () {
    this.prep = sinon.createStubInstance(Prep)
    this.prep.config = this.config
    this.remoteCozy = new RemoteCozy(this.config)
    this.remoteCozy.client = new CozyClient({
      cozyURL: this.config.cozyUrl,
      token: process.env.COZY_STACK_TOKEN
    })
    this.events = new EventEmitter()
    this.watcher = new RemoteWatcher(this.pouch, this.prep, this.remoteCozy, this.events)
  })
  afterEach(function removeEventListeners () {
    this.events.removeAllListeners()
  })
  after(pouchHelpers.cleanDatabase)
  after(configHelpers.cleanConfig)

  before(function (done) {
    // TODO: promisify pouchHelpers
    pouchHelpers.createParentFolder(this.pouch, () => {
      async.eachSeries([1, 2, 3], (i, callback) => {
        pouchHelpers.createFolder(this.pouch, i, () => {
          pouchHelpers.createFile(this.pouch, i, callback)
        })
      }, done)
    })
  })

  describe('start', function () {
    beforeEach(function () {
      sinon.stub(this.watcher, 'watch').returns(Promise.resolve())
      return this.watcher.start()
    })

    afterEach(function () {
      this.watcher.watch.restore()
    })

    it('calls watch() a first time', function () {
      this.watcher.watch.callCount.should.equal(1)
    })
  })

  describe('stop', function () {
    beforeEach(function () {
      sinon.stub(this.watcher, 'watch').returns(Promise.resolve())
    })

    afterEach(function () {
      this.watcher.watch.restore()
    })<commit_msg>test/unit/remote/watcher: Promisify setup<commit_after>/* @flow */
/* eslint-env mocha */

const EventEmitter = require('events')
const _ = require('lodash')
const path = require('path')
const sinon = require('sinon')
const should = require('should')
const CozyClient = require('cozy-client-js').Client

const pouchdbBuilders = require('../../support/builders/pouchdb')
const configHelpers = require('../../support/helpers/config')
const { onPlatform } = require('../../support/helpers/platform')
const pouchHelpers = require('../../support/helpers/pouch')
const { builders } = require('../../support/helpers/cozy')

const { createMetadata } = require('../../../core/conversion')
const metadata = require('../../../core/metadata')
const { FILES_DOCTYPE, ROOT_DIR_ID, TRASH_DIR_ID } = require('../../../core/remote/constants')
const Prep = require('../../../core/prep')
const { RemoteCozy } = require('../../../core/remote/cozy')
const { RemoteWatcher } = require('../../../core/remote/watcher')

const { assignId, ensureValidPath } = metadata

/*::
import type { RemoteChange } from '../../../core/remote/change'
import type { RemoteDoc, RemoteDeletion } from '../../../core/remote/document'
import type { Metadata } from '../../../core/metadata'
*/

describe('RemoteWatcher', function () {
  before('instanciate config', configHelpers.createConfig)
  before('register OAuth client', configHelpers.registerClient)
  before(pouchHelpers.createDatabase)
  before(function instanciateRemoteWatcher () {
    this.prep = sinon.createStubInstance(Prep)
    this.prep.config = this.config
    this.remoteCozy = new RemoteCozy(this.config)
    this.remoteCozy.client = new CozyClient({
      cozyURL: this.config.cozyUrl,
      token: process.env.COZY_STACK_TOKEN
    })
    this.events = new EventEmitter()
    this.watcher = new RemoteWatcher(this.pouch, this.prep, this.remoteCozy, this.events)
  })
  afterEach(function removeEventListeners () {
    this.events.removeAllListeners()
  })
  after(pouchHelpers.cleanDatabase)
  after(configHelpers.cleanConfig)

  before(async function () {
    await pouchHelpers.createParentFolder(this.pouch)
    // FIXME: Tests pass without folder 3 & file 3
    for (let i of [1, 2, 3]) {
      await pouchHelpers.createFolder(this.pouch, i)
      await pouchHelpers.createFile(this.pouch, i)
    }
  })

  describe('start', function () {
    beforeEach(function () {
      sinon.stub(this.watcher, 'watch').returns(Promise.resolve())
      return this.watcher.start()
    })

    afterEach(function () {
      this.watcher.watch.restore()
    })

    it('calls watch() a first time', function () {
      this.watcher.watch.callCount.should.equal(1)
    })
  })

  describe('stop', function () {
    beforeEach(function () {
      sinon.stub(this.watcher, 'watch').returns(Promise.resolve())
    })

    afterEach(function () {
      this.watcher.watch.restore()
    })
{'fraction_non_alphanumeric': 0.1727909759015553, 'fraction_numerical': 0.0017091095539224065, 'mean_word_length': 3.4808575803981623, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, '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': '4794832', 'n_tokens_mistral': 1794, 'n_tokens_neox': 1800, 'n_words': 325}