1 # frozen_string_literal: true
 
   6   class HeatmapsControllerTest < ActionDispatch::IntegrationTest
 
   8     # test all routes which lead to this controller
 
  11         { :path => "/user/username/heatmap", :method => :get },
 
  12         { :controller => "users/heatmaps", :action => "show", :user_display_name => "username" }
 
  18       # Create two changesets
 
  19       create(:changeset, :user => user, :created_at => 6.months.ago, :num_changes => 10)
 
  20       create(:changeset, :user => user, :created_at => 3.months.ago, :num_changes => 20)
 
  22       get user_heatmap_path(user)
 
  24       assert_response :success
 
  25       # The data should not be empty
 
  26       heatmap_data = assigns(:heatmap_data)
 
  27       assert_not_nil heatmap_data
 
  28       assert_predicate heatmap_data[:data], :any?
 
  29       # The data should be in the right format
 
  30       heatmap_data[:data].each_value do |entry|
 
  31         assert_equal [:date, :max_id, :total_changes], entry.keys.sort, "Heatmap data entries should have expected keys"
 
  33       assert_equal 30, heatmap_data[:count]
 
  36     def test_show_data_caching
 
  37       # Enable caching to be able to test
 
  39       @original_cache_store = Rails.cache
 
  40       Rails.cache = ActiveSupport::Cache::MemoryStore.new
 
  44       # Create an initial changeset
 
  45       create(:changeset, :user => user, :created_at => 6.months.ago, :num_changes => 15)
 
  47       # First request to populate the cache
 
  48       get user_heatmap_path(user)
 
  49       first_response_data = assigns(:heatmap_data)
 
  50       assert_not_nil first_response_data, "Expected heatmap data to be assigned on the first request"
 
  51       assert_equal 1, first_response_data[:data].values.count { |day| day[:total_changes].positive? }, "Expected one entry in the heatmap data"
 
  53       # Inspect cache after the first request
 
  54       cached_data = Rails.cache.read("heatmap_data_of_user_#{user.id}")
 
  55       assert_equal first_response_data, cached_data, "Expected the cache to contain the first response data"
 
  57       # Add a new changeset to the database
 
  58       create(:changeset, :user => user, :created_at => 3.months.ago, :num_changes => 20)
 
  61       get user_heatmap_path(user)
 
  62       second_response_data = assigns(:heatmap_data)
 
  64       # Confirm that the cache is still being used
 
  65       assert_equal first_response_data, second_response_data, "Expected cached data to be returned on the second request"
 
  67       # Clear the cache and make a third request to confirm new data is retrieved
 
  69       get user_heatmap_path(user)
 
  70       third_response_data = assigns(:heatmap_data)
 
  72       # Ensure the new entry is now included
 
  73       assert_equal 2, third_response_data[:data].values.count { |day| day[:total_changes].positive? }, "Expected two entries in the heatmap data after clearing the cache"
 
  75       # Reset caching config to defaults
 
  77       Rails.cache = @original_cache_store
 
  80     def test_show_data_no_changesets
 
  83       get user_heatmap_path(user)
 
  85       assert_response :success
 
  86       assert_empty(assigns(:heatmap_data)[:data].values)
 
  87       assert_select ".heatmap", :count => 0
 
  90     def test_show_rendering_of_user_with_no_changesets
 
  91       user_without_changesets = create(:user)
 
  93       get user_heatmap_path(user_without_changesets)
 
  95       assert_response :success
 
  96       assert_select ".heatmap", 0
 
  99     def test_show_rendering_of_user_with_changesets
 
 101       changeset39 = create(:changeset, :user => user, :created_at => 4.months.ago.beginning_of_day, :num_changes => 39)
 
 102       _changeset5 = create(:changeset, :user => user, :created_at => 3.months.ago.beginning_of_day, :num_changes => 5)
 
 103       changeset11 = create(:changeset, :user => user, :created_at => 3.months.ago.beginning_of_day, :num_changes => 11)
 
 105       get user_heatmap_path(user)
 
 107       assert_response :success
 
 108       assert_select ".heatmap a", 2
 
 110       history_path = user_history_path(user)
 
 111       assert_select ".heatmap a[data-date='#{4.months.ago.to_date}'][data-count='39'][href='#{history_path}?before=#{changeset39.id + 1}']"
 
 112       assert_select ".heatmap a[data-date='#{3.months.ago.to_date}'][data-count='16'][href='#{history_path}?before=#{changeset11.id + 1}']"
 
 113       assert_select ".heatmap [data-date='#{5.months.ago.to_date}']:not([data-count])"
 
 116     def test_headline_changeset_zero
 
 119       get user_heatmap_path(user)
 
 121       assert_response :success
 
 122       assert_select "h2.text-body-secondary.fs-5", :count => 0
 
 125     def test_headline_changeset_singular
 
 127       create(:changeset, :user => user, :created_at => 4.months.ago.beginning_of_day, :num_changes => 1)
 
 129       get user_heatmap_path(user)
 
 131       assert_response :success
 
 132       assert_select "h2.text-body-secondary.fs-5", :text => "1 contribution in the last year"
 
 135     def test_headline_changeset_plural
 
 137       create(:changeset, :user => user, :created_at => 4.months.ago.beginning_of_day, :num_changes => 12)
 
 139       get user_heatmap_path(user)
 
 141       assert_response :success
 
 142       assert_select "h2.text-body-secondary.fs-5", :text => "12 contributions in the last year"