'use strict';

var _path2 = require('path');

var _path3 = _interopRequireDefault(_path2);

var _fs = require('fs');

var _fs2 = _interopRequireDefault(_fs);

var _lodash = require('lodash');

var _lodash2 = _interopRequireDefault(_lodash);

var _bluebird = require('bluebird');

var _bluebird2 = _interopRequireDefault(_bluebird);

var _yeomanTest = require('yeoman-test');

var _yeomanTest2 = _interopRequireDefault(_yeomanTest);

var _yeomanAssert = require('yeoman-assert');

var _yeomanAssert2 = _interopRequireDefault(_yeomanAssert);

var _minimatch = require('minimatch');

var _minimatch2 = _interopRequireDefault(_minimatch);

var _getExpectedFiles = require('./get-expected-files');

var getExpectedFiles = _interopRequireWildcard(_getExpectedFiles);

var _testHelpers = require('./test-helpers');

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var TEST_DIR = __dirname;

var defaultOptions = {
  script: 'js',
  transpiler: 'babel',
  markup: 'html',
  stylesheet: 'sass',
  router: 'uirouter',
  testing: 'mocha',
  chai: 'expect',
  bootstrap: true,
  uibootstrap: true,
  odms: ['mongoose'],
  auth: true,
  oauth: [],
  socketio: true
};

function runEndpointGen(name) {
  var opt = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];

  var prompts = opt.prompts || {};
  var options = opt.options || {};
  var config = opt.config;

  return new _bluebird2.default(function (resolve, reject) {
    var dir = void 0;
    var gen = _yeomanTest2.default.run(require.resolve('../generators/endpoint')).inTmpDir(function (_dir) {
      // this will create a new temporary directory for each new generator run
      var done = this.async();
      if (DEBUG) console.log('TEMP DIR: ' + _dir);
      dir = _dir;

      // symlink our dependency directories
      return _fs2.default.symlinkAsync(__dirname + '/fixtures/node_modules', dir + '/node_modules').then(done);
    }).withOptions(options).withArguments([name]).withPrompts(prompts);

    if (config) {
      gen.withLocalConfig(config);
    }

    gen.on('error', reject).on('end', function () {
      return resolve(dir);
    });
  });
}

var eslintCmd = _path3.default.join(TEST_DIR, '/fixtures/node_modules/.bin/eslint');
function testFile(command, _path) {
  _path = _path3.default.normalize(_path);
  return _fs2.default.accessAsync(_path, _fs2.default.R_OK).then(function () {
    return (0, _testHelpers.runCmd)(command + ' ' + _path);
  });
}

function eslintDir(dir, name, folder) {
  if (!folder) folder = name;
  var endpointDir = _path3.default.join(dir, 'server/api', folder);

  var regFiles = _fs2.default.readdirAsync(endpointDir).then(function (files) {
    return files.filter(function (file) {
      return (0, _minimatch2.default)(file, '**/!(*.spec|*.mock|*.integration).js', { dot: true });
    });
  }).map(function (file) {
    return testFile(eslintCmd, _path3.default.join('./server/api/', folder, file));
  });

  var specFiles = _fs2.default.readdirAsync(endpointDir).then(function (files) {
    return files.filter(function (file) {
      return (0, _minimatch2.default)(file, '**/+(*.spec|*.mock|*.integration).js', { dot: true });
    });
  }).map(function (file) {
    return testFile(eslintCmd + ' --env node,es6,mocha --global sinon,expect', _path3.default.join('./server/api/', folder, file));
  });

  return _bluebird2.default.all([regFiles, specFiles]);
}

var config;
var genDir;

describe('angular-fullstack:endpoint', function () {
  before(function () {
    return _bluebird2.default.all([(0, _testHelpers.runGen)(defaultOptions).then(function (_dir) {
      genDir = _dir;
    }), (0, _testHelpers.readJSON)(_path3.default.join(TEST_DIR, 'fixtures/.yo-rc.json')).then(function (_config) {
      _config['generator-angular-fullstack'].insertRoutes = false;
      _config['generator-angular-fullstack'].pluralizeRoutes = false;
      _config['generator-angular-fullstack'].insertSockets = false;
      _config['generator-angular-fullstack'].insertModels = false;
      config = _config;
    })]);
  });

  describe('with a generated endpont \'foo\'', function () {
    var dir;
    beforeEach(function () {
      return runEndpointGen('foo', { config: config['generator-angular-fullstack'] }).then(function (_dir) {
        dir = _dir;

        return _bluebird2.default.all([(0, _testHelpers.copyAsync)(_path3.default.join(genDir, '/.eslintrc'), './.eslintrc'), (0, _testHelpers.copyAsync)(_path3.default.join(genDir, '/server/.eslintrc'), './server/.eslintrc')]);
      });
    });

    it('should generate the expected files', function () {
      _yeomanAssert2.default.file(getExpectedFiles.endpoint('foo'));
    });

    it('should pass lint', function () {
      return eslintDir(dir, 'foo').should.be.fulfilled();
    });
  });

  describe('with a generated capitalized endpont', function () {
    var dir;
    beforeEach(function () {
      return runEndpointGen('Foo', { config: config['generator-angular-fullstack'] }).then(function (_dir) {
        dir = _dir;

        return _bluebird2.default.all([(0, _testHelpers.copyAsync)(_path3.default.join(genDir, '/.eslintrc'), './.eslintrc'), (0, _testHelpers.copyAsync)(_path3.default.join(genDir, '/server/.eslintrc'), './server/.eslintrc')]);
      });
    });

    it('should generate the expected files', function () {
      _yeomanAssert2.default.file(getExpectedFiles.endpoint('Foo'));
    });

    it('should pass lint', function () {
      return eslintDir(dir, 'Foo').should.be.fulfilled();
    });
  });

  describe('with a generated path name endpont', function () {
    var dir;
    beforeEach(function () {
      return runEndpointGen('foo/bar', { config: config['generator-angular-fullstack'] }).then(function (_dir) {
        dir = _dir;

        return _bluebird2.default.all([(0, _testHelpers.copyAsync)(_path3.default.join(genDir, '/.eslintrc'), './.eslintrc'), (0, _testHelpers.copyAsync)(_path3.default.join(genDir, '/server/.eslintrc'), './server/.eslintrc')]);
      });
    });

    it('should generate the expected files', function () {
      _yeomanAssert2.default.file(getExpectedFiles.endpoint('bar', 'foo/bar'));
    });

    it('should pass lint', function () {
      return eslintDir(dir, 'foo', 'foo/bar').should.be.fulfilled();
    });
  });

  describe('with a generated snake-case endpoint', function () {
    var dir;
    beforeEach(function () {
      return runEndpointGen('foo-bar', { config: config['generator-angular-fullstack'] }).then(function (_dir) {
        dir = _dir;

        return _bluebird2.default.all([(0, _testHelpers.copyAsync)(_path3.default.join(genDir, '/.eslintrc'), './.eslintrc'), (0, _testHelpers.copyAsync)(_path3.default.join(genDir, '/server/.eslintrc'), './server/.eslintrc')]);
      });
    });

    it('should generate the expected files', function () {
      _yeomanAssert2.default.file(getExpectedFiles.endpoint('foo-bar'));
    });

    it('should pass lint', function () {
      return eslintDir(dir, 'foo-bar').should.be.fulfilled();
    });
  });
});