Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import {
4 compareRankedSearchResults,
5 insertRankedSearchResult,
6 normalizeSearchQuery,
7 scoreQueryMatch,
8 scoreSubsequenceMatch,
9} from "./searchRanking.ts";
10
11describe("normalizeSearchQuery", () => {
12 it("trims and lowercases queries", () => {
13 expect(normalizeSearchQuery(" UI ")).toBe("ui");
14 });
15
16 it("can strip leading trigger characters", () => {
17 expect(normalizeSearchQuery(" $ui", { trimLeadingPattern: /^\$+/ })).toBe("ui");
18 });
19});
20
21describe("scoreQueryMatch", () => {
22 it("prefers exact matches over broader contains matches", () => {
23 expect(
24 scoreQueryMatch({
25 value: "ui",
26 query: "ui",
27 exactBase: 0,
28 prefixBase: 10,
29 includesBase: 20,
30 }),
31 ).toBe(0);
32
33 expect(
34 scoreQueryMatch({
35 value: "building native ui",
36 query: "ui",
37 exactBase: 0,
38 prefixBase: 10,
39 boundaryBase: 20,
40 includesBase: 30,
41 }),
42 ).toBeGreaterThan(0);
43 });
44
45 it("treats boundary matches as stronger than generic contains matches", () => {
46 const boundaryScore = scoreQueryMatch({
47 value: "gh-fix-ci",
48 query: "fix",
49 exactBase: 0,
50 prefixBase: 10,
51 boundaryBase: 20,
52 includesBase: 30,
53 boundaryMarkers: ["-"],
54 });
55 const containsScore = scoreQueryMatch({
56 value: "highfixci",
57 query: "fix",
58 exactBase: 0,
59 prefixBase: 10,
60 boundaryBase: 20,
61 includesBase: 30,
62 boundaryMarkers: ["-"],
63 });
64
65 expect(boundaryScore).not.toBeNull();
66 expect(containsScore).not.toBeNull();
67 expect(boundaryScore!).toBeLessThan(containsScore!);
68 });
69});
70
71describe("scoreSubsequenceMatch", () => {
72 it("scores tighter subsequences ahead of looser ones", () => {
73 const compact = scoreSubsequenceMatch("ghfixci", "gfc");
74 const spread = scoreSubsequenceMatch("github-fix-ci", "gfc");
75
76 expect(compact).not.toBeNull();
77 expect(spread).not.toBeNull();
78 expect(compact!).toBeLessThan(spread!);
79 });
80});
81
82describe("insertRankedSearchResult", () => {
83 it("keeps the best-ranked candidates within the limit", () => {
84 const ranked = [
85 { item: "b", score: 20, tieBreaker: "b" },
86 { item: "d", score: 40, tieBreaker: "d" },
87 ];
88
89 insertRankedSearchResult(ranked, { item: "a", score: 10, tieBreaker: "a" }, 2);
90 insertRankedSearchResult(ranked, { item: "c", score: 30, tieBreaker: "c" }, 2);
91
92 expect(ranked.map((entry) => entry.item)).toEqual(["a", "b"]);
93 expect(compareRankedSearchResults(ranked[0]!, ranked[1]!)).toBeLessThan(0);
94 });
95});