import { Eltro as t, assert} from 'eltro'
import fs from 'fs/promises'
import Util from '../core/util.mjs'
import { defaults } from '../core/defaults.mjs'

const isWindows = process.platform === 'win32'

t.describe('#getPathFromRoot()', function() {
  t.test('should return file relative to root', async function() {
    var util = new Util(import.meta.url)
    let path = util.getPathFromRoot('')
    if (isWindows) {
      assert.ok(path.endsWith('\\test\\'))
    } else {
      assert.ok(path.endsWith('/test/'))
    }

    path = util.getPathFromRoot('../core/http.mjs')
    if (isWindows) {
      assert.ok(path.endsWith('\\core\\http.mjs'))
    } else {
      assert.ok(path.endsWith('/core/http.mjs'))
    }

    let stat = await fs.stat(util.getPathFromRoot('../core/core.mjs'))
    assert.ok(stat.size > 0)
  })
})

t.describe('#getUrlFromRoot()', function() {
  t.test('should return an import compatible path', async function() {
    var util = new Util(import.meta.url)

    let data = await import(util.getUrlFromRoot('template.mjs'))
    assert.deepStrictEqual(data.default, { a: 1 })
  })
})

t.describe('#get7zipExecutable()', function() {
  var util = new Util(import.meta.url)

  if (process.platform === 'win32') {
    console.log('Adding 7zip windows exe path test')

    t.test('should return windows executable path', function() {
      assert.ok(util.get7zipExecutable().endsWith('\\service-core\\bin\\7zdec.exe'), `${util.get7zipExecutable()} should end with 7zdec.exe`)
    })
  } else {
    console.log('Adding 7zip linux exe path test')

    t.test('should return linux executable path', function() {
      assert.ok(util.get7zipExecutable().endsWith('/service-core/bin/7zdec'), `${util.get7zipExecutable()} should end with 7zdec`)
    })
  }
})

t.describe('#getExtension()', function() {
  var util = new Util(import.meta.url)

   t.test('should return correct extension on basic extension types', function() {
     assert.strictEqual(util.getExtension('file.7z'), '.7z')
     assert.strictEqual(util.getExtension('file.zip'), '.zip')
     assert.strictEqual(util.getExtension('file.tar'), '.tar')
     assert.strictEqual(util.getExtension('file.doc'), '.doc')
     assert.strictEqual(util.getExtension('file.rar'), '.rar')
     assert.strictEqual(util.getExtension('file.test'), '.test')
   })

   t.test('should support that annoying tar extension', function() {
    assert.strictEqual(util.getExtension('file.tar.test'), '.tar.test')
    assert.strictEqual(util.getExtension('file.tar.gz'), '.tar.gz')
    assert.strictEqual(util.getExtension('file.tar.xz'), '.tar.xz')
    assert.strictEqual(util.getExtension('file.tar.bz2'), '.tar.bz2')
   })
})

