<commit_before>require 'rails_helper'

cohort_course_count = 10

describe 'the home page', type: :feature do
  before do
    cohort = create(:cohort)
    cohort_two = create(:cohort_two)

    (1..cohort_course_count).each do |i|
      course1 = create(:course,
                       id: i,
                       slug: "school/course_#{i}_(term)",
                       start: '2014-01-01'.to_date,
                       end: Date.today + 2.days)
      course1.cohorts << cohort
      course2 = create(:course,
                       id: (i + cohort_course_count),
                       slug: "school/course_#{i}_(term)",
                       start: '2014-01-01'.to_date,
                       end: Date.today + 2.days)
      course2.cohorts << cohort_two

      # STUDENTS, one per course
      create(:user, id: i, trained: true)
      create(:courses_user,
             id: i,
             course_id: i,
             user_id: i,
             role: CoursesUsers::Roles::STUDENT_ROLE)

      # INSTRUCTORS, one per course
      create(:user, id: i + cohort_course_count, trained: true)
      create(:courses_user,
             id: i + cohort_course_count,
             course_id: i,
             user_id: i + cohort_course_count,
             role: CoursesUsers::Roles::INSTRUCTOR_ROLE)
      # The instructors are also enrolled as students.
      create(:courses_user,
             id: i + cohort_course_count * 2,
             course_id: i,
             user_id: i + cohort_course_count,
             role: CoursesUsers::Roles::STUDENT_ROLE)

      article = create(:article,
                       id: i,
                       title: 'Selfie',
                       namespace: 0)
      create(:articles_course,
             course_id: course1.id,
             article_id: article.id)
      create(:revision,
             id: i,
             user_id: i,
             article_id: i,
             date: 6.days.ago,
             characters: 9000)
    end
    Course.update_all_caches
  end

  before :each do
    if page.driver.is_a?(Capybara::Webkit::Driver)
      page.driver.allow_url 'fonts.googleapis.com'
      page.driver.allow_url 'maxcdn.bootstrapcdn.com'
      page.driver.allow_url 'cdn.ravenjs.com'
      # page.driver.block_unknown_urls  # suppress warnings
    end
  end

  describe 'header' do
    it 'should display stats accurately' do
      visit root_path

      # Number of courses
      course_count = Cohort.first.courses.count
      stat_text = "#{course_count} #{I18n.t('courses.course_description')}"
      expect(page.find('.stat-display')).to have_content stat_text

      # Number of students
      # one non-instructor student per course
      student_count = cohort_course_count
      stat_text = "#{student_count} #{I18n.t('courses.students')}"
      expect(page.find('.stat-display')).to have_content stat_text

      # Characters added
      character_count = Course.all.sum(:character_sum)
      stat_text = "#{character_count} #{I18n.t('metrics.char_added')}"
      expect(page.find('.stat-display')).to have_content stat_text

      # Views
      view_count = Course.all.sum(:view_sum)
      stat_text = "#{view_count} #{I18n.t('metrics.view_count_description')}"
      expect(page.find('.stat-display')).to have_content stat_text
    end
  end

  describe 'control bar' do
    it 'should allow sorting via dropdown and loading of cohorts', js: true do
      visit root_path

      # sorting via dropdown
      find('select.sorts').find(:xpath, 'option[2]').select_option
      expect(page).to have_selector('.course-list__row__revisions.sort.desc')
      find('select.sorts').find(:xpath, 'option[3]').select_option
      expect(page).to have_selector('.course-list__row__characters.sort.desc')
      find('select.sorts').find(:xpath, 'option[4]').select_option
      expect(page).to have_selector('.course-list__row__views.sort.desc')
      find('select.sorts').find(:xpath, 'option[5]').select_option
      expect(page).to have_selector('.course-list__row__students.sort.desc')
      find('select.sorts').find(:xpath, 'option[1]').select_option
      expect(page).to have_selector('.course-list__row__title.sort.asc')

      # loading a different cohort
      expect(page).to have_content(Cohort.first.title)
      find('select.cohorts').find(:xpath, 'option[2]').select_option
      expect(page).to have_content(Cohort.last.title)
    end
  end

  describe 'course list' do
    it 'should be sortable', js: true do
      visit root_path

      # Sortable by title
      expect(page).to have_selector('.course-list__row__title.sort.asc')
      find('.course-list__row__title.sort').trigger('click')
      expect(page).to have_selector('.course-list__row__title.sort.desc')

      # Sortable by character count
      find('.course-list__row__characters.sort').trigger('click')
      expect(page).to have_selector('.course-list__row__characters.sort.desc')
      find('.course-list__row__characters.sort').trigger('click')
      expect(page).to have_selector('.course-list__row__characters.sort.asc')

      # Sortable by view count
      find('.course-list__row__views.sort').trigger('click')
      expect(page).to have_selector('.course-list__row__views.sort.desc')
      find('.course-list__row__views.sort').trigger('click')
      expect(page).to have_selector('.course-list__row__views.sort.asc')

      # Sortable by student count
      find('.course-list__row__students.sort').trigger('click')
      expect(page).to have_selector('.course-list__row__students.sort.desc')
      find('.course-list__row__students.sort').trigger('click')
      expect(page).to have_selector('.course-list__row__students.sort.asc')
    end
  end

  describe 'course rows' do
    it 'should allow navigation to a course page', js: true do
      visit root_path

      within 'ul.list' do
        find('.course-list__row:first-child a').click
      end
      expect(current_path).to eq("/courses/#{Course.first.slug}")
    end

    it 'contains the number of revisions in past 7 days' do
      visit root_path

      row = '.course-list__row:first-child ' \
            '.course-list__row__revisions p.revisions'
      within(row) do
        expect(page.text).to eq('1')
      end
    end
  end

  describe 'cohort pages' do
    # This will fail unless there are at least two cohorts in application.yml.
    it 'should load courses from the right cohorts' do
      visit root_path

      all('.course-list__row > a').each do |course_row_anchor|
        expect(course_row_anchor[:id].to_i).to be <= cohort_course_count
      end

      # This will fail unless there are at least two cohorts in application.yml.
      # load courses from a different cohort
      visit "/courses?cohort=#{Cohort.last.slug}"
      all('.course-list__row > a').each do |course_row_anchor|
        expect(course_row_anchor[:id].to_i).to be > cohort_course_count
      end
    end
  end

  describe 'non-default locales' do
    it 'should switch languages' do
      visit '/courses?locale=qqq'
      expect(page.find('header')).to have_content 'Long label for the number'
    end
  end
