<commit_before># frozen_string_literal: true

require 'rails_helper'
require "#{Rails.root}/lib/training_library"

DESIRED_TRAINING_MODULES = [{ slug: 'editing-basics' }].freeze

describe 'Training', type: :feature, js: true do
  before { TrainingModule.load_all }

  let(:user) { create(:user, id: 1) }
  let(:module_2) { TrainingModule.find_by(slug: 'editing-basics') }

  before do
    login_as(user, scope: :user)
  end

  describe 'root library' do
    library_names = TrainingLibrary.all.reject(&:exclude_from_index?).map(&:slug)
    after do
      login_as(user, scope: :user)
    end

    it 'loads for a logged-in user' do
      visit '/training'
      library_names.each do |library_name|
        expect(page).to have_content library_name.humanize.titleize
      end
    end

    it 'loads for a logged-out user' do
      logout(:user)
      visit '/training'
      library_names.each do |library_name|
        expect(page).to have_content library_name.humanize.titleize
      end
    end
  end

  describe 'libraries' do
    TrainingLibrary.all.each do |library|
      describe "'#{library.name}' library" do
        it 'renders the overview' do
          visit "/training/#{library.slug}"
          expect(page).to have_content library.name
        end
      end
    end

    after do
      login_as(user, scope: :user)
    end

    it 'load for a logged out user' do
      logout(:user)
      first_library = TrainingLibrary.all[0]
      visit "/training/#{first_library.slug}"
      expect(page).to have_content first_library.name
    end
  end

  describe 'module index page' do
    before do
      TrainingSlide.load
      visit "/training/students/#{module_2.slug}"
    end

    after do
      login_as(user, scope: :user)
    end

    it 'describes the module' do
      expect(page).to have_content module_2.name
      expect(page).to have_content 'Estimated time to complete'
      expect(page).to have_content module_2.estimated_ttc
    end

    it 'renders the table of contents' do
      expect(page).to have_content 'TABLE OF CONTENTS'
      expect(page).to have_content module_2.slides[0].title
      expect(page).to have_content module_2.slides[-1].title
    end

    it 'lets the user start the module' do
      click_link 'Start'
      slide_count = module_2.slides.count
      expect(page).to have_content "Page 1 of #{slide_count}"
      expect(TrainingModulesUsers.find_by(
               user_id: user.id,
               training_module_id: module_2.id
             )).not_to be_nil
    end

    it 'updates the last_slide_completed upon viewing a slide (not after clicking `next`)' do
      click_link 'Start'
      sleep 1.5
      tmu = TrainingModulesUsers.find_by(user_id: user.id, training_module_id: module_2.id)
      expect(tmu.last_slide_completed).to eq(module_2.slides.first.slug)
      click_link 'Next Page'
      sleep 1.5
      expect(tmu.reload.last_slide_completed).to eq(module_2.slides.second.slug)
    end

    it 'sets the module completed on viewing the last slide' do
      login_as(user, scope: :user)
      sleep 1
      click_link 'Start'
      tmu = TrainingModulesUsers.find_by(user_id: user.id, training_module_id: module_2.id)
      visit "/training/students/#{module_2.slug}/#{module_2.slides.last.slug}"
      sleep 2
      expect(tmu.reload.completed_at).to be_between(1.minute.ago, 1.minute.from_now)
    end

    it 'disables slides that have not been seen' do
      click_link 'Start'
      within('.training__slide__nav') { find('.hamburger').click }
      unseen_slide_link = find('.slide__menu__nav__dropdown li:last-child a')['disabled']
      expect(unseen_slide_link).not_to be_nil
    end

    it 'shows slide does not exist for non-existent slides' do
      visit "/training/students/#{module_2.slug}/lol-not-a-real-slide"
      expect(page).to have_content 'slide does not exist'
    end

    it 'loads for a logged out user' do
      logout(:user)
      expect(page).to have_content 'TABLE OF CONTENTS'
      expect(page).to have_content module_2.slides[0].title
      expect(page).to have_content module_2.slides[-1].title
    end
  end

  describe 'finish module button' do
    context 'logged in user' do
      it 'redirects to their dashboard' do
        login_as(user, scope: :user)
        sleep 1
        visit "/training/students/#{module_2.slug}/#{module_2.slides.last.slug}"
        sleep 1
        within '.training__slide__footer' do
          click_link 'Done!'
        end
        sleep 1
        expect(current_path).to eq(root_path)
      end
    end

    context 'logged out user' do
      it 'redirects to library index page' do
        logout(:user)
        visit "/training/students/#{module_2.slug}/#{module_2.slides.last.slug}"
        sleep 1
        within '.training__slide__footer' do
          click_link 'Done!'
        end
        expect(current_path).to eq('/training/students')
      end
    end
  end

  DESIRED_TRAINING_MODULES.each do |module_slug|
    describe "'#{module_slug[:slug]}' module" do
      before do
        TrainingSlide.load
        TrainingModule.flush
      end

      it 'lets the user go from start to finish' do
        training_module = TrainingModule.find_by(module_slug)
        go_through_module_from_start_to_finish(training_module)
      end
    end
  end
end