t.describe('#getApplications()', function() {
  var util = new Util(import.meta.url)

  t.test('should fail to find if not a valid object', function() {
    assert.deepStrictEqual(util.getAppNames({ app: [] }), [])
    assert.deepStrictEqual(util.getAppNames({ app: 1234 }), [])
    assert.deepStrictEqual(util.getAppNames({ app: '124124' }), [])
    assert.deepStrictEqual(util.getAppNames({ app: '' }), [])
    assert.deepStrictEqual(util.getAppNames({ app: {} }), [])
    assert.deepStrictEqual(util.getAppNames({ app: null }), [])
  })

  t.test('should return the name of the key if an object with port and provider is found', function() {
    assert.deepStrictEqual(util.getAppNames({ app: { port: 1234, provider: 'asdf' } }), ['app'])
  })

  t.test('should fail to find if port is missing or port not a number', function() {
    assert.deepStrictEqual(util.getAppNames({ app: { provider: 'asdf', } }), [])
    assert.deepStrictEqual(util.getAppNames({ app: { provider: 'asdf', port: null } }), [])
    assert.deepStrictEqual(util.getAppNames({ app: { provider: 'asdf', port: 'asdf' } }), [])
    assert.deepStrictEqual(util.getAppNames({ app: { provider: 'asdf', port: '1234' } }), [])
    assert.deepStrictEqual(util.getAppNames({ app: { provider: 'asdf', port: 0 } }), [])
    assert.deepStrictEqual(util.getAppNames({ app: { provider: 'asdf', port: [] } }), [])
    assert.deepStrictEqual(util.getAppNames({ app: { provider: 'asdf', port: {} } }), [])
  })

  t.test('should fail to find if provider is missing or not a string', function() {
    assert.deepStrictEqual(util.getAppNames({ app: { port: 1234 } }), [])
    assert.deepStrictEqual(util.getAppNames({ app: { provider: '', port: 1234 } }), [])
    assert.deepStrictEqual(util.getAppNames({ app: { provider: null, port: 1234 } }), [])
    assert.deepStrictEqual(util.getAppNames({ app: { provider: [], port: 1234 } }), [])
    assert.deepStrictEqual(util.getAppNames({ app: { provider: {}, port: 1234 } }), [])
    assert.deepStrictEqual(util.getAppNames({ app: { provider: 1234, port: 1234 } }), [])
  })

  function getBase(extra = {}) {
    return defaults({ app: extra }, { app: { provider: 'asdf', port: 1234, } })
  }

  t.test('should fail to find if https is defined but not a boolean', function() {
    assert.deepStrictEqual(util.getAppNames(getBase()), ['app'])
    assert.deepStrictEqual(util.getAppNames(getBase({ https: null })), ['app'])
    assert.deepStrictEqual(util.getAppNames(getBase({ https: false })), ['app'])
    assert.deepStrictEqual(util.getAppNames(getBase({ https: true })), ['app'])
    assert.deepStrictEqual(util.getAppNames(getBase({ https: 'asdf' })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ https: '1234' })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ https: 0 })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ https: [] })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ https: {} })), [])
  })

  t.test('should fail to find if updateEvery is defined but not a valid number', function() {
    assert.deepStrictEqual(util.getAppNames(getBase()), ['app'])
    assert.deepStrictEqual(util.getAppNames(getBase({ updateEvery: null })), ['app'])
    assert.deepStrictEqual(util.getAppNames(getBase({ updateEvery: 5 })), ['app'])
    assert.deepStrictEqual(util.getAppNames(getBase({ updateEvery: 1000 })), ['app'])
    assert.deepStrictEqual(util.getAppNames(getBase({ updateEvery: 'asdf' })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ updateEvery: '1234' })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ updateEvery: 0 })), ['app'])
    assert.deepStrictEqual(util.getAppNames(getBase({ updateEvery: -1 })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ updateEvery: -5 })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ updateEvery: [] })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ updateEvery: {} })), [])
  })

  t.test('should fail to find if startWaitUntilFail is defined but not a valid number', function() {
    assert.deepStrictEqual(util.getAppNames(getBase()), ['app'])
    assert.deepStrictEqual(util.getAppNames(getBase({ startWaitUntilFail: null })), ['app'])
    assert.deepStrictEqual(util.getAppNames(getBase({ startWaitUntilFail: 5 })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ startWaitUntilFail: 15 })), ['app'])
    assert.deepStrictEqual(util.getAppNames(getBase({ startWaitUntilFail: 1000 })), ['app'])
    assert.deepStrictEqual(util.getAppNames(getBase({ startWaitUntilFail: 'asdf' })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ startWaitUntilFail: '1234' })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ startWaitUntilFail: 0 })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ startWaitUntilFail: -5 })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ startWaitUntilFail: [] })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ startWaitUntilFail: {} })), [])
  })

  t.test('should fail to find if heartbeatTimeout is defined but not a valid number', function() {
    assert.deepStrictEqual(util.getAppNames(getBase()), ['app'])
    assert.deepStrictEqual(util.getAppNames(getBase({ heartbeatTimeout: null })), ['app'])
    assert.deepStrictEqual(util.getAppNames(getBase({ heartbeatTimeout: 5 })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ heartbeatTimeout: 15 })), ['app'])
    assert.deepStrictEqual(util.getAppNames(getBase({ heartbeatTimeout: 1000 })), ['app'])
    assert.deepStrictEqual(util.getAppNames(getBase({ heartbeatTimeout: 'asdf' })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ heartbeatTimeout: '1234' })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ heartbeatTimeout: 0 })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ heartbeatTimeout: -5 })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ heartbeatTimeout: [] })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ heartbeatTimeout: {} })), [])
  })

  t.test('should fail to find if heartbeatAttempts is defined but not a valid number', function() {
    assert.deepStrictEqual(util.getAppNames(getBase()), ['app'])
    assert.deepStrictEqual(util.getAppNames(getBase({ heartbeatAttempts: null })), ['app'])
    assert.deepStrictEqual(util.getAppNames(getBase({ heartbeatAttempts: 1 })), ['app'])
    assert.deepStrictEqual(util.getAppNames(getBase({ heartbeatAttempts: 15 })), ['app'])
    assert.deepStrictEqual(util.getAppNames(getBase({ heartbeatAttempts: 1000 })), ['app'])
    assert.deepStrictEqual(util.getAppNames(getBase({ heartbeatAttempts: 'asdf' })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ heartbeatAttempts: '1234' })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ heartbeatAttempts: 0 })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ heartbeatAttempts: -5 })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ heartbeatAttempts: [] })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ heartbeatAttempts: {} })), [])
  })

  t.test('should fail to find if heartbeatAttemptsWait is defined but not a valid number', function() {
    assert.deepStrictEqual(util.getAppNames(getBase()), ['app'])
    assert.deepStrictEqual(util.getAppNames(getBase({ heartbeatAttemptsWait: null })), ['app'])
    assert.deepStrictEqual(util.getAppNames(getBase({ heartbeatAttemptsWait: 5 })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ heartbeatAttemptsWait: 15 })), ['app'])
    assert.deepStrictEqual(util.getAppNames(getBase({ heartbeatAttemptsWait: 1000 })), ['app'])
    assert.deepStrictEqual(util.getAppNames(getBase({ heartbeatAttemptsWait: 'asdf' })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ heartbeatAttemptsWait: '1234' })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ heartbeatAttemptsWait: 0 })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ heartbeatAttemptsWait: -5 })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ heartbeatAttemptsWait: [] })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ heartbeatAttemptsWait: {} })), [])
  })

  t.test('should fail to find if clusterWaitOnCrash is defined but not a valid number', function() {
    assert.deepStrictEqual(util.getAppNames(getBase()), ['app'])
    assert.deepStrictEqual(util.getAppNames(getBase({ clusterWaitOnCrash: null })), ['app'])
    assert.deepStrictEqual(util.getAppNames(getBase({ clusterWaitOnCrash: 5 })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ clusterWaitOnCrash: 15 })), ['app'])
    assert.deepStrictEqual(util.getAppNames(getBase({ clusterWaitOnCrash: 1000 })), ['app'])
    assert.deepStrictEqual(util.getAppNames(getBase({ clusterWaitOnCrash: 'asdf' })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ clusterWaitOnCrash: '1234' })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ clusterWaitOnCrash: 0 })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ clusterWaitOnCrash: -5 })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ clusterWaitOnCrash: [] })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ clusterWaitOnCrash: {} })), [])
  })

  t.test('should fail to find if cluster is defined but not a valid number', function() {
    assert.deepStrictEqual(util.getAppNames(getBase()), ['app'])
    assert.deepStrictEqual(util.getAppNames(getBase({ cluster: null })), ['app'])
    assert.deepStrictEqual(util.getAppNames(getBase({ cluster: 1 })), ['app'])
    assert.deepStrictEqual(util.getAppNames(getBase({ cluster: 5 })), ['app'])
    assert.deepStrictEqual(util.getAppNames(getBase({ cluster: 15 })), ['app'])
    assert.deepStrictEqual(util.getAppNames(getBase({ cluster: 1000 })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ cluster: 'asdf' })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ cluster: '1234' })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ cluster: 0 })), ['app'])
    assert.deepStrictEqual(util.getAppNames(getBase({ cluster: -5 })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ cluster: [] })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ cluster: {} })), [])
  })

  t.test('should fail to find if heartbeatPath is defined but not a valid string', function() {
    assert.deepStrictEqual(util.getAppNames(getBase()), ['app'])
    assert.deepStrictEqual(util.getAppNames(getBase({ heartbeatPath: null })), ['app'])
    assert.deepStrictEqual(util.getAppNames(getBase({ heartbeatPath: 5 })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ heartbeatPath: 15 })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ heartbeatPath: 1000 })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ heartbeatPath: 'asdf' })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ heartbeatPath: '1234' })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ heartbeatPath: '/asdf' })), ['app'])
    assert.deepStrictEqual(util.getAppNames(getBase({ heartbeatPath: '/1234' })), ['app'])
    assert.deepStrictEqual(util.getAppNames(getBase({ heartbeatPath: 0 })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ heartbeatPath: -5 })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ heartbeatPath: [] })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ heartbeatPath: {} })), [])
  })
  
  t.test('should fail to find if log is defined but not an array', function() {
    assert.deepStrictEqual(util.getAppNames(getBase()), ['app'])
    assert.deepStrictEqual(util.getAppNames(getBase({ log: null })), ['app'])
    assert.deepStrictEqual(util.getAppNames(getBase({ log: 5 })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ log: 'asdf' })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ log: '1234' })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ log: 0 })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ log: [] })), ['app'])
    assert.deepStrictEqual(util.getAppNames(getBase({ log: {} })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ log: { length:1 }  })), [])
  })
  
  t.test('should fail to find if log has an item but level and either stream or path ', function() {
    assert.deepStrictEqual(util.getAppNames(getBase()), ['app'])
    assert.deepStrictEqual(util.getAppNames(getBase({ log: null })), ['app'])
    assert.deepStrictEqual(util.getAppNames(getBase({ log: [null] })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ log: [5] })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ log: [15] })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ log: [1000] })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ log: ['asdf'] })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ log: ['1234'] })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ log: ['/asdf'] })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ log: ['/1234'] })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ log: [0] })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ log: [-5] })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ log: [[]] })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ log: [{}] })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ log: [{ level: null, path: 'log' }] })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ log: [{ level: 5, path: 'log' }] })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ log: [{ level: 0, path: 'log' }] })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ log: [{ level: [], path: 'log' }] })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ log: [{ level: {}, path: 'log' }] })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ log: [{ level: '', path: 'log' }] })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ log: [{ level: 'asdf', path: 'log' }] })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ log: [{ level: 'fatal', path: 'log' }] })), ['app'])
    assert.deepStrictEqual(util.getAppNames(getBase({ log: [{ level: 'error', path: 'log' }] })), ['app'])
    assert.deepStrictEqual(util.getAppNames(getBase({ log: [{ level: 'warn', path: 'log' }] })), ['app'])
    assert.deepStrictEqual(util.getAppNames(getBase({ log: [{ level: 'info', path: 'log' }] })), ['app'])
    assert.deepStrictEqual(util.getAppNames(getBase({ log: [{ level: 'debug', path: 'log' }] })), ['app'])
    assert.deepStrictEqual(util.getAppNames(getBase({ log: [{ level: 'trace', path: 'log' }] })), ['app'])
    assert.deepStrictEqual(util.getAppNames(getBase({ log: [{ level: 'info', path: '' }] })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ log: [{ level: 'info', path: null }] })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ log: [{ level: 'info', path: 5 }] })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ log: [{ level: 'info', path: 0 }] })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ log: [{ level: 'info', path: [] }] })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ log: [{ level: 'info', path: {} }] })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ log: [{ level: 'info', stream: '' }] })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ log: [{ level: 'info', stream: null }] })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ log: [{ level: 'info', stream: 5 }] })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ log: [{ level: 'info', stream: 0 }] })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ log: [{ level: 'info', stream: [] }] })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ log: [{ level: 'info', stream: {} }] })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ log: [{ level: 'info', stream: 'asdf' }] })), [])
    assert.deepStrictEqual(util.getAppNames(getBase({ log: [{ level: 'info', stream: 'process.stdout' }] })), ['app'])
  })
})

