// Generated by CoffeeScript 1.7.1
(function() {
  var buftool, crypto, doEncrypt, doZip, should, testCon, _net;

  crypto = require('crypto');

  buftool = require('buffertools');

  should = require('chai').should();

  _net = require('../index');

  doEncrypt = true;

  doZip = true;

  testCon = (function() {
    function testCon(con, token, address, API) {
      this.con = con;
      this.token = token;
      this.address = address;
      this.API = API;
      this.Key1 = crypto.getDiffieHellman('modp5');
      this.Key1.generateKeys();
      this.Key2 = crypto.getDiffieHellman('modp5');
      this.Key2.generateKeys();
      this.pubkeylocal = this.Key1.getPublicKey();
      this.pubkeyremote = this.Key2.generateKeys();
      this.secret = this.Key1.computeSecret(this.pubkeyremote);
      this.toSend = {};
      this.toJoin = {};
      this.testRead = [];
      this.testWritten = [];
      this.con = {};
      this.con['write'] = (function(_this) {
        return function(aMsg) {
          _this.testWritten.push(aMsg);
        };
      })(this);
    }

    return testCon;

  })();

  describe('class: objData(objCon, data)', function() {
    it('Raw Data', function(done) {
      var tAPI, tCon, tData, tPkt;
      tData = new Buffer(5000);
      tAPI = (function(_this) {
        return function(c, r) {
          buftool.equals(tData, r).should.equal(true);
          done();
        };
      })(this);
      tCon = new testCon(null, 'test-token', 'test.com', tAPI);
      tCon.con['write'] = (function(_this) {
        return function(aData) {
          var aPkt;
          aPkt = _net.create_objData(tCon, aData);
          aPkt.recieve();
        };
      })(this);
      tPkt = _net.create_objData(tCon, tData);
      return tPkt.pack();
    });
    it('Zipped Data', function(done) {
      var tAPI, tCon, tData, tPkt;
      tData = new Buffer(5000);
      tAPI = (function(_this) {
        return function(c, r) {
          buftool.equals(tData, r).should.equal(true);
          done();
        };
      })(this);
      tCon = new testCon(null, 'test-token', 'test.com', tAPI);
      tCon.con['write'] = (function(_this) {
        return function(aData) {
          var aPkt;
          aPkt = _net.create_objData(tCon, aData);
          aPkt.recieve();
        };
      })(this);
      tPkt = _net.create_objData(tCon, tData);
      return tPkt.pack(null, doZip);
    });
    it('Encrypted Data', function(done) {
      var tAPI, tCon, tData, tPkt;
      tData = new Buffer(5000);
      tAPI = (function(_this) {
        return function(c, r) {
          buftool.equals(tData, r).should.equal(true);
          done();
        };
      })(this);
      tCon = new testCon(null, 'test-token', 'test.com', tAPI);
      tCon.con['write'] = (function(_this) {
        return function(aData) {
          var aPkt;
          aPkt = _net.create_objData(tCon, aData);
          aPkt.recieve();
        };
      })(this);
      tPkt = _net.create_objData(tCon, tData);
      return tPkt.pack(doEncrypt, null);
    });
    it('Split/Join Data', function(done) {
      var tAPI, tCon, tData, tEnd, tPkt;
      tData = new Buffer(99999);
      tAPI = function(c, aData) {
        c.testRead.push(aData);
        buftool.equals(tData, aData).should.equal(true);
      };
      tCon = new testCon(null, 'test-token', 'test.com', tAPI);
      tEnd = (function(_this) {
        return function() {
          tCon.testRead[tCon.testRead.length - 1].length.should.equal(tData.length);
          return done();
        };
      })(this);
      setTimeout(tEnd, 15);
      tCon.con['write'] = (function(_this) {
        return function(aData) {
          var aPkt;
          aPkt = _net.create_objData(tCon, aData);
          aPkt.recieve();
        };
      })(this);
      tPkt = _net.create_objData(tCon, tData);
      return tPkt.pack(false, false, false);
    });
    return it('All the Things', function(done) {
      var tAPI, tCon, tData, tEnd, tPkt;
      tData = new Buffer(99999);
      tAPI = function(c, aData) {
        c.testRead.push(aData);
        buftool.equals(tData, aData).should.equal(true);
      };
      tCon = new testCon(null, 'test-token', 'test.com', tAPI);
      tEnd = (function(_this) {
        return function() {
          tCon.testRead[tCon.testRead.length - 1].length.should.equal(tData.length);
          return done();
        };
      })(this);
      setTimeout(tEnd, 15);
      tCon.con['write'] = (function(_this) {
        return function(aData) {
          var aPkt;
          aPkt = _net.create_objData(tCon, aData);
          aPkt.recieve();
        };
      })(this);
      tPkt = _net.create_objData(tCon, tData);
      return tPkt.pack(doEncrypt, doZip);
    });
  });

  describe('class: objCon(con, token, address, sAPI)', function() {
    var tData1, tData2;
    tData1 = new Buffer(99999);
    tData2 = new Buffer(99999);
    return it('Init > sync > xfer', function(done) {
      var __Client, __Client_API, __Client_con, __Server, __Server_API, __Server_con;
      __Server = null;
      __Server_con = null;
      __Server_API = function(c, aData) {
        buftool.equals(aData, tData1).should.equal(true);
        c.send(tData2);
      };
      __Client = null;
      __Client_con = null;
      __Client_API = function(c, aData) {
        buftool.equals(aData, tData2).should.equal(true);
        done();
      };
      return _net.server(9001, __Server_API, function(c, nodes) {
        __Server_con = c;
        __Server = nodes;
        return _net.client("localhost", 9001, __Client_API, function(c, node) {
          __Client_con = c;
          __Client = node;
          return __Client.send(tData1);
        });
      });
    });
  });

}).call(this);
