package org.projectodd.nodej.bindings.os;

import static org.fest.assertions.Assertions.assertThat;

import java.net.UnknownHostException;

import org.dynjs.runtime.DynArray;
import org.dynjs.runtime.DynObject;
import org.dynjs.runtime.JSFunction;
import org.dynjs.runtime.Types;
import org.junit.Test;
import org.projectodd.nodej.NodejTestSupport;

public class OsFunctionsTest extends NodejTestSupport {
    @Test
    public void testOsHostname() throws UnknownHostException {
        assertThat(eval("process.binding('os').getHostname")).isInstanceOf(JSFunction.class);
        assertThat(eval("process.binding('os').getHostname()")).isEqualTo(java.net.InetAddress.getLocalHost().getHostName());
    }
    
    @Test
    public void testOsLoadavg() {
        assertThat(eval("process.binding('os').getLoadAvg")).isInstanceOf(JSFunction.class);
        assertThat(eval("process.binding('os').getLoadAvg()")).isInstanceOf(DynArray.class);
        assertThat(eval("process.binding('os').getLoadAvg().length")).isEqualTo(3L);
        assertThat(eval("process.binding('os').getLoadAvg()[0] > 0")).isEqualTo(true);
        assertThat(eval("process.binding('os').getLoadAvg()[1] > 0")).isEqualTo(true);
        assertThat(eval("process.binding('os').getLoadAvg()[2] > 0")).isEqualTo(true);
    }
    
    @Test
    public void testOSUptime() {
        assertThat(eval("process.binding('os').getUptime")).isInstanceOf(JSFunction.class);
        assertThat(eval("process.binding('os').getUptime()>0")).isEqualTo(true);
    }

    @Test
    public void testOSFreeMem() {
        assertThat(eval("process.binding('os').getFreeMem")).isInstanceOf(JSFunction.class);
        assertThat(eval("process.binding('os').getFreeMem()>0")).isEqualTo(true);
    }

    @Test
    public void testOSTotalMem() {
        assertThat(eval("process.binding('os').getTotalMem")).isInstanceOf(JSFunction.class);
        assertThat(eval("process.binding('os').getTotalMem()>0")).isEqualTo(true);
    }
    
    @Test
    public void testOSType() {
        assertThat(eval("process.binding('os').getOSType")).isInstanceOf(JSFunction.class);
        assertThat(eval("process.binding('os').getOSType()")).isNotEqualTo(Types.UNDEFINED);
    }
    
    @Test
    public void testOSRelease() {
        assertThat(eval("process.binding('os').getOSRelease")).isInstanceOf(JSFunction.class);
        assertThat(eval("process.binding('os').getOSRelease()")).isNotEqualTo(Types.UNDEFINED);
    }
    
    @Test
    public void testCpus() {
        assertThat(eval("process.binding('os').getCPUs")).isInstanceOf(JSFunction.class);
        assertThat(eval("var cpus = process.binding('os').getCPUs(); cpus")).isInstanceOf(DynArray.class);
        assertThat(eval("cpus.length > 0")).isEqualTo(true);
        assertThat(eval("cpus[0].model")).isNotEqualTo(Types.UNDEFINED);
        assertThat(eval("cpus[0].speed")).isNotEqualTo(Types.UNDEFINED);
        assertThat(eval("cpus[0].times")).isInstanceOf(DynObject.class);
        assertThat(eval("cpus[0].times.user")).isNotEqualTo(Types.UNDEFINED);
        assertThat(eval("cpus[0].times.nice")).isNotEqualTo(Types.UNDEFINED);
        assertThat(eval("cpus[0].times.sys")).isNotEqualTo(Types.UNDEFINED);
        assertThat(eval("cpus[0].times.idle")).isNotEqualTo(Types.UNDEFINED);
        assertThat(eval("cpus[0].times.irq")).isNotEqualTo(Types.UNDEFINED);
    }
    
    @Test
    public void testInterfaceAddresses() {
        assertThat(eval("process.binding('os').getInterfaceAddresses")).isInstanceOf(JSFunction.class);
        assertThat(eval("var ifaces = process.binding('os').getInterfaceAddresses(); ifaces")).isNotEqualTo(Types.UNDEFINED);
    }
    
    @Test
    public void testRequireOs() {
        assertThat(eval("require('os')")).isInstanceOf(DynObject.class);
    }
}

@@ -94,16 +94,37 @@
 tThat;%0A%0A
+import java.io.File;%0A
 import j
@@ -3808,10 +3808,1200 @@
 ;%0A    %7D%0A
+    %0A    @Test%0A    public void testOsModuleFunctions() throws UnknownHostException %7B%0A        eval(%22var os = require('os')%22);%0A        // These aren't testing the values, just that the API is at least%0A        // returning expected types%0A        assertThat(new File((String) eval(%22os.tmpDir()%22)).isDirectory()).isTrue();%0A        assertThat(eval(%22os.hostname()%22)).isEqualTo(java.net.InetAddress.getLocalHost().getHostName());%0A        assertThat(eval(%22os.type()%22)).isInstanceOf(String.class);%0A        assertThat(eval(%22os.platform()%22)).isInstanceOf(String.class);%0A        assertThat(eval(%22os.arch()%22)).isInstanceOf(String.class);%0A        assertThat(eval(%22os.release()%22)).isInstanceOf(String.class);%0A        assertThat(eval(%22os.uptime()%22)).isInstanceOf(Number.class);%0A        assertThat(eval(%22os.loadavg()%22)).isInstanceOf(DynArray.class);%0A        assertThat(eval(%22os.totalmem()%22)).isInstanceOf(Number.class);%0A        assertThat(eval(%22os.freemem()%22)).isInstanceOf(Number.class);%0A        assertThat(eval(%22os.cpus()%22)).isInstanceOf(DynArray.class);%0A        assertThat(eval(%22os.networkInterfaces()%22)).isInstanceOf(DynObject.class);%0A        assertThat(eval(%22os.EOL%22)).isInstanceOf(String.class);%0A    %7D%0A
 %7D%0A

Add tests for os.js API functions