<commit_before># frozen_string_literal: true

require 'rails_helper'

feature 'Leaving a comment' do
  let!(:enrollment) { create :enrollment }
  let!(:global_forum) { enrollment.case.forums.find_by community: nil }
  let!(:private_forum) do
    private_forum = create :forum, :with_community, case: enrollment.case
    enrollment.reader.invitations.create community: private_forum.community
  end

  before { login_as enrollment.reader }

  scenario 'is possible with a unique selection' do
    kase = enrollment.case
    card = kase.pages.first.cards.first
    card.raw_content = ContentState.with_text(
      'abcdefghijklmnopqrstuvwxyz'.each_char.map { |x| x * 5 }.join(' ')
    )
    card.save

    visit case_path kase

    first_page = kase.pages.first
    click_link first_page.title
    expect(page).to have_link 'Respond'
    click_link 'Respond', match: :first

    first_paragraph = find('.DraftEditor-root div[data-block]', match: :first)
    first_paragraph.double_click
    click_button 'Respond here'
    expect(page).to have_selector '.public-DraftEditorPlaceholder-root'

    reply_placeholder = find('.public-DraftEditorPlaceholder-root',
                             text: 'Write a reply...')
                        .native
    page.driver.browser.action.move_to(reply_placeholder).click.perform
    page.driver.browser.action.send_keys('Test reply').perform
    click_button 'Submit'
    expect(page).to have_selector 'blockquote', text: 'Test reply'

    find('[aria-label="Close"]').click
    click_button 'Overview'
    click_button 'Conversation'
    click_link 'Test reply'
    reply_placeholder = find('.public-DraftEditorPlaceholder-root',
                             text: 'Write a reply...')
                        .native
    page.driver.browser.action.move_to(reply_placeholder).click.perform
    page.driver.browser.action.send_keys('Conversation view works!').perform
    find('button[aria-label="Respond"]').click
    expect(page).to have_content 'Conversation view works!'
    expect(page).to have_content 'Write a reply...'
  end

  scenario 'is not possible with a non-unique selection' do
    card = enrollment.case.pages.first.cards.first
    card.raw_content = ContentState.with_text('apple ' * 50)
    card.save

    visit case_path(enrollment.case) + '/1'
    click_link 'Respond', match: :first
    first_paragraph = find('.DraftEditor-root div[data-block]', match: :first)
    first_paragraph.double_click

    expect(page).to have_content 'Please select a few more words.'
  end

  let!(:other_reader) { create :reader }

  let!(:comment_thread) do
    first_card = enrollment.case.pages.first.cards.first
    first_letter = first_card.paragraphs[0][0]
    first_card.comment_threads.create(
      original_highlight_text: first_letter,
      reader: other_reader,
      locale: I18n.locale,
      forum: global_forum
    )
  end

  scenario 'makes it show up for other people looking at the same forum' do
    enrollment.reader.update(active_community_id: nil)
    visit case_path(enrollment.case) + '/1'
    expect(first('.CommentThreads__banner')).to have_content 'RESPOND'
    sleep(1)
    comment_thread.comments.create(
      content: 'Test comment',
      reader: other_reader
    )
    expect(first('.CommentThreads__banner')).to have_content '1 RESPONSE'

    click_link GlobalCommunity.instance.name
    expect(page).to have_content private_forum.community.name
    find('.pt-menu-item', text: private_forum.community.name).click
    expect(first('.CommentThreads__banner')).to have_content 'RESPOND'
    comment_thread.comments.create(
      content: 'Test comment',
      reader: other_reader
    )
    expect(first('.CommentThreads__banner')).to have_content 'RESPOND'

    click_link private_forum.community.name
    find('.pt-menu-item', text: GlobalCommunity.instance.name).click
    expect(first('.CommentThreads__banner')).to have_content '2 RESPONSES'
    sleep(1)
    comment_thread.comments.create(
      content: 'Test comment',
      reader: other_reader
    )
    expect(first('.CommentThreads__banner')).to have_content '3 RESPONSES'
  end

  context 'in response to another comment' do
    let!(:comment) do
      comment_thread.comments.create(
        content: 'Test comment',
        reader: other_reader
      )
    end<commit_msg>test: fix tests for new language<commit_after># frozen_string_literal: true

require 'rails_helper'