t.describe('#verifyConfig()', function() {
  var util = new Util(import.meta.url)
  let input

  t.beforeEach(function() {
    input = {
      name: 'test',
      title: 'Test',
      description: 'Some description',
      app: { port: 1234, provider: 'asdf' },
    }
  })

  let testMissing = ['name']

  testMissing.forEach(function(check) {
    t.test(`should fail on missing ${check}`, function() {
      delete input[check]

      assert.throws(function() {
        util.verifyConfig(input)
      }, function(err) {
        assert.match(err.message, new RegExp(check))
        return true
      })
    })
  })

  let testOptional = ['title', 'description']
  testOptional.forEach(function(check) {
    t.test(`should succeed even if ${check} is missing`, function() {
      delete input[check]
      util.verifyConfig(input)
    })
  })

  t.test('should succeed if debug port is specified or null', function() {
    input.debugPort = 1234
    util.verifyConfig(input)
    input.debugPort = null
    util.verifyConfig(input)
  })

  t.test('should fail if debug port is invalid', function() {
    let checks = [
      [],
      {},
      0,
      'asdf',
      '1234'
    ]
    checks.forEach(function(check) {
      input.debugPort = check

      assert.throws(function() {
        util.verifyConfig(input)
      }, function(err) {
        assert.match(err.message, /debugPort/)
        assert.match(err.message, /number/)
        return true
      })
    })
  })

  t.test('should fail if no objects in config', function() {
    delete input.app
    assert.throws(function() {
      util.verifyConfig(input)
    }, function(err) {
      assert.match(err.message, /no/)
      assert.match(err.message, /app/)
      return true
    })
  })
})

