<commit_before>require "test_helper"

class TopicsControllerTest < ActionController::TestCase
  include ActionDispatch::Routing::UrlFor
  include PublicDocumentRoutesHelper

  should_be_a_public_facing_controller

  test "shows topic title and description" do
    topic = create(:topic)
    get :show, id: topic
    assert_select ".topic", text: topic.name
    assert_select ".govspeak", text: topic.description
  end

  test "shows published policies and their summaries" do
    published_policy = create(:published_policy, title: "policy-title", summary: "policy-summary")
    topic = create(:topic, policies: [published_policy])

    get :show, id: topic

    assert_select "#policies" do
      assert_select_object(published_policy) do
        assert_select "h2", text: "policy-title"
        assert_select ".summary", text: /policy-summary/
      end
    end
  end

  test "#show sets Cache-Control: max-age to the time of the next scheduled policy" do
    user = login_as(:departmental_editor)
    policy = create(:draft_policy, scheduled_publication: Time.zone.now + Whitehall.default_cache_max_age * 2)
    topic = create(:topic, policies: [policy])
    policy.schedule_as(user, force: true)

    Timecop.freeze(Time.zone.now + Whitehall.default_cache_max_age * 1.5) do
      get :show, id: topic
    end

    assert_cache_control("max-age=#{Whitehall.default_cache_max_age/2}")
  end

  test "#show sets Cache-Control: max-age to the time of the next scheduled publication in an associated policy" do
    user = login_as(:departmental_editor)
    policy = create(:published_policy, title: "policy-title", summary: "policy-summary")
    topic = create(:topic, policies: [policy])
    publication = create(:draft_publication, scheduled_publication: Time.zone.now + Whitehall.default_cache_max_age * 2, related_policies: [policy])
    publication.schedule_as(user, force: true)

    Timecop.freeze(Time.zone.now + Whitehall.default_cache_max_age * 1.5) do
      get :show, id: topic
    end

    assert_cache_control("max-age=#{Whitehall.default_cache_max_age/2}")
  end

  test "#show sets Cache-Control: max-age to the time of the next scheduled announcement in an associated policy" do
    user = login_as(:departmental_editor)
    policy = create(:published_policy, title: "policy-title", summary: "policy-summary")
    topic = create(:topic, policies: [policy])
    news = create(:draft_news_article, scheduled_publication: Time.zone.now + Whitehall.default_cache_max_age * 2, related_policies: [policy])
    news.schedule_as(user, force: true)

    Timecop.freeze(Time.zone.now + Whitehall.default_cache_max_age * 1.5) do
      get :show, id: topic
    end

    assert_cache_control("max-age=#{Whitehall.default_cache_max_age/2}")
  end

  test "shows 3 published publications and links to more" do
    policy = create(:published_policy, title: "policy-title", summary: "policy-summary")
    topic = create(:topic, policies: [policy])
    published = []
    4.times do |i|
      published << create(:published_publication, title: "title-#{i}", related_policies: [policy], published_at: i.days.ago)
    end

    get :show, id: topic

    assert_select "#publications" do<commit_msg>Fix test: publication_date is usedfor sorting pubs<commit_after>require "test_helper"

class TopicsControllerTest < ActionController::TestCase
  include ActionDispatch::Routing::UrlFor
  include PublicDocumentRoutesHelper

  should_be_a_public_facing_controller

  test "shows topic title and description" do
    topic = create(:topic)
    get :show, id: topic
    assert_select ".topic", text: topic.name
    assert_select ".govspeak", text: topic.description
  end

  test "shows published policies and their summaries" do
    published_policy = create(:published_policy, title: "policy-title", summary: "policy-summary")
    topic = create(:topic, policies: [published_policy])

    get :show, id: topic

    assert_select "#policies" do
      assert_select_object(published_policy) do
        assert_select "h2", text: "policy-title"
        assert_select ".summary", text: /policy-summary/
      end
    end
  end

  test "#show sets Cache-Control: max-age to the time of the next scheduled policy" do
    user = login_as(:departmental_editor)
    policy = create(:draft_policy, scheduled_publication: Time.zone.now + Whitehall.default_cache_max_age * 2)
    topic = create(:topic, policies: [policy])
    policy.schedule_as(user, force: true)

    Timecop.freeze(Time.zone.now + Whitehall.default_cache_max_age * 1.5) do
      get :show, id: topic
    end

    assert_cache_control("max-age=#{Whitehall.default_cache_max_age/2}")
  end

  test "#show sets Cache-Control: max-age to the time of the next scheduled publication in an associated policy" do
    user = login_as(:departmental_editor)
    policy = create(:published_policy, title: "policy-title", summary: "policy-summary")
    topic = create(:topic, policies: [policy])
    publication = create(:draft_publication, scheduled_publication: Time.zone.now + Whitehall.default_cache_max_age * 2, related_policies: [policy])
    publication.schedule_as(user, force: true)

    Timecop.freeze(Time.zone.now + Whitehall.default_cache_max_age * 1.5) do
      get :show, id: topic
    end

    assert_cache_control("max-age=#{Whitehall.default_cache_max_age/2}")
  end

  test "#show sets Cache-Control: max-age to the time of the next scheduled announcement in an associated policy" do
    user = login_as(:departmental_editor)
    policy = create(:published_policy, title: "policy-title", summary: "policy-summary")
    topic = create(:topic, policies: [policy])
    news = create(:draft_news_article, scheduled_publication: Time.zone.now + Whitehall.default_cache_max_age * 2, related_policies: [policy])
    news.schedule_as(user, force: true)

    Timecop.freeze(Time.zone.now + Whitehall.default_cache_max_age * 1.5) do
      get :show, id: topic
    end

    assert_cache_control("max-age=#{Whitehall.default_cache_max_age/2}")
  end

  test "shows 3 published publications and links to more" do
    policy = create(:published_policy, title: "policy-title", summary: "policy-summary")
    topic = create(:topic, policies: [policy])
    published = []
    4.times do |i|
      published << create(:published_publication, title: "title-#{i}", related_policies: [policy], publication_date: i.days.ago)
    end

    get :show, id: topic

    assert_select "#publications" do
{'fraction_non_alphanumeric': 0.10720398319589233, 'fraction_numerical': 0.004667807686323323, 'mean_word_length': 4.308009909165978, 'pattern_counts': {'":': 0, '<': 9, '<?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': '4880394', 'n_tokens_mistral': 1971, 'n_tokens_neox': 1935, 'n_words': 513}