2023-03-30 18:07:22 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
describe ::TopicsController do
|
2024-03-05 10:48:28 -05:00
|
|
|
fab!(:topic)
|
2023-03-30 18:07:22 -04:00
|
|
|
fab!(:topic1) { Fabricate(:topic) }
|
|
|
|
fab!(:topic2) { Fabricate(:topic) }
|
2023-03-30 20:04:34 -04:00
|
|
|
fab!(:topic3) { Fabricate(:topic) }
|
2023-03-30 18:07:22 -04:00
|
|
|
fab!(:user) { Fabricate(:admin) }
|
|
|
|
|
|
|
|
before do
|
2023-03-30 20:04:34 -04:00
|
|
|
SiteSetting.ai_embeddings_semantic_related_topics_enabled = true
|
|
|
|
SiteSetting.ai_embeddings_semantic_related_topics = 2
|
2024-01-10 22:52:50 -05:00
|
|
|
|
|
|
|
DiscourseAi::Embeddings::SemanticRelated
|
|
|
|
.any_instance
|
|
|
|
.stubs(:related_topic_ids_for)
|
|
|
|
.returns([topic1.id, topic2.id, topic3.id])
|
2023-03-30 18:07:22 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context "when a user is logged on" do
|
|
|
|
it "includes related topics in payload when configured" do
|
2023-03-30 20:04:34 -04:00
|
|
|
get("#{topic.relative_url}.json")
|
2023-05-23 17:57:52 -04:00
|
|
|
expect(response.status).to eq(200)
|
2023-03-30 20:04:34 -04:00
|
|
|
json = response.parsed_body
|
|
|
|
|
|
|
|
expect(json["suggested_topics"].length).to eq(0)
|
|
|
|
expect(json["related_topics"].length).to eq(2)
|
|
|
|
|
2023-03-30 18:07:22 -04:00
|
|
|
sign_in(user)
|
|
|
|
|
|
|
|
get("#{topic.relative_url}.json")
|
|
|
|
json = response.parsed_body
|
|
|
|
|
|
|
|
expect(json["suggested_topics"].length).to eq(0)
|
2023-03-30 20:04:34 -04:00
|
|
|
expect(json["related_topics"].length).to eq(2)
|
2023-03-30 18:07:22 -04:00
|
|
|
end
|
2024-04-14 19:31:07 -04:00
|
|
|
|
|
|
|
it "includes related topics in payload when configured" do
|
|
|
|
SiteSetting.lazy_load_categories_groups = "#{Group::AUTO_GROUPS[:everyone]}"
|
|
|
|
category = Fabricate(:category)
|
|
|
|
topic.update!(category: category)
|
|
|
|
|
|
|
|
get("#{topic.relative_url}.json")
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
json = response.parsed_body
|
|
|
|
|
|
|
|
expect(json["suggested_topics"].length).to eq(0)
|
|
|
|
expect(json["related_topics"].length).to eq(2)
|
|
|
|
expect(json["categories"].map { |c| c["id"] }).to contain_exactly(
|
|
|
|
SiteSetting.uncategorized_category_id,
|
|
|
|
category.id,
|
|
|
|
)
|
|
|
|
end
|
2023-03-30 18:07:22 -04:00
|
|
|
end
|
2024-01-10 22:52:50 -05:00
|
|
|
|
|
|
|
describe "crawler" do
|
|
|
|
let(:crawler_user_agent) do
|
|
|
|
"Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"
|
|
|
|
end
|
|
|
|
|
|
|
|
it "renders related topics in the crawler view" do
|
|
|
|
get topic.relative_url, env: { "HTTP_USER_AGENT" => crawler_user_agent }
|
|
|
|
body = response.body
|
|
|
|
expect(body).to have_tag(:div, with: { id: "related-topics" })
|
|
|
|
expect(body).to have_tag(:tr, with: { id: "topic-list-item-#{topic1.id}" })
|
|
|
|
expect(body).to have_tag(:tr, with: { id: "topic-list-item-#{topic2.id}" })
|
|
|
|
end
|
|
|
|
end
|
2023-03-30 18:07:22 -04:00
|
|
|
end
|