t.describe('#extractFile()', function() {
  var util = new Util(import.meta.url)

  t.beforeEach(function() {
    return Promise.all([
      fs.rm('./test/testapp/example.tar', { force: true }),
      fs.rm('./test/testapp/file1.txt', { force: true }),
      fs.rm('./test/testapp/file2.txt', { force: true }),
    ])
  })

  t.afterEach(function() {
    return Promise.all([
      fs.rm('./test/testapp/example.tar', { force: true }),
      fs.rm('./test/testapp/file1.txt', { force: true }),
      fs.rm('./test/testapp/file2.txt', { force: true }),
    ])
  })

  t.test('should support extracting 7z file', async function() {
    await Promise.all([
      assert.isRejected(fs.stat(util.getPathFromRoot('./testapp/file1.txt'))),
      assert.isRejected(fs.stat(util.getPathFromRoot('./testapp/file2.txt'))),
    ])

    let log = ''
    try {
      await util.extractFile(util.getPathFromRoot('./testapp/example.7z'), function(msg) {
        log += msg + '\n'
      })
    } catch (err) {
      console.log(log)
      console.log(err)
      throw err
    }

    let stats = await Promise.all([
      fs.stat(util.getPathFromRoot('./testapp/file1.txt')),
      fs.stat(util.getPathFromRoot('./testapp/file2.txt')),
    ])
    assert.strictEqual(stats[0].size, 5)
    assert.strictEqual(stats[1].size, 5)
  })

  t.test('should support extracting .tar.gz file', async function() {
    await Promise.all([
      assert.isRejected(fs.stat(util.getPathFromRoot('./testapp/file1.txt'))),
      assert.isRejected(fs.stat(util.getPathFromRoot('./testapp/file2.txt'))),
    ])

    let log = ''
    try {
      await util.extractFile(util.getPathFromRoot('./testapp/example.tar.gz'), function(msg) {
        log += msg + '\n'
      })
    } catch (err) {
      console.log(log)
      console.log(err)
      throw err
    }

    let stats = await Promise.all([
      fs.stat(util.getPathFromRoot('./testapp/file1.txt')),
      fs.stat(util.getPathFromRoot('./testapp/file2.txt')),
    ])
    assert.strictEqual(stats[0].size, 5)
    assert.strictEqual(stats[1].size, 5)

    await assert.isRejected(fs.stat(util.getPathFromRoot('./testapp/example.tar')))
  })

  t.test('should stream the process of extracting', async function() {
    let output = ''
    
    try {
      await util.extractFile(util.getPathFromRoot('./testapp/example.tar.gz'), function(msg) { output += msg + '\n' })
    } catch (err) {
      console.log(output)
      console.log(err)
      throw err
    }

    assert.match(output, /file1.txt/)
    assert.match(output, /file2.txt/)
    assert.strictEqual(output.indexOf('\r\n'), -1)
  })
})
