/* eslint-env jest */
const { execSync } = require('child_process')

describe('runjs', () => {
  let cwd

  function sh (cmd) {
    return execSync(cmd, { cwd, stdio: 'pipe' }).toString()
  }

  describe('with babel transpiler', () => {
    beforeAll(() => {
      cwd = './test/e2e/sandbox/babel'
      sh('yarn')
      sh('rm -f ./node_modules/runjs')
      sh('ln -s ../../../../../ ./node_modules/runjs')
    })

    it('executes simple task', () => {
      expect(sh('../../../../bin/run.js echo 1 2 3 --foo --bar'))
        .toContain("echo [ '1', '2', '3' ] { foo: true, bar: true }")
    })

    it('executes shell commands in a task', () => {
      expect(sh('../../../../bin/run.js commands'))
        .toContain(
          'echo "sync terminal"\nsync terminal\necho "sync pipe"\noutput sync pipe\n\n' +
          'echo "async terminal"\necho "async terminal"\nasync terminal\noutput async terminal')
    })

    it('executes name spaced tasks', () => {
      expect(sh('../../../../bin/run.js nested:echo 1 2 3 --foo --bar'))
        .toContain("echo [ '1', '2', '3' ] { foo: true, bar: true }")
    })

    it('includes ./node_modules/.bin to PATH when executing commands', () => {
      sh('cp -p ../../scripts/hello.js ./node_modules/.bin/hello')
      expect(sh('../../../../bin/run.js localbin')).toContain('Hello!')
    })

    it('executes tasks with async and await', () => {
      expect(sh('../../../../bin/run.js asyncawait'))
        .toContain('echo "async and await"\noutput async and await\n\nafter await')
    })

    it('displays help for a task', () => {
      expect(sh('../../../../bin/run.js echo --help'))
        .toContain('Simple echo task')
    })

    it('displays error from executed command', () => {
      expect(() => sh('../../../../bin/run.js error'))
        .toThrow('Command failed: ../../../../bin/run.js error')
    })
  })

  describe('with typescript transpiler', () => {
    beforeAll(() => {
      cwd = './test/e2e/sandbox/typescript'
      sh('yarn')
      sh('rm -f ./node_modules/runjs')
      sh('ln -s ../../../../../ ./node_modules/runjs')
    })

    it('executes simple task', () => {
      expect(sh('../../../../bin/run.js echo 1 2 3 --foo --bar'))
        .toContain("echo [ '1', '2', '3' ] { foo: true, bar: true }")
    })

    it('executes shell commands in a task', () => {
      expect(sh('../../../../bin/run.js commands'))
        .toContain(
          'echo "sync terminal"\nsync terminal\necho "sync pipe"\noutput sync pipe\n\n' +
          'echo "async terminal"\necho "async terminal"\nasync terminal\noutput async terminal')
    })

    it('executes name spaced tasks', () => {
      expect(sh('../../../../bin/run.js nested:echo 1 2 3 --foo --bar'))
        .toContain("echo [ '1', '2', '3' ] { foo: true, bar: true }")
    })

    it('includes ./node_modules/.bin to PATH when executing commands', () => {
      sh('cp -p ../../scripts/hello.js ./node_modules/.bin/hello')
      expect(sh('../../../../bin/run.js localbin')).toContain('Hello!')
    })

    it('executes tasks with async and await', () => {
      expect(sh('../../../../bin/run.js asyncawait'))
        .toContain('echo "async and await"\noutput async and await\n\nafter await')
    })

    it('displays error from executed command', () => {
      expect(() => sh('../../../../bin/run.js error'))
        .toThrow('Command failed: ../../../../bin/run.js error')
    })
  })
})

@@ -643,39 +643,47 @@
 , () =%3E %7B%0A      
-expect(
+const output = 
 sh('../../../../
@@ -695,32 +695,52 @@
 un.js commands')
+%0A      expect(output
 )%0A        .toCon
@@ -740,35 +740,24 @@
  .toContain(
-%0A          
 'echo %22sync 
@@ -820,56 +820,54 @@
 pipe
-%5Cn%5Cn' +%0A          'echo %22async terminal%22%5Cnecho %22
+')%0A      expect(output)%0A        .toContain('%5Cn
 asyn
@@ -876,33 +876,62 @@
 terminal
-%22
 %5Cn
-async terminal
+')%0A      expect(output)%0A        .toContain('
 %5Cnoutput
@@ -937,32 +937,34 @@
 t async terminal
+%5Cn
 ')%0A    %7D)%0A%0A    i
@@ -2380,39 +2380,47 @@
 , () =%3E %7B%0A      
-expect(
+const output = 
 sh('../../../../
@@ -2440,16 +2440,36 @@
 mmands')
+%0A      expect(output
 )%0A      
@@ -2481,27 +2481,16 @@
 Contain(
-%0A          
 'echo %22s
@@ -2557,56 +2557,54 @@
 pipe
-%5Cn%5Cn' +%0A          'echo %22async terminal%22%5Cnecho %22
+')%0A      expect(output)%0A        .toContain('%5Cn
 asyn
@@ -2617,25 +2617,54 @@
 inal
-%22
 %5Cn
-async terminal
+')%0A      expect(output)%0A        .toContain('
 %5Cnou
@@ -2682,16 +2682,18 @@
 terminal
+%5Cn
 ')%0A    %7D

Fix e2e test for async commands