141 lines
4.9 KiB
TypeScript
141 lines
4.9 KiB
TypeScript
import { describe, expect, test } from "bun:test";
|
|
import type { CorpConfig } from "../config/types.js";
|
|
import type { FuzzyMatchOptions } from "./types.js";
|
|
import { filterCorpsFuzzy } from "./rank.js";
|
|
|
|
// Golden evaluation set for corp search: real-shaped (query -> expected result)
|
|
// pairs collected from usage regressions and CLI tests. Every tuning change to
|
|
// scores/thresholds must keep this green; grow it with every real-world
|
|
// mis-hit or miss instead of re-tuning against a single complaint.
|
|
|
|
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
|
|
};
|
|
}
|
|
|
|
const HOSE_SOURCE = {
|
|
type: "hose",
|
|
corpId: "ID01VAdCnJEdFd",
|
|
staffId: "staff-channel",
|
|
domain: "https://app.ekuaibao.com"
|
|
} as any;
|
|
|
|
const CORPUS: CorpConfig[] = [
|
|
makeCorp("corp-xiezihao", "谢子豪"),
|
|
makeCorp("corp-qudao", "渠道AI演示环境", { source: HOSE_SOURCE }),
|
|
makeCorp("corp-acme", "Acme Global", { environment: "test" }),
|
|
makeCorp("corp-yuance", "园测信息科技股份有限公司"),
|
|
makeCorp("corp-yutai", "钰泰半导体股份有限公司"),
|
|
makeCorp("corp-huaxin", "华芯半导体股份有限公司"),
|
|
makeCorp("corp-nanjing", "南京测绘"),
|
|
makeCorp("corp-pingan", "中国平安人寿保险股份有限公司"),
|
|
makeCorp("corp-gb", "GB国际(集团)有限公司"),
|
|
makeCorp("corp-shengchan", "生产正式企业", {
|
|
source: {
|
|
type: "seal",
|
|
token: "shengchan-token",
|
|
sealUrl: "https://shengchan.seal.test",
|
|
expiresIn: 3600,
|
|
staffId: "admin-zhangsan"
|
|
} as any
|
|
})
|
|
];
|
|
|
|
interface GoldenCase {
|
|
query: string;
|
|
/** The id that must rank first. */
|
|
top?: string;
|
|
/** Ids that must appear somewhere in the results. */
|
|
includes?: string[];
|
|
/** Ids that must not appear (noise suppression checks). */
|
|
excludes?: string[];
|
|
/** No corp may match at all. */
|
|
empty?: boolean;
|
|
options?: FuzzyMatchOptions;
|
|
}
|
|
|
|
const GOLDEN: GoldenCase[] = [
|
|
// Exact & substring bands
|
|
{ query: "园测信息科技股份有限公司", top: "corp-yuance" },
|
|
{ query: "园测信息", top: "corp-yuance" },
|
|
{ query: "信息科技", top: "corp-yuance" },
|
|
{ query: "平安", top: "corp-pingan" },
|
|
{ query: "渠道", top: "corp-qudao" },
|
|
// Initials / pinyin / homophones
|
|
{ query: "xzh", top: "corp-xiezihao" },
|
|
{ query: "xiezihao", top: "corp-xiezihao" },
|
|
{ query: "鞋子豪", top: "corp-xiezihao" },
|
|
{ query: "谢自豪", top: "corp-xiezihao" },
|
|
{ query: "qudao", top: "corp-qudao" },
|
|
{ query: "sc", top: "corp-shengchan" },
|
|
{ query: "盛产", top: "corp-shengchan" },
|
|
{ query: "yuan-ce", top: "corp-yuance" },
|
|
// Typo tolerance
|
|
{ query: "xieziha", top: "corp-xiezihao" },
|
|
{ query: "zhangsa", top: "corp-shengchan" },
|
|
// Tokenized Chinese queries with a contextual typo
|
|
{ query: "瑞泰半导体", top: "corp-yutai", excludes: ["corp-huaxin"] },
|
|
{ query: "瑞泰 半导体", top: "corp-yutai", excludes: ["corp-huaxin"] },
|
|
{ query: "半导体 瑞泰", top: "corp-yutai", excludes: ["corp-huaxin"] },
|
|
{ query: "钰泰 半导体", top: "corp-yutai" },
|
|
// Compact name inside a long query (abbreviation + weak suppression)
|
|
{ query: "苏州园区测绘", top: "corp-yuance", excludes: ["corp-nanjing"] },
|
|
// Identifier fields
|
|
{ query: "staff-channel", top: "corp-qudao" },
|
|
{ query: "ID01VAdCnJEdFd", top: "corp-qudao" },
|
|
{ query: "test", top: "corp-acme" },
|
|
// Full-width / half-width (NFKC)
|
|
{ query: "GB", top: "corp-gb" },
|
|
{ query: "gb", top: "corp-gb" },
|
|
// Strict mode
|
|
{ query: "钰泰半导体", options: { strict: true }, top: "corp-yutai" },
|
|
{ query: "瑞泰半导体", options: { strict: true }, empty: true },
|
|
{ query: "苏州园区测绘", options: { strict: true }, empty: true },
|
|
// Unrelated queries match nothing
|
|
{ query: "alibaba", empty: true },
|
|
{ query: "腾讯", empty: true }
|
|
];
|
|
|
|
describe("corp search golden set", () => {
|
|
for (const golden of GOLDEN) {
|
|
const label = `golden: ${golden.query}${golden.options?.strict ? " --strict" : ""}`;
|
|
test(label, () => {
|
|
const results = filterCorpsFuzzy(CORPUS, golden.query, golden.options ?? {});
|
|
const ids = results.map((corp) => corp.id);
|
|
|
|
if (golden.empty) {
|
|
expect(ids).toEqual([]);
|
|
}
|
|
if (golden.top) {
|
|
expect(ids[0]).toBe(golden.top);
|
|
}
|
|
for (const id of golden.includes ?? []) {
|
|
expect(ids).toContain(id);
|
|
}
|
|
for (const id of golden.excludes ?? []) {
|
|
expect(ids).not.toContain(id);
|
|
}
|
|
});
|
|
}
|
|
});
|