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

import should from 'should'

import RemoteCozy, { DirectoryNotFound } from '../../../src/remote/cozy'
import configHelpers from '../../helpers/config'
import { COZY_URL, builders, deleteAll } from '../../helpers/cozy'
import CozyStackDouble from '../../doubles/cozy_stack'

const cozyStackDouble = new CozyStackDouble()

describe('RemoteCozy', function () {
  if (process.env.APPVEYOR) {
    it('is unstable on AppVeyor')
    return
  }

  before(() => cozyStackDouble.start())
  beforeEach(deleteAll)
  before('instanciate config', configHelpers.createConfig)
  before('register OAuth client', configHelpers.registerClient)
  after('clean config directory', configHelpers.cleanConfig)
  after(() => cozyStackDouble.stop())
  afterEach(() => cozyStackDouble.clearStub())

  let remoteCozy

  beforeEach(function () {
    this.config.cozyUrl = COZY_URL
    remoteCozy = new RemoteCozy(this.config)
  })

  describe('changes', function () {
    it('rejects when response status is not ok', function () {
      this.config.cozyUrl = cozyStackDouble.url()
      const remoteCozy = new RemoteCozy(this.config)

      cozyStackDouble.stub((req, res) => {
        res.writeHead(404, {'Content-Type': 'text/plain'})
        res.end('Not Found')
      })

      return should(remoteCozy.changes()).be.rejected()
    })

    it('rejects when cozy sends invalid JSON', function () {
      this.config.cozyUrl = cozyStackDouble.url()
      const remoteCozy = new RemoteCozy(this.config)

      cozyStackDouble.stub((req, res) => {
        res.writeHead(200, {'Content-Type': 'application/json'})
        res.end('')
      })

      return should(remoteCozy.changes()).be.rejected()
    })

    context('when cozy works', function () {
      context('without an update sequence', function () {
        it('lists all changes since the database creation', async function () {
          let dir = await builders.remoteDir().create()
          let file = await builders.remoteFile().inDir(dir).create()

          let { docs } = await remoteCozy.changes()
          const ids = docs.map(doc => doc._id)

          should(ids).containEql(dir._id)
          should(ids).containEql(file._id)
          should(ids.length).be.greaterThan(2)
        })
      })

      context('with an update sequence', function () {
        it('lists only changes that occured since then', async function () {
          let { last_seq } = await remoteCozy.changes()

          let dir = await builders.remoteDir().create()
          let file = await builders.remoteFile().inDir(dir).create()

          let { docs } = await remoteCozy.changes(last_seq)
          const ids = docs.map(doc => doc._id)

          should(ids.sort()).eql([file._id, dir._id].sort())
        })
      })
    })
  })

  describe('find', function () {
    it('fetches a remote directory matching the given id', async function () {
      const remoteDir = await builders.remoteDir().create()

      const foundDir = await remoteCozy.find(remoteDir._id)

      foundDir.should.be.deepEqual(remoteDir)
    })

    it('fetches a remote root file including its path', async function () {
      const remoteFile = await builders.remoteFile().inRootDir().named('foo').create()

      const foundFile = await remoteCozy.find(remoteFile._id)

      foundFile.should.deepEqual({
        ...remoteFile,
        path: '/foo'
      })
    })<commit_msg>Reorganize RemoteCozy#changes unit tests<commit_after>/* eslint-env mocha */
/* @flow weak */

import should from 'should'

import RemoteCozy, { DirectoryNotFound } from '../../../src/remote/cozy'
import configHelpers from '../../helpers/config'
import { COZY_URL, builders, deleteAll } from '../../helpers/cozy'
import CozyStackDouble from '../../doubles/cozy_stack'

const cozyStackDouble = new CozyStackDouble()

describe('RemoteCozy', function () {
  if (process.env.APPVEYOR) {
    it('is unstable on AppVeyor')
    return
  }

  before(() => cozyStackDouble.start())
  beforeEach(deleteAll)
  before('instanciate config', configHelpers.createConfig)
  before('register OAuth client', configHelpers.registerClient)
  after('clean config directory', configHelpers.cleanConfig)
  after(() => cozyStackDouble.stop())
  afterEach(() => cozyStackDouble.clearStub())

  let remoteCozy

  beforeEach(function () {
    this.config.cozyUrl = COZY_URL
    remoteCozy = new RemoteCozy(this.config)
  })

  describe('changes', function () {
    it('resolves with changes since then given seq', async function () {
      const { last_seq } = await remoteCozy.changes()

      const dir = await builders.remoteDir().create()
      const file = await builders.remoteFile().inDir(dir).create()

      const { docs } = await remoteCozy.changes(last_seq)
      const ids = docs.map(doc => doc._id)

      should(ids.sort()).eql([file._id, dir._id].sort())
    })

    it('resolves with all changes since the db creation when no seq given', async function () {
      const dir = await builders.remoteDir().create()
      const file = await builders.remoteFile().inDir(dir).create()

      const { docs } = await remoteCozy.changes()
      const ids = docs.map(doc => doc._id)

      should(ids).containEql(dir._id)
      should(ids).containEql(file._id)
      should(ids.length).be.greaterThan(2)
    })

    it('does not swallow errors', function () {
      this.config.cozyUrl = cozyStackDouble.url()
      const remoteCozy = new RemoteCozy(this.config)

      cozyStackDouble.stub((req, res) => {
        res.writeHead(500, {'Content-Type': 'text/plain'})
        res.end('whatever')
      })

      return should(remoteCozy.changes()).be.rejected()
    })
  })

  describe('find', function () {
    it('fetches a remote directory matching the given id', async function () {
      const remoteDir = await builders.remoteDir().create()

      const foundDir = await remoteCozy.find(remoteDir._id)

      foundDir.should.be.deepEqual(remoteDir)
    })

    it('fetches a remote root file including its path', async function () {
      const remoteFile = await builders.remoteFile().inRootDir().named('foo').create()

      const foundFile = await remoteCozy.find(remoteFile._id)

      foundFile.should.deepEqual({
        ...remoteFile,
        path: '/foo'
      })
    })
{'fraction_non_alphanumeric': 0.14106433677521843, 'fraction_numerical': 0.0017474185861795076, 'mean_word_length': 3.077720207253886, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 16, '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': '5421334', 'n_tokens_mistral': 1909, 'n_tokens_neox': 1838, 'n_words': 427}