import {exec} from 'child_process';

import semver from 'semver';

const {major, minor, patch} = semver.parse(atom.appVersion);
const atomVersion = `${major}.${minor}.${patch}`;
const requiredVersion = '>=1.14.0';

import GitPromptServer from '../lib/git-prompt-server';

// Will not pass on Appveyor
if (process.platform !== 'win32' && semver.satisfies(atomVersion, requiredVersion)) {
  describe('GitPromptServer', function() {
    it('prompts for user input and writes the response to stdout', async function() {
      this.timeout(10000);

      const server = new GitPromptServer();
      const {helper, socket, electron} = await server.start(question => {
        assert.equal(question, 'What... is your favorite color?\u0000');
        return 'Green. I mean blue! AAAhhhh...';
      });

      await new Promise(resolve => {
        const command = `"${electron}" "${helper}" "${socket}" "What... is your favorite color?"`;
        exec(command, {
          env: {
            ELECTRON_RUN_AS_NODE: 1,
            ELECTRON_NO_ATTACH_CONSOLE: 1,
          },
        }, (err, stdout, stderr) => {
          assert.isNull(err);
          assert.equal(stdout, 'Green. I mean blue! AAAhhhh...\n');
          resolve();
        });
      });

      await server.terminate();
    });
  });
}

@@ -780,32 +780,63 @@
 ..';%0A      %7D);%0A%0A
+      let err, stdout, stderr;%0A
       await new 
@@ -843,23 +843,33 @@
 Promise(
+(
 resolve
+, reject)
  =%3E %7B%0A  
@@ -1111,29 +1111,31 @@
     %7D, (
+_
 err, 
+_
 stdout, 
 stderr) 
@@ -1122,24 +1122,25 @@
 r, _stdout, 
+_
 stderr) =%3E %7B
@@ -1140,24 +1140,142 @@
 r) =%3E %7B%0A    
+      err = _err;%0A          stdout = _stdout;%0A          stderr = _stderr;%0A          resolve();%0A        %7D);%0A      %7D);%0A%0A
       assert
@@ -1280,13 +1280,14 @@
 rt.i
-sNull
+fError
 (err
@@ -1291,28 +1291,24 @@
 err);%0A      
-   
- 
 assert.equal
@@ -1356,51 +1356,8 @@
 n');
-%0A          resolve();%0A        %7D);%0A      %7D);
 %0A%0A  

Fix GitPromptServer test failure reporting