/* eslint func-names: 0, prefer-arrow-callback: 0 */
'use strict';

const assert = require('assert');
const jenkins = require('../lib/jenkins');

// const gh_org = process.env.JENKINS_GH_ORG;
// const gh_repo = process.env.JENKINS_GH_REPO;
// const gh_pr = parseInt(process.env.JENKINS_GH_PR, 10);

describe('jenkins', function () {
  describe('destructBuildParams()', function () {
    it('returns destructed build parameters', function () {
      const build = {
        actions: [{
          parameters: [{
            name: 'TARGET_GITHUB_ORG',
            value: 'nodejs',
          }, {
            name: 'TARGET_REPO_NAME',
            value: 'node',
          }, {
            name: 'PR_ID',
            value: '4215',
          }, {
            name: 'POST_STATUS_TO_PR',
            value: true,
          }, {
            name: 'REBASE_ONTO',
            value: 'master',
          }],
        }, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}],
        building: false,
        duration: 1504452,
        id: '969',
        timestamp: 1449695224345,
      };

      jenkins.destructBuildParams(build);

      assert.equal(typeof build.actions, 'undefined');
      assert.equal(build.org, 'nodejs');
      assert.equal(build.repo, 'node');
      assert.equal(build.pr_id, '4215');
      assert.equal(build.post_status, true);
    });
  });

  describe('getBuildsUrl()', function () {
    it.skip('returns a generator for builds url', function () {
      // console.log(jenkins.getBuildsUrl(0, 10));
    });
  });

  describe('getBuilds()', function () {
    it('loops one time', function (done) {
      this.timeout(10000);

      let count = 0;
      const limit = 10;

      jenkins.getBuilds({ limit }, (build, next) => {
        count++;
        next(null);
      }, err => {
        assert.ifError(err);
        assert.equal(count, limit);
        done();
      });
    });

    it('loops multiple times', function (done) {
      this.timeout(10000);

      let count = 0;
      const limit = 10;
      const loops = 2;

      jenkins.getBuilds({ limit }, (build, next) => {
        next(null, count++ < limit * (loops - 1) ? true : undefined);
      }, err => {
        assert.ifError(err);
        assert.equal(count, limit * loops);
        done();
      });
    });
  });

  describe('getBuild()', function () {
    it('returns build for build id', function (done) {
      this.timeout(5000);

      jenkins.getBuild(1519, (err, build) => {
        assert.ifError(err);

        assert.equal(build.building, false);
        assert.equal(build.displayName, '#1519');
        assert.equal(build.result, 'SUCCESS');
        assert.equal(typeof build.subBuilds, 'object');
        assert.equal(build.post_status, true);

        done();
      });
    });
  });
});

@@ -1394,13 +1394,8 @@
   it
-.skip
 ('re
@@ -1404,31 +1404,23 @@
 rns 
-a generator for
+jenkins
  builds 
 url'
@@ -1415,16 +1415,20 @@
  builds 
+api 
 url', fu
@@ -1449,22 +1449,20 @@
     
-// console.log
+assert.equal
 (jen
@@ -1485,16 +1485,219 @@
 l(0, 10)
+, %5B%0A        'https://ci.nodejs.org/job/node-test-pull-request/api/json',%0A        '?depth=2&tree=builds%5Bduration,timestamp,id,building,',%0A        'actions%5Bparameters%5Bname,value%5D%5D%5D%7B0,10%7D',%0A      %5D.join('')
 );%0A    %7D

implement propper getBuildsUrl test case