const assert = require('assert')
const spawn = require('child_process').spawn
const hepjs = require('hep-js')
const dgram = require('dgram')
const command = '../src/captagent'
const ipserver = '127.0.0.1'
const iptarget = '127.0.0.1'

/*
 * Start Captagent, Check Exit Code
 */
describe('CaptAgent HEP Basic', () => {
  let args = [
    "-n"
  ]

  let decoded
  let network
  var sipmessage = 'SIP/2.0 200 OK\r\nVia: SIP/2.0/UDP there.com:5060\r\nFrom: LittleGuy <sip:UserB@there.com>\r\nTo: LittleGuy <sip:UserB@there.com>\r\nCall-ID: 123456789@there.com\r\nCSeq: 2 REGISTER\r\n\r\n'
  var udpmessage = new Buffer(sipmessage);
  var in_socket = dgram.createSocket('udp4')
  var out_socket = dgram.createSocket('udp4')

  before((done) => {

    let captagent = spawn(command, args);
    captagent.on('exit', () => {
	in_socket.close();
	out_socket.close();
	done()
    })

    in_socket.on('message', (message,socket) => {
      decoded = hepjs.decapsulate(message);
      network = socket;
      captagent.kill();
    })
    var sendHep = function(){
	out_socket.send(udpmessage, 0, udpmessage.length, 5060, iptarget, function(err) {
          if (err) console.log(err);
	});
    }

    in_socket.on('listening', function () {
	captagent.stdout.on('data', (data) => {
	     //if(!data.includes('ready')) return;	
	     setTimeout(sendHep, 1500);
    	})
    })
    in_socket.bind(9061, ipserver)
  })

  it('HEP should originate from 127.0.0.1', (done) => {
    assert.ok(network.address === '127.0.0.1');
    done();
  })
  it('should return HEP data', (done) => {
    assert.ok(decoded);
    done();
  })
  it('should return HEP payload', (done) => {
    assert.ok(decoded.payload.length > 0);
    done();
  })
 })


@@ -986,16 +986,55 @@
 socket;%0A
+      // console.log(network,decoded);%0A
       ca
@@ -1443,18 +1443,18 @@
 %0A%0A  it('
-HE
+UD
 P should
@@ -1568,30 +1568,119 @@
 it('
+UDP 
 should 
-return HEP data
+decode to HEP', (done) =%3E %7B%0A    assert.ok(decoded);%0A    done();%0A  %7D)%0A  it('HEP should contain rcinfo
 ', (
@@ -1703,32 +1703,39 @@
 ssert.ok(decoded
+.rcinfo
 );%0A    done();%0A 
@@ -1748,88 +1748,351 @@
 it('
+HEP 
 should 
-return HEP payload', (done) =%3E %7B%0A    assert.ok(decoded.payload.length %3E 0
+contain payload', (done) =%3E %7B%0A    assert.ok(decoded.payload.length %3E 0);%0A    done();%0A  %7D)%0A  it('HEP payload should equal sent payload', (done) =%3E %7B%0A    assert.ok(decoded.payload.length === udpmessage.length);%0A    done();%0A  %7D)%0A  it('HEP capturePass should be %22myhep%22', (done) =%3E %7B%0A    assert.ok(decoded.rcinfo.capturePass === 'myhep'
 );%0A 

extend HEP unit tests