feat(frontend): in-book text search core (TDD)
This commit is contained in:
@@ -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" });
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user