process.env.NODE_ENV = 'test';
var
  service = require( '../server' ),
  mongoose = require( 'mongoose' ),
  config = require( '../config' ).test,
  helpers = require( './helpers/helpers' ),
  chai    = require( 'chai' ),
  should  = chai.should(),
  expect  = chai.expect,
  postManifest = helpers.postManifest,
  getPackage = helpers.getPackage,
  db, models;

// Clear out test database before start
before(function ( done ) {
  db = mongoose.connection;
  models = require( '../models' )({ mongoose: mongoose, db: db });
  models.Packages.find( {} ).remove(function ( err, pkg ) {
    if ( !err ) { done(); }
  });
});

after(function ( done ) {
  models.Packages.find( {} ).remove(function ( err, pkg ) {
    if ( !err ) { done(); }
  });
});

//
// POST /packages
//

describe( 'POST /packages', function () {
  it( 'rejects manifests without a name', function ( done ) {
    postManifest( 'no-name', function ( err, res, body ) {
      validatorError( err, res, body, done );
    });
  });
  
  it( 'rejects manifests without a repo', function ( done ) {
    postManifest( 'no-repo', function ( err, res, body ) {
      validatorError( err, res, body, done );
    });  
  });

  it( 'rejects manifests without a description', function ( done ) {
    postManifest( 'no-description', function ( err, res, body ) {
      validatorError( err, res, body, done );
    });
  });

  it( 'rejects manifests without a script', function ( done ) {
    postManifest( 'no-script', function ( err, res, body ) {
      validatorError( err, res, body, done );
    });
  });

  // TODO a more robust test
  it( 'rejects manifests with names containing characters other than alpha, numeric and dashes', function ( done ) {
    postManifest( 'invalid-characters-name', function ( err, res, body ) {
      validatorError( err, res, body, done );
    });
  });

  it( 'rejects manifests with names that start with a number', function ( done ) {
    postManifest( 'starts-with-number-name', function ( err, res, body ) {
      validatorError( err, res, body, done );
    });
  });
  
  it( 'rejects manifests with names that start with a dash', function ( done ) {
    postManifest( 'starts-with-dash-name', function ( err, res, body ) {
      validatorError( err, res, body, done );
    });
  });

  it( 'rejects manifests with names less than 1 characters', function ( done ) {
    postManifest( 'less-than-one-name', function ( err, res, body ) {
      validatorError( err, res, body, done );
    });
  });
  
  it( 'rejects manifests with more than 30 characters in the name', function ( done ) {
    postManifest( '31-characters-name', function ( err, res, body ) {
      validatorError( err, res, body, done );
    });
  });

  it( 'some repo validation?', function () {
    
  });

  it( 'adds valid packages to the repo', function ( done ) {
    var count = 0;
    postManifest( 'valid-simple-reverb', function ( err, res, body ) {
      res.statusCode.should.equal( 200 );
      doneCount();
    });
    postManifest( 'valid-simple-gain', function ( err, res, body ) {
      res.statusCode.should.equal( 200 );
      doneCount();
    });
    postManifest( 'valid-simple-delay', function ( err, res, body ) {
      res.statusCode.should.equal( 200 );
      doneCount();
    });

    // TODO use async module
    function doneCount () { count++; if ( count === 3 ) { done(); } }
  });
});

describe( 'GET /packages', function () {

  it( 'returns all available packages', function ( done ) {
    getPackage(function ( err, res, body ) {
      expect( err ).to.be.falsey;
      body.should.have.length( 3 );
      done();
    });
  });

  it( 'returns available packages in alphabetical order', function ( done ) {
    done();
  });
});


describe( 'GET /packages/:name', function () {

});

describe( 'GET /packages/search/:name', function () {

});

function validatorError ( err, res, body, done ) {
  res.statusCode.should.equal( 400 );
  body.error.should.contain( 'ValidatorError' );
  done();
}

@@ -3365,24 +3365,363 @@
 ); %7D %7D%0A  %7D);
+%0A%0A  // TODO probably should return a 400 with a better message!!%0A  it( 'rejects a manifest with the same name as an existing one', function ( done ) %7B%0A    postManifest( 'valid-simple-reverb', function ( err, res, body ) %7B%0A      res.statusCode.should.equal( 500 );%0A      body.error.should.contain( 'duplicate' );%0A      done();%0A    %7D);%0A  %7D);
 %0A%7D);%0A%0Adescri

Check for duplicates