package org.codingmatters.value.objects.js;

import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.TimeUnit;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

public class RunJsTest {

    private static ProcessBuilder processBuilder;

    @BeforeClass
    public static void setUp() throws Exception {
        String dir = System.getProperty( "project.build.directory" ) + "/js-test";
        processBuilder = new ProcessBuilder();
        processBuilder.directory( new File( dir ) );
        processBuilder.command( "hbshed", "install" );
        System.out.println( "Running 'hbshed install'" );
        Process process = processBuilder.start();
        process.waitFor( 180, TimeUnit.SECONDS );
        if( process.exitValue() != 0 ){
            printError( process );
        }
        assertThat( process.exitValue(), is( 0 ) );

        // BIND LOCAL SOURCE
        /*
        */
//        processBuilder.command( "yarn", "link", "flexio-jshelpers" );
//        System.out.println( "Running 'yarn link flexio-jshelpers'" );
//        process = processBuilder.start();
//        process.waitFor( 120, TimeUnit.SECONDS );
//        if( process.exitValue() != 0 ){
//            printError( process );
//        }
//        assertThat( process.exitValue(), is( 0 ) );
    }

    private static void printError( Process process ) throws IOException {
        byte[] buffer = new byte[1024];
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try( InputStream stream = process.getInputStream() ) {
            while( stream.read( buffer ) != -1 ){
                out.write( buffer );
            }
            System.out.println( "Out = " + new String( out.toByteArray() ) );
        }
        try( InputStream stream = process.getErrorStream() ) {
            while( stream.read( buffer ) != -1 ){
                out.write( buffer );
            }
            System.out.println( "Error = " + new String( out.toByteArray() ) );
        }
    }

//    @Ignore
    @Test
    public void whenName_then() throws Exception {
        String dir = System.getProperty( "project.build.directory" ) + "/js-test";
        System.out.println( "Running 'hbshed test' in " + dir );
        processBuilder.directory( new File( dir ) );
        processBuilder.command( "hbshed", "test", "-V" );
        Process process = null;
        try {
            process = processBuilder.start();
        } catch( Exception e ){
            e.printStackTrace();
        }
        if( process != null ){
            process.waitFor( 120, TimeUnit.SECONDS );
            if( process.exitValue() != 0 ){
                printError( process );
            }
            assertThat( process.exitValue(), is( 0 ) );
            System.out.println( "EXIT == " + process.exitValue() );
        } else {
            System.out.println( "Process is null" );
        }
    }
}

@@ -1049,445 +1049,8 @@
 ) );
-%0A%0A        // BIND LOCAL SOURCE%0A        /*%0A        */%0A//        processBuilder.command( %22yarn%22, %22link%22, %22flexio-jshelpers%22 );%0A//        System.out.println( %22Running 'yarn link flexio-jshelpers'%22 );%0A//        process = processBuilder.start();%0A//        process.waitFor( 120, TimeUnit.SECONDS );%0A//        if( process.exitValue() != 0 )%7B%0A//            printError( process );%0A//        %7D%0A//        assertThat( process.exitValue(), is( 0 ) );
 %0A   
@@ -2045,33 +2045,32 @@
 Builder.command(
- 
 %22hbshed%22, %22test%22
@@ -2073,15 +2073,8 @@
 est%22
-, %22-V%22 
 );%0A 
@@ -2102,184 +2102,33 @@
 s = 
-null;%0A        try %7B%0A            process = processBuilder.start();%0A        %7D catch( Exception e )%7B%0A            e.printStackTrace();%0A        %7D%0A        if( process != null )%7B%0A    
+processBuilder.start();%0A%0A
     
@@ -2147,17 +2147,16 @@
 waitFor(
- 
 120, Tim
@@ -2172,64 +2172,11 @@
 ONDS
- 
 );%0A
-            if( process.exitValue() != 0 )%7B%0A        
     
@@ -2194,38 +2194,18 @@
 ror(
- 
 process
- 
 );%0A
-            %7D%0A    
     
@@ -2211,33 +2211,32 @@
      assertThat(
- 
 process.exitValu
@@ -2239,29 +2239,26 @@
 Value(), is(
- 0 ) 
+0)
 );%0A         
@@ -2248,36 +2248,32 @@
 is(0));%0A        
- 
-   
 System.out.print
@@ -2275,17 +2275,16 @@
 println(
- 
 %22EXIT ==
@@ -2311,92 +2311,11 @@
 ue()
- 
 );%0A
-        %7D else %7B%0A            System.out.println( %22Process is null%22 );%0A        %7D%0A
     

fix launch hbtest