Remove obsolete specs

This commit is contained in:
Eugene Burmakin 2025-08-29 14:23:38 +02:00
parent 30fb51940c
commit e95ad88382
5 changed files with 28 additions and 303 deletions

View file

@ -24,14 +24,6 @@ RSpec.describe Tracks::CleanupJob, type: :job do
described_class.new.perform(older_than: 1.day.ago) described_class.new.perform(older_than: 1.day.ago)
end 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 end
context 'with users having insufficient points' do context 'with users having insufficient points' do

View file

@ -7,13 +7,10 @@ RSpec.describe Tracks::CreateJob, type: :job do
describe '#perform' do describe '#perform' do
let(:generator_instance) { instance_double(Tracks::Generator) } let(:generator_instance) { instance_double(Tracks::Generator) }
let(:notification_service) { instance_double(Notifications::Create) }
before do before do
allow(Tracks::Generator).to receive(:new).and_return(generator_instance) allow(Tracks::Generator).to receive(:new).and_return(generator_instance)
allow(generator_instance).to receive(:call) 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) allow(generator_instance).to receive(:call).and_return(2)
end end
@ -27,13 +24,6 @@ RSpec.describe Tracks::CreateJob, type: :job do
mode: :daily mode: :daily
) )
expect(generator_instance).to have_received(:call) 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
context 'with custom parameters' do context 'with custom parameters' do
@ -44,8 +34,6 @@ RSpec.describe Tracks::CreateJob, type: :job do
before do before do
allow(Tracks::Generator).to receive(:new).and_return(generator_instance) allow(Tracks::Generator).to receive(:new).and_return(generator_instance)
allow(generator_instance).to receive(:call) 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) allow(generator_instance).to receive(:call).and_return(1)
end end
@ -59,37 +47,16 @@ RSpec.describe Tracks::CreateJob, type: :job do
mode: :daily mode: :daily
) )
expect(generator_instance).to have_received(:call) 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
end end
context 'when generator raises an error' do context 'when generator raises an error' do
let(:error_message) { 'Something went wrong' } let(:error_message) { 'Something went wrong' }
let(:notification_service) { instance_double(Notifications::Create) }
before do before do
allow(Tracks::Generator).to receive(:new).and_return(generator_instance) allow(Tracks::Generator).to receive(:new).and_return(generator_instance)
allow(generator_instance).to receive(:call).and_raise(StandardError, error_message) allow(generator_instance).to receive(:call).and_raise(StandardError, error_message)
allow(Notifications::Create).to receive(:new).and_return(notification_service) allow(ExceptionReporter).to receive(:call)
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)
end end
it 'reports the error using ExceptionReporter' do it 'reports the error using ExceptionReporter' do
@ -135,13 +102,6 @@ RSpec.describe Tracks::CreateJob, type: :job do
mode: :incremental mode: :incremental
) )
expect(generator_instance).to have_received(:call) 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 end
end end
@ -152,32 +112,6 @@ RSpec.describe Tracks::CreateJob, type: :job do
end end
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 context 'when not self-hosted' do
let(:generator_instance) { instance_double(Tracks::Generator) } let(:generator_instance) { instance_double(Tracks::Generator) }
let(:notification_service) { instance_double(Notifications::Create) } let(:notification_service) { instance_double(Notifications::Create) }

View file

@ -36,26 +36,6 @@ RSpec.describe Tracks::ParallelGeneratorJob do
job.perform(user_id) job.perform(user_id)
end 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 it 'accepts custom parameters' do
start_at = 1.week.ago start_at = 1.week.ago
end_at = Time.current 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) job.perform(user_id, start_at: start_at, end_at: end_at, mode: mode, chunk_size: chunk_size)
end 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 end
context 'when an error occurs' do context 'when an error occurs' do
@ -127,52 +63,6 @@ RSpec.describe Tracks::ParallelGeneratorJob do
job.perform(user_id) job.perform(user_id)
end 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 end
context 'with different modes' do context 'with different modes' do
@ -238,7 +128,7 @@ RSpec.describe Tracks::ParallelGeneratorJob do
it 'follows the same notification pattern as Tracks::CreateJob' do it 'follows the same notification pattern as Tracks::CreateJob' do
# Compare with existing Tracks::CreateJob behavior # Compare with existing Tracks::CreateJob behavior
# Should create similar notifications and handle errors similarly # Should create similar notifications and handle errors similarly
expect { expect {
job.perform(user.id) job.perform(user.id)
}.not_to raise_error }.not_to raise_error
@ -262,59 +152,4 @@ RSpec.describe Tracks::ParallelGeneratorJob do
}.to have_enqueued_job(described_class) }.to have_enqueued_job(described_class)
end end
end 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

