<commit_before>/**
 * Copyright (C) 2013 <NAME> (born 1979)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package test.e2e

import com.debiki.v0.Prelude._
import com.debiki.v0.ActionId
import org.scalatest.time.{Seconds, Span}


/** Adds replies to the article or other comments.
  */
trait TestReplyer {
  self: DebikiBrowserSpec with StuffTestClicker with TestLoginner =>

  private var replyFormSno = 0


  /** Adds a new reply; returns its id.
    */
  def replyToArticle(text: String): ActionId = {
    clickArticleReplyLink()
    writeAndSubmitReply(text)
  }


  def replyToComment(postId: ActionId, text: String): ActionId = {
    showActionLinks(postId)
    val replyLink = findActionLink_!(postId, "dw-a-reply")
    scrollIntoView(replyLink)
    click on replyLink
    writeAndSubmitReply(text)
  }


  private def writeAndSubmitReply(text: String): ActionId = {
    writeReply(text)

    val postIdsBefore = findAllPostIds

    clickPostReply()

    eventually {
      // Handle any login dialogs.
      // find("dw-fs-lgi-simple") foreach { _ =>
      // }

      // Handle any email notifications question.
      if (find("dw-f-eml-prf").map(_.isDisplayed) == Some(true)) {
        click on noEmailBtn
      }

      val postIdsNow = findAllPostIds
      val anyNewPostId = (postIdsNow -- postIdsBefore).headOption
      val newPostId = anyNewPostId getOrDie "Waiting for the new reply to appear..."

      find(cssSelector(s"#post-$newPostId .dw-p-bd-blk > *")).map(_.text) match {
        case Some(`text`) => // ok
        case Some(x) => throw new Exception(s"Bad reply text: $x")
        case None => throw new Exception("No reply text")
      }

      newPostId
    }
  }


  /** Finds the ids of all posts (without the "post-" prefix).
    */
  private def findAllPostIds =
    findAll(cssSelector(".dw-p")).flatMap(_.attribute("id").map(_.drop(5).toInt)).toSet


  def clickArticleReplyLink() {
    eventually {
      scrollIntoView(articleReplyLink)
      click on articleReplyLink
    }
  }


  def writeReply(text: String) {
    replyFormSno += 1
    val textAreaId = s"dw-fi-reply-text_sno-$replyFormSno"
    eventually {
      click on textAreaId
      enter(text)
      textArea(textAreaId).value must be === text
    }
  }


  def clickPostReply() {
    eventually {
      click on cssSelector(".dw-fi-submit")
    }
  }


  def anyCommentIsPendingModeration =<commit_msg>E2E tests: Let `reloadPage()` reset `replyFormSno` to 0.<commit_after>/**
 * Copyright (C) 2013 <NAME> (born 1979)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package test.e2e

import com.debiki.v0.Prelude._
import com.debiki.v0.ActionId
import org.scalatest.time.{Seconds, Span}


/** Adds replies to the article or other comments.
  */
trait TestReplyer {
  self: DebikiBrowserSpec with StuffTestClicker with TestLoginner =>

  private var replyFormSno = 0


  /** Adds a new reply; returns its id.
    */
  def replyToArticle(text: String): ActionId = {
    clickArticleReplyLink()
    writeAndSubmitReply(text)
  }


  def replyToComment(postId: ActionId, text: String): ActionId = {
    showActionLinks(postId)
    val replyLink = findActionLink_!(postId, "dw-a-reply")
    scrollIntoView(replyLink)
    click on replyLink
    writeAndSubmitReply(text)
  }


  private def writeAndSubmitReply(text: String): ActionId = {
    writeReply(text)

    val postIdsBefore = findAllPostIds

    clickPostReply()

    eventually {
      // Handle any login dialogs.
      // find("dw-fs-lgi-simple") foreach { _ =>
      // }

      // Handle any email notifications question.
      if (find("dw-f-eml-prf").map(_.isDisplayed) == Some(true)) {
        click on noEmailBtn
      }

      val postIdsNow = findAllPostIds
      val anyNewPostId = (postIdsNow -- postIdsBefore).headOption
      val newPostId = anyNewPostId getOrDie "Waiting for the new reply to appear..."

      find(cssSelector(s"#post-$newPostId .dw-p-bd-blk > *")).map(_.text) match {
        case Some(`text`) => // ok
        case Some(x) => throw new Exception(s"Bad reply text: $x")
        case None => throw new Exception("No reply text")
      }

      newPostId
    }
  }


  /** Finds the ids of all posts (without the "post-" prefix).
    */
  private def findAllPostIds =
    findAll(cssSelector(".dw-p")).flatMap(_.attribute("id").map(_.drop(5).toInt)).toSet


  def clickArticleReplyLink() {
    eventually {
      scrollIntoView(articleReplyLink)
      click on articleReplyLink
    }
  }


  def writeReply(text: String) {
    replyFormSno += 1
    val textAreaId = s"dw-fi-reply-text_sno-$replyFormSno"
    eventually {
      click on textAreaId
      enter(text)
      textArea(textAreaId).value must be === text
    }
  }


  override def reloadPage()(implicit driver: org.openqa.selenium.WebDriver) {
    replyFormSno = 0
    // I don't know how to call WebBrowser.reloadPage() from here?
    // self.reloadPage() recurses, and super.reloadPage() -> compilation error.
    // So I just copied the implementation to here. This is error prone?
    // In case some other function also override reloadPage().  ??
    driver.navigate.refresh()
  }


  def clickPostReply() {
    eventually {
      click on cssSelector(".dw-fi-submit")
    }
  }


  def anyCommentIsPendingModeration =
{'fraction_non_alphanumeric': 0.10409320864632837, 'fraction_numerical': 0.00505902192242833, 'mean_word_length': 3.0073710073710074, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6314432', 'n_tokens_mistral': 2041, 'n_tokens_neox': 1952, 'n_words': 614}