| 1 |
describe("Basic html theme search", function () { |
| 2 |
function loadFixture(name) { |
| 3 |
req = new XMLHttpRequest(); |
| 4 |
req.open("GET", `__src__/tests/js/fixtures/${name}`, false); |
| 5 |
req.send(null); |
| 6 |
return req.responseText; |
| 7 |
} |
| 8 |
|
| 9 |
function checkRanking(expectedRanking, results) { |
| 10 |
let [nextExpected, ...remainingItems] = expectedRanking; |
| 11 |
|
| 12 |
for (result of results.reverse()) { |
| 13 |
if (!nextExpected) break; |
| 14 |
|
| 15 |
let [expectedPage, expectedTitle, expectedTarget] = nextExpected; |
| 16 |
let [page, title, target] = result; |
| 17 |
|
| 18 |
if ( |
| 19 |
page == expectedPage |
| 20 |
&& title == expectedTitle |
| 21 |
&& target == expectedTarget |
| 22 |
) { |
| 23 |
[nextExpected, ...remainingItems] = remainingItems; |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
expect(remainingItems.length).toEqual(0); |
| 28 |
expect(nextExpected).toEqual(undefined); |
| 29 |
} |
| 30 |
|
| 31 |
describe("terms search", function () { |
| 32 |
it('should find "C++" when in index', function () { |
| 33 |
eval(loadFixture("cpp/searchindex.js")); |
| 34 |
|
| 35 |
[_searchQuery, searchterms, excluded, ..._remainingItems] = |
| 36 |
Search._parseQuery("C++"); |
| 37 |
|
| 38 |
// prettier-ignore |
| 39 |
hits = [[ |
| 40 |
"index", |
| 41 |
"<no title>", |
| 42 |
"", |
| 43 |
null, |
| 44 |
5, |
| 45 |
"index.rst", |
| 46 |
"text" |
| 47 |
]]; |
| 48 |
expect(Search.performTermsSearch(searchterms, excluded)).toEqual(hits); |
| 49 |
}); |
| 50 |
|
| 51 |
it("should be able to search for multiple terms", function () { |
| 52 |
eval(loadFixture("multiterm/searchindex.js")); |
| 53 |
|
| 54 |
[_searchQuery, searchterms, excluded, ..._remainingItems] = |
| 55 |
Search._parseQuery("main page"); |
| 56 |
// prettier-ignore |
| 57 |
hits = [[ |
| 58 |
'index', |
| 59 |
'Main Page', |
| 60 |
'', |
| 61 |
null, |
| 62 |
15, |
| 63 |
'index.rst', |
| 64 |
'text' |
| 65 |
]]; |
| 66 |
expect(Search.performTermsSearch(searchterms, excluded)).toEqual(hits); |
| 67 |
}); |
| 68 |
|
| 69 |
it('should partially-match "sphinx" when in title index', function () { |
| 70 |
eval(loadFixture("partial/searchindex.js")); |
| 71 |
|
| 72 |
[_searchQuery, searchterms, excluded, ..._remainingItems] = |
| 73 |
Search._parseQuery("sphinx"); |
| 74 |
|
| 75 |
// prettier-ignore |
| 76 |
hits = [[ |
| 77 |
"index", |
| 78 |
"sphinx_utils module", |
| 79 |
"", |
| 80 |
null, |
| 81 |
7, |
| 82 |
"index.rst", |
| 83 |
"text" |
| 84 |
]]; |
| 85 |
expect(Search.performTermsSearch(searchterms, excluded)).toEqual(hits); |
| 86 |
}); |
| 87 |
|
| 88 |
it('should partially-match within "possible" when in term index', function () { |
| 89 |
eval(loadFixture("partial/searchindex.js")); |
| 90 |
|
| 91 |
[_searchQuery, searchterms, excluded, ..._remainingItems] = |
| 92 |
Search._parseQuery("ossibl"); |
| 93 |
terms = Search._index.terms; |
| 94 |
titleterms = Search._index.titleterms; |
| 95 |
|
| 96 |
// prettier-ignore |
| 97 |
hits = [[ |
| 98 |
"index", |
| 99 |
"sphinx_utils module", |
| 100 |
"", |
| 101 |
null, |
| 102 |
2, |
| 103 |
"index.rst", |
| 104 |
"text" |
| 105 |
]]; |
| 106 |
expect( |
| 107 |
Search.performTermsSearch(searchterms, excluded, terms, titleterms), |
| 108 |
).toEqual(hits); |
| 109 |
}); |
| 110 |
}); |
| 111 |
|
| 112 |
describe("aggregation of search results", function () { |
| 113 |
it("should combine document title and document term matches", function () { |
| 114 |
eval(loadFixture("multiterm/searchindex.js")); |
| 115 |
|
| 116 |
searchParameters = Search._parseQuery("main page"); |
| 117 |
|
| 118 |
// prettier-ignore |
| 119 |
hits = [ |
| 120 |
[ |
| 121 |
'index', |
| 122 |
'Main Page', |
| 123 |
'', |
| 124 |
null, |
| 125 |
16, |
| 126 |
'index.rst', |
| 127 |
'title' |
| 128 |
] |
| 129 |
]; |
| 130 |
expect(Search._performSearch(...searchParameters)).toEqual(hits); |
| 131 |
}); |
| 132 |
}); |
| 133 |
|
| 134 |
describe("search result ranking", function () { |
| 135 |
/* |
| 136 |
* These tests should not proscribe precise expected ordering of search |
| 137 |
* results; instead each test case should describe a single relevance rule |
| 138 |
* that helps users to locate relevant information efficiently. |
| 139 |
* |
| 140 |
* If you think that one of the rules seems to be poorly-defined or is |
| 141 |
* limiting the potential for search algorithm improvements, please check |
| 142 |
* for existing discussion/bugreports related to it on GitHub[1] before |
| 143 |
* creating one yourself. Suggestions for possible improvements are also |
| 144 |
* welcome. |
| 145 |
* |
| 146 |
* [1] - https://github.com/sphinx-doc/sphinx.git/ |
| 147 |
*/ |
| 148 |
|
| 149 |
it("should score a code module match above a page-title match", function () { |
| 150 |
eval(loadFixture("titles/searchindex.js")); |
| 151 |
|
| 152 |
// prettier-ignore |
| 153 |
expectedRanking = [ |
| 154 |
['index', 'relevance', '#module-relevance'], /* py:module documentation */ |
| 155 |
['relevance', 'Relevance', ''], /* main title */ |
| 156 |
]; |
| 157 |
|
| 158 |
searchParameters = Search._parseQuery("relevance"); |
| 159 |
results = Search._performSearch(...searchParameters); |
| 160 |
|
| 161 |
checkRanking(expectedRanking, results); |
| 162 |
}); |
| 163 |
|
| 164 |
it("should score a main-title match above an object member match", function () { |
| 165 |
eval(loadFixture("titles/searchindex.js")); |
| 166 |
|
| 167 |
// prettier-ignore |
| 168 |
expectedRanking = [ |
| 169 |
['relevance', 'Relevance', ''], /* main title */ |
| 170 |
['index', 'relevance.Example.relevance', '#relevance.Example.relevance'], /* py:class attribute */ |
| 171 |
]; |
| 172 |
|
| 173 |
searchParameters = Search._parseQuery("relevance"); |
| 174 |
results = Search._performSearch(...searchParameters); |
| 175 |
|
| 176 |
checkRanking(expectedRanking, results); |
| 177 |
}); |
| 178 |
|
| 179 |
it("should score a title match above a standard index entry match", function () { |
| 180 |
eval(loadFixture("titles/searchindex.js")); |
| 181 |
|
| 182 |
// prettier-ignore |
| 183 |
expectedRanking = [ |
| 184 |
['relevance', 'Relevance', ''], /* title */ |
| 185 |
['index', 'Main Page', '#index-1'], /* index entry */ |
| 186 |
]; |
| 187 |
|
| 188 |
searchParameters = Search._parseQuery("relevance"); |
| 189 |
results = Search._performSearch(...searchParameters); |
| 190 |
|
| 191 |
checkRanking(expectedRanking, results); |
| 192 |
}); |
| 193 |
|
| 194 |
it("should score a priority index entry match above a title match", function () { |
| 195 |
eval(loadFixture("titles/searchindex.js")); |
| 196 |
|
| 197 |
// prettier-ignore |
| 198 |
expectedRanking = [ |
| 199 |
['index', 'Main Page', '#index-0'], /* index entry */ |
| 200 |
['index', 'Main Page > Result Scoring', '#result-scoring'], /* title */ |
| 201 |
]; |
| 202 |
|
| 203 |
searchParameters = Search._parseQuery("scoring"); |
| 204 |
results = Search._performSearch(...searchParameters); |
| 205 |
|
| 206 |
checkRanking(expectedRanking, results); |
| 207 |
}); |
| 208 |
|
| 209 |
it("should score a main-title match above a subheading-title match", function () { |
| 210 |
eval(loadFixture("titles/searchindex.js")); |
| 211 |
|
| 212 |
// prettier-ignore |
| 213 |
expectedRanking = [ |
| 214 |
['relevance', 'Relevance', ''], /* main title */ |
| 215 |
['index', 'Main Page > Relevance', '#relevance'], /* subsection heading title */ |
| 216 |
]; |
| 217 |
|
| 218 |
searchParameters = Search._parseQuery("relevance"); |
| 219 |
results = Search._performSearch(...searchParameters); |
| 220 |
|
| 221 |
checkRanking(expectedRanking, results); |
| 222 |
}); |
| 223 |
}); |
| 224 |
|
| 225 |
describe("can handle edge-case search queries", function () { |
| 226 |
it("does not find the javascript prototype property in unrelated documents", function () { |
| 227 |
eval(loadFixture("partial/searchindex.js")); |
| 228 |
|
| 229 |
searchParameters = Search._parseQuery("__proto__"); |
| 230 |
|
| 231 |
// prettier-ignore |
| 232 |
hits = []; |
| 233 |
expect(Search._performSearch(...searchParameters)).toEqual(hits); |
| 234 |
}); |
| 235 |
}); |
| 236 |
}); |
| 237 |
|
| 238 |
describe("htmlToText", function () { |
| 239 |
const testHTML = `<html> |
| 240 |
<body> |
| 241 |
<script src="directory/filename.js"></script> |
| 242 |
<div class="body" role="main"> |
| 243 |
<script> |
| 244 |
console.log('dynamic'); |
| 245 |
</script> |
| 246 |
<style> |
| 247 |
div.body p.centered { |
| 248 |
text-align: center; |
| 249 |
margin-top: 25px; |
| 250 |
} |
| 251 |
</style> |
| 252 |
<!-- main content --> |
| 253 |
<section id="getting-started"> |
| 254 |
<h1>Getting Started <a class="headerlink" href="#getting-started" title="Link to this heading">¶</a></h1> |
| 255 |
<p>Some text</p> |
| 256 |
</section> |
| 257 |
<section id="other-section"> |
| 258 |
<h1>Other Section <a class="headerlink" href="#other-section" title="Link to this heading">¶</a></h1> |
| 259 |
<p>Other text</p> |
| 260 |
</section> |
| 261 |
<section id="yet-another-section"> |
| 262 |
<h1>Yet Another Section <a class="headerlink" href="#yet-another-section" title="Link to this heading">¶</a></h1> |
| 263 |
<p>More text</p> |
| 264 |
</section> |
| 265 |
</div> |
| 266 |
</body> |
| 267 |
</html>`; |
| 268 |
|
| 269 |
it("basic case", () => { |
| 270 |
expect(Search.htmlToText(testHTML).trim().split(/\s+/)).toEqual( |
| 271 |
/* prettier-ignore */ [ |
| 272 |
"Getting", "Started", "Some", "text", |
| 273 |
"Other", "Section", "Other", "text", |
| 274 |
"Yet", "Another", "Section", "More", "text" |
| 275 |
], |
| 276 |
); |
| 277 |
}); |
| 278 |
|
| 279 |
it("will start reading from the anchor", () => { |
| 280 |
expect( |
| 281 |
Search.htmlToText(testHTML, "#other-section").trim().split(/\s+/), |
| 282 |
).toEqual(["Other", "Section", "Other", "text"]); |
| 283 |
}); |
| 284 |
}); |
| 285 |
|
| 286 |
// Regression test for https://github.com/sphinx-doc/sphinx/issues/3150 |
| 287 |
describe("splitQuery regression tests", () => { |
| 288 |
it("can split English words", () => { |
| 289 |
const parts = splitQuery(" Hello World "); |
| 290 |
expect(parts).toEqual(["Hello", "World"]); |
| 291 |
}); |
| 292 |
|
| 293 |
it("can split special characters", () => { |
| 294 |
const parts = splitQuery("Pin-Code"); |
| 295 |
expect(parts).toEqual(["Pin", "Code"]); |
| 296 |
}); |
| 297 |
|
| 298 |
it("can split Chinese characters", () => { |
| 299 |
const parts = splitQuery("Hello from 中国 上海"); |
| 300 |
expect(parts).toEqual(["Hello", "from", "中国", "上海"]); |
| 301 |
}); |
| 302 |
|
| 303 |
it("can split Emoji (surrogate pair) characters. It should keep emojis.", () => { |
| 304 |
const parts = splitQuery("😁😁"); |
| 305 |
expect(parts).toEqual(["😁😁"]); |
| 306 |
}); |
| 307 |
|
| 308 |
it("can split umlauts. It should keep umlauts.", () => { |
| 309 |
const parts = splitQuery("Löschen Prüfung Abändern ærlig spørsmål"); |
| 310 |
// prettier-ignore |
| 311 |
expect(parts).toEqual(["Löschen", "Prüfung", "Abändern", "ærlig", "spørsmål"]) |
| 312 |
}); |
| 313 |
}); |