返回 CodeWhale
search-utils.test.ts
根目录 / web / lib / search-utils.test.ts
1 import { describe, it, expect } from "vitest";
2 import { readFileSync } from "node:fs";
3 import {
4 docTopicHaystack,
5 filterDocTopics,
6 highlightSpan,
7 normalizeQuery,
8 matches,
9 } from "./search-utils";
10 import { DOC_TOPICS } from "./docs-map";
11
12 describe("normalizeQuery", () => {
13 it("trims, lowercases, and collapses repeated whitespace", () => {
14 expect(normalizeQuery(" Hello World\t\nNow ")).toBe("hello world now");
15 });
16
17 it("returns empty string for whitespace-only input", () => {
18 expect(normalizeQuery(" ")).toBe("");
19 });
20 });
21
22 describe("matches", () => {
23 it("returns true for empty query (shows everything)", () => {
24 expect(matches("anything", "")).toBe(true);
25 expect(matches("anything", " ")).toBe(true);
26 });
27
28 it("does case-insensitive substring matching", () => {
29 expect(matches("Install Guide", "install")).toBe(true);
30 expect(matches("install guide", "INSTALL")).toBe(true);
31 expect(matches("Configuration", "config")).toBe(true);
32 expect(matches("Install Guide", "install guide")).toBe(true);
33 expect(matches("Install Guide", "install guide")).toBe(true);
34 });
35
36 it("returns false when no match", () => {
37 expect(matches("Install", "docker")).toBe(false);
38 });
39 });
40
41 describe("docTopicHaystack", () => {
42 it("includes the topic id and slug", () => {
43 const install = DOC_TOPICS.find((t) => t.id === "install")!;
44 const hay = docTopicHaystack(install);
45 expect(hay).toContain("install");
46 });
47
48 it("includes both EN and ZH labels", () => {
49 const mcp = DOC_TOPICS.find((t) => t.id === "mcp")!;
50 const hay = docTopicHaystack(mcp);
51 expect(hay).toContain("mcp");
52 // ZH label is also "MCP" but description has Chinese
53 expect(hay).toContain("stdio");
54 expect(hay).toContain("工具"); // tools in Chinese description
55 });
56
57 it("includes source file paths", () => {
58 const config = DOC_TOPICS.find((t) => t.id === "configuration")!;
59 const hay = docTopicHaystack(config);
60 expect(hay).toContain("docs/configuration.md");
61 expect(hay).toContain("docs/legacy_paths.md");
62 });
63
64 it("includes category name in both locales", () => {
65 const install = DOC_TOPICS.find((t) => t.id === "install")!;
66 const hay = docTopicHaystack(install);
67 expect(hay).toContain("getting-started");
68 expect(hay).toContain("入门"); // ZH for getting-started
69 });
70 });
71
72 describe("filterDocTopics", () => {
73 it("returns all indices when query is empty", () => {
74 const result = filterDocTopics(DOC_TOPICS, "");
75 expect(result.length).toBe(DOC_TOPICS.length);
76 });
77
78 it("returns all indices when query is whitespace", () => {
79 const result = filterDocTopics(DOC_TOPICS, " ");
80 expect(result.length).toBe(DOC_TOPICS.length);
81 });
82
83 it("filters by English keyword", () => {
84 const result = filterDocTopics(DOC_TOPICS, "install");
85 const ids = result.map((i) => DOC_TOPICS[i].id);
86 expect(ids).toContain("install");
87 expect(ids.length).toBeGreaterThanOrEqual(1);
88 });
89
90 it("filters by Chinese keyword", () => {
91 const result = filterDocTopics(DOC_TOPICS, "沙箱"); // sandbox
92 const ids = result.map((i) => DOC_TOPICS[i].id);
93 expect(ids).toContain("sandbox");
94 });
95
96 it("filters by source file name", () => {
97 const result = filterDocTopics(DOC_TOPICS, "configuration.md");
98 const ids = result.map((i) => DOC_TOPICS[i].id);
99 expect(ids).toContain("configuration");
100 });
101
102 it("filters by category", () => {
103 const result = filterDocTopics(DOC_TOPICS, "extending");
104 const ids = result.map((i) => DOC_TOPICS[i].id);
105 // extending category includes mcp, hooks, runtime-api
106 expect(ids).toContain("mcp");
107 expect(ids).toContain("hooks");
108 expect(ids).toContain("runtime-api");
109 });
110
111 it("is case-insensitive", () => {
112 const lower = filterDocTopics(DOC_TOPICS, "mcp");
113 const upper = filterDocTopics(DOC_TOPICS, "MCP");
114 expect(lower).toEqual(upper);
115 });
116
117 it("returns empty array for gibberish query", () => {
118 const result = filterDocTopics(DOC_TOPICS, "zzzzzzz_nonexistent");
119 expect(result).toEqual([]);
120 });
121
122 it("matches partial keywords", () => {
123 const result = filterDocTopics(DOC_TOPICS, "tool");
124 const ids = result.map((i) => DOC_TOPICS[i].id);
125 // "tool" should match "tools" topic (id contains "tool")
126 expect(ids).toContain("tools");
127 });
128
129 it("matches across locales (EN query matches ZH content)", () => {
130 // "安装" is the ZH label for "install"
131 const result = filterDocTopics(DOC_TOPICS, "安装");
132 const ids = result.map((i) => DOC_TOPICS[i].id);
133 expect(ids).toContain("install");
134 });
135
136 it("collapses repeated whitespace in queries", () => {
137 const single = filterDocTopics(DOC_TOPICS, "getting started");
138 const repeated = filterDocTopics(DOC_TOPICS, "getting started");
139 expect(repeated).toEqual(single);
140 expect(repeated.length).toBeGreaterThan(0);
141 });
142 });
143
144 describe("highlightSpan", () => {
145 const webRoot = new URL("../", import.meta.url);
146 const read = (p: string) => readFileSync(new URL(p, webRoot), "utf8");
147
148 it("splits a plain match into three reassembling pieces", () => {
149 const span = highlightSpan("Install Guide", "install")!;
150 expect(span).toEqual({ before: "", match: "Install", after: " Guide" });
151 expect(span.before + span.match + span.after).toBe("Install Guide");
152 });
153
154 it("returns null for an empty query or no match", () => {
155 expect(highlightSpan("Install", "")).toBeNull();
156 expect(highlightSpan("Install", " ")).toBeNull();
157 expect(highlightSpan("Install", "docker")).toBeNull();
158 expect(highlightSpan("", "install")).toBeNull();
159 });
160
161 it("keeps indices in the source string when lowercasing changes length", () => {
162 // "İ".toLowerCase() is two code units, so a lowercased copy of this
163 // string is one longer than the string itself. Index arithmetic done on
164 // the copy highlighted "tanbul " instead of "stanbul".
165 const text = "İstanbul kurulumu";
166 expect(text.toLowerCase().length).toBe(text.length + 1);
167 const span = highlightSpan(text, "stanbul")!;
168 expect(span.match).toBe("stanbul");
169 expect(span.before).toBe("İ");
170 expect(span.after).toBe(" kurulumu");
171 expect(span.before + span.match + span.after).toBe(text);
172 });
173
174 it("never claims half of a source character", () => {
175 const span = highlightSpan("İstanbul", "i")!;
176 expect(span.match).toBe("İ");
177 expect(span.before + span.match + span.after).toBe("İstanbul");
178 });
179
180 it("includes the whole final source character when its lowercase form expands", () => {
181 expect(highlightSpan("See İzmir notes", "see i")).toEqual({
182 before: "",
183 match: "See İ",
184 after: "zmir notes",
185 });
186 });
187
188 it("matches contextual lowercase forms the same way as the search", () => {
189 expect(highlightSpan("Guide ΟΣ setup", "ος")).toEqual({
190 before: "Guide ",
191 match: "ΟΣ",
192 after: " setup",
193 });
194 });
195
196 it("aligns highlights across repeated whitespace and Unicode case expansion", () => {
197 const collapsed = highlightSpan("Getting started guide", "getting started")!;
198 expect(collapsed).toEqual({
199 before: "",
200 match: "Getting started",
201 after: " guide",
202 });
203 expect(collapsed.before + collapsed.match + collapsed.after).toBe("Getting started guide");
204
205 const expanded = highlightSpan("See İzmir\n notes", "see i")!;
206 expect(expanded).toEqual({
207 before: "",
208 match: "See İ",
209 after: "zmir\n notes",
210 });
211 expect(expanded.before + expanded.match + expanded.after).toBe("See İzmir\n notes");
212
213 const afterDotted = highlightSpan("İstanbul kurulumu rehberi", "stanbul kurulumu")!;
214 expect(afterDotted).toEqual({
215 before: "İ",
216 match: "stanbul kurulumu",
217 after: " rehberi",
218 });
219 expect(afterDotted.before + afterDotted.match + afterDotted.after).toBe("İstanbul kurulumu rehberi");
220 });
221
222 it("is the one match rule both search surfaces use", () => {
223 for (const file of ["components/docs-search.tsx", "components/faq-search.tsx"]) {
224 const source = read(file);
225 expect(source, file).toContain("highlightSpan(text, query)");
226 expect(source, file).not.toContain("text.toLowerCase()");
227 expect(source, file).not.toContain("text.slice(");
228 }
229 });
230 });
231
231 lines TYPESCRIPT