diff --git a/app/controllers/search_controller.rb b/app/controllers/search_controller.rb index c0cf0f62..58799bc6 100644 --- a/app/controllers/search_controller.rb +++ b/app/controllers/search_controller.rb @@ -45,7 +45,7 @@ def results # Render the response in HTML or JSON format respond_to do |format| - format.json { render json: { results: @results, pagination: @pagination, errors: @errors } } + format.json { render json: { results: @results, hits: hit_count, errors: @errors } } format.turbo_stream { render :results } format.html { render :results } end @@ -53,6 +53,15 @@ def results private + # This returns the total result count from whichever instance variable is present + def hit_count + if @pagination_load_more_enabled + @load_more&.[](:total_hits) || 0 + else + @pagination&.[](:hits) || 0 + end + end + # Sleep to simulate latency for testing loading indicators when responses are fast def sleep_if_too_fast start_time = Time.now diff --git a/test/controllers/search_controller_test.rb b/test/controllers/search_controller_test.rb index 0062ed31..95b19c82 100644 --- a/test/controllers/search_controller_test.rb +++ b/test/controllers/search_controller_test.rb @@ -1264,6 +1264,34 @@ def source_filter_count(controller) get "/results?q=test&format=json&format_token=#{secret_value}", headers: { 'HTTP_USER_AGENT' => quepid_ua } assert_response :success assert_equal 'application/json; charset=utf-8', response.content_type + response_json = JSON.parse(response.body) + assert_equal %w[results hits errors], response_json.keys + assert_instance_of Array, response_json['results'] + assert_instance_of Integer, response_json['hits'] + end + end + + test 'results in JSON format will report zero hit count in the face of missing data' do + SearchController.any_instance.stubs(:load_timdex_results).returns(nil) + secret_value = 'sooper_sekret' + quepid_ua = 'Quepid/1.0 (Web Scraper)' + # Test @pagination pathway + ClimateControl.modify FORMAT_TOKEN: secret_value do + get "/results?q=test&tab=timdex&format=json&format_token=#{secret_value}", + headers: { 'HTTP_USER_AGENT' => quepid_ua } + assert_response :success + response_json = JSON.parse(response.body) + assert_equal %w[results hits errors], response_json.keys + assert_equal 0, response_json['hits'] + end + # Test @load_more pathway + ClimateControl.modify(FORMAT_TOKEN: secret_value, FEATURE_PAGINATION_LOAD_MORE: 'true') do + get "/results?q=test&tab=timdex&format=json&format_token=#{secret_value}", + headers: { 'HTTP_USER_AGENT' => quepid_ua } + assert_response :success + response_json = JSON.parse(response.body) + assert_equal %w[results hits errors], response_json.keys + assert_equal 0, response_json['hits'] end end @@ -1276,6 +1304,25 @@ def source_filter_count(controller) get "/results?q=test&format=json&format_token=#{secret_value}", headers: { 'HTTP_USER_AGENT' => quepid_ua } assert_response :success assert_equal 'application/json; charset=utf-8', response.content_type + response_json = JSON.parse(response.body) + assert_equal %w[results hits errors], response_json.keys + assert_instance_of Array, response_json['results'] + assert_instance_of Integer, response_json['hits'] + end + end + + test 'results can be returned in JSON format when env is set while load-more pagination is enabled' do + secret_value = 'sooper_sekret' + quepid_ua = 'Quepid/1.0 (Web Scraper)' + ClimateControl.modify(FORMAT_TOKEN: secret_value, FEATURE_PAGINATION_LOAD_MORE: 'true') do + mock_timdex_search_with_hits(10) + get "/results?q=test&format=json&format_token=#{secret_value}", headers: { 'HTTP_USER_AGENT' => quepid_ua } + assert_response :success + assert_equal 'application/json; charset=utf-8', response.content_type + response_json = JSON.parse(response.body) + assert_equal %w[results hits errors], response_json.keys + assert_instance_of Array, response_json['results'] + assert_instance_of Integer, response_json['hits'] end end