View file

@ -12,7 +12,7 @@ RSpec.describe Tracks::BoundaryDetector do
allow(user).to receive(:safe_settings).and_return(safe_settings) 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(:minutes_between_routes).and_return(30)
allow(safe_settings).to receive(:meters_between_routes).and_return(500) allow(safe_settings).to receive(:meters_between_routes).and_return(500)
# Stub Geocoder for consistent distance calculations # Stub Geocoder for consistent distance calculations
allow_any_instance_of(Point).to receive(:distance_to_geocoder).and_return(100) # 100 meters 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 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 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: 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) 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 # Mock distance to be greater than threshold
allow_any_instance_of(Point).to receive(:distance_to_geocoder).and_return(1000) # 1000 meters > 500 threshold allow_any_instance_of(Point).to receive(:distance_to_geocoder).and_return(1000) # 1000 meters > 500 threshold
end end
@ -73,45 +73,21 @@ RSpec.describe Tracks::BoundaryDetector do
expect(detector.resolve_cross_chunk_tracks).to eq(1) expect(detector.resolve_cross_chunk_tracks).to eq(1)
end 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 it 'creates a merged track with all points' do
expect { expect {
detector.resolve_cross_chunk_tracks detector.resolve_cross_chunk_tracks
}.to change { user.tracks.count }.by(-1) # 2 tracks become 1 }.to change { user.tracks.count }.by(-1) # 2 tracks become 1
merged_track = user.tracks.first merged_track = user.tracks.first
expect(merged_track.points.count).to eq(4) # All points from both tracks expect(merged_track.points.count).to eq(4) # All points from both tracks
end end
it 'deletes original tracks' do it 'deletes original tracks' do
original_track_ids = [track1.id, track2.id] 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 detector.resolve_cross_chunk_tracks
expect(Track.where(id: original_track_ids)).to be_empty
end end
end end
@ -126,18 +102,12 @@ RSpec.describe Tracks::BoundaryDetector do
before do before do
# Mock tracks as connected # Mock tracks as connected
allow(detector).to receive(:find_boundary_track_candidates).and_return([[track1, track2]]) allow(detector).to receive(:find_boundary_track_candidates).and_return([[track1, track2]])
# Mock merge failure # Mock merge failure
allow(detector).to receive(:create_track_from_points).and_return(nil) allow(detector).to receive(:create_track_from_points).and_return(nil)
end end
it 'returns 0 and logs warning' do 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) expect(detector.resolve_cross_chunk_tracks).to eq(0)
end end
@ -181,7 +151,7 @@ RSpec.describe Tracks::BoundaryDetector do
it 'handles tracks with no points' 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) 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] all_tracks_with_empty = all_tracks + [track_no_points]
expect { expect {
detector.send(:find_connected_tracks, base_track, all_tracks_with_empty) detector.send(:find_connected_tracks, base_track, all_tracks_with_empty)
}.not_to raise_error }.not_to raise_error
@ -240,14 +210,14 @@ RSpec.describe Tracks::BoundaryDetector do
it 'returns true when points are within threshold' do it 'returns true when points are within threshold' do
allow(point1).to receive(:distance_to_geocoder).with(point2, :m).and_return(300) allow(point1).to receive(:distance_to_geocoder).with(point2, :m).and_return(300)
result = detector.send(:points_are_close?, point1, point2, threshold) result = detector.send(:points_are_close?, point1, point2, threshold)
expect(result).to be true expect(result).to be true
end end
it 'returns false when points exceed threshold' do it 'returns false when points exceed threshold' do
allow(point1).to receive(:distance_to_geocoder).with(point2, :m).and_return(700) allow(point1).to receive(:distance_to_geocoder).with(point2, :m).and_return(700)
result = detector.send(:points_are_close?, point1, point2, threshold) result = detector.send(:points_are_close?, point1, point2, threshold)
expect(result).to be false expect(result).to be false
end end
@ -255,7 +225,7 @@ RSpec.describe Tracks::BoundaryDetector do
it 'returns false when points are nil' do it 'returns false when points are nil' do
result = detector.send(:points_are_close?, nil, point2, threshold) result = detector.send(:points_are_close?, nil, point2, threshold)
expect(result).to be false expect(result).to be false
result = detector.send(:points_are_close?, point1, nil, threshold) result = detector.send(:points_are_close?, point1, nil, threshold)
expect(result).to be false expect(result).to be false
end end
@ -322,7 +292,7 @@ RSpec.describe Tracks::BoundaryDetector do
it 'sorts points by timestamp' do it 'sorts points by timestamp' do
# Create points out of order # Create points out of order
point_early = create(:point, user: user, track: track2, timestamp: 3.hours.ago.to_i) point_early = create(:point, user: user, track: track2, timestamp: 3.hours.ago.to_i)
captured_points = nil captured_points = nil
allow(detector).to receive(:create_track_from_points) do |points, _distance| allow(detector).to receive(:create_track_from_points) do |points, _distance|
captured_points = points captured_points = points
@ -368,4 +338,4 @@ RSpec.describe Tracks::BoundaryDetector do
end end
end end
end end
end end

