var assert  = require('assert');
var path    = require('path');
// var fs      = require('fs');
// var _       = require('lodash');
var Manager = require('../lib/core/manager');
var rimraf  = require('rimraf');
var config  = require('../lib/core/config');

describe('manager', function () {
  beforeEach(function (done) {
    rimraf(config.directory, function (err) {
      if (err) {
        throw new Error('Unable to delete local directory.');
      }
      done();
    });
  });

  it('Should resolve JSON dependencies', function (next) {
    var manager = new Manager([]);
    manager.cwd = __dirname + '/assets/project';

    manager.on('resolve', function () {
      assert.deepEqual(manager.dependencies["jquery-ui"][0].version, "1.8.23");
      assert.deepEqual(manager.dependencies["jquery"][0].version, "1.8.1");
      assert.deepEqual(manager.dependencies["package-bootstrap"][0].version, "2.0.6");

      rimraf(config.directory, function (err) {
        next();
      });
    });

    manager.resolve();

  });

  it('Should resolve nested JSON dependencies', function (next) {
    var manager = new Manager([]);
    manager.cwd = __dirname + '/assets/project-nested';

    manager.on('resolve', function () {
      assert.deepEqual(manager.dependencies["jquery"][0].version, "1.7.2");
      assert.deepEqual(manager.dependencies["jquery-pjax"][0].version, "1.0.0");

      rimraf(config.directory, function (err) {
        next();
      });
    });

    manager.resolve();
  });

  it('Should detect unresolvable packages in nested JSON dependencies', function (next) {
    var manager = new Manager([]);
    manager.cwd = __dirname + '/assets/project-nested-conflict';

    var detected = false;
    manager.on('error', function (err) {
      if (/no resolvable.* jquery$/i) detected = true;
    });
    manager.on('resolve', function () {
      if (!detected) throw new Error('A conflict in jquery should have been detected.');
      next();
    });

    manager.resolve();
  });
});

@@ -248,16 +248,49 @@
 onfig');
+%0Avar semver  = require('semver');
 %0A%0Adescri
@@ -703,33 +703,37 @@
      assert.
-deepEqual
+ok(semver.gte
 (manager.dep
@@ -753,11 +753,8 @@
 uery
--ui
 %22%5D%5B0
@@ -773,11 +773,11 @@
 1.8.
-23%22
+1%22)
 );%0A 
@@ -784,33 +784,37 @@
      assert.
-deepEqual
+ok(semver.gte
 (manager.dep
@@ -816,38 +816,49 @@
 r.dependencies%5B%22
-jquery
+package-bootstrap
 %22%5D%5B0%5D.version, %22
@@ -857,22 +857,23 @@
 rsion, %22
-1.8.1%22
+2.0.0%22)
 );%0A     
@@ -876,33 +876,37 @@
      assert.
-deepEqual
+ok(semver.gte
 (manager.dep
@@ -916,33 +916,25 @@
 encies%5B%22
-package-bootstrap
+jquery-ui
 %22%5D%5B0%5D.ve
@@ -945,17 +945,17 @@
 n, %22
-2.0.6%22);%0A
+1.8.0%22));
 %0A   

Tweak tests so they don't break in the future.