vomebook commited on
Commit ·
0ababbd
1
Parent(s): e970b77
Fix Reader search page retry and stale navigation
Browse files- static/reader.js +33 -7
- tests/test_reader_refactor.py +101 -0
static/reader.js
CHANGED
|
@@ -413,6 +413,7 @@ let chapterSearchConfiguration = null;
|
|
| 413 |
let chapterSearchModulePromise = null;
|
| 414 |
let chapterSearchClient = null;
|
| 415 |
let chapterSearchPage = null;
|
|
|
|
| 416 |
let chapterSearchInProgress = false;
|
| 417 |
let fullSearchComplete = false;
|
| 418 |
trackReaderResource(() => {
|
|
@@ -4854,7 +4855,15 @@ fullSearchRetry.className = "text-action";
|
|
| 4854 |
fullSearchRetry.textContent = "重试搜索";
|
| 4855 |
fullSearchRetry.hidden = true;
|
| 4856 |
fullSearchCancel.after(fullSearchRetry);
|
| 4857 |
-
fullSearchRetry.addEventListener("click",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4858 |
fullSearchCancel.addEventListener("click", () => {
|
| 4859 |
nextReaderGeneration("search");
|
| 4860 |
beginReaderNavigation();
|
|
@@ -4862,12 +4871,14 @@ fullSearchCancel.addEventListener("click", () => {
|
|
| 4862 |
pdfSearchInProgress = false;
|
| 4863 |
chapterSearchInProgress = false;
|
| 4864 |
chapterSearchPage = null;
|
|
|
|
| 4865 |
pdfSearchPageLoader = null;
|
| 4866 |
fullSearchComplete = false;
|
| 4867 |
updateSearchState({ results: [], index: -1 });
|
| 4868 |
renderFullSearchResults();
|
| 4869 |
fullSearchCancel.hidden = true;
|
| 4870 |
fullSearchRetry.hidden = false;
|
|
|
|
| 4871 |
fullSearchStatus.textContent = "搜索已取消";
|
| 4872 |
});
|
| 4873 |
chapterSearchPagination
|
|
@@ -4986,6 +4997,7 @@ function updateChapterSearchPagination() {
|
|
| 4986 |
async function loadChapterSearchPage(offset, selectLast = null) {
|
| 4987 |
if (pdfSearchInProgress || chapterSearchInProgress || !chapterSearchPage ||
|
| 4988 |
(!chapterSearchClient && !pdfSearchPageLoader)) return;
|
|
|
|
| 4989 |
const generation = nextReaderGeneration("search");
|
| 4990 |
beginReaderNavigation();
|
| 4991 |
fullSearchCancel.hidden = false;
|
|
@@ -5006,6 +5018,8 @@ async function loadChapterSearchPage(offset, selectLast = null) {
|
|
| 5006 |
} catch (error) {
|
| 5007 |
if (isReaderGenerationCurrent("search", generation)) {
|
| 5008 |
fullSearchStatus.textContent = error.message || "搜索结果加载失败";
|
|
|
|
|
|
|
| 5009 |
fullSearchRetry.hidden = false;
|
| 5010 |
}
|
| 5011 |
} finally {
|
|
@@ -5159,7 +5173,8 @@ function fullSearchLocation(node, fallback, offset, total) {
|
|
| 5159 |
}
|
| 5160 |
return `正文 ${Math.min(100, Math.floor(offset / Math.max(1, total) * 100))}%`;
|
| 5161 |
}
|
| 5162 |
-
async function fullSearchDomMatches(root, fallback, query, generation, progressive = false,
|
|
|
|
| 5163 |
root = foliateSectionRoot(root);
|
| 5164 |
const nodes = await fullSearchTextNodes(root, generation);
|
| 5165 |
if (!nodes || !isReaderGenerationCurrent("search", generation)) return [];
|
|
@@ -5176,7 +5191,6 @@ async function fullSearchDomMatches(root, fallback, query, generation, progressi
|
|
| 5176 |
}
|
| 5177 |
}
|
| 5178 |
const text = chunks.join(""), groups = [], firstHits = [];
|
| 5179 |
-
const navigationGeneration = readerRuntime.currentGeneration("navigation");
|
| 5180 |
const patternText = fullSearchEscape(query);
|
| 5181 |
let nextStart = 0, total = 0;
|
| 5182 |
const locateIndex = (index) => {
|
|
@@ -5208,6 +5222,7 @@ async function fullSearchDomMatches(root, fallback, query, generation, progressi
|
|
| 5208 |
location: fullSearchLocation(part.node, fallback, match.index, text.length),
|
| 5209 |
snippet: fullSearchSnippet(text, match.index, match.value.length),
|
| 5210 |
async activate(navigationGeneration = readerRuntime.currentGeneration("navigation")) {
|
|
|
|
| 5211 |
const searchGeneration = readerRuntime.currentGeneration("search");
|
| 5212 |
clearFullSearchMarks();
|
| 5213 |
const currentNodes = await fullSearchTextNodes(root, searchGeneration);
|
|
@@ -5269,9 +5284,10 @@ async function fullSearchDomMatches(root, fallback, query, generation, progressi
|
|
| 5269 |
chapterSearchPage = page(0);
|
| 5270 |
}
|
| 5271 |
if (occurrence !== null) {
|
|
|
|
| 5272 |
const matchedPage = page(occurrence);
|
| 5273 |
const result = matchedPage.results[occurrence - matchedPage.offset];
|
| 5274 |
-
if (!result || !await result.activate()) return [];
|
| 5275 |
return [{ target: fullSearchActiveMarks[0] }];
|
| 5276 |
}
|
| 5277 |
const fallbackResults = progressive ? null : firstHits.map(resultFor);
|
|
@@ -5315,7 +5331,8 @@ async function navigateFoliateSearchResult(result, generation) {
|
|
| 5315 |
searchState.query,
|
| 5316 |
readerRuntime.currentGeneration("search"),
|
| 5317 |
false,
|
| 5318 |
-
result.occurrence || 0
|
|
|
|
| 5319 |
);
|
| 5320 |
target = matches[0]?.target;
|
| 5321 |
}
|
|
@@ -5544,6 +5561,7 @@ async function runFullSearch() {
|
|
| 5544 |
fullSearchInputTimer = 0;
|
| 5545 |
const query = fullSearchInput.value.trim();
|
| 5546 |
fullSearchComplete = false;
|
|
|
|
| 5547 |
const generation = nextReaderGeneration("search");
|
| 5548 |
beginReaderNavigation();
|
| 5549 |
clearFullSearchMarks();
|
|
@@ -5552,6 +5570,7 @@ async function runFullSearch() {
|
|
| 5552 |
pdfSearchPageLoader = null;
|
| 5553 |
updateChapterSearchPagination();
|
| 5554 |
fullSearchRetry.hidden = true;
|
|
|
|
| 5555 |
fullSearchCancel.hidden = !["epub-chapters", "pdf", "pdf-pages", "foliate"].includes(capability.mode) || !query;
|
| 5556 |
pdfSearchInProgress = ["pdf", "pdf-pages", "foliate"].includes(capability.mode) && !!query;
|
| 5557 |
chapterSearchInProgress = capability.mode === "epub-chapters" && !!query;
|
|
@@ -5656,8 +5675,13 @@ async function activateFullSearchResult(index, closePanel = true) {
|
|
| 5656 |
setReaderPanelOpen(false, true);
|
| 5657 |
else if (!closePanel) fullSearchResultsNode.children[index]?.scrollIntoView({ block: "nearest" });
|
| 5658 |
} catch (error) {
|
| 5659 |
-
if (
|
| 5660 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5661 |
reportNavigationError(error, generation);
|
| 5662 |
}
|
| 5663 |
}
|
|
@@ -5697,6 +5721,7 @@ function toggleFullSearch() {
|
|
| 5697 |
updateFullSearchClear();
|
| 5698 |
fullSearchInput.focus();
|
| 5699 |
if (searchState.query !== fullSearchInput.value.trim() || !fullSearchComplete) runFullSearch();
|
|
|
|
| 5700 |
}
|
| 5701 |
fullSearchButton.addEventListener("click", toggleFullSearch);
|
| 5702 |
function queueFullSearch() {
|
|
@@ -5708,6 +5733,7 @@ function queueFullSearch() {
|
|
| 5708 |
pdfSearchInProgress = false;
|
| 5709 |
chapterSearchInProgress = false;
|
| 5710 |
chapterSearchPage = null;
|
|
|
|
| 5711 |
pdfSearchPageLoader = null;
|
| 5712 |
updateSearchState({ results: [], index: -1 });
|
| 5713 |
renderFullSearchResults();
|
|
|
|
| 413 |
let chapterSearchModulePromise = null;
|
| 414 |
let chapterSearchClient = null;
|
| 415 |
let chapterSearchPage = null;
|
| 416 |
+
let failedSearchPage = null;
|
| 417 |
let chapterSearchInProgress = false;
|
| 418 |
let fullSearchComplete = false;
|
| 419 |
trackReaderResource(() => {
|
|
|
|
| 4855 |
fullSearchRetry.textContent = "重试搜索";
|
| 4856 |
fullSearchRetry.hidden = true;
|
| 4857 |
fullSearchCancel.after(fullSearchRetry);
|
| 4858 |
+
fullSearchRetry.addEventListener("click", async () => {
|
| 4859 |
+
const failed = failedSearchPage;
|
| 4860 |
+
if (!failed || searchState.query !== failed.query) return runFullSearch();
|
| 4861 |
+
if (chapterSearchClient?.failed) {
|
| 4862 |
+
await runFullSearch();
|
| 4863 |
+
if (!fullSearchComplete || searchState.query !== failed.query) return;
|
| 4864 |
+
}
|
| 4865 |
+
return loadChapterSearchPage(failed.offset, failed.selectLast);
|
| 4866 |
+
});
|
| 4867 |
fullSearchCancel.addEventListener("click", () => {
|
| 4868 |
nextReaderGeneration("search");
|
| 4869 |
beginReaderNavigation();
|
|
|
|
| 4871 |
pdfSearchInProgress = false;
|
| 4872 |
chapterSearchInProgress = false;
|
| 4873 |
chapterSearchPage = null;
|
| 4874 |
+
failedSearchPage = null;
|
| 4875 |
pdfSearchPageLoader = null;
|
| 4876 |
fullSearchComplete = false;
|
| 4877 |
updateSearchState({ results: [], index: -1 });
|
| 4878 |
renderFullSearchResults();
|
| 4879 |
fullSearchCancel.hidden = true;
|
| 4880 |
fullSearchRetry.hidden = false;
|
| 4881 |
+
fullSearchRetry.textContent = "重试搜索";
|
| 4882 |
fullSearchStatus.textContent = "搜索已取消";
|
| 4883 |
});
|
| 4884 |
chapterSearchPagination
|
|
|
|
| 4997 |
async function loadChapterSearchPage(offset, selectLast = null) {
|
| 4998 |
if (pdfSearchInProgress || chapterSearchInProgress || !chapterSearchPage ||
|
| 4999 |
(!chapterSearchClient && !pdfSearchPageLoader)) return;
|
| 5000 |
+
failedSearchPage = null;
|
| 5001 |
const generation = nextReaderGeneration("search");
|
| 5002 |
beginReaderNavigation();
|
| 5003 |
fullSearchCancel.hidden = false;
|
|
|
|
| 5018 |
} catch (error) {
|
| 5019 |
if (isReaderGenerationCurrent("search", generation)) {
|
| 5020 |
fullSearchStatus.textContent = error.message || "搜索结果加载失败";
|
| 5021 |
+
failedSearchPage = { offset, selectLast, query: searchState.query };
|
| 5022 |
+
fullSearchRetry.textContent = "重试本页";
|
| 5023 |
fullSearchRetry.hidden = false;
|
| 5024 |
}
|
| 5025 |
} finally {
|
|
|
|
| 5173 |
}
|
| 5174 |
return `正文 ${Math.min(100, Math.floor(offset / Math.max(1, total) * 100))}%`;
|
| 5175 |
}
|
| 5176 |
+
async function fullSearchDomMatches(root, fallback, query, generation, progressive = false,
|
| 5177 |
+
occurrence = null, navigationGeneration = readerRuntime.currentGeneration("navigation")) {
|
| 5178 |
root = foliateSectionRoot(root);
|
| 5179 |
const nodes = await fullSearchTextNodes(root, generation);
|
| 5180 |
if (!nodes || !isReaderGenerationCurrent("search", generation)) return [];
|
|
|
|
| 5191 |
}
|
| 5192 |
}
|
| 5193 |
const text = chunks.join(""), groups = [], firstHits = [];
|
|
|
|
| 5194 |
const patternText = fullSearchEscape(query);
|
| 5195 |
let nextStart = 0, total = 0;
|
| 5196 |
const locateIndex = (index) => {
|
|
|
|
| 5222 |
location: fullSearchLocation(part.node, fallback, match.index, text.length),
|
| 5223 |
snippet: fullSearchSnippet(text, match.index, match.value.length),
|
| 5224 |
async activate(navigationGeneration = readerRuntime.currentGeneration("navigation")) {
|
| 5225 |
+
if (!isReaderGenerationCurrent("navigation", navigationGeneration)) return false;
|
| 5226 |
const searchGeneration = readerRuntime.currentGeneration("search");
|
| 5227 |
clearFullSearchMarks();
|
| 5228 |
const currentNodes = await fullSearchTextNodes(root, searchGeneration);
|
|
|
|
| 5284 |
chapterSearchPage = page(0);
|
| 5285 |
}
|
| 5286 |
if (occurrence !== null) {
|
| 5287 |
+
if (!isReaderGenerationCurrent("navigation", navigationGeneration)) return [];
|
| 5288 |
const matchedPage = page(occurrence);
|
| 5289 |
const result = matchedPage.results[occurrence - matchedPage.offset];
|
| 5290 |
+
if (!result || !await result.activate(navigationGeneration)) return [];
|
| 5291 |
return [{ target: fullSearchActiveMarks[0] }];
|
| 5292 |
}
|
| 5293 |
const fallbackResults = progressive ? null : firstHits.map(resultFor);
|
|
|
|
| 5331 |
searchState.query,
|
| 5332 |
readerRuntime.currentGeneration("search"),
|
| 5333 |
false,
|
| 5334 |
+
result.occurrence || 0,
|
| 5335 |
+
generation
|
| 5336 |
);
|
| 5337 |
target = matches[0]?.target;
|
| 5338 |
}
|
|
|
|
| 5561 |
fullSearchInputTimer = 0;
|
| 5562 |
const query = fullSearchInput.value.trim();
|
| 5563 |
fullSearchComplete = false;
|
| 5564 |
+
failedSearchPage = null;
|
| 5565 |
const generation = nextReaderGeneration("search");
|
| 5566 |
beginReaderNavigation();
|
| 5567 |
clearFullSearchMarks();
|
|
|
|
| 5570 |
pdfSearchPageLoader = null;
|
| 5571 |
updateChapterSearchPagination();
|
| 5572 |
fullSearchRetry.hidden = true;
|
| 5573 |
+
fullSearchRetry.textContent = "重试搜索";
|
| 5574 |
fullSearchCancel.hidden = !["epub-chapters", "pdf", "pdf-pages", "foliate"].includes(capability.mode) || !query;
|
| 5575 |
pdfSearchInProgress = ["pdf", "pdf-pages", "foliate"].includes(capability.mode) && !!query;
|
| 5576 |
chapterSearchInProgress = capability.mode === "epub-chapters" && !!query;
|
|
|
|
| 5675 |
setReaderPanelOpen(false, true);
|
| 5676 |
else if (!closePanel) fullSearchResultsNode.children[index]?.scrollIntoView({ block: "nearest" });
|
| 5677 |
} catch (error) {
|
| 5678 |
+
if (error?.name !== "AbortError" && isReaderGenerationCurrent("navigation", generation) &&
|
| 5679 |
+
isReaderGenerationCurrent("search", searchGeneration) && searchState.results[index] === result) {
|
| 5680 |
+
updateSearchState({ index: previousIndex });
|
| 5681 |
+
fullSearchStatus.textContent = capability.mode === "epub-chapters"
|
| 5682 |
+
? error.message || "搜索结果定位失败,请重试"
|
| 5683 |
+
: "搜索结果定位失败,请重试";
|
| 5684 |
+
}
|
| 5685 |
reportNavigationError(error, generation);
|
| 5686 |
}
|
| 5687 |
}
|
|
|
|
| 5721 |
updateFullSearchClear();
|
| 5722 |
fullSearchInput.focus();
|
| 5723 |
if (searchState.query !== fullSearchInput.value.trim() || !fullSearchComplete) runFullSearch();
|
| 5724 |
+
else if (failedSearchPage) fullSearchRetry.hidden = false;
|
| 5725 |
}
|
| 5726 |
fullSearchButton.addEventListener("click", toggleFullSearch);
|
| 5727 |
function queueFullSearch() {
|
|
|
|
| 5733 |
pdfSearchInProgress = false;
|
| 5734 |
chapterSearchInProgress = false;
|
| 5735 |
chapterSearchPage = null;
|
| 5736 |
+
failedSearchPage = null;
|
| 5737 |
pdfSearchPageLoader = null;
|
| 5738 |
updateSearchState({ results: [], index: -1 });
|
| 5739 |
renderFullSearchResults();
|
tests/test_reader_refactor.py
CHANGED
|
@@ -428,6 +428,40 @@ class ReaderRefactorTest(unittest.TestCase):
|
|
| 428 |
self.assertEqual(self.page.locator("#full-search-status").text_content(), "搜索已取消")
|
| 429 |
self.assertEqual(self.page.locator(".full-search-result").count(), 0)
|
| 430 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 431 |
def test_pending_foliate_result_does_not_close_reopened_search(self):
|
| 432 |
self.serve(support.epub_with_many_chapters(3), "application/epub+zip")
|
| 433 |
self.open(self.reader_url("epub"))
|
|
@@ -468,6 +502,53 @@ class ReaderRefactorTest(unittest.TestCase):
|
|
| 468 |
self.assertEqual(self.page.locator("#full-search-status").text_content(), "3 个结果")
|
| 469 |
self.assertEqual(self.page.locator(".foliate-continuous mark.full-search-highlight").count(), 1)
|
| 470 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 471 |
def test_foliate_many_hits_paginate_and_cancel(self):
|
| 472 |
with zipfile.ZipFile(io.BytesIO(support.epub_with_many_chapters(3))) as archive:
|
| 473 |
files = {name: archive.read(name) for name in archive.namelist()}
|
|
@@ -1260,6 +1341,26 @@ class ReaderRefactorTest(unittest.TestCase):
|
|
| 1260 |
self.assertEqual(self.page.locator("#full-search-page").input_value(), "4")
|
| 1261 |
self.assertEqual(sum(item.endswith("epub-search-index.json.gz") for item in requests), 1)
|
| 1262 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1263 |
def test_chapter_search_emits_partial_results_before_exact_total(self):
|
| 1264 |
self.serve_chapter_search()
|
| 1265 |
self.page.locator("#history").click()
|
|
|
|
| 428 |
self.assertEqual(self.page.locator("#full-search-status").text_content(), "搜索已取消")
|
| 429 |
self.assertEqual(self.page.locator(".full-search-result").count(), 0)
|
| 430 |
|
| 431 |
+
def test_failed_pdf_search_page_retry_keeps_target_page(self):
|
| 432 |
+
def instrument(route):
|
| 433 |
+
response = route.fetch()
|
| 434 |
+
needle = "async function loadPdfSearchText(pdf, page, generation) {"
|
| 435 |
+
script = response.text()
|
| 436 |
+
self.assertIn(needle, script)
|
| 437 |
+
route.fulfill(response=response, body=script.replace(needle, needle + """
|
| 438 |
+
if (page === 2 && window.__failSearchPage) {
|
| 439 |
+
window.__failSearchPage = false;
|
| 440 |
+
throw new Error('temporary page load');
|
| 441 |
+
}"""))
|
| 442 |
+
self.page.route("**/static/reader.js?*", instrument)
|
| 443 |
+
module = support.PDF_MODULE.replace("numPages: 30", "numPages: 3").replace(
|
| 444 |
+
"getPage: () => Promise.resolve(page)", """getPage: () => Promise.resolve({ ...page,
|
| 445 |
+
getTextContent() { return Promise.resolve({ items: [{ str: 'needle '.repeat(60), hasEOL: false }] }); } })""")
|
| 446 |
+
self.page.route("**/static/vendor/pdf.min.*.mjs", lambda route: route.fulfill(
|
| 447 |
+
content_type="text/javascript", body=module))
|
| 448 |
+
self.open(self.reader_url("pdf"))
|
| 449 |
+
self.search("needle")
|
| 450 |
+
self.page.locator("#full-search-page-next").click()
|
| 451 |
+
self.page.wait_for_function("() => document.querySelector('.full-search-rank')?.textContent === '51.'")
|
| 452 |
+
self.page.evaluate("window.__failSearchPage = true")
|
| 453 |
+
self.page.locator("#full-search-page-next").click()
|
| 454 |
+
self.page.wait_for_function("() => document.querySelector('#full-search-status').textContent === 'temporary page load'")
|
| 455 |
+
self.assertEqual(self.page.locator("#full-search-retry").text_content(), "重试本页")
|
| 456 |
+
self.assertEqual(self.page.locator(".full-search-rank").first.text_content(), "51.")
|
| 457 |
+
self.page.locator("#full-search-toggle").click()
|
| 458 |
+
self.page.locator("#full-search-toggle").click()
|
| 459 |
+
self.assertTrue(self.page.locator("#full-search-retry").is_visible())
|
| 460 |
+
self.page.locator("#full-search-retry").click()
|
| 461 |
+
self.page.wait_for_function("() => document.querySelector('.full-search-rank')?.textContent === '101.'")
|
| 462 |
+
self.assertEqual(self.page.locator("#full-search-page").input_value(), "3")
|
| 463 |
+
self.assertEqual(self.page.locator("#full-search-status").text_content(), "180 个结果")
|
| 464 |
+
|
| 465 |
def test_pending_foliate_result_does_not_close_reopened_search(self):
|
| 466 |
self.serve(support.epub_with_many_chapters(3), "application/epub+zip")
|
| 467 |
self.open(self.reader_url("epub"))
|
|
|
|
| 502 |
self.assertEqual(self.page.locator("#full-search-status").text_content(), "3 个结果")
|
| 503 |
self.assertEqual(self.page.locator(".foliate-continuous mark.full-search-highlight").count(), 1)
|
| 504 |
|
| 505 |
+
def test_foliate_navigation_exception_reports_failure_and_retries(self):
|
| 506 |
+
self.serve(support.epub_with_many_chapters(3), "application/epub+zip")
|
| 507 |
+
self.open(self.reader_url("epub"))
|
| 508 |
+
self.search("正文")
|
| 509 |
+
self.page.evaluate("""() => {
|
| 510 |
+
const view = document.querySelector('foliate-view');
|
| 511 |
+
const original = view.resolveNavigation.bind(view);
|
| 512 |
+
view.resolveNavigation = async cfi => {
|
| 513 |
+
view.resolveNavigation = original;
|
| 514 |
+
throw new Error('temporary navigation failure');
|
| 515 |
+
};
|
| 516 |
+
}""")
|
| 517 |
+
self.page.locator(".full-search-result").first.click()
|
| 518 |
+
self.page.wait_for_function("() => document.querySelector('#full-search-status').textContent === '搜索结果定位失败,请重试'")
|
| 519 |
+
self.assertTrue(self.page.locator("#history-panel").evaluate("node => node.classList.contains('is-open')"))
|
| 520 |
+
self.page.locator(".full-search-result").first.click()
|
| 521 |
+
self.page.wait_for_function("() => !document.querySelector('#history-panel').classList.contains('is-open')")
|
| 522 |
+
self.assertEqual(self.page.locator("#full-search-status").text_content(), "3 个结果")
|
| 523 |
+
|
| 524 |
+
def test_stale_foliate_fallback_cannot_highlight_after_navigation(self):
|
| 525 |
+
def hold_fallback(route):
|
| 526 |
+
response = route.fetch()
|
| 527 |
+
needle = "const nodes = await fullSearchTextNodes(root, generation);"
|
| 528 |
+
script = response.text()
|
| 529 |
+
self.assertIn(needle, script)
|
| 530 |
+
script = script.replace(needle, """if (occurrence !== null && !window.__fallbackHeld) {
|
| 531 |
+
window.__fallbackHeld = true;
|
| 532 |
+
await new Promise(resolve => { window.__releaseFallback = resolve; });
|
| 533 |
+
} """ + needle)
|
| 534 |
+
route.fulfill(response=response, body=script.replace(
|
| 535 |
+
"target = matches[0]?.target;", "target = matches[0]?.target; window.__fallbackDone = true;"))
|
| 536 |
+
self.page.route("**/static/reader.js?*", hold_fallback)
|
| 537 |
+
self.serve(support.epub_with_many_chapters(3), "application/epub+zip")
|
| 538 |
+
self.open(self.reader_url("epub"))
|
| 539 |
+
self.search("正文")
|
| 540 |
+
self.page.evaluate("""() => {
|
| 541 |
+
const view = document.querySelector('foliate-view');
|
| 542 |
+
const original = view.resolveNavigation.bind(view);
|
| 543 |
+
view.resolveNavigation = async cfi => ({ ...(await original(cfi)), anchor: null });
|
| 544 |
+
}""")
|
| 545 |
+
self.page.locator(".full-search-result").first.click()
|
| 546 |
+
self.page.wait_for_function("() => !!window.__releaseFallback")
|
| 547 |
+
self.page.evaluate("document.querySelector('#viewport').dispatchEvent(new PointerEvent('pointerdown', { bubbles: true }))")
|
| 548 |
+
self.page.evaluate("window.__releaseFallback()")
|
| 549 |
+
self.page.wait_for_function("() => !!window.__fallbackDone")
|
| 550 |
+
self.assertEqual(self.page.locator(".foliate-continuous mark.full-search-highlight").count(), 0)
|
| 551 |
+
|
| 552 |
def test_foliate_many_hits_paginate_and_cancel(self):
|
| 553 |
with zipfile.ZipFile(io.BytesIO(support.epub_with_many_chapters(3))) as archive:
|
| 554 |
files = {name: archive.read(name) for name in archive.namelist()}
|
|
|
|
| 1341 |
self.assertEqual(self.page.locator("#full-search-page").input_value(), "4")
|
| 1342 |
self.assertEqual(sum(item.endswith("epub-search-index.json.gz") for item in requests), 1)
|
| 1343 |
|
| 1344 |
+
def test_failed_chapter_page_restarts_worker_and_restores_target_page(self):
|
| 1345 |
+
def expose_worker_failure(route):
|
| 1346 |
+
response = route.fetch()
|
| 1347 |
+
needle = 'worker.postMessage({ type: "init", configuration });'
|
| 1348 |
+
script = response.text()
|
| 1349 |
+
self.assertIn(needle, script)
|
| 1350 |
+
route.fulfill(response=response, body=script.replace(needle,
|
| 1351 |
+
"window.__breakChapterWorker = () => worker.onerror({ preventDefault() {} }); " + needle))
|
| 1352 |
+
self.page.route("**/static/reader-chapter-search.mjs*", expose_worker_failure)
|
| 1353 |
+
self.serve_chapter_search()
|
| 1354 |
+
self.search("needle")
|
| 1355 |
+
self.page.evaluate("window.__breakChapterWorker()")
|
| 1356 |
+
self.page.locator("#full-search-page").fill("3")
|
| 1357 |
+
self.page.locator("#full-search-page").dispatch_event("change")
|
| 1358 |
+
self.page.wait_for_function("() => document.querySelector('#full-search-status').textContent.includes('全文搜索组件加载失败')")
|
| 1359 |
+
self.page.locator("#full-search-retry").click()
|
| 1360 |
+
self.page.wait_for_function("() => document.querySelector('.full-search-rank')?.textContent === '101.'")
|
| 1361 |
+
self.assertEqual(self.page.locator("#full-search-page").input_value(), "3")
|
| 1362 |
+
self.assertEqual(self.page.locator("#full-search-status").text_content(), "155 个结果")
|
| 1363 |
+
|
| 1364 |
def test_chapter_search_emits_partial_results_before_exact_total(self):
|
| 1365 |
self.serve_chapter_search()
|
| 1366 |
self.page.locator("#history").click()
|