2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-07-12 14:38:20 -04:00
|
|
|
require "suggested_topics_builder"
|
|
|
|
|
2022-07-27 22:27:38 -04:00
|
|
|
RSpec.describe SuggestedTopicsBuilder do
|
2023-11-09 17:47:59 -05:00
|
|
|
fab!(:topic)
|
2013-08-27 20:51:49 -04:00
|
|
|
let(:builder) { SuggestedTopicsBuilder.new(topic) }
|
2013-07-12 14:38:20 -04:00
|
|
|
|
2017-07-07 02:09:14 -04:00
|
|
|
before { SiteSetting.suggested_topics = 5 }
|
2013-07-12 14:38:20 -04:00
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
describe "splicing category results" do
|
2013-08-27 20:51:49 -04:00
|
|
|
def fake_topic(topic_id, category_id)
|
|
|
|
build(:topic, id: topic_id, category_id: category_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:builder) { SuggestedTopicsBuilder.new(fake_topic(1, 1)) }
|
|
|
|
|
|
|
|
it "prioritizes category correctly" do
|
|
|
|
builder.splice_results([fake_topic(2, 2)], :high)
|
|
|
|
builder.splice_results([fake_topic(3, 1)], :high)
|
|
|
|
builder.splice_results([fake_topic(4, 1)], :high)
|
|
|
|
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(builder.results.map(&:id)).to eq([3, 4, 2])
|
2013-08-27 20:51:49 -04:00
|
|
|
|
|
|
|
# we have 2 items in category 1
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(builder.category_results_left).to eq(3)
|
2013-08-27 20:51:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "inserts using default approach for non high priority" do
|
|
|
|
builder.splice_results([fake_topic(2, 2)], :high)
|
|
|
|
builder.splice_results([fake_topic(3, 1)], :low)
|
|
|
|
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(builder.results.map(&:id)).to eq([2, 3])
|
2013-08-27 20:51:49 -04:00
|
|
|
end
|
2014-01-13 15:02:08 -05:00
|
|
|
|
|
|
|
it "inserts multiple results and puts topics in the correct order" do
|
|
|
|
builder.splice_results([fake_topic(2, 1), fake_topic(3, 2), fake_topic(4, 1)], :high)
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(builder.results.map(&:id)).to eq([2, 4, 3])
|
2014-01-13 15:02:08 -05:00
|
|
|
end
|
2013-08-27 20:51:49 -04:00
|
|
|
end
|
|
|
|
|
2013-07-12 14:38:20 -04:00
|
|
|
it "has the correct defaults" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(builder.excluded_topic_ids.include?(topic.id)).to eq(true)
|
|
|
|
expect(builder.results_left).to eq(5)
|
|
|
|
expect(builder.size).to eq(0)
|
|
|
|
expect(builder).not_to be_full
|
2013-07-12 14:38:20 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns full correctly" do
|
|
|
|
builder.stubs(:results_left).returns(0)
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(builder).to be_full
|
2013-07-12 14:38:20 -04:00
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
describe "adding results" do
|
2013-07-12 14:38:20 -04:00
|
|
|
it "adds nothing with nil results" do
|
|
|
|
builder.add_results(nil)
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(builder.results_left).to eq(5)
|
|
|
|
expect(builder.size).to eq(0)
|
|
|
|
expect(builder).not_to be_full
|
2013-07-12 14:38:20 -04:00
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when adding topics" do
|
2019-05-06 23:12:20 -04:00
|
|
|
fab!(:other_topic) { Fabricate(:topic) }
|
2013-07-12 14:38:20 -04:00
|
|
|
|
|
|
|
before do
|
|
|
|
# Add all topics
|
|
|
|
builder.add_results(Topic)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "added the result correctly" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(builder.size).to eq(1)
|
|
|
|
expect(builder.results_left).to eq(4)
|
|
|
|
expect(builder).not_to be_full
|
|
|
|
expect(builder.excluded_topic_ids.include?(topic.id)).to eq(true)
|
|
|
|
expect(builder.excluded_topic_ids.include?(other_topic.id)).to eq(true)
|
2013-07-12 14:38:20 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when adding topics that are not open" do
|
2019-05-06 23:12:20 -04:00
|
|
|
fab!(:archived_topic) { Fabricate(:topic, archived: true) }
|
|
|
|
fab!(:closed_topic) { Fabricate(:topic, closed: true) }
|
|
|
|
fab!(:invisible_topic) { Fabricate(:topic, visible: false) }
|
2013-07-12 14:38:20 -04:00
|
|
|
|
2014-02-04 12:26:38 -05:00
|
|
|
it "adds archived and closed, but not invisible topics" do
|
2013-07-12 14:38:20 -04:00
|
|
|
builder.add_results(Topic)
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(builder.size).to eq(2)
|
|
|
|
expect(builder).not_to be_full
|
2013-07-12 14:38:20 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when category definition topics" do
|
2019-08-06 06:26:54 -04:00
|
|
|
fab!(:category) { Fabricate(:category_with_definition) }
|
2013-11-21 16:43:22 -05:00
|
|
|
|
|
|
|
it "doesn't add a category definition topic" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(category.topic_id).to be_present
|
2013-11-21 16:43:22 -05:00
|
|
|
builder.add_results(Topic)
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(builder.size).to eq(0)
|
|
|
|
expect(builder).not_to be_full
|
2013-11-21 16:43:22 -05:00
|
|
|
end
|
|
|
|
end
|
2024-10-10 11:03:26 -04:00
|
|
|
|
|
|
|
context "with suggested_topics_add_results modifier registered" do
|
|
|
|
fab!(:included_topic) { Fabricate(:topic) }
|
|
|
|
fab!(:excluded_topic) { Fabricate(:topic) }
|
|
|
|
|
|
|
|
let(:modifier_block) do
|
|
|
|
Proc.new { |results| results.filter { |topic| topic.id != excluded_topic.id } }
|
|
|
|
end
|
|
|
|
|
|
|
|
it "Allows modifications to added results" do
|
|
|
|
plugin_instance = Plugin::Instance.new
|
|
|
|
plugin_instance.register_modifier(:suggested_topics_add_results, &modifier_block)
|
|
|
|
|
|
|
|
builder.add_results(Topic.where(id: [included_topic.id, excluded_topic.id]))
|
|
|
|
|
|
|
|
expect(builder.results).to include(included_topic)
|
|
|
|
expect(builder.results).not_to include(excluded_topic)
|
|
|
|
ensure
|
|
|
|
DiscoursePluginRegistry.unregister_modifier(
|
|
|
|
plugin_instance,
|
|
|
|
:suggested_topics_add_results,
|
|
|
|
&modifier_block
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2013-07-12 14:38:20 -04:00
|
|
|
end
|
|
|
|
end
|