<commit_before># frozen_string_literal: true
require 'rails_helper'

describe 'Surveys', type: :feature, js: true do
  include Rapidfire::QuestionSpecHelper
  include Rapidfire::AnswerSpecHelper

  before do
    include Devise::TestHelpers, type: :feature
    Capybara.current_driver = :poltergeist
    page.current_window.resize_to(1920, 1080)
  end

  # describe 'The Survey index' do
  #   before :each do
  #     user = create(:admin,
  #                   id: 200,
  #                   wiki_token: 'foo',
  #                   wiki_secret: 'bar')
  #     login_as(user, scope: :user)
  #     visit '/surveys'
  #   end

  # it 'Lists all surveys and allows an admin to create a new one.' do
  #   click_link('New Survey')
  #   expect(page.find("h1")).to have_content("New Survey")
  #   fill_in('survey[name]', :with => 'My New Awesome Survey')
  #   click_button('Create Survey')
  #   expect(page.find(".course-list__row")).to have_content("My New Awesome Survey")
  #   click_link('Delete')
  #   prompt = page.driver.browser.switch_to.alert
  #   prompt.accept
  #   expect(page).not_to have_select('.course-list__row')
  # end

  # it 'Has a link to Question Groups' do
  #   click_link('Question Groups')
  #   expect(page).to have_content("Question Groups")
  # end
  # end

  describe 'Editing a Survey' do
    # let!(:question_group)  { FactoryGirl.create(:question_group, name: "Survey Section 1") }
    # let!(:survey)  { create(:survey, name: "Dumb Survey", :rapidfire_question_groups => [question_group]) }

    # before :each do
    #   create_questions(question_group)
    #   @intro_text = "My introduction"
    #   visit '/surveys'
    #   expect(page).to have_content(survey.name)
    #   within('.course-list__row') do
    #     find(:link, 'Edit').click
    #   end
    #   expect(page).to have_content("Editing #{survey.name}")
    # end

    # it 'An admin can the survey introduction' do
    #   fill_in_trix_editor("trix-toolbar-1", @intro_text)
    #   click_button "Update Survey";
    #   within('.course-list__row') do
    #     click_link("View Survey"); sleep 2;
    #     expect(page.find(".survey__title")).to have_content(survey.name)
    #     expect(page.find("[data-survey-block='0']")).to have_content(@intro_text)
    #   end
    # end
  end

  describe 'Instructor takes survey' do
    before do
      @instructor = create(:user)
      course = create(:course, title: 'My Active Course')
      article = create(:article)
      create(:articles_course, article_id: article.id, course_id: course.id)

      @courses_user = create(
        :courses_user,
        user_id: @instructor.id,
        course_id: course.id,
        role: 1
      )

      @survey = create(
        :survey,
        name: 'Instructor Survey',
        intro: 'Welcome to survey',
        thanks: 'You made it!'
      )

      question_group = create(:question_group, id: 1, name: 'Basic Questions')
      @survey.rapidfire_question_groups << question_group
      @survey.save

      # Q1
      # Matrix question at the start
      create(:matrix_question, question_text: 'first line', question_group_id: question_group.id)
      create(:matrix_question, question_text: 'second line', question_group_id: question_group.id)
      create(:matrix_question, question_text: 'third line', question_group_id: question_group.id)

      # Q2
      create(:q_checkbox, question_group_id: question_group.id, conditionals: '')

      # Q3
      q_radio = create(:q_radio, question_group_id: question_group.id, conditionals: '4|=|hindi|multi')
      q_radio.rules[:presence] = '0'
      q_radio.save

      # Q4
      q_long = create(:q_long, question_group_id: question_group.id)
      q_long.rules[:presence] = '0'
      q_long.save

      # Q5
      q_select = create(:q_select, question_group_id: question_group.id)
      q_select.rules[:presence] = '0'
      q_select.save
      # Q6
      q_short = create(:q_short, question_group_id: question_group.id)
      q_short.rules[:presence] = '0'
      q_short.save
      # Q7
      q_numeric = create(:q_numeric, question_group_id: question_group.id)
      q_numeric.rules[:maximum] = '500'
      q_numeric.rules[:minimum] = '1'
      q_numeric.save

      create(:q_checkbox, question_group_id: question_group.id, answer_options: '', course_data_type: 'Students')
      # Q8
      create(:q_checkbox, question_group_id: question_group.id, answer_options: '', course_data_type: 'Articles')
      create(:q_checkbox, question_group_id: question_group.id, answer_options: '', course_data_type: 'WikiEdu Staff')

      # Q9
      create(:q_rangeinput, question_group_id: question_group.id)

      # Matrix questions back-to-back, and matrix question at the end of survey
      # Q10
      create(:matrix_question, question_text: 'first line', question_group_id: question_group.id)
      create(:matrix_question, question_text: 'second line', question_group_id: question_group.id)
      create(:matrix_question, question_text: 'third line', question_group_id: question_group.id)
      # Q11
      create(:matrix_question2, question_text: 'first line', question_group_id: question_group.id)
      create(:matrix_question2, question_text: 'second line', question_group_id: question_group.id)
      create(:matrix_question2, question_text: 'third line', question_group_id: question_group.id)

      survey_assignment = create(
        :survey_assignment,
        survey_id: @survey.id
      )
      create(:survey_notification,
             course_id: course.id,
             survey_assignment_id: survey_assignment.id,
             courses_users_id: @courses_user.id)
    end

    it 'navigates correctly between each question and submits' do
      Capybara.current_driver = :selenium

      # FIXME: form actions fail on travis, although they works locally.
      pending 'passes locally but not on travis-ci'

      expect(Rapidfire::Answer.count).to eq(0)
      expect(SurveyNotification.last.completed).to eq(false)
      login_as(@instructor, scope: :user)
      visit survey_path(@survey)
      select('My Active Course', from: 'course_slug')
      click_button('Start Survey', visible: true)
      click_button('Start')

      sleep 1
      click_button('Next', visible: true) # Q1

      find('.label', text: 'hindi').click
      sleep 1
      click_button('Next', visible: true) # Q2

      find('.label', text: 'female').click
      sleep 1
      click_button('Next', visible: true) # Q3

      fill_in('answer_group_6_answer_text', with: 'testing')
      sleep 1
      click_button('Next', visible: true) # Q4

      select('mac', from: 'answer_group_7_answer_text')
      sleep 1
      click_button('Next', visible: true) # Q5

      fill_in('answer_group_8_answer_text', with: 'testing')
      sleep 1
      click_button('Next', visible: true) # Q6

      sleep 1
      fill_in('answer_group_9_answer_text', with: '50')
      click_button('Next', visible: true) # Q7

      find('.label', text: 'None of the above').click<commit_msg>Improve survey spec<commit_after># frozen_string_literal: true
