176 lines
5.2 KiB
TypeScript
176 lines
5.2 KiB
TypeScript
import { describe, expect, test } from "bun:test";
|
|
import { KyInstance } from "ky";
|
|
import { CorpConfig } from "../../core/config/types.js";
|
|
import { searchApprovalContent } from "./search.js";
|
|
|
|
function fakeSearchClient(): KyInstance {
|
|
return {
|
|
get: (url: string) => ({
|
|
json: async () => {
|
|
if (url.startsWith("api/v1/rules")) {
|
|
return {
|
|
data: {
|
|
rules: [
|
|
{
|
|
id: "rule-101",
|
|
scope: "差旅报销",
|
|
strictness: "MUST_FOLLOW",
|
|
status: "active",
|
|
description: "差旅住宿发票必须提供明细与住宿清单"
|
|
},
|
|
{
|
|
id: "rule-102",
|
|
scope: "餐饮招待",
|
|
strictness: "SHOULD_FOLLOW",
|
|
status: "active",
|
|
description: "餐饮发票单笔超过 500 元需附同人同行名单"
|
|
}
|
|
]
|
|
}
|
|
};
|
|
}
|
|
if (url.startsWith("api/v1/knowledge/articles")) {
|
|
return {
|
|
data: {
|
|
total: 1,
|
|
articles: [
|
|
{
|
|
id: "doc-1",
|
|
title: "企业差旅管理制度",
|
|
purpose: "规范集团员工出差标准",
|
|
contentType: "policy",
|
|
content: "第一条 出差前需提交申请单\n第二条 住宿费标准每晚最高 800 元\n第三条 报销须在出差结束后 15 天内提交",
|
|
status: "published",
|
|
scenarios: ["差旅"],
|
|
tags: ["制度"]
|
|
}
|
|
]
|
|
}
|
|
};
|
|
}
|
|
if (url === "api/v1/agent/ai-approval/config") {
|
|
return {
|
|
data: {
|
|
tone: "professional",
|
|
decisionPreferences: {
|
|
explicitApprove: "规则全过且无风险时直接通过",
|
|
review: "存疑单据转人工复核"
|
|
},
|
|
integrations: [
|
|
{
|
|
type: "hosecloud",
|
|
outboundName: "合思推送",
|
|
mode: "automatic"
|
|
}
|
|
]
|
|
}
|
|
};
|
|
}
|
|
return { data: {} };
|
|
}
|
|
})
|
|
} as unknown as KyInstance;
|
|
}
|
|
|
|
function fakeCorp(): CorpConfig {
|
|
return {
|
|
id: "corp-search-test",
|
|
name: "Search Test Corp",
|
|
environment: "production",
|
|
seal: {
|
|
url: "https://seal.test",
|
|
endpoints: {
|
|
approvalStylePreferences: "api/v1/agent/ai-approval/config"
|
|
}
|
|
},
|
|
source: {
|
|
type: "direct",
|
|
token: "tok",
|
|
sealUrl: "https://seal.test",
|
|
expiresIn: 3600,
|
|
staffId: "direct"
|
|
},
|
|
auth: {
|
|
refreshTtl: 300
|
|
}
|
|
};
|
|
}
|
|
|
|
describe("searchApprovalContent", () => {
|
|
test("searches across rules, documents, and preferences by default", async () => {
|
|
const client = fakeSearchClient();
|
|
const corp = fakeCorp();
|
|
|
|
const result = await searchApprovalContent(client, corp, {
|
|
keywords: ["差旅"],
|
|
refresh: true
|
|
});
|
|
|
|
expect(result.total).toBeGreaterThan(0);
|
|
const areas = new Set(result.results.map((r) => r.area));
|
|
expect(areas.has("rules")).toBe(true);
|
|
expect(areas.has("documents")).toBe(true);
|
|
});
|
|
|
|
test("respects matchMode 'all' vs 'any'", async () => {
|
|
const client = fakeSearchClient();
|
|
const corp = fakeCorp();
|
|
|
|
const anyResult = await searchApprovalContent(client, corp, {
|
|
keywords: ["住宿", "发票"],
|
|
matchMode: "any",
|
|
refresh: true
|
|
});
|
|
expect(anyResult.total).toBeGreaterThan(0);
|
|
|
|
const allResult = await searchApprovalContent(client, corp, {
|
|
keywords: ["住宿", "发票"],
|
|
matchMode: "all",
|
|
refresh: true
|
|
});
|
|
// Line: "差旅住宿发票必须提供明细与住宿清单" contains both "住宿" and "发票"
|
|
expect(allResult.total).toBeGreaterThan(0);
|
|
expect(allResult.results[0]?.matchedKeywords).toEqual(["住宿", "发票"]);
|
|
});
|
|
|
|
test("filters search by explicit area", async () => {
|
|
const client = fakeSearchClient();
|
|
const corp = fakeCorp();
|
|
|
|
const rulesOnly = await searchApprovalContent(client, corp, {
|
|
keywords: ["住宿"],
|
|
areas: ["rules"],
|
|
refresh: true
|
|
});
|
|
|
|
expect(rulesOnly.results.every((r) => r.area === "rules")).toBe(true);
|
|
});
|
|
|
|
test("supports snippetOnly and maxChars truncation", async () => {
|
|
const client = fakeSearchClient();
|
|
const corp = fakeCorp();
|
|
|
|
const snippetResult = await searchApprovalContent(client, corp, {
|
|
keywords: ["住宿"],
|
|
snippetOnly: true,
|
|
maxChars: 10,
|
|
refresh: true
|
|
});
|
|
|
|
const first = snippetResult.results[0];
|
|
expect(first).toBeDefined();
|
|
expect(first?.context.before).toEqual([]);
|
|
expect(first?.context.after).toEqual([]);
|
|
expect(first?.matchedText.length).toBeLessThanOrEqual(13); // 10 chars + "..."
|
|
});
|
|
|
|
test("throws error when keywords array is empty", () => {
|
|
const client = fakeSearchClient();
|
|
const corp = fakeCorp();
|
|
|
|
expect(searchApprovalContent(client, corp, { keywords: [" "] })).rejects.toThrow(
|
|
"keywords must contain at least one non-empty string"
|
|
);
|
|
});
|
|
});
|