end<commit_msg>Refactor homepage spec to reduce page loads<commit_after>require 'rails_helper'

cohort_course_count = 10

describe 'the home page', type: :feature do
  before do
    cohort = create(:cohort)
    cohort_two = create(:cohort_two)

    (1..cohort_course_count).each do |i|
      course1 = create(:course,
                       id: i,
                       slug: "school/course_#{i}_(term)",
                       start: '2014-01-01'.to_date,
                       end: Date.today + 2.days)
      course1.cohorts << cohort
      course2 = create(:course,
                       id: (i + cohort_course_count),
                       slug: "school/course_#{i}_(term)",
                       start: '2014-01-01'.to_date,
                       end: Date.today + 2.days)
      course2.cohorts << cohort_two

      # STUDENTS, one per course
      create(:user, id: i, trained: true)
      create(:courses_user,
             id: i,
             course_id: i,
             user_id: i,
             role: CoursesUsers::Roles::STUDENT_ROLE)

      # INSTRUCTORS, one per course
      create(:user, id: i + cohort_course_count, trained: true)
      create(:courses_user,
             id: i + cohort_course_count,
             course_id: i,
             user_id: i + cohort_course_count,
             role: CoursesUsers::Roles::INSTRUCTOR_ROLE)
      # The instructors are also enrolled as students.
      create(:courses_user,
             id: i + cohort_course_count * 2,
             course_id: i,
             user_id: i + cohort_course_count,
             role: CoursesUsers::Roles::STUDENT_ROLE)

      article = create(:article,
                       id: i,
                       title: 'Selfie',
                       namespace: 0)
      create(:articles_course,
             course_id: course1.id,
             article_id: article.id)
      create(:revision,
             id: i,
             user_id: i,
             article_id: i,
             date: 6.days.ago,
             characters: 9000)
    end
    Course.update_all_caches
  end

  before :each do
    if page.driver.is_a?(Capybara::Webkit::Driver)
      page.driver.allow_url 'fonts.googleapis.com'
      page.driver.allow_url 'maxcdn.bootstrapcdn.com'
      page.driver.allow_url 'cdn.ravenjs.com'
      # page.driver.block_unknown_urls  # suppress warnings
    end
  end

  describe 'header' do
    it 'should display stats accurately' do
      visit root_path

      # Number of courses
      course_count = Cohort.first.courses.count
      stat_text = "#{course_count} #{I18n.t('courses.course_description')}"
      expect(page.find('.stat-display')).to have_content stat_text

      # Number of students
      # one non-instructor student per course
      student_count = cohort_course_count
      stat_text = "#{student_count} #{I18n.t('courses.students')}"
      expect(page.find('.stat-display')).to have_content stat_text

      # Characters added
      character_count = Course.all.sum(:character_sum)
      stat_text = "#{character_count} #{I18n.t('metrics.char_added')}"
      expect(page.find('.stat-display')).to have_content stat_text

      # Views
      view_count = Course.all.sum(:view_sum)
      stat_text = "#{view_count} #{I18n.t('metrics.view_count_description')}"
      expect(page.find('.stat-display')).to have_content stat_text

      # Recent revisions
      row = '.course-list__row:first-child ' \
            '.course-list__row__revisions p.revisions'
      within(row) do
        expect(page.text).to eq('1')
      end
    end
  end

  describe 'control bar' do
    it 'should allow sorting via dropdown and loading of cohorts', js: true do
      visit root_path

      # sorting via dropdown
      find('select.sorts').find(:xpath, 'option[2]').select_option
      expect(page).to have_selector('.course-list__row__revisions.sort.desc')
      find('select.sorts').find(:xpath, 'option[3]').select_option
      expect(page).to have_selector('.course-list__row__characters.sort.desc')
      find('select.sorts').find(:xpath, 'option[4]').select_option
      expect(page).to have_selector('.course-list__row__views.sort.desc')
      find('select.sorts').find(:xpath, 'option[5]').select_option
      expect(page).to have_selector('.course-list__row__students.sort.desc')
      find('select.sorts').find(:xpath, 'option[1]').select_option
      expect(page).to have_selector('.course-list__row__title.sort.asc')

      # loading a different cohort
      expect(page).to have_content(Cohort.first.title)
      find('select.cohorts').find(:xpath, 'option[2]').select_option
      expect(page).to have_content(Cohort.last.title)
    end
  end

  describe 'course list' do
    it 'should be sortable', js: true do
      visit root_path

      # Sortable by title
      expect(page).to have_selector('.course-list__row__title.sort.asc')
      find('.course-list__row__title.sort').trigger('click')
      expect(page).to have_selector('.course-list__row__title.sort.desc')

      # Sortable by character count
      find('.course-list__row__characters.sort').trigger('click')
      expect(page).to have_selector('.course-list__row__characters.sort.desc')
      find('.course-list__row__characters.sort').trigger('click')
      expect(page).to have_selector('.course-list__row__characters.sort.asc')

      # Sortable by view count
      find('.course-list__row__views.sort').trigger('click')
      expect(page).to have_selector('.course-list__row__views.sort.desc')
      find('.course-list__row__views.sort').trigger('click')
      expect(page).to have_selector('.course-list__row__views.sort.asc')

      # Sortable by student count
      find('.course-list__row__students.sort').trigger('click')
      expect(page).to have_selector('.course-list__row__students.sort.desc')
      find('.course-list__row__students.sort').trigger('click')
      expect(page).to have_selector('.course-list__row__students.sort.asc')
    end
  end

  describe 'course rows' do
    it 'should allow navigation to a course page', js: true do
      visit root_path

      within 'ul.list' do
        find('.course-list__row:first-child a').click
      end
      expect(current_path).to eq("/courses/#{Course.first.slug}")
    end
  end

  describe 'cohort pages' do
    # This will fail unless there are at least two cohorts in application.yml.
    it 'should load courses from the right cohorts' do
      visit root_path

      all('.course-list__row > a').each do |course_row_anchor|
        expect(course_row_anchor[:id].to_i).to be <= cohort_course_count
      end

      # This will fail unless there are at least two cohorts in application.yml.
      # load courses from a different cohort
      visit "/courses?cohort=#{Cohort.last.slug}"
      all('.course-list__row > a').each do |course_row_anchor|
        expect(course_row_anchor[:id].to_i).to be > cohort_course_count
      end
    end
  end

  describe 'non-default locales' do
    it 'should switch languages' do
      visit '/courses?locale=qqq'
      expect(page.find('header')).to have_content 'Long label for the number'
    end
  end
end
{'fraction_non_alphanumeric': 0.118688293370945, 'fraction_numerical': 0.006840620592383639, 'mean_word_length': 2.6166794185156848, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5193556', 'n_tokens_mistral': 4518, 'n_tokens_neox': 4286, 'n_words': 893}