Add test/hk_search_pipeline_tests.erl
Browse files
test/hk_search_pipeline_tests.erl
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
-module(hk_search_pipeline_tests).
|
| 2 |
+
-include_lib("eunit/include/eunit.hrl").
|
| 3 |
+
-include("hyperkitty.hrl").
|
| 4 |
+
|
| 5 |
+
normalize_url_lowercases_and_strips_trailing_slash_test() ->
|
| 6 |
+
?assertEqual(<<"https://example.com/path">>,
|
| 7 |
+
hk_search_pipeline:normalize_url(<<"HTTPS://Example.COM/path/">>)).
|
| 8 |
+
|
| 9 |
+
normalize_url_drops_fragment_test() ->
|
| 10 |
+
Normalized = hk_search_pipeline:normalize_url(<<"https://example.com/x#section-2">>),
|
| 11 |
+
?assertEqual(nomatch, binary:match(Normalized, <<"#">>)).
|
| 12 |
+
|
| 13 |
+
dedupe_by_keeps_first_occurrence_in_order_test() ->
|
| 14 |
+
Items = [#{normalized_url => <<"a">>, n => 1},
|
| 15 |
+
#{normalized_url => <<"b">>, n => 2},
|
| 16 |
+
#{normalized_url => <<"a">>, n => 3}],
|
| 17 |
+
Deduped = hk_search_pipeline:dedupe_by(normalized_url, Items),
|
| 18 |
+
?assertEqual([<<"a">>, <<"b">>], [maps:get(normalized_url, I) || I <- Deduped]),
|
| 19 |
+
?assertEqual(1, maps:get(n, hd(Deduped))).
|
| 20 |
+
|
| 21 |
+
rank_sources_prefers_extracted_documents_test() ->
|
| 22 |
+
Results = [
|
| 23 |
+
#{url => <<"https://a.example/">>, normalized_url => <<"https://a.example">>,
|
| 24 |
+
title => <<"A">>, snippet => <<"snip">>},
|
| 25 |
+
#{url => <<"https://b.example/">>, normalized_url => <<"https://b.example">>,
|
| 26 |
+
title => <<"B">>, snippet => <<"snip">>}
|
| 27 |
+
],
|
| 28 |
+
%% Only "b" has an associated extracted document -> it must rank first
|
| 29 |
+
%% despite appearing second in the provider's own ordering.
|
| 30 |
+
Docs = #{<<"https://b.example/">> =>
|
| 31 |
+
#source_document{document_id = <<"doc1">>, url = <<"https://b.example/">>,
|
| 32 |
+
fetched_at_ms = 0, extracted_text = <<"some content here">>,
|
| 33 |
+
citation_map = #{}}},
|
| 34 |
+
[First | _] = hk_search_pipeline:rank_sources(Results, Docs, <<"job1">>),
|
| 35 |
+
?assertEqual(<<"https://b.example/">>, First#search_result.url).
|