From e95ad88382e027073f5a46742ddafca1600c1fb3 Mon Sep 17 00:00:00 2001 From: Eugene Burmakin Date: Fri, 29 Aug 2025 14:23:38 +0200 Subject: [PATCH] Remove obsolete specs --- spec/jobs/tracks/cleanup_job_spec.rb | 8 - spec/jobs/tracks/create_job_spec.rb | 68 +------ .../tracks/parallel_generator_job_spec.rb | 169 +----------------- .../services/tracks/boundary_detector_spec.rb | 54 ++---- .../tracks/parallel_generator_spec.rb | 32 ++-- 5 files changed, 28 insertions(+), 303 deletions(-) diff --git a/spec/jobs/tracks/cleanup_job_spec.rb b/spec/jobs/tracks/cleanup_job_spec.rb index d4823f86..66cb6923 100644 --- a/spec/jobs/tracks/cleanup_job_spec.rb +++ b/spec/jobs/tracks/cleanup_job_spec.rb @@ -24,14 +24,6 @@ RSpec.describe Tracks::CleanupJob, type: :job do described_class.new.perform(older_than: 1.day.ago) end - - it 'logs processing information' do - allow(Tracks::Generator).to receive(:new).and_return(double(call: nil)) - - expect(Rails.logger).to receive(:info).with(/Processing missed tracks for user #{user.id}/) - - described_class.new.perform(older_than: 1.day.ago) - end end context 'with users having insufficient points' do diff --git a/spec/jobs/tracks/create_job_spec.rb b/spec/jobs/tracks/create_job_spec.rb index bddf430a..b23fea8d 100644 --- a/spec/jobs/tracks/create_job_spec.rb +++ b/spec/jobs/tracks/create_job_spec.rb @@ -7,13 +7,10 @@ RSpec.describe Tracks::CreateJob, type: :job do describe '#perform' do let(:generator_instance) { instance_double(Tracks::Generator) } - let(:notification_service) { instance_double(Notifications::Create) } before do allow(Tracks::Generator).to receive(:new).and_return(generator_instance) allow(generator_instance).to receive(:call) - allow(Notifications::Create).to receive(:new).and_return(notification_service) - allow(notification_service).to receive(:call) allow(generator_instance).to receive(:call).and_return(2) end @@ -27,13 +24,6 @@ RSpec.describe Tracks::CreateJob, type: :job do mode: :daily ) expect(generator_instance).to have_received(:call) - expect(Notifications::Create).to have_received(:new).with( - user: user, - kind: :info, - title: 'Tracks Generated', - content: 'Created 2 tracks from your location data. Check your tracks section to view them.' - ) - expect(notification_service).to have_received(:call) end context 'with custom parameters' do @@ -44,8 +34,6 @@ RSpec.describe Tracks::CreateJob, type: :job do before do allow(Tracks::Generator).to receive(:new).and_return(generator_instance) allow(generator_instance).to receive(:call) - allow(Notifications::Create).to receive(:new).and_return(notification_service) - allow(notification_service).to receive(:call) allow(generator_instance).to receive(:call).and_return(1) end @@ -59,37 +47,16 @@ RSpec.describe Tracks::CreateJob, type: :job do mode: :daily ) expect(generator_instance).to have_received(:call) - expect(Notifications::Create).to have_received(:new).with( - user: user, - kind: :info, - title: 'Tracks Generated', - content: 'Created 1 tracks from your location data. Check your tracks section to view them.' - ) - expect(notification_service).to have_received(:call) end end context 'when generator raises an error' do let(:error_message) { 'Something went wrong' } - let(:notification_service) { instance_double(Notifications::Create) } before do allow(Tracks::Generator).to receive(:new).and_return(generator_instance) allow(generator_instance).to receive(:call).and_raise(StandardError, error_message) - allow(Notifications::Create).to receive(:new).and_return(notification_service) - allow(notification_service).to receive(:call) - end - - it 'creates an error notification' do - described_class.new.perform(user.id) - - expect(Notifications::Create).to have_received(:new).with( - user: user, - kind: :error, - title: 'Track Generation Failed', - content: "Failed to generate tracks from your location data: #{error_message}" - ) - expect(notification_service).to have_received(:call) + allow(ExceptionReporter).to receive(:call) end it 'reports the error using ExceptionReporter' do @@ -135,13 +102,6 @@ RSpec.describe Tracks::CreateJob, type: :job do mode: :incremental ) expect(generator_instance).to have_received(:call) - expect(Notifications::Create).to have_received(:new).with( - user: user, - kind: :info, - title: 'Tracks Generated', - content: 'Created 2 tracks from your location data. Check your tracks section to view them.' - ) - expect(notification_service).to have_received(:call) end end end @@ -152,32 +112,6 @@ RSpec.describe Tracks::CreateJob, type: :job do end end - context 'when self-hosted' do - let(:generator_instance) { instance_double(Tracks::Generator) } - let(:notification_service) { instance_double(Notifications::Create) } - let(:error_message) { 'Something went wrong' } - - before do - allow(DawarichSettings).to receive(:self_hosted?).and_return(true) - allow(Tracks::Generator).to receive(:new).and_return(generator_instance) - allow(generator_instance).to receive(:call).and_raise(StandardError, error_message) - allow(Notifications::Create).to receive(:new).and_return(notification_service) - allow(notification_service).to receive(:call) - end - - it 'creates a failure notification when self-hosted' do - described_class.new.perform(user.id) - - expect(Notifications::Create).to have_received(:new).with( - user: user, - kind: :error, - title: 'Track Generation Failed', - content: "Failed to generate tracks from your location data: #{error_message}" - ) - expect(notification_service).to have_received(:call) - end - end - context 'when not self-hosted' do let(:generator_instance) { instance_double(Tracks::Generator) } let(:notification_service) { instance_double(Notifications::Create) } diff --git a/spec/jobs/tracks/parallel_generator_job_spec.rb b/spec/jobs/tracks/parallel_generator_job_spec.rb index 45188af5..7428dd2c 100644 --- a/spec/jobs/tracks/parallel_generator_job_spec.rb +++ b/spec/jobs/tracks/parallel_generator_job_spec.rb @@ -36,26 +36,6 @@ RSpec.describe Tracks::ParallelGeneratorJob do job.perform(user_id) end - it 'logs the start of the operation' do - # Allow other logs to pass through - allow(Rails.logger).to receive(:info).and_call_original - - expect(Rails.logger).to receive(:info) - .with("Starting parallel track generation for user #{user_id} (mode: bulk)") - - job.perform(user_id) - end - - it 'logs successful session creation' do - # Allow other logs to pass through - allow(Rails.logger).to receive(:info).and_call_original - - expect(Rails.logger).to receive(:info) - .with(/Parallel track generation initiated for user #{user_id}/) - - job.perform(user_id) - end - it 'accepts custom parameters' do start_at = 1.week.ago end_at = Time.current @@ -68,50 +48,6 @@ RSpec.describe Tracks::ParallelGeneratorJob do job.perform(user_id, start_at: start_at, end_at: end_at, mode: mode, chunk_size: chunk_size) end - - it 'does not create notifications when session is created successfully' do - expect(Notifications::Create).not_to receive(:new) - job.perform(user_id) - end - end - - context 'when no tracks are generated (no time chunks)' do - let(:user_no_points) { create(:user) } - - it 'logs a warning' do - # Allow other logs to pass through - allow(Rails.logger).to receive(:info).and_call_original - allow(Rails.logger).to receive(:warn).and_call_original - - expect(Rails.logger).to receive(:warn) - .with("No tracks to generate for user #{user_no_points.id} (no time chunks created)") - - job.perform(user_no_points.id) - end - - it 'creates info notification with 0 tracks' do - notification_service = double('notification_service') - expect(Notifications::Create).to receive(:new) - .with( - user: user_no_points, - kind: :info, - title: 'Track Generation Complete', - content: 'Generated 0 tracks from your location data. Check your tracks section to view them.' - ).and_return(notification_service) - expect(notification_service).to receive(:call) - - job.perform(user_no_points.id) - end - end - - context 'when user is not found' do - let(:invalid_user_id) { 99999 } - - it 'raises ActiveRecord::RecordNotFound' do - expect { - job.perform(invalid_user_id) - }.to raise_error(ActiveRecord::RecordNotFound) - end end context 'when an error occurs' do @@ -127,52 +63,6 @@ RSpec.describe Tracks::ParallelGeneratorJob do job.perform(user_id) end - - it 'logs the error' do - # Allow other logs to pass through - allow(Rails.logger).to receive(:info).and_call_original - allow(Rails.logger).to receive(:error).and_call_original - - expect(Rails.logger).to receive(:error) - .with("Parallel track generation failed for user #{user_id}: #{error_message}") - - job.perform(user_id) - end - - it 'creates error notification for self-hosted instances' do - allow(DawarichSettings).to receive(:self_hosted?).and_return(true) - - notification_service = double('notification_service') - expect(Notifications::Create).to receive(:new) - .with( - user: user, - kind: :error, - title: 'Track Generation Failed', - content: "Failed to generate tracks from your location data: #{error_message}" - ).and_return(notification_service) - expect(notification_service).to receive(:call) - - job.perform(user_id) - end - - it 'does not create error notification for hosted instances' do - allow(DawarichSettings).to receive(:self_hosted?).and_return(false) - - expect(Notifications::Create).not_to receive(:new) - - job.perform(user_id) - end - - context 'when user is nil (error before user is found)' do - before do - allow(User).to receive(:find).and_raise(StandardError.new('Database error')) - end - - it 'does not create notification' do - expect(Notifications::Create).not_to receive(:new) - job.perform(user_id) - end - end end context 'with different modes' do @@ -238,7 +128,7 @@ RSpec.describe Tracks::ParallelGeneratorJob do it 'follows the same notification pattern as Tracks::CreateJob' do # Compare with existing Tracks::CreateJob behavior # Should create similar notifications and handle errors similarly - + expect { job.perform(user.id) }.not_to raise_error @@ -262,59 +152,4 @@ RSpec.describe Tracks::ParallelGeneratorJob do }.to have_enqueued_job(described_class) end end - - describe 'private methods' do - describe '#create_info_notification' do - it 'creates info notification with correct parameters' do - tracks_created = 5 - - notification_service = double('notification_service') - expect(Notifications::Create).to receive(:new) - .with( - user: user, - kind: :info, - title: 'Track Generation Complete', - content: "Generated #{tracks_created} tracks from your location data. Check your tracks section to view them." - ).and_return(notification_service) - expect(notification_service).to receive(:call) - - job.send(:create_info_notification, user, tracks_created) - end - end - - describe '#create_error_notification' do - let(:error) { StandardError.new('Test error') } - - context 'when self-hosted' do - before do - allow(DawarichSettings).to receive(:self_hosted?).and_return(true) - end - - it 'creates error notification' do - notification_service = double('notification_service') - expect(Notifications::Create).to receive(:new) - .with( - user: user, - kind: :error, - title: 'Track Generation Failed', - content: "Failed to generate tracks from your location data: #{error.message}" - ).and_return(notification_service) - expect(notification_service).to receive(:call) - - job.send(:create_error_notification, user, error) - end - end - - context 'when not self-hosted' do - before do - allow(DawarichSettings).to receive(:self_hosted?).and_return(false) - end - - it 'does not create notification' do - expect(Notifications::Create).not_to receive(:new) - job.send(:create_error_notification, user, error) - end - end - end - end -end \ No newline at end of file +end diff --git a/spec/services/tracks/boundary_detector_spec.rb b/spec/services/tracks/boundary_detector_spec.rb index bed7befb..7a02b205 100644 --- a/spec/services/tracks/boundary_detector_spec.rb +++ b/spec/services/tracks/boundary_detector_spec.rb @@ -12,7 +12,7 @@ RSpec.describe Tracks::BoundaryDetector do allow(user).to receive(:safe_settings).and_return(safe_settings) allow(safe_settings).to receive(:minutes_between_routes).and_return(30) allow(safe_settings).to receive(:meters_between_routes).and_return(500) - + # Stub Geocoder for consistent distance calculations allow_any_instance_of(Point).to receive(:distance_to_geocoder).and_return(100) # 100 meters allow(Point).to receive(:calculate_distance_for_array_geocoder).and_return(1000) # 1000 meters @@ -45,7 +45,7 @@ RSpec.describe Tracks::BoundaryDetector do # Create points that are far apart (no spatial connection) create(:point, user: user, track: track1, latitude: 40.0, longitude: -74.0, timestamp: 2.hours.ago.to_i) create(:point, user: user, track: track2, latitude: 41.0, longitude: -73.0, timestamp: 1.hour.ago.to_i) - + # Mock distance to be greater than threshold allow_any_instance_of(Point).to receive(:distance_to_geocoder).and_return(1000) # 1000 meters > 500 threshold end @@ -73,45 +73,21 @@ RSpec.describe Tracks::BoundaryDetector do expect(detector.resolve_cross_chunk_tracks).to eq(1) end - it 'logs the operation' do - # Use allow() to handle all the SQL debug logs, then expect the specific ones we care about - allow(Rails.logger).to receive(:debug).and_call_original - allow(Rails.logger).to receive(:info).and_call_original - - expect(Rails.logger).to receive(:debug).with(/Found \d+ boundary track candidates for user #{user.id}/) - expect(Rails.logger).to receive(:info).with(/Resolved 1 boundary tracks for user #{user.id}/) - - detector.resolve_cross_chunk_tracks - end - it 'creates a merged track with all points' do expect { detector.resolve_cross_chunk_tracks }.to change { user.tracks.count }.by(-1) # 2 tracks become 1 - + merged_track = user.tracks.first expect(merged_track.points.count).to eq(4) # All points from both tracks end it 'deletes original tracks' do original_track_ids = [track1.id, track2.id] - - detector.resolve_cross_chunk_tracks - - expect(Track.where(id: original_track_ids)).to be_empty - end - it 'logs track deletion and creation' do - # Use allow() to handle all the SQL debug logs, then expect the specific ones we care about - allow(Rails.logger).to receive(:debug).and_call_original - allow(Rails.logger).to receive(:info).and_call_original - - expect(Rails.logger).to receive(:debug).with(/Merging \d+ boundary tracks for user #{user.id}/) - expect(Rails.logger).to receive(:debug).with(/Deleting boundary track #{track1.id}/) - expect(Rails.logger).to receive(:debug).with(/Deleting boundary track #{track2.id}/) - expect(Rails.logger).to receive(:info).with(/Created merged boundary track \d+ with \d+ points/) - detector.resolve_cross_chunk_tracks + + expect(Track.where(id: original_track_ids)).to be_empty end end @@ -126,18 +102,12 @@ RSpec.describe Tracks::BoundaryDetector do before do # Mock tracks as connected allow(detector).to receive(:find_boundary_track_candidates).and_return([[track1, track2]]) - + # Mock merge failure allow(detector).to receive(:create_track_from_points).and_return(nil) end it 'returns 0 and logs warning' do - # Use allow() to handle all the SQL debug logs - allow(Rails.logger).to receive(:debug).and_call_original - allow(Rails.logger).to receive(:info).and_call_original - allow(Rails.logger).to receive(:warn).and_call_original - - expect(Rails.logger).to receive(:warn).with(/Failed to create merged boundary track for user #{user.id}/) expect(detector.resolve_cross_chunk_tracks).to eq(0) end @@ -181,7 +151,7 @@ RSpec.describe Tracks::BoundaryDetector do it 'handles tracks with no points' do track_no_points = create(:track, user: user, start_at: 1.hour.ago, end_at: 30.minutes.ago) all_tracks_with_empty = all_tracks + [track_no_points] - + expect { detector.send(:find_connected_tracks, base_track, all_tracks_with_empty) }.not_to raise_error @@ -240,14 +210,14 @@ RSpec.describe Tracks::BoundaryDetector do it 'returns true when points are within threshold' do allow(point1).to receive(:distance_to_geocoder).with(point2, :m).and_return(300) - + result = detector.send(:points_are_close?, point1, point2, threshold) expect(result).to be true end it 'returns false when points exceed threshold' do allow(point1).to receive(:distance_to_geocoder).with(point2, :m).and_return(700) - + result = detector.send(:points_are_close?, point1, point2, threshold) expect(result).to be false end @@ -255,7 +225,7 @@ RSpec.describe Tracks::BoundaryDetector do it 'returns false when points are nil' do result = detector.send(:points_are_close?, nil, point2, threshold) expect(result).to be false - + result = detector.send(:points_are_close?, point1, nil, threshold) expect(result).to be false end @@ -322,7 +292,7 @@ RSpec.describe Tracks::BoundaryDetector do it 'sorts points by timestamp' do # Create points out of order point_early = create(:point, user: user, track: track2, timestamp: 3.hours.ago.to_i) - + captured_points = nil allow(detector).to receive(:create_track_from_points) do |points, _distance| captured_points = points @@ -368,4 +338,4 @@ RSpec.describe Tracks::BoundaryDetector do end end end -end \ No newline at end of file +end diff --git a/spec/services/tracks/parallel_generator_spec.rb b/spec/services/tracks/parallel_generator_spec.rb index f0349adf..26d89802 100644 --- a/spec/services/tracks/parallel_generator_spec.rb +++ b/spec/services/tracks/parallel_generator_spec.rb @@ -26,7 +26,7 @@ RSpec.describe Tracks::ParallelGenerator do it 'accepts custom options' do start_time = 1.week.ago end_time = Time.current - + custom_generator = described_class.new( user, start_at: start_time, @@ -123,16 +123,10 @@ RSpec.describe Tracks::ParallelGenerator do it 'cleans existing tracks' do expect(user.tracks.count).to eq(2) - - generator.call - - expect(user.tracks.count).to eq(0) - end - it 'logs track cleanup' do - allow(Rails.logger).to receive(:info) # Allow any log messages - expect(Rails.logger).to receive(:info).with(/Cleaning \d+ existing tracks for bulk regeneration/).at_least(:once) generator.call + + expect(user.tracks.count).to eq(0) end end @@ -141,9 +135,9 @@ RSpec.describe Tracks::ParallelGenerator do it 'cleans tracks for the specific day' do expect(user.tracks.count).to eq(2) - + generator.call - + # Should only clean tracks from the specified day remaining_tracks = user.tracks.count expect(remaining_tracks).to be < 2 @@ -155,9 +149,9 @@ RSpec.describe Tracks::ParallelGenerator do it 'does not clean existing tracks' do expect(user.tracks.count).to eq(2) - + generator.call - + expect(user.tracks.count).to eq(2) end end @@ -172,9 +166,9 @@ RSpec.describe Tracks::ParallelGenerator do it 'only cleans tracks within the specified range' do expect(user.tracks.count).to eq(2) - + generator.call - + # Should only clean the track within the time range remaining_tracks = user.tracks expect(remaining_tracks.count).to eq(1) @@ -215,13 +209,13 @@ RSpec.describe Tracks::ParallelGenerator do context 'error handling in private methods' do it 'handles unknown mode in should_clean_tracks?' do generator.instance_variable_set(:@mode, :unknown) - + expect(generator.send(:should_clean_tracks?)).to be false end it 'raises error for unknown mode in clean_existing_tracks' do generator.instance_variable_set(:@mode, :unknown) - + expect { generator.send(:clean_existing_tracks) }.to raise_error(ArgumentError, 'Unknown mode: unknown') @@ -230,7 +224,7 @@ RSpec.describe Tracks::ParallelGenerator do context 'user settings integration' do let(:mock_settings) { double('SafeSettings') } - + before do # Create a proper mock and stub user.safe_settings to return it allow(mock_settings).to receive(:minutes_between_routes).and_return(60) @@ -388,4 +382,4 @@ RSpec.describe Tracks::ParallelGenerator do end end end -end \ No newline at end of file +end