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

searchRanking.test.ts

66 lines2.0 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 it("matches full Unicode code points instead of combining unrelated surrogate halves", () => {
47 expect(scoreSubsequenceMatch("😀🤨.md", "🔨")).toBeNull();
48 expect(scoreSubsequenceMatch("😀🔨.md", "🔨")).toBe(9);
49 expect(scoreSubsequenceMatch("😀🔨", "😀🔨")).toBe(0);
50 expect(scoreSubsequenceMatch("🔨.md", "🔨")).toBe(3);
51 });
52});
53​
54describe("insertRankedSearchResult", () => {
55 it("keeps the best-ranked candidates within the limit", () => {
56 const ranked = [
57 { item: "b", score: 20, tieBreaker: "b" },
58 { item: "d", score: 40, tieBreaker: "d" },
59 ];
60​
61 insertRankedSearchResult(ranked, { item: "a", score: 10, tieBreaker: "a" }, 2);
62 insertRankedSearchResult(ranked, { item: "c", score: 30, tieBreaker: "c" }, 2);
63​
64 expect(ranked.map((entry) => entry.item)).toEqual(["a", "b"]);
65 });
66});