var vows       = require('vows');
var ytdl       = require('..');
var fs         = require('fs');
var path       = require('path');
var existsSync = require('fs').existsSync;
var assert     = require('assert');
var video      = 'http://www.youtube.com/watch?v=90AiXO1pAiA';


vows.describe('download').addBatch({
  'a video': {
    'topic': function() {
        var dl = ytdl.download(video, __dirname),
            cb = this.callback;

        dl.on('error', cb);

        var progress;
        dl.on('progress', function(data) {
          progress = data;
        });

        dl.on('end', function(data) {
          cb(null, progress, data);
        });
      },

    'data returned': function(err, progress, data) {
      if (err) throw err;

      assert.isObject(progress);
      assert.isString(progress.percent);
      assert.isString(progress.speed);
      assert.isString(progress.eta);

      assert.isObject(data);
      assert.equal(data.id, '90AiXO1pAiA');
      assert.equal(data.filename, 'lol-90AiXO1pAiA.flv');
      assert.equal(data.size, '1.26MiB');
      assert.isNumber(data.timeTakenms);
      assert.isString(data.timeTaken);
      assert.isNumber(data.averageSpeedBytes);
      assert.isString(data.averageSpeed);
    },

    'file was downloaded': function(err, progress, data) {
      if (err) throw err;

      process.nextTick(function() {
        // Delete file after each test.
        fs.unlink(filepath);
      });

      // Check existance.
      var filepath = path.join(__dirname, data.filename);
      assert.isTrue(existsSync(filepath));
    }
  }
}).export(module);

@@ -129,51 +129,8 @@
 ');%0A
-var existsSync = require('fs').existsSync;%0A
 var 
@@ -938,50 +938,52 @@
 ert.
-equal(data.filename, 'lol-90AiXO1pAiA.flv'
+isTrue(/lol-90AiXO1pAiA/.test(data.filename)
 );%0A 
@@ -1296,34 +1296,146 @@
     
-process.nextTick(function(
+// Check existance.%0A      var filepath = path.join(__dirname, data.filename);%0A      var exists = fs.existsSync(filepath);%0A      if (exists
 ) %7B%0A
@@ -1491,16 +1491,20 @@
 s.unlink
+Sync
 (filepat
@@ -1518,96 +1518,18 @@
    %7D
-);%0A%0A      // Check existance.%0A      var filepath = path.join(__dirname, data.filename);%0A
+ else %7B%0A  
     
@@ -1554,24 +1554,18 @@
 ists
-Sync(filepath));
+);%0A      %7D
 %0A   

make sure to delete downloaded file