Skip to content

Commit

Permalink
test find_search_items/3
Browse files Browse the repository at this point in the history
  • Loading branch information
ruslandoga committed Nov 10, 2024
1 parent 4e8ad21 commit 9003f20
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 26 deletions.
24 changes: 14 additions & 10 deletions lib/hexdocs/search/search.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,28 @@ defmodule Hexdocs.Search do
@spec find_search_items(package, version, [{Path.t(), content :: iodata}]) ::
{proglang, search_items} | nil
def find_search_items(package, version, files) do
search_data_json =
search_data_js =
Enum.find_value(files, fn {path, content} ->
case Path.basename(path) do
"search_data-" <> _digest ->
case content do
"searchData=" <> json -> json
_ -> nil
end

_other ->
nil
"search_data-" <> _digest -> content
_other -> nil
end
end)

unless search_data_json do
unless search_data_js do
Logger.info("Failed to find search data for #{package} #{version}")
end

search_data_json =
case search_data_js do
"searchData=" <> json ->
json

_ ->
Logger.error("Unexpected search_data format for #{package} #{version}")
nil
end

search_data =
if search_data_json do
case Jason.decode(search_data_json) do
Expand Down
143 changes: 127 additions & 16 deletions test/hexdocs/search_test.exs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
defmodule Hexdocs.SearchTest do
use ExUnit.Case
import ExUnit.CaptureLog
alias Hexdocs.Search.Typesense

@moduletag :typesense
Expand All @@ -20,31 +21,32 @@ defmodule Hexdocs.SearchTest do
:ok
end

test "indexes public search_data", %{test: test} do
search_data = """
searchData={"items":[\
{"type":"module","title":"Example","doc":"example text","ref":"Example.html"},\
{"type":"function","title":"Example.test/4","doc":"does example things","ref":"Example.html#test/4"}\
],"content_type":"text/markdown","producer":{"name":"ex_doc","version":[48,46,51,52,46,50]}}\
"""

key = "docs/#{test}-1.0.0.tar.gz"

tar =
Hexdocs.Tar.create([
{"index.html", "contents"},
{"dist/search_data-0F918FFD.js", search_data}
])

defp run_queue(:put, package, version, files) do
tar = Hexdocs.Tar.create(files)
key = "docs/#{package}-#{version}.tar.gz"
Hexdocs.Store.put!(:repo_bucket, key, tar)
ref = Broadway.test_message(Hexdocs.Queue, queue_put_message(key))
assert_receive {:ack, ^ref, [_], []}
end

test "indexes public search_data", %{test: test} do
run_queue(:put, _package = test, _version = "1.0.0", [
{"index.html", "contents"},
{"dist/search_data-0F918FFD.js",
"""
searchData={"items":[\
{"type":"module","title":"Example","doc":"example text","ref":"Example.html"},\
{"type":"function","title":"Example.test/4","doc":"does example things","ref":"Example.html#test/4"}\
],"content_type":"text/markdown","producer":{"name":"ex_doc","version":[48,46,51,52,46,50]}}\
"""}
])

assert [
%{
"document" => %{
"doc" => "example text",
"id" => "0",
"proglang" => "elixir",
"package" => "test indexes public search_data-1.0.0",
"ref" => "Example.html",
"title" => "Example",
Expand All @@ -55,6 +57,7 @@ defmodule Hexdocs.SearchTest do
"document" => %{
"doc" => "does example things",
"id" => "1",
"proglang" => "elixir",
"package" => "test indexes public search_data-1.0.0",
"ref" => "Example.html#test/4",
"title" => "Example.test/4",
Expand All @@ -68,6 +71,7 @@ defmodule Hexdocs.SearchTest do
"document" => %{
"doc" => "does example things",
"id" => "1",
"proglang" => "elixir",
"package" => "test indexes public search_data-1.0.0",
"ref" => "Example.html#test/4",
"title" => "Example.test/4",
Expand Down Expand Up @@ -114,4 +118,111 @@ defmodule Hexdocs.SearchTest do
assert %{"hits" => hits} = Jason.decode!(body)
hits
end

describe "find_search_items/3" do
test "extracts proglang from search items" do
files = [
{"index.html", "contents"},
{"dist/search_data-0F918FFD.js",
"""
searchData={"items":[\
{"type":"function","title":"Example.test/4","doc":"does example things","ref":"Example.html#test/4"},\
{"type":"module","title":"Example","doc":"example text","ref":"Example.html"}\
],"content_type":"text/markdown","producer":{"name":"ex_doc","version":[48,46,51,52,46,50]}}\
"""}
]

assert {"elixir", _search_items} = Hexdocs.Search.find_search_items("test", "1.0.0", files)
end

test "extracts proglang from search_data if available" do
files = [
{"index.html", "contents"},
{"dist/search_data-0F918FFD.js",
"""
searchData={"items":[{"type":"module","title":"Example","doc":"example text","ref":"Example.html"}],\
"content_type":"text/markdown","producer":{"name":"ex_doc","version":[48,46,51,52,46,50]},\
"proglang": "erlang"}\
"""}
]

assert {"erlang", _search_items} = Hexdocs.Search.find_search_items("test", "1.0.0", files)
end

test "logs an info message if search_data is not found" do
files = [
{"index.html", "contents"}
]

original_log_level = Logger.level()
Logger.configure(level: :info)
on_exit(fn -> Logger.configure(level: original_log_level) end)

log =
capture_log(fn ->
refute Hexdocs.Search.find_search_items("package_name", "1.0.0", files)
end)

assert log =~ "[info] Failed to find search data for package_name 1.0.0"
end

test "logs an error message if search_data.js file has unexpected format" do
files = [
{"index.html", "contents"},
{"dist/search_data-0F918FFD.js", "unexpected format"}
]

log =
capture_log(fn ->
refute Hexdocs.Search.find_search_items("package_name", "1.0.0", files)
end)

assert log =~ "[error] Unexpected search_data format for package_name 1.0.0"
end

test "logs an error message if search_data.json cannot be decoded" do
files = [
{"index.html", "contents"},
{"dist/search_data-0F918FFD.js", "searchData={\"items\":["}
]

log =
capture_log(fn ->
refute Hexdocs.Search.find_search_items("package_name", "1.0.0", files)
end)

assert log =~
"[error] Failed to decode search data json for package_name 1.0.0: unexpected end of input at position 10"
end

test "logs an error message if search_data has empty items" do
files = [
{"index.html", "contents"},
{"dist/search_data-0F918FFD.js", "searchData={\"items\":[]}"}
]

log =
capture_log(fn ->
refute Hexdocs.Search.find_search_items("package_name", "1.0.0", files)
end)

assert log =~
"[error] Failed to extract search items and proglang from search data for package_name 1.0.0"
end

test "logs an error message if search_data has no items" do
files = [
{"index.html", "contents"},
{"dist/search_data-0F918FFD.js", "searchData={\"not_items\":[]}"}
]

log =
capture_log(fn ->
refute Hexdocs.Search.find_search_items("package_name", "1.0.0", files)
end)

assert log =~
"[error] Failed to extract search items and proglang from search data for package_name 1.0.0"
end
end
end

0 comments on commit 9003f20

Please sign in to comment.