Skip to content
Source
Sign inSign up
Open History to see the last commit

searchRanking.test.ts

59 lines1.7 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import {
4 insertRankedSearchResult,
5 scoreQueryMatch,
6 scoreSubsequenceMatch,
7} from "./searchRanking.ts";
8​
9describe("scoreQueryMatch", () => {
10 it("treats boundary matches as stronger than generic contains matches", () => {
11 const boundaryScore = scoreQueryMatch({
12 value: "gh-fix-ci",
13 query: "fix",
14 exactBase: 0,
15 prefixBase: 10,
16 boundaryBase: 20,
17 includesBase: 30,
18 boundaryMarkers: ["-"],
19 });
20 const containsScore = scoreQueryMatch({
21 value: "highfixci",
22 query: "fix",
23 exactBase: 0,
24 prefixBase: 10,
25 boundaryBase: 20,
26 includesBase: 30,
27 boundaryMarkers: ["-"],
28 });
29​
30 expect(boundaryScore).not.toBeNull();
31 expect(containsScore).not.toBeNull();
32 expect(boundaryScore!).toBeLessThan(containsScore!);
33 });
34});
35​
36describe("scoreSubsequenceMatch", () => {
37 it("scores tighter subsequences ahead of looser ones", () => {
38 const compact = scoreSubsequenceMatch("ghfixci", "gfc");
39 const spread = scoreSubsequenceMatch("github-fix-ci", "gfc");
40​
41 expect(compact).not.toBeNull();
42 expect(spread).not.toBeNull();
43 expect(compact!).toBeLessThan(spread!);
44 });
45});
46​
47describe("insertRankedSearchResult", () => {
48 it("keeps the best-ranked candidates within the limit", () => {
49 const ranked = [
50 { item: "b", score: 20, tieBreaker: "b" },
51 { item: "d", score: 40, tieBreaker: "d" },
52 ];
53​
54 insertRankedSearchResult(ranked, { item: "a", score: 10, tieBreaker: "a" }, 2);
55 insertRankedSearchResult(ranked, { item: "c", score: 30, tieBreaker: "c" }, 2);
56​
57 expect(ranked.map((entry) => entry.item)).toEqual(["a", "b"]);
58 });
59});