Files
seal-use/src/core/corp-search/rank.test.ts
T

157 lines
5.8 KiB
TypeScript

import { describe, expect, test } from "bun:test";
import type { CorpConfig } from "../config/types.js";
import { filterCorpsFuzzy } from "./rank.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: "seal",
token: `${id}-token`,
sealUrl: `https://${id}.seal.test`,
expiresIn: 3600,
staffId: `staff-${id}`
},
auth: {
refreshTtl: 300
},
...overrides
};
}
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");
});
test("finds a compact enterprise name inside a longer Chinese query", () => {
const compactName = makeCorp("corp-yuan-ce", "园测信息科技股份有限公司");
const unrelated = makeCorp("corp-other", "南京测绘");
const results = filterCorpsFuzzy([unrelated, compactName], "苏州园区测绘");
expect(results[0]!.id).toBe("corp-yuan-ce");
expect(results).toHaveLength(1);
});
test("keeps strict search limited to exact substrings", () => {
const compactName = makeCorp("corp-yuan-ce", "园测信息科技股份有限公司");
const results = filterCorpsFuzzy([compactName], "苏州园区测绘", { strict: true });
expect(results).toEqual([]);
});
test("matches Chinese name and industry words despite a contextual name typo", () => {
const target = makeCorp("corp-yutai", "钰泰半导体股份有限公司");
const sameIndustry = makeCorp("corp-other", "华芯半导体股份有限公司");
for (const query of ["瑞泰半导体", "瑞泰 半导体", "半导体 瑞泰", "钰泰 半导体"]) {
expect(filterCorpsFuzzy([sameIndustry, target], query).map((corp) => corp.id))
.toEqual([target.id]);
}
});
test("requires another matching word before tolerating a typo in a short Chinese name", () => {
const target = makeCorp("corp-yutai", "钰泰半导体股份有限公司");
expect(filterCorpsFuzzy([target], "瑞泰")).toEqual([]);
expect(filterCorpsFuzzy([target], "瑞泰 半导体", { strict: true })).toEqual([]);
expect(filterCorpsFuzzy([target], "钰泰半导体", { strict: true })).toEqual([target]);
});
test("breaks relevance ties by recent usage", () => {
const a = makeCorp("corp-a", "匹配企业甲");
const b = makeCorp("corp-b", "匹配企业乙");
// Both names prefix-match at the same score; higher usage wins even against
// input order and zh-CN name collation (甲 < 乙 alphabetically).
const usage = new Map([["corp-a", 1], ["corp-b", 5]]);
const results = filterCorpsFuzzy([a, b], "匹配企业", {}, usage);
expect(results.map((corp) => corp.id)).toEqual(["corp-b", "corp-a"]);
});
test("usage never outranks a better match", () => {
const weak = makeCorp("corp-weak", "匹配企业甲");
const strong = makeCorp("corp-strong", "匹配企业");
const usage = new Map([["corp-weak", 99]]);
const results = filterCorpsFuzzy([weak, strong], "匹配企业", {}, usage);
expect(results[0]!.id).toBe("corp-strong");
});
test("falls back to name and id order when usage is absent", () => {
const second = makeCorp("corp-b", "匹配企业乙");
const first = makeCorp("corp-a", "匹配企业甲");
const results = filterCorpsFuzzy([second, first], "匹配企业");
expect(results.map((corp) => corp.id)).toEqual(["corp-a", "corp-b"]);
});
});