/* eslint no-unused-vars: 0 */

const mock = require('mock-fs');
const path = require('path');
const mockRepo = require('../utils').mockRepo;
const parse = require('../../src/parsers/javascript');
const github = require('../../src/github');

function toComment(fn, filename) {
  return parse(
    {
      file: filename,
      source: fn instanceof Function ? '(' + fn.toString() + ')' : fn
    },
    {}
  ).map(github);
}

const root = path.parse(__dirname).root;

function evaluate(fn) {
  return toComment(
    fn,
    root + path.join('my', 'repository', 'path', 'index.js')
  );
}

test('github', function() {
  mock(mockRepo.master);

  expect(
    evaluate(function() {
      /**
       * get one
       * @returns {number} one
       */
      function getOne() {
        return 1;
      }
    })[0].context.github
  ).toEqual({
    path: 'index.js',
    url: 'https://github.com/foo/bar/blob/this_is_the_sha/index.js#L6-L8'
  });

  mock.restore();
});

test('malformed repository', function() {
  mock(mockRepo.malformed);

  expect(
    evaluate(function() {
      /**
       * get one
       * @returns {number} one
       */
      function getOne() {
        return 1;
      }
    })[0].context.github
  ).toBe(undefined);

  mock.restore();
});

test('enterprise repository', function() {
  mock(mockRepo.enterprise);

  expect(
    evaluate(function() {
      /**
       * get one
       * @returns {number} one
       */
      function getOne() {
        return 1;
      }
    })[0].context.github
  ).toEqual({
    path: 'index.js',
    url:
      'https://github.enterprise.com/foo/bar/blob/this_is_the_sha/index.js#L6-L8'
  });

  mock.restore();
});