def go_through_module_from_start_to_finish(training_module)
  visit "/training/students/#{training_module.slug}"
  click_link 'Start'
  click_through_slides(training_module)
  sleep 1
  expect(TrainingModulesUsers.find_by(
    user_id: 1,<commit_msg>Fix training_tool_spec.rb<commit_after># frozen_string_literal: true

require 'rails_helper'
require "#{Rails.root}/lib/training_library"

DESIRED_TRAINING_MODULES = [{ slug: 'editing-basics' }].freeze

describe 'Training', type: :feature, js: true do
  before { TrainingModule.load_all }

  let(:user) { create(:user, id: 1) }
  let(:module_2) { TrainingModule.find_by(slug: 'editing-basics') }

  before do
    login_as(user, scope: :user)
  end

  describe 'root library' do
    library_names = TrainingLibrary.all.reject(&:exclude_from_index?).map(&:slug)
    after do
      login_as(user, scope: :user)
    end

    it 'loads for a logged-in user' do
      visit '/training'
      library_names.each do |library_name|
        expect(page).to have_content library_name.humanize.titleize
      end
    end

    it 'loads for a logged-out user' do
      logout(:user)
      visit '/training'
      library_names.each do |library_name|
        expect(page).to have_content library_name.humanize.titleize
      end
    end
  end

  describe 'libraries' do
    TrainingLibrary.all.each do |library|
      describe "'#{library.name}' library" do
        it 'renders the overview' do
          visit "/training/#{library.slug}"
          expect(page).to have_content library.name
        end
      end
    end

    after do
      login_as(user, scope: :user)
    end

    it 'load for a logged out user' do
      logout(:user)
      first_library = TrainingLibrary.all[0]
      visit "/training/#{first_library.slug}"
      expect(page).to have_content first_library.name
    end
  end

  describe 'module index page' do
    before do
      TrainingSlide.load
      visit "/training/students/#{module_2.slug}"
    end

    after do
      login_as(user, scope: :user)
    end

    it 'describes the module' do
      expect(page).to have_content module_2.name
      expect(page).to have_content 'Estimated time to complete'
      expect(page).to have_content module_2.estimated_ttc
    end

    it 'renders the table of contents' do
      expect(page).to have_content 'TABLE OF CONTENTS'
      expect(page).to have_content module_2.slides[0].title
      expect(page).to have_content module_2.slides[-1].title
    end

    it 'lets the user start the module' do
      click_link 'Start'
      slide_count = module_2.slides.count
      expect(page).to have_content "Page 1 of #{slide_count}"
      expect(TrainingModulesUsers.find_by(
               user_id: user.id,
               training_module_id: module_2.id
             )).not_to be_nil
    end

    it 'updates the last_slide_completed upon viewing a slide (not after clicking `next`)' do
      click_link 'Start'
      sleep 1.5
      tmu = TrainingModulesUsers.find_by(user_id: user.id, training_module_id: module_2.id)
      expect(tmu.last_slide_completed).to eq(module_2.slides.first.slug)
      click_link 'Next Page'
      sleep 1.5
      expect(tmu.reload.last_slide_completed).to eq(module_2.slides.second.slug)
    end

    it 'sets the module completed on viewing the last slide' do
      login_as(user, scope: :user)
      sleep 1
      click_link 'Start'
      tmu = TrainingModulesUsers.find_by(user_id: user.id, training_module_id: module_2.id)
      visit "/training/students/#{module_2.slug}/#{module_2.slides.last.slug}"
      sleep 2
      expect(tmu.reload.completed_at).to be_between(1.minute.ago, 1.minute.from_now)
    end

    it 'disables slides that have not been seen' do
      click_link 'Start'
      within('.training__slide__nav') { find('.hamburger').click }
      unseen_slide_link = find('.slide__menu__nav__dropdown li:last-child a')['disabled']
      expect(unseen_slide_link).not_to be_nil
    end

    it 'shows slide does not exist for non-existent slides' do
      visit "/training/students/#{module_2.slug}/lol-not-a-real-slide"
      expect(page).to have_content 'slide does not exist'
    end

    it 'loads for a logged out user' do
      logout(:user)
      expect(page).to have_content 'TABLE OF CONTENTS'
      expect(page).to have_content module_2.slides[0].title
      expect(page).to have_content module_2.slides[-1].title
    end
  end

  describe 'finish module button' do
    context 'logged in user' do
      it 'redirects to their dashboard' do
        login_as(user, scope: :user)
        sleep 1
        visit "/training/students/#{module_2.slug}/#{module_2.slides.last.slug}"
        sleep 1
        within '.training__slide__footer' do
          click_link 'Done!'
        end
        sleep 1
        expect(current_path).to eq(root_path)
      end
    end

    context 'logged out user' do
      it 'redirects to library index page' do
        logout(:user)
        visit "/training/students/#{module_2.slug}/#{module_2.slides.last.slug}"
        sleep 1
        within '.training__slide__footer' do
          click_link 'Done!'
        end
        expect(current_path).to eq('/training/students')
      end
    end
  end

  DESIRED_TRAINING_MODULES.each do |module_slug|
    describe "'#{module_slug[:slug]}' module" do
      before do
        TrainingSlide.load
      end

      it 'lets the user go from start to finish' do
        training_module = TrainingModule.find_by(module_slug)
        go_through_module_from_start_to_finish(training_module)
      end
    end
  end
end

def go_through_module_from_start_to_finish(training_module)
  visit "/training/students/#{training_module.slug}"
  click_link 'Start'
  click_through_slides(training_module)
  sleep 1
  expect(TrainingModulesUsers.find_by(
    user_id: 1,
{'fraction_non_alphanumeric': 0.09583972565652919, 'fraction_numerical': 0.007580543272267846, 'mean_word_length': 2.817430244574578, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4105672', 'n_tokens_mistral': 3590, 'n_tokens_neox': 3541, 'n_words': 875}