require 'test_helper'

class ContributorsControllerTest < ActionController::TestCase
  def test_index_main_listing
    # Order by ncommits DESC, url_id ASC.
    expected = [[:jeremy, 3], [:david, 2], [:jose, 1], [:vijay, 1], [:xavier, 1]]

    get :index

    assert_response :success

    actual = assigns(:contributors)
    assert_equal expected.size, actual.size

    expected.zip(actual).each do |e, a|
      assert_equal contributors(e.first).name, a.name
      assert_equal e.second, a.ncommits
    end
  end

  def test_index_by_release
    releases = {
      'v3.2.0'  => [[:jeremy, 1], [:jose, 1], [:vijay, 1]],
      'v0.14.4' => [[:david, 1]]
    }

    Release.all.each do |release|
      get :index, release_id: release

      assert_response :success

      actual = assigns(:contributors)

      if expected = releases[release.tag]
        assert_equal expected.size, actual.size
        expected.zip(actual) do |e, a|
          assert_equal contributors(e.first).name, a.name
          assert_equal e.second, a.ncommits
        end
      else
        assert_equal [], actual
      end
    end
  end

  def test_in_time_window
    since      = '20121219'
    date_range = '20121201-20121231'

    time_windows = {
      'all-time'   => [[:jeremy, 3], [:david, 2], [:jose, 1], [:vijay, 1], [:xavier, 1]],
      'today'      => [[:jeremy, 1]],
      'this-week'  => [[:jeremy, 1], [:xavier, 1]],
      'this-month' => [[:david, 1], [:jeremy, 1], [:xavier, 1]],
      'this-year'  => [[:jeremy, 3], [:david, 1], [:jose, 1], [:vijay, 1], [:xavier, 1]],
      since        => [[:jeremy, 1], [:xavier, 1]],
      date_range   => [[:david, 1], [:jeremy, 1], [:xavier, 1]],
    }

    time_travel do
      time_windows.each do |time_window, expected|
        get :in_time_window, time_window: time_window

        assert_response :success

        actual = assigns(:contributors)
        assert_equal expected.size, actual.size

        expected.zip(actual).each do |e, a|
          assert_equal contributors(e.first).name, a.name
          assert_equal e.second, a.ncommits
        end
      end
    end
  end

  def test_in_edge
    # Order by ncommits DESC, url_id ASC.
    expected = [[:jeremy, 2], [:david, 1], [:xavier, 1]]

    get :in_edge

    assert_response :success

    actual = assigns(:contributors)
    assert_equal expected.size, actual.size

    expected.zip(actual).each do |e, a|
      assert_equal contributors(e.first).name, a.name
      assert_equal e.second, a.ncommits
    end
  end
end
