# frozen_string_literal: true
require 'rails_helper'

describe 'multiwiki assignments', type: :feature, js: true do
  let(:admin) { create(:admin) }
  let(:course) { create(:course, submitted: true) }
  let(:user) { create(:user) }

  before do
    page.current_window.resize_to(1920, 1080)

    allow(Features).to receive(:disable_wiki_output?).and_return(true)
    login_as(admin)
    course.campaigns << Campaign.last
    create(:courses_user, course_id: course.id, user_id: user.id,
                          role: CoursesUsers::Roles::STUDENT_ROLE)
  end

  it 'creates a valid assignment from a wiki URL' do
    VCR.use_cassette 'multiwiki_assignment' do
      visit "/courses/#{course.slug}/students"
      click_button 'Assign Articles'
      click_button 'Assign an article'
      within('#users') do
        first('input').set('https://ta.wiktionary.org/wiki/%E0%AE%86%E0%AE%99%E0%AF%8D%E0%AE%95%E0%AE%BF%E0%AE%B2%E0%AE%AE%E0%AF%8D')
      end
      click_button 'Assign'
      click_button 'OK'
      visit "/courses/#{course.slug}/students"

      expect(page).to have_content 'ta:wiktionary:ஆங்கிலம்'
      expect(Assignment.last.wiki.language).to eq('ta')
      expect(Assignment.last.wiki.project).to eq('wiktionary')
      expect(Assignment.last.article.title).to eq('ஆங்கிலம்')
      expect(Assignment.last.article.wiki.language).to eq('ta')
      expect(Assignment.last.article.wiki.project).to eq('wiktionary')
    end
  end

  it 'creates a valid assignment from an article and an alternative project and language' do
    VCR.use_cassette 'multiwiki_assignment' do
      pending 'This sometimes fails on travis.'

      visit "/courses/#{course.slug}/students"
      click_button 'Assign Articles'
      click_button 'Assign an article'

      within('#users') do
        find('input', visible: true).set('No le des prisa, dolor')
        click_link 'Change'
        find('div.language-select').click
        within('.language-select') do
          find('div.Select--single div.Select-value').send_keys('es', :enter)
        end

        find('div.project-select').click
        within('.project-select') do
          find('div.Select--single div.Select-value').send_keys('wikisource', :enter)
        end
      end

      click_button 'Assign'
      click_button 'OK'

      visit "/courses/#{course.slug}/students"

      expect(page).to have_content 'es:wikisource:No le des prisa, dolor'

      expect(Assignment.last.wiki.language).to eq('es')
      expect(Assignment.last.wiki.project).to eq('wikisource')
      expect(Assignment.last.article.title).to eq('No_le_des_prisa,_dolor')
      expect(Assignment.last.article.wiki.language).to eq('es')
      expect(Assignment.last.article.wiki.project).to eq('wikisource')

      puts 'PASSED'
      raise 'this test passed — this time'
    end
  end
end
