Expose which search strategy produced each result

query_packages now assigns @search_sources - a hash mapping package
ids to the matching strategy that put them into the results: :exact,
:name_prefix, :name_contains, :semantic or :fulltext. The grid and
list views render a small badge with a humanized label per result,
making the search tiers observable. semantic_results() was folded
into search_packages() so each branch tags its own source. Adds the
rails-controller-testing gem for assigns() in tests.
This commit is contained in:
Christoph Haas 2026-08-23 10:39:50 +02:00
parent 61b64fae25
commit 323b7004ed
6 changed files with 69 additions and 24 deletions

View file

@ -28,6 +28,8 @@ class PackagesControllerTest < ActionController::TestCase
get :grid, params: { search: 'vim' }
assert_response :success
assert_select 'div', 'vim'
assert_equal({ packages(:vim).id => :exact }, assigns(:search_sources))
assert_select '.search-source .label', 'exact match'
end
end
@ -39,6 +41,7 @@ class PackagesControllerTest < ActionController::TestCase
# the grid renders every package in the database when no search
# filter applies - so an empty result must not fall back to that
assert_select 'div', text: /package-\d+/, count: 0
assert_empty assigns(:search_sources)
end
end
@ -51,6 +54,9 @@ class PackagesControllerTest < ActionController::TestCase
assert_select 'div', 'package-0'
# ...and before any semantically found package
assert response.body.index('package-0') < response.body.index('vim')
sources = assigns(:search_sources)
assert_equal :name_prefix, sources[Package.find_by(name: 'package-0').id]
assert_equal :semantic, sources[packages(:vim).id]
end
end
@ -60,6 +66,8 @@ class PackagesControllerTest < ActionController::TestCase
get :grid, params: { search: 'fire' }
assert_response :success
assert_select 'div', text: 'firefox', count: 1
# the higher tier wins the attribution
assert_equal :name_prefix, assigns(:search_sources)[packages(:firefox).id]
end
end
@ -70,6 +78,9 @@ class PackagesControllerTest < ActionController::TestCase
get :grid, params: { search: 'fire fox' }
assert_response :success
assert_select 'div', text: 'firefox', count: 1
assert_equal({ packages(:firefox).id => :name_contains },
assigns(:search_sources))
assert_select '.search-source .label', 'name match'
end
end
@ -94,6 +105,7 @@ class PackagesControllerTest < ActionController::TestCase
get :grid, params: { search: 'editor' }
assert_response :success
assert_select 'div', 'vim'
assert_equal({ packages(:vim).id => :semantic }, assigns(:search_sources))
end
end
@ -109,6 +121,7 @@ class PackagesControllerTest < ActionController::TestCase
assert_response :success
assert_select 'div', 'vim'
assert flash[:error].present?
assert_equal({ packages(:vim).id => :fulltext }, assigns(:search_sources))
end
end