/*jslint node: true */
/*global describe, it */
"use strict";

var chai = require('chai');
var util = require('util');
var serialPort = require('../serialport');

describe ('requiresComPort', function() {
  
  describe('echo hello', function() {
    it('sends hello to the last port and validates that it is received back (see arduinoEcho.ino for echo sketch)', function(done) {
      serialPort.list(function(err, ports) {

        chai.assert.isUndefined(err, util.inspect(err));
        chai.assert.isDefined(ports, 'ports is not defined');
        chai.assert.isTrue(ports.length > 0, 'no ports found');

        var data = new Buffer("hello");

        var port = new serialPort.SerialPort(ports.slice(-1)[0].comName, null, false);
        port.on('error', function(err) {
          chai.assert.fail(util.inspect(err));
        });

        port.on('data', function(d) {
          chai.assert.equal(data.toString(), d.toString(), 'incorrect data received');
          port.close(function(err) {
            chai.assert.isUndefined(err, util.inspect(err));
            done();
          });
        });

        port.open(function(err) {
          chai.assert.isUndefined(err, util.inspect(err));
          port.write(data);
        });
      });
    });
  });

  describe('relaxed baud rate', function() {
    it('opens a port with a non-standard baud rate', function(done) {
      serialPort.list(function(err, ports) {

        chai.assert.isUndefined(err, util.inspect(err));
        chai.assert.isDefined(ports, 'ports is not defined');
        chai.assert.isTrue(ports.length > 0, 'no ports found');

        var port = new serialPort.SerialPort(ports.slice(-1)[0].comName, {baudrate: 5}, false);
        port.on('error', function(err) {
          chai.assert.fail(util.inspect(err));
        });

        port.open(function(err) {
          chai.assert.isUndefined(err, util.inspect(err));
          port.close(function(err) {
            chai.assert.isUndefined(err, util.inspect(err));
            done();
          });
        });
      });
    });
  });

  describe('simple write', function() {
    it('opens a port and sends data without encountering error', function(done) {
      serialPort.list(function(err, ports) {

        chai.assert.isUndefined(err, util.inspect(err));
        chai.assert.isDefined(ports, 'ports is not defined');
        chai.assert.isTrue(ports.length > 0, 'no ports found');

        var data = new Buffer("hello");

        var port = new serialPort.SerialPort(ports.slice(-1)[0].comName, null, false);
        port.on('error', function(err) {
          chai.assert.fail(util.inspect(err));
        });

        port.open(function(err) {
          chai.assert.isUndefined(err, util.inspect(err));
          port.write(data);
          port.close(function(err) {
            chai.assert.isUndefined(err, util.inspect(err));
            done();
          });
        });
      });
    });
  });
});
@@ -2933,11 +2933,738 @@
 ;%0A  %7D);%0A
+%0A    describe('validate close event', function() %7B%0A    it('opens a port then closes it using events', function(done) %7B%0A      serialPort.list(function(err, ports) %7B%0A%0A        chai.assert.isUndefined(err, util.inspect(err));%0A        chai.assert.isDefined(ports, 'ports is not defined');%0A        chai.assert.isTrue(ports.length %3E 0, 'no ports found');%0A%0A        var port = new serialPort.SerialPort(ports.slice(-1)%5B0%5D.comName, null, false);%0A%0A        port.on('error', function(err) %7B%0A          chai.assert.fail(util.inspect(err));%0A        %7D);%0A%0A        port.on('open', function() %7B%0A          port.close();%0A        %7D);%0A%0A        port.on('close', function() %7B%0A          done();%0A        %7D);%0A%0A        port.open();%0A      %7D);%0A    %7D);%0A  %7D);%0A%0A
 %7D);

Add an events based unit test to validate that open and close work as expected