Files
2026-11-16 17:26:44 -08:00

165 lines
7.6 KiB
TypeScript

// MUST be first: sets DATABASE_URL before db_client is evaluated.
import "./setup_env";
import { test, expect, describe } from "bun:test";
import {
containment, lexicalScore, usefulnessFactor, rankClaims, isStandingKind, extractTerms,
} from "../src/kb_retrieval";
import { earnsSemanticTier } from "../src/kb_db";
import type { KbClaim } from "../src/kb_db";
/**
* 检索策略的三条规则,各自钉一遍。
*
* 这三条都是被生产数据逼出来的,不是设计出来的 —— 所以测试里写的是**当时那批数据
* 长什么样**,而不是抽象的性质。将来谁要改阈值或权重,应该先能解释这几条为什么不再
* 成立。
*/
const claim = (over: Partial<KbClaim> = {}): KbClaim => ({
id: over.id ?? "c1", kind: "env", platform: "android",
scopeKey: "android:构建", scopeLabel: "构建", subjectKey: "airb远端",
statement: "airb 远端是共享池,批次之间会被其它任务 repurpose",
payload: null, status: "active", confidence: 0.9,
supportCount: 3, refuteCount: 0, firstSeenAt: 0, lastValidatedAt: 0, lastRefutedAt: null,
supersedesId: null, version: 1, source: "distilled", createdBy: null, updatedAt: 0,
embedding: null, domainKey: null,
tier: "semantic", injectedCount: 0, citedCount: 0, lastInjectedAt: null,
...over,
} as KbClaim);
describe("分层闸 · earnsSemanticTier", () => {
test("与具体任务无关的两类,天生可检索", () => {
expect(earnsSemanticTier({ kind: "env" })).toBe(true);
expect(earnsSemanticTier({ kind: "account" })).toBe(true);
});
test("产品面知识要先被第二个任务见证过,或被起草者引用过", () => {
expect(earnsSemanticTier({ kind: "entry_path" })).toBe(false);
expect(earnsSemanticTier({ kind: "entry_path", witnessedTargets: 1 })).toBe(false);
expect(earnsSemanticTier({ kind: "entry_path", witnessedTargets: 2 })).toBe(true);
expect(earnsSemanticTier({ kind: "entry_path", citedCount: 1 })).toBe(true);
});
test("人工录入的知识不需要再自证一次", () => {
expect(earnsSemanticTier({ kind: "entry_path", source: "human" })).toBe(true);
});
});
describe("有用度先验 · usefulnessFactor", () => {
test("没见过的知识落在中性点 —— 没被用过 ≠ 没用", () => {
expect(usefulnessFactor(0, 0)).toBeCloseTo(1, 5);
});
test("反复注入、一次没被引用的,会被压下去", () => {
expect(usefulnessFactor(20, 0)).toBeLessThan(0.7);
// 但压不到零:排序降权不等于删除,那是人的决定。
expect(usefulnessFactor(50, 0)).toBeGreaterThan(0);
});
test("被真正用过的会被抬起来,且有上限", () => {
expect(usefulnessFactor(10, 5)).toBeGreaterThan(1);
expect(usefulnessFactor(10, 10)).toBeLessThanOrEqual(1.6);
});
});
describe("文本信号", () => {
test("稀有词比常见词更有分量(BM25 的 idf)", () => {
const df = new Map([["cold_boot_sp", 1], ["页面", 40]]);
const rare = lexicalScore("… cold_boot_sp …", ["cold_boot_sp", "页面"], df, 40);
const common = lexicalScore("… 页面 …", ["cold_boot_sp", "页面"], df, 40);
expect(rare).toBeGreaterThan(common);
});
test("包含度而不是 Jaccard —— 一句话原样出现在长工单里应该算命中", () => {
const short = "特效面板打不开";
const long = `用户反馈进入相机后${short}反复出现,重启也不行,影响一批灰度用户`;
expect(containment(short, long)).toBeGreaterThan(0.9);
});
test("抽词会滤掉通用词", () => {
const terms = extractTerms("页面显示异常", "Story 相机 cold_boot_sp 配置");
expect(terms).toContain("cold_boot_sp");
expect(terms).not.toContain("页面");
});
test("英文产品词会扩展成中文知识词", () => {
const terms = extractTerms("remove the comment like button from the app");
expect(terms).toContain("comment");
expect(terms).toContain("评论");
expect(terms).toContain("点赞");
// Generic UI/action words deliberately stay untranslated: they widen the
// pool far more than domain-bearing concepts do.
expect(terms).not.toContain("按钮");
expect(terms).not.toContain("删除");
});
test("中文产品词也会扩展成英文知识词", () => {
const terms = extractTerms("移除评论区的点赞按钮");
expect(terms).toContain("comment");
expect(terms).toContain("digg");
expect(terms).not.toContain("delete");
});
test("短英文别名按完整 token 匹配,不会把 image 当成 IM", () => {
const terms = extractTerms("image loading failed");
expect(terms).toContain("图片");
expect(terms).not.toContain("私信");
});
test("英文 query 能把中文产品知识排在无关中文知识前", () => {
const hit = claim({
id: "hit", kind: "entry_path", subjectKey: "评论点赞入口",
statement: "评论区的点赞按钮由 CommentItemBinder 控制", scopeKey: "android:评论", scopeLabel: "评论区",
});
const miss = claim({
id: "miss", kind: "entry_path", subjectKey: "隐私设置入口",
statement: "隐私设置入口在个人主页右上角", scopeKey: "android:设置", scopeLabel: "设置",
});
const query = "remove the comment like button";
const ranked = rankClaims([miss, hit], { terms: extractTerms(query), ticketText: query });
expect(ranked[0].claim.id).toBe("hit");
expect(ranked[0].relevance).toBeGreaterThan(ranked[1].relevance);
});
});
describe("排序 · 常驻知识与产品面知识不是同一把尺子", () => {
test("常驻类走「历史表现 × 置信度」,不看工单词命中", () => {
expect(isStandingKind("env")).toBe(true);
expect(isStandingKind("entry_path")).toBe(false);
// 两条构建知识,工单一个字都没提到它们(相关性都≈0);差别只在过去有没有被用上。
const proven = claim({ id: "proven", injectedCount: 10, citedCount: 6 });
const ignored = claim({ id: "ignored", injectedCount: 20, citedCount: 0 });
const ranked = rankClaims([ignored, proven], { terms: ["直播", "礼物"], ticketText: "直播间礼物面板错位" });
expect(ranked[0].claim.id).toBe("proven");
// 这正是老排序做不到的事:两者置信度相同,老排序只能看置信度。
expect(ranked[0].relevance).toBeLessThan(0.1);
});
test("产品面类没命中工单词就应该排到后面去", () => {
const hit = claim({
id: "hit", kind: "entry_path", subjectKey: "入口路径",
statement: "礼物面板入口在直播间右下角", scopeKey: "android:直播", scopeLabel: "直播",
});
const miss = claim({
id: "miss", kind: "entry_path", subjectKey: "设置页",
statement: "隐私设置入口在我的→设置", scopeKey: "android:设置", scopeLabel: "设置",
});
const ranked = rankClaims([miss, hit], {
terms: ["直播", "礼物", "面板"], ticketText: "直播间礼物面板错位",
});
expect(ranked[0].claim.id).toBe("hit");
expect(ranked[0].relevance).toBeGreaterThan(ranked[1].relevance);
});
test("重放模式下先验来自调用方传入的计数,而不是行上的累计值", () => {
// 这一条挡的是效果度量里最容易犯的错:拿「已经发生的引用」去预测「会不会被引用」。
const c = claim({ id: "x", injectedCount: 99, citedCount: 99 });
const [scored] = rankClaims([c], {
terms: ["直播"], ticketText: "直播间礼物面板错位",
counters: new Map([["x", { injected: 0, cited: 0 }]]),
});
const [leaky] = rankClaims([c], { terms: ["直播"], ticketText: "直播间礼物面板错位" });
expect(scored.score).toBeLessThan(leaky.score);
});
});