require 'rails_helper'

describe 'Surveys', type: :feature, js: true do
  include Rapidfire::QuestionSpecHelper
  include Rapidfire::AnswerSpecHelper

  before do
    include Devise::TestHelpers, type: :feature
    Capybara.current_driver = :poltergeist
    page.current_window.resize_to(1920, 1080)
  end

  # describe 'The Survey index' do
  #   before :each do
  #     user = create(:admin,
  #                   id: 200,
  #                   wiki_token: 'foo',
  #                   wiki_secret: 'bar')
  #     login_as(user, scope: :user)
  #     visit '/surveys'
  #   end

  # it 'Lists all surveys and allows an admin to create a new one.' do
  #   click_link('New Survey')
  #   expect(page.find("h1")).to have_content("New Survey")
  #   fill_in('survey[name]', :with => 'My New Awesome Survey')
  #   click_button('Create Survey')
  #   expect(page.find(".course-list__row")).to have_content("My New Awesome Survey")
  #   click_link('Delete')
  #   prompt = page.driver.browser.switch_to.alert
  #   prompt.accept
  #   expect(page).not_to have_select('.course-list__row')
  # end

  # it 'Has a link to Question Groups' do
  #   click_link('Question Groups')
  #   expect(page).to have_content("Question Groups")
  # end
  # end

  describe 'Editing a Survey' do
    # let!(:question_group)  { FactoryGirl.create(:question_group, name: "Survey Section 1") }
    # let!(:survey)  { create(:survey, name: "Dumb Survey", :rapidfire_question_groups => [question_group]) }

    # before :each do
    #   create_questions(question_group)
    #   @intro_text = "My introduction"
    #   visit '/surveys'
    #   expect(page).to have_content(survey.name)
    #   within('.course-list__row') do
    #     find(:link, 'Edit').click
    #   end
    #   expect(page).to have_content("Editing #{survey.name}")
    # end

    # it 'An admin can the survey introduction' do
    #   fill_in_trix_editor("trix-toolbar-1", @intro_text)
    #   click_button "Update Survey";
    #   within('.course-list__row') do
    #     click_link("View Survey"); sleep 2;
    #     expect(page.find(".survey__title")).to have_content(survey.name)
    #     expect(page.find("[data-survey-block='0']")).to have_content(@intro_text)
    #   end
    # end
  end

  describe 'Instructor takes survey' do
    before do
      @instructor = create(:user)
      course = create(:course, title: 'My Active Course')
      article = create(:article)
      create(:articles_course, article_id: article.id, course_id: course.id)

      @courses_user = create(
        :courses_user,
        user_id: @instructor.id,
        course_id: course.id,
        role: 1
      )

      @survey = create(
        :survey,
        name: 'Instructor Survey',
        intro: 'Welcome to survey',
        thanks: 'You made it!'
      )

      question_group = create(:question_group, id: 1, name: 'Basic Questions')
      @survey.rapidfire_question_groups << question_group
      @survey.save

      # Q1
      # Matrix question at the start
      create(:matrix_question, question_text: 'first line', question_group_id: question_group.id)
      create(:matrix_question, question_text: 'second line', question_group_id: question_group.id)
      create(:matrix_question, question_text: 'third line', question_group_id: question_group.id)

      # Q2
      create(:q_checkbox, question_group_id: question_group.id, conditionals: '')

      # Q3
      q_radio = create(:q_radio, question_group_id: question_group.id, conditionals: '4|=|hindi|multi')
      q_radio.rules[:presence] = '0'
      q_radio.save

      # Q4
      q_long = create(:q_long, question_group_id: question_group.id)
      q_long.rules[:presence] = '0'
      q_long.save

      # Q5
      q_select = create(:q_select, question_group_id: question_group.id)
      q_select.rules[:presence] = '0'
      q_select.save
      # Q6
      q_short = create(:q_short, question_group_id: question_group.id)
      q_short.rules[:presence] = '0'
      q_short.save
      # Q7
      q_numeric = create(:q_numeric, question_group_id: question_group.id)
      q_numeric.rules[:maximum] = '500'
      q_numeric.rules[:minimum] = '1'
      q_numeric.save

      create(:q_checkbox, question_group_id: question_group.id, answer_options: '', course_data_type: 'Students')
      # Q8
      create(:q_checkbox, question_group_id: question_group.id, answer_options: '', course_data_type: 'Articles')
      create(:q_checkbox, question_group_id: question_group.id, answer_options: '', course_data_type: 'WikiEdu Staff')

      # Q9
      create(:q_rangeinput, question_group_id: question_group.id)

      # Matrix questions back-to-back, and matrix question at the end of survey
      # Q10
      create(:matrix_question, question_text: 'first line', question_group_id: question_group.id)
      create(:matrix_question, question_text: 'second line', question_group_id: question_group.id)
      create(:matrix_question, question_text: 'third line', question_group_id: question_group.id)
      # Q11
      create(:matrix_question2, question_text: 'first line', question_group_id: question_group.id)
      create(:matrix_question2, question_text: 'second line', question_group_id: question_group.id)
      create(:matrix_question2, question_text: 'third line', question_group_id: question_group.id)

      survey_assignment = create(
        :survey_assignment,
        survey_id: @survey.id
      )
      create(:survey_notification,
             course_id: course.id,
             survey_assignment_id: survey_assignment.id,
             courses_users_id: @courses_user.id)
    end

    it 'sets the course and shows the progress bar' do
      login_as(@instructor, scope: :user)
      visit survey_path(@survey)
      # Sets the course automatically
      expect(page).to have_content 'Survey for My Active Course'
      expect(page).to have_content 'progress'
    end

    it 'navigates correctly between each question and submits' do
      Capybara.current_driver = :selenium

      # FIXME: form actions fail on travis, although they works locally.
      pending 'passes locally but not on travis-ci'

      expect(Rapidfire::Answer.count).to eq(0)
      expect(SurveyNotification.last.completed).to eq(false)
      login_as(@instructor, scope: :user)
      visit survey_path(@survey)

      click_button('Start')

      sleep 1
      click_button('Next', visible: true) # Q1

      find('.label', text: 'hindi').click
      sleep 1
      click_button('Next', visible: true) # Q2

      find('.label', text: 'female').click
      sleep 1
      click_button('Next', visible: true) # Q3

      fill_in('answer_group_6_answer_text', with: 'testing')
      sleep 1
      click_button('Next', visible: true) # Q4

      select('mac', from: 'answer_group_7_answer_text')
      sleep 1
      click_button('Next', visible: true) # Q5

      fill_in('answer_group_8_answer_text', with: 'testing')
      sleep 1
      click_button('Next', visible: true) # Q6

      sleep 1
      fill_in('answer_group_9_answer_text', with: '50')
      click_button('Next', visible: true) # Q7

      find('.label', text: 'None of the above').click
{'fraction_non_alphanumeric': 0.11748113673224737, 'fraction_numerical': 0.009026161765742896, 'mean_word_length': 3.1601642710472277, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '5603785', 'n_tokens_mistral': 4642, 'n_tokens_neox': 4477, 'n_words': 1010}