import { describe, expect, test } from "bun:test"; import { rankBySimilarity, scoreAndFilterMatches } from "./rank"; describe("rank by similarity", () => { test("keeps only close high-confidence matches", () => { const matches = rankBySimilarity( "browser automation", [1, 0], [ { name: "agent-browser", searchText: "agent-browser: cli for browser automation, page interaction, and screenshots.", pathOrder: 0, vector: [1, 0], }, { name: "playwright-core", searchText: "playwright-core: browser automation library and runner.", pathOrder: 2, vector: [0.96, 0.28], }, { name: "firefox", searchText: "firefox: graphical web browser for general browsing.", pathOrder: 1, vector: [0.5, 0.5], }, { name: "git", searchText: "git: distributed version control system.", pathOrder: 3, vector: [-1, 0], }, ], ); expect(matches).toEqual(["agent-browser", "playwright-core"]); }); test("returns no matches when the top score is too weak", () => { const matches = rankBySimilarity( "browser automation", [1, 0], [ { name: "firefox", searchText: "firefox: graphical web browser for general browsing.", pathOrder: 1, vector: [0.2, 0.98], }, { name: "git", searchText: "git: distributed version control system.", pathOrder: 0, vector: [0.1, 0.99], }, ], ); expect(matches).toEqual([]); }); test("preserves path and name tie-breaks for equal scores", () => { const matches = rankBySimilarity( "editor", [1, 0], [ { name: "zed", searchText: "zed: code editor.", pathOrder: 2, vector: [1, 0] }, { name: "code", searchText: "code: code editor.", pathOrder: 1, vector: [1, 0] }, { name: "vim", searchText: "vim: terminal editor.", pathOrder: 1, vector: [1, 0] }, ], ); expect(matches).toEqual(["code", "vim", "zed"]); }); test("caps the result set to the strongest eight matches", () => { const matches = scoreAndFilterMatches( "tooling", [1, 0], Array.from({ length: 10 }, (_, index) => ({ name: `cmd-${index}`, searchText: `cmd-${index}: tooling command.`, pathOrder: index, vector: [1, 0], })), ); expect(matches).toHaveLength(8); expect(matches.map((entry) => entry.name)).toEqual([ "cmd-0", "cmd-1", "cmd-2", "cmd-3", "cmd-4", "cmd-5", "cmd-6", "cmd-7", ]); }); test("fails fast on malformed command vectors", () => { expect(() => rankBySimilarity("broken", [1, 0], [ { name: "broken", searchText: "broken: malformed.", pathOrder: 0, vector: [1] }, ]), ).toThrow("embedding dimension mismatch for broken"); }); test("fails fast on zero-magnitude vectors", () => { expect(() => rankBySimilarity("broken", [1, 0], [ { name: "broken", searchText: "broken: zero vector.", pathOrder: 0, vector: [0, 0] }, ]), ).toThrow("zero-magnitude embedding vector for broken"); }); test("prefers lexical sanity matches when vector ranking is noisy", () => { const matches = rankBySimilarity( "browser automation", [1, 0], [ { name: "env", searchText: "env: run a command in a modified environment.", pathOrder: 0, vector: [1, 0], }, { name: "agent-browser", searchText: "agent-browser: cli for browser automation and screenshots.", pathOrder: 1, vector: [0.92, 0.39], }, { name: "playwright-core", searchText: "playwright-core: browser automation library and runner.", pathOrder: 2, vector: [0.9, 0.43], }, ], ); expect(matches).toEqual(["agent-browser", "playwright-core"]); }); test("keeps the highest lexical overlap cluster when available", () => { const matches = rankBySimilarity( "browser automation", [1, 0], [ { name: "codex", searchText: "codex: agentic automation cli for repository work.", pathOrder: 0, vector: [1, 0], }, { name: "chromium", searchText: "chromium: browser command for page navigation and automation.", pathOrder: 1, vector: [0.94, 0.34], }, { name: "google-chrome-stable", searchText: "google-chrome-stable: launches the google chrome browser.", pathOrder: 2, vector: [0.93, 0.36], }, ], ); expect(matches).toEqual(["chromium"]); }); });