evOo-local/packages/core/src/search.test.ts

40 lines
1.6 KiB
TypeScript

import { describe, expect, it } from "vitest";
import {
extractLinksFromText,
matchesSearchQuery,
searchHitThreadTarget,
snippetAroundMatch,
} from "./search.js";
describe("search helpers", () => {
it("extracts http and https links from text", () => {
expect(extractLinksFromText("see https://example.com/docs and http://test.local/x.")).toEqual([
"https://example.com/docs",
"http://test.local/x",
]);
});
it("matches case-insensitive substrings and multi-token queries", () => {
expect(matchesSearchQuery("Venue", "Research venues")).toBe(true);
expect(matchesSearchQuery("research berlin", "Research top venues in Berlin")).toBe(true);
expect(matchesSearchQuery("research tokyo", "Research top venues in Berlin")).toBe(false);
expect(matchesSearchQuery("missing", "Research venues")).toBe(false);
});
it("builds a short snippet around the query or first matching token", () => {
const text = "alpha beta gamma delta epsilon zeta eta theta";
expect(snippetAroundMatch(text, "gamma", 20)).toContain("gamma");
expect(snippetAroundMatch(text, "zeta alpha", 25)).toContain("zeta");
});
it("uses groupId for group search message jumps", () => {
expect(searchHitThreadTarget({ groupId: "group-1" })).toEqual({ groupId: "group-1" });
expect(searchHitThreadTarget({ botId: "bot-1" })).toEqual({ botId: "bot-1" });
});
it("rejects ambiguous search hit targets", () => {
expect(() => searchHitThreadTarget({})).toThrow();
expect(() => searchHitThreadTarget({ botId: "bot-1", groupId: "group-1" })).toThrow();
});
});