2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
require 'unread'
|
|
|
|
|
2022-07-27 22:27:38 -04:00
|
|
|
RSpec.describe Unread do
|
2022-06-29 20:18:12 -04:00
|
|
|
let(:whisperers_group) { Fabricate(:group) }
|
2022-10-25 03:29:09 -04:00
|
|
|
let(:user) { Fabricate(:user, groups: [whisperers_group]) }
|
2022-06-29 20:18:12 -04:00
|
|
|
let(:topic) do
|
2022-10-25 03:29:09 -04:00
|
|
|
Fabricate(:topic,
|
|
|
|
posts_count: 13,
|
|
|
|
highest_staff_post_number: 15,
|
|
|
|
highest_post_number: 13,
|
|
|
|
)
|
2016-12-02 01:03:31 -05:00
|
|
|
end
|
|
|
|
|
2022-10-25 03:29:09 -04:00
|
|
|
let(:topic_user) do
|
|
|
|
Fabricate(:topic_user,
|
|
|
|
notification_level: TopicUser.notification_levels[:tracking],
|
|
|
|
topic_id: topic.id,
|
|
|
|
user_id: user.id
|
|
|
|
)
|
2016-12-02 01:03:31 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def unread
|
|
|
|
Unread.new(topic, topic_user, Guardian.new(user))
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'staff counts' do
|
2021-05-20 21:43:47 -04:00
|
|
|
it 'should correctly return based on staff post number' do
|
2022-12-16 11:42:51 -05:00
|
|
|
SiteSetting.whispers_allowed_groups = "#{Group::AUTO_GROUPS[:staff]}"
|
|
|
|
user.grant_admin!
|
2016-12-02 01:03:31 -05:00
|
|
|
|
|
|
|
topic_user.last_read_post_number = 13
|
|
|
|
|
2021-07-05 02:17:31 -04:00
|
|
|
expect(unread.unread_posts).to eq(2)
|
2016-12-02 01:03:31 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'unread_posts' do
|
2021-07-05 02:17:31 -04:00
|
|
|
it 'should have 0 unread posts if the user has read all posts' do
|
2016-12-02 01:03:31 -05:00
|
|
|
topic_user.last_read_post_number = 13
|
|
|
|
expect(unread.unread_posts).to eq(0)
|
2013-02-25 11:42:20 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2021-07-05 02:17:31 -04:00
|
|
|
it 'returns the right unread posts for a user' do
|
|
|
|
topic_user.last_read_post_number = 10
|
|
|
|
expect(unread.unread_posts).to eq(3)
|
2013-02-25 11:42:20 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2021-07-05 02:17:31 -04:00
|
|
|
it 'returns the right unread posts for a staff user' do
|
2022-12-16 11:42:51 -05:00
|
|
|
SiteSetting.whispers_allowed_groups = "#{Group::AUTO_GROUPS[:staff]}"
|
|
|
|
user.grant_admin!
|
2021-07-05 02:17:31 -04:00
|
|
|
topic_user.last_read_post_number = 10
|
|
|
|
expect(unread.unread_posts).to eq(5)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2022-06-29 20:18:12 -04:00
|
|
|
it 'returns the right unread posts for a whisperer user' do
|
|
|
|
SiteSetting.whispers_allowed_groups = "#{whisperers_group.id}"
|
|
|
|
topic_user.last_read_post_number = 10
|
|
|
|
expect(unread.unread_posts).to eq(5)
|
|
|
|
end
|
|
|
|
|
2021-07-05 02:17:31 -04:00
|
|
|
it 'should have 0 unread posts if the user has read more posts than exist (deleted)' do
|
|
|
|
topic_user.last_read_post_number = 14
|
|
|
|
expect(unread.unread_posts).to eq(0)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2013-02-25 11:42:20 -05:00
|
|
|
|
2021-07-05 02:17:31 -04:00
|
|
|
it 'has 0 unread posts if the user has read 10 posts but is not tracking' do
|
|
|
|
topic_user.last_read_post_number = 10
|
2016-12-02 01:03:31 -05:00
|
|
|
topic_user.notification_level = TopicUser.notification_levels[:regular]
|
2021-07-05 02:17:31 -04:00
|
|
|
expect(unread.unread_posts).to eq(0)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2022-10-25 03:29:09 -04:00
|
|
|
it 'has 0 unread posts if the user has not seen the topic' do
|
2021-07-05 02:17:31 -04:00
|
|
|
topic_user.last_read_post_number = nil
|
|
|
|
expect(unread.unread_posts).to eq(0)
|
2013-02-25 11:42:20 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|