discourse/plugins/discourse-topic-voting/spec/lib/topic_extension_spec.rb
Martin Brennan 062b112e51
DEV: Improve vote queries (#39394)
The change is limited to the topic-voting voter preview and vote-list
queries.

`/voting/who` now only loads a capped preview of active voters instead
of loading every vote and mapping users in Ruby., then limiting on the
client. The cap is 104, archived votes are excluded from the preview,
and the client now uses `topic.vote_count` as the total so the popup can
show “and N more...” without fetching the full voter list.

Also tightened the two vote-list queries so archived votes no longer
appear in `state=my_votes` or `/topics/voted-by/:username`.

Finally, the vote and vote-count models now use `with_deleted` for their
topic association so soft-deleted topics can still be resolved where
those records reference them.
2026-04-21 16:10:28 +10:00

67 lines
2.1 KiB
Ruby
Vendored

# frozen_string_literal: true
describe DiscourseTopicVoting::TopicExtension do
let(:user) { Fabricate(:user) }
let(:user2) { Fabricate(:user) }
let(:topic) { Fabricate(:topic) }
let(:topic2) { Fabricate(:topic) }
before do
SiteSetting.topic_voting_enabled = true
SiteSetting.topic_voting_show_who_voted = true
end
describe "#update_vote_count" do
it "upserts topic votes count" do
topic.update_vote_count
topic2.update_vote_count
expect(topic.reload.topic_vote_count.votes_count).to eq(0)
expect(topic2.reload.topic_vote_count.votes_count).to eq(0)
DiscourseTopicVoting::Vote.create!(user: user, topic: topic)
topic.update_vote_count
topic2.update_vote_count
expect(topic.reload.topic_vote_count.votes_count).to eq(1)
expect(topic2.reload.topic_vote_count.votes_count).to eq(0)
DiscourseTopicVoting::Vote.create!(user: user2, topic: topic)
DiscourseTopicVoting::Vote.create!(user: user, topic: topic2)
topic.update_vote_count
topic2.update_vote_count
expect(topic.reload.topic_vote_count.votes_count).to eq(2)
expect(topic2.reload.topic_vote_count.votes_count).to eq(1)
end
end
describe "#who_voted" do
it "returns recent active voters up to the requested limit" do
DiscourseTopicVoting::Vote.create!(user: user, topic: topic, created_at: 2.hours.ago)
DiscourseTopicVoting::Vote.create!(user: user2, topic: topic, created_at: 1.hour.ago)
archived_user = Fabricate(:user)
DiscourseTopicVoting::Vote.create!(
user: archived_user,
topic: topic,
archive: true,
created_at: Time.zone.now,
)
expect(topic.who_voted(limit: 1)).to eq([user2])
end
end
describe "topic associations" do
it "keeps soft-deleted topics available from votes and vote counts" do
vote = DiscourseTopicVoting::Vote.create!(user: user, topic: topic)
topic_vote_count = DiscourseTopicVoting::TopicVoteCount.create!(topic: topic, votes_count: 1)
topic.trash!
expect(vote.reload.topic).to eq(topic)
expect(topic_vote_count.reload.topic).to eq(topic)
end
end
end