//    Copyright 2017 drillbits
//
//    Licensed under the Apache License, Version 2.0 (the "License");
//    you may not use this file except in compliance with the License.
//    You may obtain a copy of the License at
//
//        http://www.apache.org/licenses/LICENSE-2.0
//
//    Unless required by applicable law or agreed to in writing, software
//    distributed under the License is distributed on an "AS IS" BASIS,
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//    See the License for the specific language governing permissions and
//    limitations under the License.

const fs = require('fs');
const path = require('path');

const test = require('ava');
const pify = require('pify');
const execFile = require('child_process').execFile;

test('session', async t => {
    const stdout = await pify(execFile)('./bin/dullahan', [
        'session',
        '-e', process.env.DULLAHAN_TEST_EMAIL,
        '-p', process.env.DULLAHAN_TEST_PASSWORD,
        '-f', '0B7rtUHJTGP6DbGc5SmZNV19fdzQ',
    ]);
    let session;
    try {
        session = JSON.parse(stdout);
    } catch (err) {
        t.fail('failed to parse as json: ' + stdout);
    }
    const cookie = session.cookie;
    t.truthy(cookie, 'session has a cookie');
    t.is(cookie.name, 'DRIVE_STREAM', 'cookie name is DRIVE_STREAM');
    const streamMap = session.stream_map;
    t.truthy(streamMap, 'session has a stream_map');
});

test('download', async t => {
    const output = path.join(path.resolve(''), 'test.mp4');
    const stdout = await pify(execFile)('./bin/dullahan', [
        'download',
        '-e', process.env.DULLAHAN_TEST_EMAIL,
        '-p', process.env.DULLAHAN_TEST_PASSWORD,
        '-f', '0B7rtUHJTGP6DbGc5SmZNV19fdzQ',
        '-o', output,
    ]);

    let stat;
    try {
        stat = fs.statSync(output);
    } catch (err) {
        t.fail(`failed to open ${output}`);
    }
    t.is(stat.size, 660955);
    fs.unlinkSync(output);
});
@@ -808,29 +808,53 @@
  t =%3E %7B%0A    
-const
+let stdout;%0A    try %7B%0A       
  stdout = aw
@@ -900,16 +900,20 @@
         
+   
+ 
 'session
@@ -915,16 +915,20 @@
 ssion',%0A
+    
         
@@ -966,32 +966,36 @@
 _EMAIL,%0A        
+    
 '-p', process.en
@@ -1020,32 +1020,36 @@
 SSWORD,%0A        
+ 
+   
 '-f', '0B7rtUHJT
@@ -1070,24 +1070,28 @@
 9fdzQ',%0A
+    
     %5D);%0A
     let 
@@ -1082,16 +1082,307 @@
     %5D);%0A
+    %7D catch (err) %7B%0A        // err includes password, etc.%0A        let errMsg = (err + '')%0A            .replace(new RegExp(process.env.DULLAHAN_TEST_EMAIL, 'g'), '********')%0A            .replace(new RegExp(process.env.DULLAHAN_TEST_PASSWORD, 'g'), '********');%0A        t.fail(errMsg);%0A    %7D%0A
     let 
@@ -1863,21 +1863,45 @@
 ');%0A    
-const
+let stdout;%0A    try %7B%0A       
  stdout 
@@ -1951,16 +1951,20 @@
         
+    
 'downloa
@@ -1967,32 +1967,36 @@
 nload',%0A        
+ 
+   
 '-e', process.en
@@ -2010,32 +2010,36 @@
 HAN_TEST_EMAIL,%0A
+    
         '-p', pr
@@ -2072,32 +2072,36 @@
 SSWORD,%0A        
+   
+ 
 '-f', '0B7rtUHJT
@@ -2130,16 +2130,20 @@
         
+    
 '-o', ou
@@ -2152,19 +2152,314 @@
 ut,%0A    
-%5D);
+    %5D);%0A    %7D catch (err) %7B%0A        // err includes password, etc.%0A        let errMsg = (err + '')%0A            .replace(new RegExp(process.env.DULLAHAN_TEST_EMAIL, 'g'), '********')%0A            .replace(new RegExp(process.env.DULLAHAN_TEST_PASSWORD, 'g'), '********');%0A        t.fail(errMsg);%0A    %7D
 %0A%0A    le

mask error message