diff --git a/app/jobs/users/mailer_sending_job.rb b/app/jobs/users/mailer_sending_job.rb index ae8b80dc..742db9eb 100644 --- a/app/jobs/users/mailer_sending_job.rb +++ b/app/jobs/users/mailer_sending_job.rb @@ -7,7 +7,11 @@ class Users::MailerSendingJob < ApplicationJob user = User.find(user_id) if should_skip_email?(user, email_type) - Rails.logger.info "Skipping #{email_type} email for user #{user_id} - #{skip_reason(user, email_type)}" + ExceptionReporter.call( + 'Users::MailerSendingJob', + "Skipping #{email_type} email for user ID #{user_id} - #{skip_reason(user, email_type)}" + ) + return end @@ -15,7 +19,10 @@ class Users::MailerSendingJob < ApplicationJob UsersMailer.with(params).public_send(email_type).deliver_later rescue ActiveRecord::RecordNotFound - Rails.logger.warn "User with ID #{user_id} not found. Skipping #{email_type} email." + ExceptionReporter.call( + 'Users::MailerSendingJob', + "User with ID #{user_id} not found. Skipping #{email_type} email." + ) end private diff --git a/spec/jobs/cache/preheating_job_spec.rb b/spec/jobs/cache/preheating_job_spec.rb index 79483db7..e148cee5 100644 --- a/spec/jobs/cache/preheating_job_spec.rb +++ b/spec/jobs/cache/preheating_job_spec.rb @@ -10,6 +10,10 @@ RSpec.describe Cache::PreheatingJob do let!(:user2) { create(:user) } let!(:import1) { create(:import, user: user1) } let!(:import2) { create(:import, user: user2) } + let(:user_1_years_tracked_key) { "dawarich/user_#{user1.id}_years_tracked" } + let(:user_2_years_tracked_key) { "dawarich/user_#{user2.id}_years_tracked" } + let(:user_1_points_geocoded_stats_key) { "dawarich/user_#{user1.id}_points_geocoded_stats" } + let(:user_2_points_geocoded_stats_key) { "dawarich/user_#{user2.id}_points_geocoded_stats" } before do create_list(:point, 3, user: user1, import: import1, reverse_geocoded_at: Time.current) @@ -17,42 +21,59 @@ RSpec.describe Cache::PreheatingJob do end it 'preheats years_tracked cache for all users' do - expect(Rails.cache).to receive(:write).with( - "dawarich/user_#{user1.id}_years_tracked", - anything, - expires_in: 1.day - ) - expect(Rails.cache).to receive(:write).with( - "dawarich/user_#{user2.id}_years_tracked", - anything, - expires_in: 1.day - ) + # Clear cache before test to ensure clean state + Rails.cache.clear described_class.new.perform + + # Verify that cache keys exist after job runs + expect(Rails.cache.exist?(user_1_years_tracked_key)).to be true + expect(Rails.cache.exist?(user_2_years_tracked_key)).to be true + + # Verify the cached data is reasonable + user1_years = Rails.cache.read(user_1_years_tracked_key) + user2_years = Rails.cache.read(user_2_years_tracked_key) + + expect(user1_years).to be_an(Array) + expect(user2_years).to be_an(Array) end it 'preheats points_geocoded_stats cache for all users' do - expect(Rails.cache).to receive(:write).with( - "dawarich/user_#{user1.id}_points_geocoded_stats", - { geocoded: 3, without_data: 0 }, - expires_in: 1.day - ) - expect(Rails.cache).to receive(:write).with( - "dawarich/user_#{user2.id}_points_geocoded_stats", - { geocoded: 2, without_data: 0 }, - expires_in: 1.day - ) + # Clear cache before test to ensure clean state + Rails.cache.clear described_class.new.perform + + # Verify that cache keys exist after job runs + expect(Rails.cache.exist?(user_1_points_geocoded_stats_key)).to be true + expect(Rails.cache.exist?(user_2_points_geocoded_stats_key)).to be true + + # Verify the cached data has the expected structure + user1_stats = Rails.cache.read(user_1_points_geocoded_stats_key) + user2_stats = Rails.cache.read(user_2_points_geocoded_stats_key) + + expect(user1_stats).to be_a(Hash) + expect(user1_stats).to have_key(:geocoded) + expect(user1_stats).to have_key(:without_data) + expect(user1_stats[:geocoded]).to eq(3) + + expect(user2_stats).to be_a(Hash) + expect(user2_stats).to have_key(:geocoded) + expect(user2_stats).to have_key(:without_data) + expect(user2_stats[:geocoded]).to eq(2) end it 'actually writes to cache' do described_class.new.perform - expect(Rails.cache.exist?("dawarich/user_#{user1.id}_years_tracked")).to be true - expect(Rails.cache.exist?("dawarich/user_#{user1.id}_points_geocoded_stats")).to be true - expect(Rails.cache.exist?("dawarich/user_#{user2.id}_years_tracked")).to be true - expect(Rails.cache.exist?("dawarich/user_#{user2.id}_points_geocoded_stats")).to be true + expect(Rails.cache.exist?(user_1_years_tracked_key)).to be true + expect(Rails.cache.exist?(user_1_points_geocoded_stats_key)).to be true + expect(Rails.cache.exist?(user_1_countries_visited_key)).to be true + expect(Rails.cache.exist?(user_1_cities_visited_key)).to be true + expect(Rails.cache.exist?(user_2_years_tracked_key)).to be true + expect(Rails.cache.exist?(user_2_points_geocoded_stats_key)).to be true + expect(Rails.cache.exist?(user_2_countries_visited_key)).to be true + expect(Rails.cache.exist?(user_2_cities_visited_key)).to be true end it 'handles users with no points gracefully' do diff --git a/spec/jobs/users/mailer_sending_job_spec.rb b/spec/jobs/users/mailer_sending_job_spec.rb index 2df6afa2..92781395 100644 --- a/spec/jobs/users/mailer_sending_job_spec.rb +++ b/spec/jobs/users/mailer_sending_job_spec.rb @@ -117,28 +117,4 @@ RSpec.describe Users::MailerSendingJob, type: :job do end end end - - describe '#trial_related_email?' do - subject { described_class.new } - - it 'returns true for trial_expires_soon' do - expect(subject.send(:trial_related_email?, 'trial_expires_soon')).to be true - end - - it 'returns true for trial_expired' do - expect(subject.send(:trial_related_email?, 'trial_expired')).to be true - end - - it 'returns false for welcome' do - expect(subject.send(:trial_related_email?, 'welcome')).to be false - end - - it 'returns false for explore_features' do - expect(subject.send(:trial_related_email?, 'explore_features')).to be false - end - - it 'returns false for unknown email types' do - expect(subject.send(:trial_related_email?, 'unknown_email')).to be false - end - end end diff --git a/spec/mailers/users_mailer_spec.rb b/spec/mailers/users_mailer_spec.rb index eed70c31..9d0195e3 100644 --- a/spec/mailers/users_mailer_spec.rb +++ b/spec/mailers/users_mailer_spec.rb @@ -48,4 +48,22 @@ RSpec.describe UsersMailer, type: :mailer do expect(mail.to).to eq(['test@example.com']) end end + + describe 'post_trial_reminder_early' do + let(:mail) { UsersMailer.with(user: user).post_trial_reminder_early } + + it 'renders the headers' do + expect(mail.subject).to eq('🚀 Still interested in Dawarich? Subscribe now!') + expect(mail.to).to eq(['test@example.com']) + end + end + + describe 'post_trial_reminder_late' do + let(:mail) { UsersMailer.with(user: user).post_trial_reminder_late } + + it 'renders the headers' do + expect(mail.subject).to eq('📍 Your location data is waiting - Subscribe to Dawarich') + expect(mail.to).to eq(['test@example.com']) + end + end end