docbrown/spec/services/articles/feeds_spec.rb
Jeremy Friesen 60dc2cc12e
Fixing weighted query hotness grab logic (#15528)
* Fixing weighted query hotness grab logic

Prior to this commit, I carried over (albeit imprecisely) logic from the
LargeForemExperimental.  That logic was to help limit articles to those
that were published since the user's latest page view.

However, I introduced a bug in this transcription.  Now both
LargeForemExperimental and WeightedQueryStrategy use the same logic to
determine the oldest publication date to search for in the feed.

This resolves a bug reported where users were not seeing a large number
of items in their feed.

Incidentally, if a forem has little activity in the 18 hours, there
might be very few items in the feed.

I believe, going forward, we may need to better parameterize how many
hours is considered "stale since last page view".

Related to #15240

* Fixing implementation detail

* Extracting helper method

Prior to this commit, I had introduced a method an put it in a less
ideal module space.  This commit extracts that method to a more readily
shareable module space.

I've added a few more specs to help clarify and verify behavior.

* Updating documentation

* Renaming and documenting variables/constants

* Fixing that which I broke
2021-11-30 12:55:48 -05:00

44 lines
1.4 KiB
Ruby

require "rails_helper"
RSpec.describe Articles::Feeds do
describe ".oldest_published_at_to_consider_for" do
subject(:function_call) { described_class.oldest_published_at_to_consider_for(user: user) }
context "when the given user is nil" do
let(:user) { nil }
it { is_expected.to be_a ActiveSupport::TimeWithZone }
end
context "when the given user has no page views" do
let(:user) { instance_double(User) }
before do
allow(user).to receive(:page_views).and_return(nil)
end
it { is_expected.to be_a ActiveSupport::TimeWithZone }
end
context "when the user has page views" do
let(:user) { instance_double(User) }
let(:page_viewed_at) { Time.current }
let(:expected_result) do
page_viewed_at - described_class::NUMBER_OF_HOURS_TO_OFFSET_USERS_LATEST_ARTICLE_VIEWS.hours
end
before do
# This is a distinct code smell. The other option is adding a
# method to user and perhaps to page views and then testing
# delegation. This is, instead, an ActiveRecord call chain,
# so I'm going to ask that we accept this smell to ease
# testing.
# rubocop:disable RSpec/MessageChain
allow(user).to receive_message_chain(:page_views, :second_to_last, :created_at).and_return(page_viewed_at)
# rubocop:enable RSpec/MessageChain
end
it { is_expected.to eq(expected_result) }
end
end
end