View file

@ -26,7 +26,7 @@ RSpec.describe Tracks::ParallelGenerator do
it 'accepts custom options' do it 'accepts custom options' do
start_time = 1.week.ago start_time = 1.week.ago
end_time = Time.current end_time = Time.current
custom_generator = described_class.new( custom_generator = described_class.new(
user, user,
start_at: start_time, start_at: start_time,
@ -123,16 +123,10 @@ RSpec.describe Tracks::ParallelGenerator do
it 'cleans existing tracks' do it 'cleans existing tracks' do
expect(user.tracks.count).to eq(2) 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 generator.call
expect(user.tracks.count).to eq(0)
end end
end end
@ -141,9 +135,9 @@ RSpec.describe Tracks::ParallelGenerator do
it 'cleans tracks for the specific day' do it 'cleans tracks for the specific day' do
expect(user.tracks.count).to eq(2) expect(user.tracks.count).to eq(2)
generator.call generator.call
# Should only clean tracks from the specified day # Should only clean tracks from the specified day
remaining_tracks = user.tracks.count remaining_tracks = user.tracks.count
expect(remaining_tracks).to be < 2 expect(remaining_tracks).to be < 2
@ -155,9 +149,9 @@ RSpec.describe Tracks::ParallelGenerator do
it 'does not clean existing tracks' do it 'does not clean existing tracks' do
expect(user.tracks.count).to eq(2) expect(user.tracks.count).to eq(2)
generator.call generator.call
expect(user.tracks.count).to eq(2) expect(user.tracks.count).to eq(2)
end end
end end
@ -172,9 +166,9 @@ RSpec.describe Tracks::ParallelGenerator do
it 'only cleans tracks within the specified range' do it 'only cleans tracks within the specified range' do
expect(user.tracks.count).to eq(2) expect(user.tracks.count).to eq(2)
generator.call generator.call
# Should only clean the track within the time range # Should only clean the track within the time range
remaining_tracks = user.tracks remaining_tracks = user.tracks
expect(remaining_tracks.count).to eq(1) expect(remaining_tracks.count).to eq(1)
@ -215,13 +209,13 @@ RSpec.describe Tracks::ParallelGenerator do
context 'error handling in private methods' do context 'error handling in private methods' do
it 'handles unknown mode in should_clean_tracks?' do it 'handles unknown mode in should_clean_tracks?' do
generator.instance_variable_set(:@mode, :unknown) generator.instance_variable_set(:@mode, :unknown)
expect(generator.send(:should_clean_tracks?)).to be false expect(generator.send(:should_clean_tracks?)).to be false
end end
it 'raises error for unknown mode in clean_existing_tracks' do it 'raises error for unknown mode in clean_existing_tracks' do
generator.instance_variable_set(:@mode, :unknown) generator.instance_variable_set(:@mode, :unknown)
expect { expect {
generator.send(:clean_existing_tracks) generator.send(:clean_existing_tracks)
}.to raise_error(ArgumentError, 'Unknown mode: unknown') }.to raise_error(ArgumentError, 'Unknown mode: unknown')
@ -230,7 +224,7 @@ RSpec.describe Tracks::ParallelGenerator do
context 'user settings integration' do context 'user settings integration' do
let(:mock_settings) { double('SafeSettings') } let(:mock_settings) { double('SafeSettings') }
before do before do
# Create a proper mock and stub user.safe_settings to return it # Create a proper mock and stub user.safe_settings to return it
allow(mock_settings).to receive(:minutes_between_routes).and_return(60) allow(mock_settings).to receive(:minutes_between_routes).and_return(60)
@ -388,4 +382,4 @@ RSpec.describe Tracks::ParallelGenerator do
end end
end end
end end
end end