require 'rails_helper' RSpec.describe ScheduledMessage, type: :model do let(:account) { create(:account) } let(:author) { create(:user, account: account) } let(:automation_rule) { create(:automation_rule, account: account) } let(:inbox) { create(:inbox, account: account) } let(:contact) { create(:contact, account: account) } let(:contact_inbox) { create(:contact_inbox, contact: contact, inbox: inbox) } let(:conversation) { create(:conversation, account: account, inbox: inbox, contact: contact, contact_inbox: contact_inbox) } def build_scheduled_message(attrs = {}) ScheduledMessage.new({ account: account, inbox: inbox, conversation: conversation, author: author, content: 'Hello', status: :pending, scheduled_at: 1.hour.from_now }.merge(attrs)) end def create_scheduled_message(attrs = {}) build_scheduled_message(attrs).tap(&:save!) end describe 'validations' do describe 'content' do it 'requires content when no template params or attachment' do scheduled_message = build_scheduled_message(content: nil, template_params: {}) expect(scheduled_message).not_to be_valid expect(scheduled_message.errors[:content]).to be_present end it 'allows template params without content' do scheduled_message = build_scheduled_message(content: nil, template_params: { id: '123456789', name: 'test_template' }) expect(scheduled_message).to be_valid end it 'allows attachment without content' do scheduled_message = build_scheduled_message(content: nil, template_params: {}) scheduled_message.attachment.attach( io: Rails.root.join('spec/assets/avatar.png').open, filename: 'avatar.png', content_type: 'image/png' ) expect(scheduled_message).to be_valid end end describe 'author' do it 'accepts automation rules as author' do scheduled_message = build_scheduled_message(author: automation_rule) expect(scheduled_message).to be_valid end end describe 'status on create' do it 'allows creating with draft status' do scheduled_message = build_scheduled_message(status: :draft, scheduled_at: nil) expect(scheduled_message).to be_valid end it 'allows creating with pending status' do scheduled_message = build_scheduled_message expect(scheduled_message).to be_valid end it 'does not allow creating with sent status' do scheduled_message = build_scheduled_message(status: :sent) expect(scheduled_message).not_to be_valid expect(scheduled_message.errors[:status]).to be_present end it 'does not allow creating with failed status' do scheduled_message = build_scheduled_message(status: :failed) expect(scheduled_message).not_to be_valid expect(scheduled_message.errors[:status]).to be_present end end describe 'scheduled_at' do it 'requires scheduled_at when status is pending' do scheduled_message = build_scheduled_message(status: :pending, scheduled_at: nil) expect(scheduled_message).not_to be_valid expect(scheduled_message.errors[:scheduled_at]).to be_present end it 'does not require scheduled_at when status is draft' do scheduled_message = build_scheduled_message(status: :draft, scheduled_at: nil) expect(scheduled_message).to be_valid end it 'requires scheduled_at to be in the future when pending' do scheduled_message = build_scheduled_message(status: :pending, scheduled_at: 1.minute.ago) expect(scheduled_message).not_to be_valid expect(scheduled_message.errors[:scheduled_at]).to include('must be in the future') end it 'validates future scheduled_at when changing status to pending' do scheduled_message = create_scheduled_message(status: :draft, scheduled_at: nil) scheduled_message.status = :pending scheduled_message.scheduled_at = 1.minute.ago expect(scheduled_message).not_to be_valid expect(scheduled_message.errors[:scheduled_at]).to include('must be in the future') end end describe 'editability' do it 'allows editing draft messages' do scheduled_message = create_scheduled_message(status: :draft, scheduled_at: nil) scheduled_message.content = 'Updated content' expect(scheduled_message).to be_valid end it 'allows editing pending messages' do scheduled_message = create_scheduled_message scheduled_message.content = 'Updated content' expect(scheduled_message).to be_valid end it 'does not allow editing content of sent messages' do scheduled_message = create_scheduled_message scheduled_message.update!(status: :sent) expect { scheduled_message.update!(content: 'Updated content') }.to raise_error(ActiveRecord::RecordInvalid) do |error| expect(error.record.errors[:base]).to include('Scheduled message can only be modified while draft or pending') end end it 'does not allow editing content of failed messages' do scheduled_message = create_scheduled_message scheduled_message.update!(status: :failed) expect { scheduled_message.update!(content: 'Updated content') }.to raise_error(ActiveRecord::RecordInvalid) do |error| expect(error.record.errors[:base]).to include('Scheduled message can only be modified while draft or pending') end end it 'allows changing status from sent to failed' do scheduled_message = create_scheduled_message scheduled_message.update!(status: :sent) expect { scheduled_message.update!(status: :failed) }.not_to raise_error expect(scheduled_message.reload.status).to eq('failed') end it 'allows changing status from failed to sent' do scheduled_message = create_scheduled_message scheduled_message.update!(status: :failed) expect { scheduled_message.update!(status: :sent) }.not_to raise_error expect(scheduled_message.reload.status).to eq('sent') end end describe 'destroy' do it 'allows deleting draft messages' do scheduled_message = create_scheduled_message(status: :draft, scheduled_at: nil) expect { scheduled_message.destroy }.to change(described_class, :count).by(-1) end it 'allows deleting pending messages' do scheduled_message = create_scheduled_message expect { scheduled_message.destroy }.to change(described_class, :count).by(-1) end it 'does not allow deleting sent messages' do scheduled_message = create_scheduled_message scheduled_message.update!(status: :sent) expect { scheduled_message.destroy }.not_to change(described_class, :count) expect(scheduled_message.errors[:base]).to include('Cannot delete a scheduled message that has already been sent or failed') end it 'does not allow deleting failed messages' do scheduled_message = create_scheduled_message scheduled_message.update!(status: :failed) expect { scheduled_message.destroy }.not_to change(described_class, :count) expect(scheduled_message.errors[:base]).to include('Cannot delete a scheduled message that has already been sent or failed') end end end describe '.due_for_sending' do it 'returns only pending messages scheduled up to the current minute' do freeze_time due_same_minute = create_scheduled_message( scheduled_at: 1.minute.from_now ) overdue = create_scheduled_message( scheduled_at: 2.minutes.from_now ) create_scheduled_message( content: 'Future message', scheduled_at: 10.minutes.from_now ) create_scheduled_message( scheduled_at: 1.minute.from_now, status: :draft ) sent_message = create_scheduled_message( scheduled_at: 1.minute.from_now ) sent_message.update!(status: :sent) failed_message = create_scheduled_message( scheduled_at: 1.minute.from_now ) failed_message.update!(status: :failed) # NOTE: Travel to a time where due_same_minute and overdue are due but not_due_yet is not travel_to(5.minutes.from_now) expect(described_class.due_for_sending.pluck(:id)).to contain_exactly(due_same_minute.id, overdue.id) end end end