File size: 5,044 Bytes
ac6d993
2c1d035
d4b284a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ac6d993
 
 
 
 
210b1e7
 
 
 
ac6d993
 
210b1e7
 
 
02628d5
 
 
 
ac6d993
 
02628d5
ac6d993
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6a3ae39
 
 
 
 
 
 
 
 
ac6d993
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import assert from "node:assert/strict";
import { countMatches, isSearchIndexProxyURL, resultPage, scanText, validateIndex } from "../static/reader/search/workers/reader-chapter-search-worker.mjs";

const origin = "https://voiceofml-search.hf.space";
const indexURL = path => new URL(origin + "/api/reader-bucket-resource?path=" + encodeURIComponent(path));
const prefix = "chapters/ebook/epub/" + "a".repeat(64) + "/" + "b".repeat(16) + "/";
assert.ok(isSearchIndexProxyURL(new URL(origin + "/api/reader-content?url=source"), origin));
for (const extension of ["epub", "mobi", "azw3", "fb2", "chm"])
  assert.ok(isSearchIndexProxyURL(indexURL(prefix.replace("/epub/", "/" + extension + "/") + "epub-search-index.json.gz"), origin));
for (const path of [prefix + "chapter-manifest.json", prefix + "chapters/chapter-0001.xhtml",
                    prefix + "../epub-search-index.json.gz", prefix + "%2e%2e/epub-search-index.json.gz",
                    prefix.replace("/epub/", "/pdf/") + "epub-search-index.json.gz"])
  assert.equal(isSearchIndexProxyURL(indexURL(path), origin), false, path);
for (const url of [indexURL(prefix + "epub-search-index.json.gz").href + "&path=other",
                   indexURL(prefix + "epub-search-index.json.gz").href + "&other=1",
                   indexURL(prefix + "epub-search-index.json.gz").href + "#fragment",
                   indexURL(prefix + "epub-search-index.json.gz").href.replace("https://", "https://user@"),
                   indexURL(prefix + "epub-search-index.json.gz").href.replace(origin, "https://other.hf.space")])
  assert.equal(isSearchIndexProxyURL(new URL(url), origin), false, url);

const chapters = [
  { index: 1, title: "开头", path: "chapters/1.xhtml", text: "手机 手。机 a+b [term] " + "needle ".repeat(125) },
  { index: 2, title: "结尾", path: "chapters/2.xhtml", text: "needle ".repeat(130) + "远方命中" }
];
const progressPages = [];
const counted = await countMatches(chapters, "needle", () => true, partial => {
  progressPages.push({ total: partial.total, results: partial.firstPage.slice() });
});
assert.equal(counted.total, 255);
assert.deepEqual(counted.counts, [125, 130]);
assert.equal(progressPages[0].total, 125);
assert.equal(progressPages[0].results.length, 50);
assert.equal(progressPages.at(-1).total, 255);
assert.ok(counted.checkpoints.length >= 1);
const checkpointed = await countMatches([{ index: 3, title: "检查点", path: "chapters/3.xhtml", text: "needle ".repeat(600) }], "needle", () => true);
assert.deepEqual(checkpointed.checkpoints.map(checkpoint => checkpoint.ordinal), [0, 256, 512]);
assert.deepEqual(counted.firstPage, await resultPage(chapters, "needle", counted.counts, 0, () => true, counted.checkpoints));
const all = [];
for (let offset = 0; offset < counted.total; offset += 50) {
  const page = await resultPage(chapters, "needle", counted.counts, offset, () => true, counted.checkpoints);
  assert.ok(page.length <= 50);
  all.push(...page);
}
assert.equal(new Set(all.map(item => `${item.chapterIndex}:${item.start}`)).size, 255);
assert.equal(all.at(-1).chapterIndex, 2);
for (const query of ["手机", "手。机", "a+b", "[term]", "远方命中"]) {
  assert.equal((await countMatches(chapters, query, () => true)).total, 1, query);
}
const boundaryText = "x".repeat(65534) + "跨边界😀" + "y".repeat(65536) + "跨边界😀";
const found = [];
await scanText(boundaryText, "跨边界😀", (start, length) => found.push([start, length]));
assert.deepEqual(found.map(([start, length]) => boundaryText.slice(start, start + length)), ["跨边界😀", "跨边界😀"]);
let current = true;
const cancelled = scanText("x".repeat(1000000), "x", () => { current = false; }, () => current);
await assert.rejects(cancelled, { name: "AbortError" });
assert.equal(validateIndex({ version: 1, kind: "epub-search-index", chapters }, chapters).length, 2);
assert.throws(() => validateIndex({ version: 1, kind: "epub-search-index", chapters: chapters.slice(0, 1) }, chapters));
assert.throws(() => validateIndex({ version: 1, kind: "epub-search-index", chapters: [chapters[1], chapters[0]] }, chapters));
const exactIndex = { version: 1, kind: "epub-search-index", text_profile: "dom-text-v1", chapters };
assert.equal(validateIndex(exactIndex, chapters, "dom-text-v1").length, 2);
assert.throws(() => validateIndex(exactIndex, chapters));
assert.throws(() => validateIndex({ version: 1, kind: "epub-search-index", chapters }, chapters, "dom-text-v1"));
assert.throws(() => validateIndex({ ...exactIndex, text_profile: "unknown" }, chapters, "unknown"));
const literals = [{ index: 1, title: "literal", path: "chapters/1.xhtml", text: "&#0 &nbsp; ".repeat(70) }];
const literalCounts = await countMatches(literals, "&nbsp;", () => true);
assert.equal(literalCounts.total, 70);
assert.equal((await resultPage(literals, "&nbsp;", literalCounts.counts, 50, () => true)).length, 20);
console.log("chapter search: complete counts, paging, literal matches, Unicode boundaries, cancellation and index identity passed");