autocomplete: refactor javascript to use /autocomplete endpoint.

This refactors the autocomplete Javascript to use a single dedicated
/autocomplete.json endpoint instead of a bunch of separate endpoints.

This simplifies the autocomplete Javascript by making it so that instead
of calling a different endpoint for each type of query (for users, wiki
pages, pools, artists, etc), then having to parse the results of each
call to get the data we need, we can call a single endpoint that returns
exactly what we need.

This also means we don't have to parse searches clientside in order to
autocomplete metatags. Instead we can just pass the search term to the
server and let it parse the search, which is easy to do serverside.

Finally, this makes autocomplete easier to test, and it makes it easier
to add more sophisticated autocomplete behavior, since most of the logic
lives serverside.
This commit is contained in:
evazion
2020-12-13 00:45:22 -06:00
parent 1484f8852c
commit adc1c2c2cc
8 changed files with 345 additions and 251 deletions

View File

@@ -1,6 +1,17 @@
require "test_helper"
class AutocompleteControllerTest < ActionDispatch::IntegrationTest
def autocomplete(query, type)
get autocomplete_index_path(search: { query: query, type: type }), as: :json
assert_response :success
response.parsed_body.map { |result| result["value"] }
end
def assert_autocomplete_equals(expected_value, query, type)
assert_equal(expected_value, autocomplete(query, type))
end
context "Autocomplete controller" do
context "index action" do
setup do
@@ -8,9 +19,20 @@ class AutocompleteControllerTest < ActionDispatch::IntegrationTest
end
should "work for opensearch queries" do
get autocomplete_index_path(query: "azur", variant: "opensearch"), as: :json
get autocomplete_index_path(search: { query: "azur", type: "opensearch" }), as: :json
assert_response :success
assert_equal(["azur", ["azur lane"]], response.parsed_body)
assert_equal(["azur", ["azur_lane"]], response.parsed_body)
end
should "work for tag queries" do
assert_autocomplete_equals(["azur_lane"], "azur", "tag_query")
assert_autocomplete_equals(["azur_lane"], "-azur", "tag_query")
assert_autocomplete_equals(["azur_lane"], "~azur", "tag_query")
assert_autocomplete_equals(["azur_lane"], "AZUR", "tag_query")
assert_autocomplete_equals(["rating:safe"], "rating:s", "tag_query")
assert_autocomplete_equals(["rating:safe"], "-rating:s", "tag_query")
end
end
end