<commit_before>/* @flow */

const Promise = require('bluebird')
const fs = require('fs-extra')
const _ = require('lodash')
const path = require('path')

const metadata = require('../../core/metadata')
const Pouch = require('../../core/pouch')
const RemoteCozy = require('../../core/remote/cozy')

const configHelpers = require('../../test/support/helpers/config')
const cozyHelpers = require('../../test/support/helpers/cozy')

const debug = process.env.DEBUG != null ? console.log : (...args) => {}

const createInitialTree = async function (scenario: *, cozy: *, pouch: Pouch) {
  if (!scenario.init) return
  debug('[init]')
  for (let doc of scenario.init) {
    let relpath = '/' + doc.path
    if (relpath.endsWith('/')) {
      relpath = _.trimEnd(relpath, '/') // XXX: Check in metadata.id?
      debug('- mkdir', relpath)
      const remoteDir = await cozy.files.createDirectoryByPath(relpath)
      await pouch.db.put({
        _id: metadata.id(relpath),
        docType: 'folder',
        updated_at: new Date(),
        path: relpath,
        ino: doc.ino,
        tags: [],
        sides: {local: 1, remote: 1},
        remote: {_id: remoteDir._id, _rev: remoteDir._rev}
      })
    } else {
      debug('- >', relpath)
      const parent = await cozy.files.statByPath(path.posix.dirname(relpath))
      let remoteFile = await cozy.files.create(Buffer.from(''), {
        dirID: parent._id,
        name: path.basename(relpath)
      })
      await pouch.db.put({
        _id: metadata.id(relpath),
        md5sum: '1B2M2Y8AsgTpgAmY7PhCfg==', // ''
        class: 'text',
        docType: 'file',
        executable: false,
        updated_at: new Date(),
        mime: 'text/plain',
        path: relpath,
        ino: doc.ino,
        size: 0,
        tags: [],
        sides: {local: 1, remote: 1},
        remote: {_id: remoteFile._id, _rev: remoteFile._rev}
      })
    }
  }
}

const runActions = (scenario: *, cozy: *) => {
  debug('[actions]')
  return Promise.each(scenario.actions, async (action) => {
    switch (action.type) {
      case 'mkdir':
        debug('- mkdir', action.path)
        return cozy.files.createDirectoryByPath(`/${action.path}`)

      case '>':
        debug('- >', action.path)
        {
          const parentDir = await cozy.files.statByPath(
            `/${path.posix.dirname(action.path)}`)
          return cozy.files.create('whatever', {
            name: path.posix.basename(action.path),
            dirID: parentDir._id,
            contentType: 'text/plain'
          })
        }

      case '>>':
        debug('- >>', action.path)
        {
          const remoteFile = await cozy.files.statByPath(`/${action.path}`)
          return cozy.files.updateById(remoteFile._id, 'whatever blah', {
            contentType: 'text/plain'
          })
        }

      case 'trash':
        debug('- trash', action.path)<commit_msg>dev/capture/remote: Actually append content for `>>` action<commit_after>/* @flow */

const Promise = require('bluebird')
const fs = require('fs-extra')
const _ = require('lodash')
const path = require('path')

const metadata = require('../../core/metadata')
const Pouch = require('../../core/pouch')
const RemoteCozy = require('../../core/remote/cozy')

const configHelpers = require('../../test/support/helpers/config')
const cozyHelpers = require('../../test/support/helpers/cozy')

const debug = process.env.DEBUG != null ? console.log : (...args) => {}

const createInitialTree = async function (scenario: *, cozy: *, pouch: Pouch) {
  if (!scenario.init) return
  debug('[init]')
  for (let doc of scenario.init) {
    let relpath = '/' + doc.path
    if (relpath.endsWith('/')) {
      relpath = _.trimEnd(relpath, '/') // XXX: Check in metadata.id?
      debug('- mkdir', relpath)
      const remoteDir = await cozy.files.createDirectoryByPath(relpath)
      await pouch.db.put({
        _id: metadata.id(relpath),
        docType: 'folder',
        updated_at: new Date(),
        path: relpath,
        ino: doc.ino,
        tags: [],
        sides: {local: 1, remote: 1},
        remote: {_id: remoteDir._id, _rev: remoteDir._rev}
      })
    } else {
      debug('- >', relpath)
      const parent = await cozy.files.statByPath(path.posix.dirname(relpath))
      let remoteFile = await cozy.files.create(Buffer.from(''), {
        dirID: parent._id,
        name: path.basename(relpath)
      })
      await pouch.db.put({
        _id: metadata.id(relpath),
        md5sum: '1B2M2Y8AsgTpgAmY7PhCfg==', // ''
        class: 'text',
        docType: 'file',
        executable: false,
        updated_at: new Date(),
        mime: 'text/plain',
        path: relpath,
        ino: doc.ino,
        size: 0,
        tags: [],
        sides: {local: 1, remote: 1},
        remote: {_id: remoteFile._id, _rev: remoteFile._rev}
      })
    }
  }
}

const runActions = (scenario: *, cozy: *) => {
  debug('[actions]')
  return Promise.each(scenario.actions, async (action) => {
    switch (action.type) {
      case 'mkdir':
        debug('- mkdir', action.path)
        return cozy.files.createDirectoryByPath(`/${action.path}`)

      case '>':
        debug('- >', action.path)
        {
          const parentDir = await cozy.files.statByPath(
            `/${path.posix.dirname(action.path)}`)
          return cozy.files.create('whatever', {
            name: path.posix.basename(action.path),
            dirID: parentDir._id,
            contentType: 'text/plain'
          })
        }

      case '>>':
        debug('- >>', action.path)
        {
          const remoteFile = await cozy.files.statByPath(`/${action.path}`)
          const resp = await cozy.files.downloadByPath(`/${action.path}`)
          const contents = await resp.text()
          return cozy.files.updateById(remoteFile._id, `${contents} blah`, {
            contentType: 'text/plain'
          })
        }

      case 'trash':
        debug('- trash', action.path)
{'fraction_non_alphanumeric': 0.16655450874831762, 'fraction_numerical': 0.0037012113055181696, 'mean_word_length': 2.25218818380744, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6847923', 'n_tokens_mistral': 1905, 'n_tokens_neox': 1867, 'n_words': 400}