feat(frontend): in-book text search core (TDD)

This commit is contained in:
2026-09-16 16:51:36 +08:00
parent 0d7cd5f322
commit 74e5e9d5fe
2 changed files with 76 additions and 0 deletions
+28
View File
@@ -0,0 +1,28 @@
import { describe, expect, it } from "vitest";
import { searchChapters, splitHighlight } from "../src/lib/search";
import { splitChapters } from "../src/lib/chapters";
const TEXT = ["第一章 起风", "风来了又走。", "第二章 落雨", "雨点敲窗,风声相伴。", "风止"].join("\n");
describe("searchChapters", () => {
const chs = splitChapters(TEXT);
it("finds case-insensitive hits with chapter index and position", () => {
const hits = searchChapters(TEXT, chs, "风");
expect(hits.length).toBeGreaterThanOrEqual(3);
expect(hits[0].ch).toBe(0);
expect(hits.every((h) => h.snippet.includes("风"))).toBe(true);
});
it("returns empty for no match / empty query", () => {
expect(searchChapters(TEXT, chs, "不存在")).toEqual([]);
expect(searchChapters(TEXT, chs, " ")).toEqual([]);
});
it("caps at limit", () => {
expect(searchChapters(TEXT, chs, "风", 2)).toHaveLength(2);
});
});
describe("splitHighlight", () => {
it("splits snippet into before/match/after", () => {
expect(splitHighlight("abc风def", 3, 1)).toEqual({ before: "abc", match: "风", after: "def" });
});
});