125 lines
4.2 KiB
Ruby
125 lines
4.2 KiB
Ruby
require "test_helper"
|
|
|
|
class Mailings::HourlyThroughputTest < ActiveSupport::TestCase
|
|
setup do
|
|
@project = projects(:matchlivetv)
|
|
@identity = create_mail_identity
|
|
@mailing = create_mailing(identity: @identity, audience: "all")
|
|
end
|
|
|
|
test "counts confirmed sends in the last hour and last 15 minutes" do
|
|
add_recipient status: "sent", sent_at: 10.minutes.ago
|
|
add_recipient status: "sent", sent_at: 40.minutes.ago, mailing: extra_mailing
|
|
add_recipient status: "sent", sent_at: 2.hours.ago, mailing: extra_mailing("Vecchia")
|
|
add_recipient status: "queued", mailing: extra_mailing("In coda")
|
|
|
|
stats = Mailings::HourlyThroughput.new(@project)
|
|
|
|
assert_equal 2, stats.sent
|
|
assert_equal 1, stats.recent_sent
|
|
assert_equal 0, stats.failed
|
|
assert_equal 2, stats.drained
|
|
end
|
|
|
|
test "counts failed recipients that left the queue in the last hour" do
|
|
recipient = add_recipient(status: "failed")
|
|
recipient.update_columns(updated_at: 5.minutes.ago)
|
|
|
|
stale = add_recipient(status: "failed", mailing: extra_mailing)
|
|
stale.update_columns(updated_at: 2.hours.ago)
|
|
|
|
stats = Mailings::HourlyThroughput.new(@project)
|
|
|
|
assert_equal 0, stats.sent
|
|
assert_equal 1, stats.failed
|
|
assert_equal 1, stats.drained
|
|
end
|
|
|
|
test "ignores sends from other projects" do
|
|
other = create_mailing(project: projects(:riskmeter), identity: @identity, audience: "all")
|
|
add_recipient status: "sent", sent_at: 5.minutes.ago, mailing: other
|
|
add_recipient status: "sent", sent_at: 5.minutes.ago
|
|
|
|
stats = Mailings::HourlyThroughput.new(@project)
|
|
|
|
assert_equal 1, stats.sent
|
|
assert_equal 1, stats.recent_sent
|
|
end
|
|
|
|
test "builds twelve hourly buckets of confirmed sends" do
|
|
now = Time.zone.parse("2026-09-07 12:40")
|
|
travel_to now do
|
|
add_recipient status: "sent", sent_at: now - 20.minutes
|
|
add_recipient status: "sent", sent_at: now - 70.minutes, mailing: extra_mailing("Ore 11")
|
|
add_recipient status: "sent", sent_at: now - 80.minutes, mailing: extra_mailing("Ore 11 bis")
|
|
add_recipient status: "sent", sent_at: now - 13.hours, mailing: extra_mailing("Fuori finestra")
|
|
|
|
stats = Mailings::HourlyThroughput.new(@project, now: now)
|
|
history = stats.history
|
|
|
|
assert_equal 12, history.size
|
|
assert_equal now.beginning_of_hour, history.last[:at]
|
|
assert_equal 1, history.last[:count]
|
|
assert_equal 2, history[-2][:count]
|
|
assert_equal 0, history.first[:count]
|
|
assert_equal 3, stats.history_total
|
|
end
|
|
end
|
|
|
|
test "includes the previous day when the range is 24 hours" do
|
|
now = Time.zone.parse("2026-09-07 12:40")
|
|
travel_to now do
|
|
add_recipient status: "sent", sent_at: now - 20.hours
|
|
|
|
twelve = Mailings::HourlyThroughput.new(@project, now: now, range: "12h")
|
|
day = Mailings::HourlyThroughput.new(@project, now: now, range: "24h")
|
|
|
|
assert_equal 0, twelve.history_total
|
|
assert_equal 24, day.history.size
|
|
assert_equal 1, day.history_total
|
|
end
|
|
end
|
|
|
|
test "groups the last week by day" do
|
|
now = Time.zone.parse("2026-09-07 12:40")
|
|
travel_to now do
|
|
add_recipient status: "sent", sent_at: now - 3.days
|
|
add_recipient status: "sent", sent_at: now - 8.days, mailing: extra_mailing("Vecchia")
|
|
|
|
stats = Mailings::HourlyThroughput.new(@project, now: now, range: "7d")
|
|
history = stats.history
|
|
|
|
assert_equal 7, history.size
|
|
assert_equal now.beginning_of_day, history.last[:at]
|
|
assert_equal 1, history[-4][:count]
|
|
assert_equal 0, history.last[:count]
|
|
assert_equal 1, stats.history_total
|
|
assert_equal "7d", stats.range_key
|
|
end
|
|
end
|
|
|
|
test "falls back to 12 hours for an unknown range" do
|
|
stats = Mailings::HourlyThroughput.new(@project, range: "nope")
|
|
|
|
assert_equal "12h", stats.range_key
|
|
assert_equal 12, stats.history.size
|
|
end
|
|
|
|
private
|
|
|
|
def extra_mailing(name = "Altra campagna")
|
|
create_mailing(identity: @identity, audience: "all", name: name)
|
|
end
|
|
|
|
def add_recipient(status:, sent_at: nil, mailing: @mailing)
|
|
org = organizations(:acme)
|
|
mailing.mailing_recipients.create!(
|
|
organization: org,
|
|
contact: org.contacts.first,
|
|
email: org.email,
|
|
status: status,
|
|
sent_at: sent_at
|
|
)
|
|
end
|
|
end
|