|
| 1 | +import { describe, expect, it } from "vitest" |
| 2 | +import { Bm25Ranker } from "../Bm25Ranker" |
| 3 | +import { ToolDoc } from "../types" |
| 4 | + |
| 5 | +const tool = (serverName: string, toolName: string, description: string): ToolDoc => ({ |
| 6 | + serverName, |
| 7 | + toolName, |
| 8 | + description, |
| 9 | +}) |
| 10 | + |
| 11 | +describe("Bm25Ranker", () => { |
| 12 | + it("ranks matching MCP tools by BM25 relevance", () => { |
| 13 | + const items = [ |
| 14 | + tool("slack", "postMessage", "Send a Slack message"), |
| 15 | + tool("jira", "createIssue", "Send messages and create Jira issues"), |
| 16 | + tool("postgres", "query", "Send data and query a Postgres database"), |
| 17 | + ] |
| 18 | + |
| 19 | + expect(new Bm25Ranker().rank("send slack message", items, 3)).toEqual([items[0], items[1], items[2]]) |
| 20 | + }) |
| 21 | + |
| 22 | + it("returns an empty result for empty queries, empty items, and no matches", () => { |
| 23 | + const ranker = new Bm25Ranker() |
| 24 | + const items = [tool("slack", "postMessage", "Send a message")] |
| 25 | + |
| 26 | + expect(ranker.rank(" ", items, 10)).toEqual([]) |
| 27 | + expect(ranker.rank("message", [], 10)).toEqual([]) |
| 28 | + expect(ranker.rank("unrelated", items, 10)).toEqual([]) |
| 29 | + }) |
| 30 | + |
| 31 | + it("truncates results to the requested top-k", () => { |
| 32 | + const items = [ |
| 33 | + tool("one", "search", "Search records"), |
| 34 | + tool("two", "search", "Search records"), |
| 35 | + tool("three", "search", "Search records"), |
| 36 | + ] |
| 37 | + |
| 38 | + expect(new Bm25Ranker().rank("search", items, 2)).toEqual(items.slice(0, 2)) |
| 39 | + expect(new Bm25Ranker().rank("search", items, 0)).toEqual([]) |
| 40 | + }) |
| 41 | + |
| 42 | + it("preserves input order for equal scores", () => { |
| 43 | + const items = [tool("first", "lookup", "Find a record"), tool("second", "lookup", "Find a record")] |
| 44 | + |
| 45 | + expect(new Bm25Ranker().rank("record", items, 10)).toEqual(items) |
| 46 | + }) |
| 47 | + |
| 48 | + it("tokenizes case-insensitively around punctuation", () => { |
| 49 | + const matching = tool("Slack-Server", "POST.Message", "Send a message") |
| 50 | + const nonMatching = tool("calendar", "createEvent", "Create a calendar event") |
| 51 | + |
| 52 | + expect(new Bm25Ranker().rank("SLACK post message", [nonMatching, matching], 10)).toEqual([matching]) |
| 53 | + }) |
| 54 | + |
| 55 | + it("handles tools with empty descriptions", () => { |
| 56 | + const item = tool("GitHub", "listRepos", "") |
| 57 | + |
| 58 | + expect(new Bm25Ranker().rank("github repos", [item], 10)).toEqual([item]) |
| 59 | + }) |
| 60 | + |
| 61 | + it("rebuilds only when the items array reference changes", () => { |
| 62 | + const items = [tool("slack", "send", "Send a message")] |
| 63 | + const ranker = new Bm25Ranker() |
| 64 | + |
| 65 | + ranker.rank("send", items, 10) |
| 66 | + items.push(tool("jira", "create", "Create an issue")) |
| 67 | + expect(ranker.rank("issue", items, 10)).toEqual([]) |
| 68 | + |
| 69 | + const newItems = items.slice() |
| 70 | + expect(ranker.rank("issue", newItems, 10)).toEqual([newItems[1]]) |
| 71 | + }) |
| 72 | +}) |
0 commit comments