feature 'Leaving a comment' do
  let!(:enrollment) { create :enrollment }
  let!(:global_forum) { enrollment.case.forums.find_by community: nil }
  let!(:private_forum) do
    private_forum = create :forum, :with_community, case: enrollment.case
    enrollment.reader.invitations.create community: private_forum.community
  end

  before { login_as enrollment.reader }

  scenario 'is possible with a unique selection' do
    kase = enrollment.case
    card = kase.pages.first.cards.first
    card.raw_content = ContentState.with_text(
      'abcdefghijklmnopqrstuvwxyz'.each_char.map { |x| x * 5 }.join(' ')
    )
    card.save

    visit case_path kase

    first_page = kase.pages.first
    click_link first_page.title
    expect(page).to have_link 'Respond'
    click_link 'Respond', match: :first

    first_paragraph = find('.DraftEditor-root div[data-block]', match: :first)
    first_paragraph.double_click
    click_button 'Respond Here'
    expect(page).to have_selector '.public-DraftEditorPlaceholder-root'

    reply_placeholder = find('.public-DraftEditorPlaceholder-root',
                             text: 'Write a reply...')
                        .native
    page.driver.browser.action.move_to(reply_placeholder).click.perform
    page.driver.browser.action.send_keys('Test reply').perform
    click_button 'Submit'
    expect(page).to have_selector 'blockquote', text: 'Test reply'

    find('[aria-label="Close"]').click
    click_button 'Overview'
    click_button 'Conversation'
    click_link 'Test reply'
    reply_placeholder = find('.public-DraftEditorPlaceholder-root',
                             text: 'Write a reply...')
                        .native
    page.driver.browser.action.move_to(reply_placeholder).click.perform
    page.driver.browser.action.send_keys('Conversation view works!').perform
    find('button[aria-label="Respond"]').click
    expect(page).to have_content 'Conversation view works!'
    expect(page).to have_content 'Write a reply...'
  end

  scenario 'is not possible with a non-unique selection' do
    card = enrollment.case.pages.first.cards.first
    card.raw_content = ContentState.with_text('apple ' * 50)
    card.save

    visit case_path(enrollment.case) + '/1'
    click_link 'Respond', match: :first
    first_paragraph = find('.DraftEditor-root div[data-block]', match: :first)
    first_paragraph.double_click

    expect(page).to have_content 'Please select a few more words.'
  end

  let!(:other_reader) { create :reader }

  let!(:comment_thread) do
    first_card = enrollment.case.pages.first.cards.first
    first_letter = first_card.paragraphs[0][0]
    first_card.comment_threads.create(
      original_highlight_text: first_letter,
      reader: other_reader,
      locale: I18n.locale,
      forum: global_forum
    )
  end

  scenario 'makes it show up for other people looking at the same forum' do
    enrollment.reader.update(active_community_id: nil)
    visit case_path(enrollment.case) + '/1'
    expect(first('.CommentThreads__banner')).to have_content 'RESPOND'
    sleep(1)
    comment_thread.comments.create(
      content: 'Test comment',
      reader: other_reader
    )
    expect(first('.CommentThreads__banner')).to have_content '1 COMMENT'

    click_link GlobalCommunity.instance.name
    expect(page).to have_content private_forum.community.name
    find('.pt-menu-item', text: private_forum.community.name).click
    expect(first('.CommentThreads__banner')).to have_content 'RESPOND'
    comment_thread.comments.create(
      content: 'Test comment',
      reader: other_reader
    )
    expect(first('.CommentThreads__banner')).to have_content 'RESPOND'

    click_link private_forum.community.name
    find('.pt-menu-item', text: GlobalCommunity.instance.name).click
    expect(first('.CommentThreads__banner')).to have_content '2 COMMENTS'
    sleep(1)
    comment_thread.comments.create(
      content: 'Test comment',
      reader: other_reader
    )
    expect(first('.CommentThreads__banner')).to have_content '3 COMMENTS'
  end

  context 'in response to another comment' do
    let!(:comment) do
      comment_thread.comments.create(
        content: 'Test comment',
        reader: other_reader
      )
    end
{'fraction_non_alphanumeric': 0.10896248263084761, 'fraction_numerical': 0.003242241778601204, 'mean_word_length': 3.4612603305785123, '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': '7575362', 'n_tokens_mistral': 2673, 'n_tokens_neox': 2580, 'n_words': 550}