<commit_before>require "integration_test_helper"

class AtomFeedsTest < ActionDispatch::IntegrationTest
  include OrganisationFeedHelpers

  it "renders an organisation atom feed when there is content" do
    given_there_is_an_organisation_content_item
    and_content_for_that_organisation
    when_i_visit_the_atom_feed
    then_i_can_see_the_feed
    with_the_feed_updated_time_set_to_the_latest_item
    and_feed_items
  end

  it "renders a valid organisation atom feed when there is no content" do
    given_there_is_an_organisation_content_item
    but_no_content_for_that_organisation
    when_i_visit_the_atom_feed
    then_i_can_see_the_feed
    but_no_feed_items
  end

  def given_there_is_an_organisation_content_item
    @updated_at = "2018-12-25T00:00:00Z"
    @organisation_slug = "ministry-of-magic"
    @base_path = "/government/organisations/#{@organisation_slug}"

    content_store_has_schema_example("organisation")
  end

  def and_content_for_that_organisation
    stub_content_for_organisation_feed(@organisation_slug, results_from_rummager)
  end

  def but_no_content_for_that_organisation
    stub_empty_results
  end

  def when_i_visit_the_atom_feed
    visit "#{@base_path}.atom"
  end

  def then_i_can_see_the_feed
    with_a_title
    and_an_alternate_link
  end

  def with_a_title
    title = page.first("feed>title").text(:all)
    assert_equal title, "Ministry of Magic - Activity on GOV.UK"
  end

  def and_an_alternate_link
    assert page.has_css?("feed>link[rel='alternate'][href$='#{@base_path}']")
  end

  def with_the_feed_updated_time_set_to_the_latest_item
    updated = page.first("feed>updated").text(:all)
    assert_equal updated, @updated_at
  end

  def and_feed_items<commit_msg>Rename test method for reuse<commit_after>require "integration_test_helper"

class AtomFeedsTest < ActionDispatch::IntegrationTest
  include OrganisationFeedHelpers

  it "renders an organisation atom feed when there is content" do
    given_there_is_an_organisation_content_item
    and_content_for_that_organisation
    when_i_visit_the_organisation_atom_feed
    then_i_can_see_the_feed
    with_the_feed_updated_time_set_to_the_latest_item
    and_feed_items
  end

  it "renders a valid organisation atom feed when there is no content" do
    given_there_is_an_organisation_content_item
    but_no_content_for_that_organisation
    when_i_visit_the_organisation_atom_feed
    then_i_can_see_the_feed
    but_no_feed_items
  end

  def given_there_is_an_organisation_content_item
    @updated_at = "2018-12-25T00:00:00Z"
    @organisation_slug = "ministry-of-magic"
    @base_path = "/government/organisations/#{@organisation_slug}"

    content_store_has_schema_example("organisation")
  end

  def and_content_for_that_organisation
    stub_content_for_organisation_feed(@organisation_slug, results_from_rummager)
  end

  def but_no_content_for_that_organisation
    stub_empty_results
  end

  def when_i_visit_the_organisation_atom_feed
    visit "#{@base_path}.atom"
  end

  def then_i_can_see_the_feed
    with_a_title
    and_an_alternate_link
  end

  def with_a_title
    title = page.first("feed>title").text(:all)
    assert_equal title, "Ministry of Magic - Activity on GOV.UK"
  end

  def and_an_alternate_link
    assert page.has_css?("feed>link[rel='alternate'][href$='#{@base_path}']")
  end

  def with_the_feed_updated_time_set_to_the_latest_item
    updated = page.first("feed>updated").text(:all)
    assert_equal updated, @updated_at
  end

  def and_feed_items
{'fraction_non_alphanumeric': 0.058456299659477864, 'fraction_numerical': 0.007945516458569807, 'mean_word_length': 5.261101243339254, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, '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': '1348409', 'n_tokens_mistral': 1260, 'n_tokens_neox': 1224, 'n_words': 205}