'use strict';

const context = require('mocha').describe;

import assert from 'power-assert';
import nock from 'nock';

import GitHubAPI from 'github';
import {GitHubClient} from '../lib/github_client';

let sut;
describe('GitHubClient', () => {

  beforeEach(() => {
    const api = new GitHubAPI();

    api.authenticate({
      type: 'token',
      token: 'GITHUB_TOKEN'
    });

    sut = new GitHubClient(api);
  });

  it('set auth info', () => {
    assert(sut.client.auth.type === 'token');
    assert(sut.client.auth.token === 'GITHUB_TOKEN');
  });

  const ENTRY_POINT_OF_GITHUB = 'https://api.github.com/';

  describe('#getCommitsFromPullRequest', () => {

    const ENTRY_POINT_OF_PR_COMMITS = '/repos/kompiro/awesome-app/pulls/3/commits';

    context('no paging', () => {
      nock(ENTRY_POINT_OF_GITHUB).
      get(ENTRY_POINT_OF_PR_COMMITS).
      reply(200, [
        { sha: 'sha0' },
        { sha: 'sha1' }
      ]);

      it ('returns commits', (done) => {
        sut.getCommitsFromPullRequest('kompiro', 'awesome-app', 3).then((commits) => {
          assert(commits.length === 2);
          assert(commits[0].sha === 'sha0');
          done();
        }).catch(done);
      });
    });

    context('have paging', () => {
      nock(ENTRY_POINT_OF_GITHUB).
      get(ENTRY_POINT_OF_PR_COMMITS).
      reply(200, [
        { sha: 'sha0' },
        { sha: 'sha1' }
      ],{
        Link: `<https://api.github.com${ENTRY_POINT_OF_PR_COMMITS}?page=2>; rel="next",` +
        `<https://api.github.com${ENTRY_POINT_OF_PR_COMMITS}?page=2>; rel="last"`
      }).
      get(ENTRY_POINT_OF_PR_COMMITS).
      query({page: 2}).
      reply(200, [
        { sha: 'sha2' },
        { sha: 'sha3' }
      ]);

      it ('returns commits', (done) => {
        sut.getCommitsFromPullRequest('kompiro', 'awesome-app', 3).then((commits) => {
          assert(commits.length === 4);
          assert(commits[2].sha === 'sha2');
          done();
        }).catch(done);
      });
    });

  });

  describe('#fetchPullRequests', () => {

  });

});
@@ -2039,16 +2039,1240 @@
 () =%3E %7B%0A
+    const ENTRY_POINT_OF_PULL_REQUESTS = '/repos/kompiro/awesome-app/pulls';%0A%0A    context('pulls are not found', () =%3E %7B%0A      nock(ENTRY_POINT_OF_GITHUB).%0A      get(ENTRY_POINT_OF_PULL_REQUESTS).%0A      query(true).%0A      reply(200, %5B%0A        %7B number: 1, head: %7Bsha: 'sha0' %7D ,merged_at: '2017-01-08T21:56:36+09:00' %7D,%0A        %7B number: 2, head: %7Bsha: 'sha1' %7D ,merged_at: '2017-01-08T21:56:36+09:00' %7D%0A      %5D);%0A%0A      it ('cannot resolve pull request', (done) =%3E %7B%0A        sut.fetchPullRequests('kompiro', 'awesome-app', %5B%7B sha: 'not found'%7D%5D).then((prs) =%3E %7B%0A          assert(prs.length === 0);%0A          done();%0A        %7D).catch(done);%0A      %7D);%0A%0A    %7D);%0A%0A    context('pull is found', () =%3E %7B%0A      nock(ENTRY_POINT_OF_GITHUB).%0A      get(ENTRY_POINT_OF_PULL_REQUESTS).%0A      query(true).%0A      reply(200, %5B%0A        %7B number: 1, head: %7Bsha: 'sha0' %7D ,merged_at: '2017-01-08T21:56:36+09:00' %7D,%0A        %7B number: 2, head: %7Bsha: 'sha1' %7D ,merged_at: '2017-01-08T21:56:36+09:00' %7D%0A      %5D);%0A%0A      it ('resolves pull request', (done) =%3E %7B%0A        sut.fetchPullRequests('kompiro', 'awesome-app', %5B%7B sha: 'sha0'%7D%5D).then((prs) =%3E %7B%0A          assert(prs.length === 1);%0A          done();%0A        %7D).catch(done);%0A      %7D);%0A    %7D);
 %0A  %7D);%0A%0A

Define #fetchPullRequests