const path = require('path')
const test = require('ava')
const analyzePath = require('..')

const options = {
  digestAlgorithm: 'md5',
  logger: {
    log: () => {}
  }
}

test('should not update token if already analyzed', async t => {
  const token = {
    analyzed: true
  }
  const save = Object.assign({}, token)

  const ret = await analyzePath(token)

  t.is(ret, false)
  t.deepEqual(token, save)
})

test('should throw if the file is not found', async t => {
  const token = {
    path: path.resolve(__dirname, 'fixtures/not-found.txt')
  }

  await t.throws(analyzePath(token, options), /ENOENT: no such file or directory/)
})

test('should analyze a text file completely', async t => {
  const filePath = path.resolve(__dirname, 'fixtures/file.txt')

  const token = {
    path: filePath
  }

  await analyzePath(token, options)

  t.deepEqual(token, {
    digest: 'md5-yJfRQQr48sdPuhGx21Eeng==',
    fileName: 'file.txt',
    fileSize: 13,
    fileTypes: [{
      ext: 'txt',
      mime: 'text/plain',
      source: 'path:filename'
    }],
    analyzed: true,
    path: filePath,
    type: 'file'
  })
})

test('should analyze a directory completely', async t => {
  const filePath = path.resolve(__dirname, 'fixtures/directory')

  const token = {
    path: filePath
  }

  await analyzePath(token, options)

  t.deepEqual(token, {
    children: [
      {
        filePath: '1.txt',
        inputType: 'path',
        path: path.join(filePath, '1.txt')
      },
      {
        filePath: '2.txt',
        inputType: 'path',
        path: path.join(filePath, '2.txt')
      }
    ],
    analyzed: true,
    path: filePath,
    type: 'directory'
  })
})

test('should error on unknown file types (symlink)', async t => {
  const token = {
    path: path.resolve(__dirname, 'fixtures/link.txt')
  }

  await t.throws(analyzePath(token, options, 'Unsupported file type'))
})

test('should remove the stats from the token on error', async t => {
  const token = {
    path: path.resolve(__dirname, 'fixtures/link.txt')
  }

  await t.throws(analyzePath(token, options, 'Unsupported file type'))

  t.is(token.stats, undefined)
})

@@ -32,27 +32,96 @@
 nst 
-test = require('ava
+%7Bpromisify%7D = require('util')%0Aconst test = require('ava')%0Aconst rimraf = require('rimraf
 ')%0Ac
@@ -154,16 +154,46 @@
 ('..')%0A%0A
+const rm = promisify(rimraf)%0A%0A
 const op
@@ -258,19 +258,44 @@
 ) =%3E %7B%7D%0A
-
   %7D
+,%0A  extractArchives: true
 %0A%7D%0A%0Atest
@@ -1776,32 +1776,785 @@
 ctory'%0A  %7D)%0A%7D)%0A%0A
+test('should analyze an archive completely', async t =%3E %7B%0A  const filePath = path.resolve(__dirname, 'fixtures/file.zip')%0A%0A  const token = %7B%0A    path: filePath%0A  %7D%0A%0A  await analyzePath(token, options)%0A%0A  const tmp = token.temporary%0A%0A  t.deepEqual(token, %7B%0A    children: %5B%0A      %7B%0A        inputType: 'path',%0A        filePath: 'file.zip',%0A        path: path.join(tmp, '_unarchived')%0A      %7D%0A    %5D,%0A    analyzed: true,%0A    path: filePath,%0A    fileTypes: %5B%0A      %7B%0A        ext: 'zip',%0A        mime: 'application/zip',%0A        source: 'path:chunk'%0A      %7D,%0A      %7B%0A        ext: 'zip',%0A        mime: 'application/zip',%0A        source: 'path:filename'%0A      %7D%0A    %5D,%0A    temporary: tmp,%0A    fileName: 'file.zip',%0A    type: 'archive'%0A  %7D)%0A%0A  return rm(tmp)%0A%7D)%0A%0A
 test('should err

Add complete archive test