|
| 1 | +require 'rails_helper' |
| 2 | + |
| 3 | +RSpec.describe 'Profile Image Performance', type: :request do |
| 4 | + include ActiveSupport::Testing::TimeHelpers |
| 5 | + |
| 6 | + before do |
| 7 | + configure_host_platform |
| 8 | + end |
| 9 | + |
| 10 | + context 'with multiple platform members' do |
| 11 | + let!(:platform) { create(:better_together_platform) } |
| 12 | + let!(:people) { create_list(:better_together_person, 3) } # Reduce to 3 for faster test |
| 13 | + let!(:role) { create(:better_together_role, :platform_role) } # Create platform role |
| 14 | + let!(:memberships) do |
| 15 | + people.map do |person| |
| 16 | + create(:better_together_person_platform_membership, |
| 17 | + joinable: platform, |
| 18 | + member: person, |
| 19 | + role: role) # Reuse the same platform role |
| 20 | + end |
| 21 | + end |
| 22 | + |
| 23 | + it 'loads platform show page efficiently with profile images' do |
| 24 | + # Measure execution time |
| 25 | + start_time = Time.current |
| 26 | + |
| 27 | + # Make request to platform show page |
| 28 | + get better_together.platform_path(platform, locale: I18n.default_locale) |
| 29 | + |
| 30 | + end_time = Time.current |
| 31 | + execution_time = end_time - start_time |
| 32 | + |
| 33 | + # Verify response is successful |
| 34 | + expect(response).to have_http_status(:success) |
| 35 | + |
| 36 | + # Log performance metrics (this will help track improvements) |
| 37 | + Rails.logger.info "Platform show page with #{people.count} members loaded in #{execution_time} seconds" |
| 38 | + |
| 39 | + # Performance expectation - should load in under 5 seconds |
| 40 | + expect(execution_time).to be < 5.seconds |
| 41 | + |
| 42 | + # Verify content includes member count (proving the associations loaded) |
| 43 | + expect(response.body).to include('membership-column') |
| 44 | + |
| 45 | + # Count the membership cards rendered |
| 46 | + membership_count = response.body.scan('membership-column').length |
| 47 | + expect(membership_count).to eq(people.count) |
| 48 | + end |
| 49 | + end |
| 50 | + |
| 51 | + context 'profile_image_url method performance' do |
| 52 | + let(:person) { create(:better_together_person) } |
| 53 | + |
| 54 | + it 'calls profile_image_url without errors when no image is attached' do |
| 55 | + start_time = Time.current |
| 56 | + |
| 57 | + # Call our optimized method (should return nil gracefully) |
| 58 | + result = person.profile_image_url(size: 150) |
| 59 | + |
| 60 | + end_time = Time.current |
| 61 | + execution_time = end_time - start_time |
| 62 | + |
| 63 | + expect(result).to be_nil |
| 64 | + |
| 65 | + # Should be reasonably fast (under 2 seconds for no image) |
| 66 | + expect(execution_time).to be < 2.seconds |
| 67 | + |
| 68 | + Rails.logger.info "profile_image_url method executed in #{execution_time} seconds" |
| 69 | + end |
| 70 | + end |
| 71 | +end |
0 commit comments