Files
eminuxCRM/test/services/mailings/hourly_throughput_test.rb
T
eminuxandCursor 1c6b4bbabe
CI / scan_js (push) Has been cancelled
CI / lint (push) Has been cancelled
CI / scan_ruby (push) Has been cancelled
Allinea i KPI su due righe e aggiunge lo storico orario degli invii confermati.
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-07 12:53:38 +02:00

86 lines
2.9 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.hourly_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
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