require "test_helper" class Mailings::SendClockTest < ActiveSupport::TestCase setup do @mailing = create_mailing( send_window_enabled: true, send_window_start_minutes: 10 * 60, send_window_end_minutes: 18 * 60, interval_seconds: 120 ) end test "is open inside weekday window" do travel_to Time.zone.local(2026, 4, 1, 11, 0, 0) do clock = Mailings::SendClock.new(@mailing) assert clock.open? assert_equal Time.zone.now, clock.next_send_at end end test "waits until window start the same weekday" do travel_to Time.zone.local(2026, 4, 1, 8, 30, 0) do clock = Mailings::SendClock.new(@mailing) assert_not clock.open? assert_equal Time.zone.local(2026, 4, 1, 10, 0, 0), clock.next_open_at end end test "after window end skips weekend and Italian holiday to next workday" do # Venerdì 3 aprile 2026 18:30. Lunedi 6 è Pasquetta → martedì 7 alle 10:00. travel_to Time.zone.local(2026, 4, 3, 18, 30, 0) do clock = Mailings::SendClock.new(@mailing) assert_not clock.open? assert_equal Time.zone.local(2026, 4, 7, 10, 0, 0), clock.next_open_at end end test "interval that would land after 18:00 moves to next workday" do travel_to Time.zone.local(2026, 4, 1, 17, 59, 0) do clock = Mailings::SendClock.new(@mailing) at = clock.next_send_at(earliest: Time.current + 120.seconds) assert_equal Time.zone.local(2026, 4, 2, 10, 0, 0), at end end test "disabled window sends anytime including weekend" do @mailing.update!(send_window_enabled: false) travel_to Time.zone.local(2026, 4, 4, 21, 0, 0) do clock = Mailings::SendClock.new(@mailing) assert clock.open? assert_equal Time.zone.now, clock.next_send_at end end end class Italy::HolidaysTest < ActiveSupport::TestCase test "computes Easter and Italian fixed holidays" do assert_equal Date.new(2026, 4, 5), Italy::Holidays.easter_date(2026) assert Italy::Holidays.holiday?(Date.new(2026, 4, 5)) assert Italy::Holidays.holiday?(Date.new(2026, 4, 6)) assert Italy::Holidays.holiday?(Date.new(2026, 8, 15)) assert_not Italy::Holidays.holiday?(Date.new(2026, 4, 7)) end end