Files
seal-use/src/core/search/fuzzy.test.ts
T
gitea b6aa4efd09 Add intelligent soft fuzzy search to corps search command
Support Chinese homophones, front/back nasal vowels, initials, typo/missing letter tolerance, and --strict exact mode fallback.
2026-09-11 17:43:58 +08:00

228 lines
8.0 KiB
TypeScript

import { describe, expect, test } from "bun:test";
import type { CorpConfig } from "../config/types.js";
import {
filterCorpsFuzzy,
fuzzyMatchString,
levenshteinDistance,
matchCorp,
minSubstringLevenshtein,
normalizeFuzzyPinyin
} from "./fuzzy.js";
function makeCorp(id: string, name: string, overrides: Partial<CorpConfig> = {}): CorpConfig {
return {
id,
name,
environment: "production",
seal: {
tenantSlug: `${id}-slug`,
endpoints: {
approvalStylePreferences: "api/v1/agent/ai-approval/config"
}
},
source: {
type: "direct",
token: `${id}-token`,
sealUrl: `https://${id}.seal.test`,
expiresIn: 3600,
staffId: `staff-${id}`
},
auth: {
refreshTtl: 300
},
...overrides
};
}
describe("fuzzy search utilities", () => {
describe("levenshteinDistance", () => {
test("calculates distance correctly", () => {
expect(levenshteinDistance("", "")).toBe(0);
expect(levenshteinDistance("abc", "abc")).toBe(0);
expect(levenshteinDistance("abc", "ab")).toBe(1);
expect(levenshteinDistance("abc", "abcd")).toBe(1);
expect(levenshteinDistance("kitten", "sitting")).toBe(3);
});
});
describe("minSubstringLevenshtein", () => {
test("detects exact substring with distance 0", () => {
expect(minSubstringLevenshtein("abc", "xyzabcdef")).toBe(0);
});
test("detects missing letter with distance 1", () => {
expect(minSubstringLevenshtein("acm", "acme corp")).toBe(0); // acm is substring of acme
expect(minSubstringLevenshtein("xiezihao", "xieziha")).toBe(1); // target missing 'o'
expect(minSubstringLevenshtein("xiezhao", "xiezihao")).toBe(1); // query missing 'i'
});
test("detects single substitution typo with distance 1", () => {
expect(minSubstringLevenshtein("xiezhao", "xiezihao")).toBe(1);
});
});
describe("normalizeFuzzyPinyin", () => {
test("normalizes retroflex to flat consonants", () => {
expect(normalizeFuzzyPinyin("张")).toBe(normalizeFuzzyPinyin("詹")); // zhang -> zhang/zhan -> zan
expect(normalizeFuzzyPinyin("超")).toContain("cao");
expect(normalizeFuzzyPinyin("沙")).toContain("sa");
});
test("normalizes front and back nasals", () => {
expect(normalizeFuzzyPinyin("陈")).toBe(normalizeFuzzyPinyin("程")); // chen / cheng -> cen
expect(normalizeFuzzyPinyin("金")).toBe(normalizeFuzzyPinyin("京")); // jin / jing -> jin
});
test("normalizes homophones to identical representations", () => {
expect(normalizeFuzzyPinyin("谢子豪")).toBe(normalizeFuzzyPinyin("鞋子豪"));
expect(normalizeFuzzyPinyin("渠道")).toBe(normalizeFuzzyPinyin("渠到"));
});
});
describe("fuzzyMatchString", () => {
test("exact match", () => {
const res = fuzzyMatchString("谢子豪", "谢子豪");
expect(res.matched).toBe(true);
expect(res.reason).toBe("exact");
expect(res.score).toBe(1.0);
});
test("substring match", () => {
const res = fuzzyMatchString("谢子", "谢子豪");
expect(res.matched).toBe(true);
expect(res.reason).toBe("substring");
expect(res.score).toBe(0.95);
});
test("initials match (简拼)", () => {
const res = fuzzyMatchString("xzh", "谢子豪");
expect(res.matched).toBe(true);
expect(res.reason).toBe("initials");
expect(res.score).toBe(0.9);
});
test("pinyin match (全拼)", () => {
const res = fuzzyMatchString("xiezihao", "谢子豪");
expect(res.matched).toBe(true);
expect(res.reason).toBe("pinyin");
});
test("homophones (同音字)", () => {
const res1 = fuzzyMatchString("鞋子豪", "谢子豪");
expect(res1.matched).toBe(true);
expect(res1.reason).toBe("fuzzy_pinyin");
const res2 = fuzzyMatchString("渠到", "渠道AI演示环境");
expect(res2.matched).toBe(true);
expect(res2.reason).toBe("fuzzy_pinyin");
});
test("front/back nasals & flat/retroflex (不区分前后鼻音和平翘舌)", () => {
const res1 = fuzzyMatchString("詹三", "张三");
expect(res1.matched).toBe(true);
const res2 = fuzzyMatchString("谢自豪", "谢子豪");
expect(res2.matched).toBe(true);
});
test("missing 1 letter or typo tolerance (缺失字母或轻微拼写错误)", () => {
const res1 = fuzzyMatchString("xieziha", "谢子豪");
expect(res1.matched).toBe(true);
const res2 = fuzzyMatchString("xiezhao", "谢子豪");
expect(res2.matched).toBe(true);
const res3 = fuzzyMatchString("acme", "acm corp");
expect(res3.matched).toBe(true);
expect(res3.reason).toBe("levenshtein");
});
test("strict mode rejects fuzzy / homophone / typo matches", () => {
expect(fuzzyMatchString("鞋子豪", "谢子豪", { strict: true }).matched).toBe(false);
expect(fuzzyMatchString("xzh", "谢子豪", { strict: true }).matched).toBe(false);
expect(fuzzyMatchString("xieziha", "谢子豪", { strict: true }).matched).toBe(false);
// But exact substring is allowed in strict mode
expect(fuzzyMatchString("谢子", "谢子豪", { strict: true }).matched).toBe(true);
expect(fuzzyMatchString("谢子豪", "谢子豪", { strict: true }).matched).toBe(true);
});
test("rejects completely unrelated terms", () => {
expect(fuzzyMatchString("alibaba", "谢子豪").matched).toBe(false);
expect(fuzzyMatchString("tencent", "ID01VAdCnJEdFd").matched).toBe(false);
expect(fuzzyMatchString("xyz", "渠道环境").matched).toBe(false);
});
});
describe("filterCorpsFuzzy", () => {
const corp1 = makeCorp("xiezihao", "谢子豪", {
testCorpId: "test-xzh"
});
const corp2 = makeCorp("ID01VAdCnJEdFd", "渠道AI演示环境", {
source: {
type: "hose",
corpId: "ID01VAdCnJEdFd",
staffId: "staff-channel",
domain: "https://app.ekuaibao.com"
} as any
});
const corp3 = makeCorp("acme-corp", "Acme Global", {
environment: "test"
});
const corps = [corp1, corp2, corp3];
test("finds enterprise by homophone", () => {
const results = filterCorpsFuzzy(corps, "鞋子豪");
expect(results.length).toBe(1);
expect(results[0]!.id).toBe("xiezihao");
});
test("finds enterprise by initials", () => {
const results = filterCorpsFuzzy(corps, "xzh");
expect(results.length).toBe(1);
expect(results[0]!.id).toBe("xiezihao");
});
test("finds enterprise by missing letter query", () => {
const results = filterCorpsFuzzy(corps, "xieziha");
expect(results.length).toBe(1);
expect(results[0]!.id).toBe("xiezihao");
});
test("finds enterprise by pinyin of Chinese name", () => {
const results = filterCorpsFuzzy(corps, "qudao");
expect(results.length).toBe(1);
expect(results[0]!.id).toBe("ID01VAdCnJEdFd");
});
test("finds enterprise by hose corpId or staffId", () => {
const results = filterCorpsFuzzy(corps, "ID01VAdCnJEdFd");
expect(results.length).toBe(1);
expect(results[0]!.id).toBe("ID01VAdCnJEdFd");
const staffResults = filterCorpsFuzzy(corps, "staff-channel");
expect(staffResults.length).toBe(1);
expect(staffResults[0]!.id).toBe("ID01VAdCnJEdFd");
});
test("strict mode filtering excludes fuzzy homophone matches", () => {
const fuzzyRes = filterCorpsFuzzy(corps, "鞋子豪", { strict: false });
expect(fuzzyRes.length).toBe(1);
const strictRes = filterCorpsFuzzy(corps, "鞋子豪", { strict: true });
expect(strictRes.length).toBe(0);
const strictExact = filterCorpsFuzzy(corps, "谢子豪", { strict: true });
expect(strictExact.length).toBe(1);
});
test("ranks exact match higher than fuzzy matches", () => {
const cExact = makeCorp("test-qudao", "渠道");
const cFuzzy = makeCorp("test-other", "渠到另外一家");
const ranked = filterCorpsFuzzy([cFuzzy, cExact], "渠道");
expect(ranked[0]!.id).toBe("test-qudao");
});
});
});