Files
hose-use/src/domains/docs/docs.test.ts
T
gitea 6ba748e531 feat(docs): 支持多词分词模糊检索与匹配依据片段高亮展示
- 增强 `src/domains/docs/tools.ts` 的搜索评分引擎,支持自然多词组合与分词(如 "员工 手机"、"报销 审批")
- 扩充正文段落、请求参数表格、返回报文 JSON 与 CURL 示例的多层级深度匹配
- 新增 `matchReason` 匹配依据与上下文片段(Snippet)生成,在 CLI 表格及 JSON 结果中清晰呈现命中理由
- 完善 `src/domains/docs/docs.test.ts` 跨正文分词检索测试用例
2026-11-16 16:42:43 +08:00

156 lines
4.9 KiB
TypeScript

import { describe, expect, it } from "bun:test";
import { parseRawSearchIndexToCatalog } from "./api.js";
import {
getDocDetail,
getDocsCatalog,
listCategories,
searchDocs,
} from "./tools.js";
const mockRawSections = [
{
documents: [
{
i: 1001,
t: "查询角色组和角色",
u: "/docs/open-api/corporation/get-roles-group",
b: ["Open API", "企业通讯录"],
},
{
i: 1002,
t: "下载附件",
u: "/docs/open-api/attachment/attachment-download",
b: ["Open API", "附件"],
},
{
i: 1003,
t: "其他非 API 页面",
u: "/blog/archive",
b: [],
},
],
},
{
documents: [
{
i: 2001,
t: "Query Parameters",
u: "/docs/open-api/corporation/get-roles-group",
h: "#query-parameters",
p: 1001,
},
{
i: 2002,
t: "CURL",
u: "/docs/open-api/corporation/get-roles-group",
h: "#curl",
p: 1001,
},
],
},
{
documents: [
{
i: 3001,
t: "GET /api/openapi/v1/roleDefGroups 更新日志 0.7.135 🆕 新增了本接口。",
s: "查询角色组和角色",
p: 1001,
},
{
i: 3002,
t: "accessToken String 认证token 必填 - 通过 获取授权 获取 accessToken",
s: "Query Parameters",
p: 1001,
},
{
i: 3003,
t: "curl --location --request GET 'https://app.ekuaibao.com/api/openapi/v1/roleDefGroups?accessToken=TOKEN'",
s: "CURL",
p: 1001,
},
{
i: 3004,
t: "POST /api/openapi/v2/attachment/downloadurls",
s: "下载附件",
p: 1002,
},
],
},
];
describe("Docs Domain - 开放平台文档域", () => {
it("parseRawSearchIndexToCatalog 正确解析原始搜索索引并提取 HTTP 属性", () => {
const catalog = parseRawSearchIndexToCatalog(mockRawSections as any);
expect(catalog.totalDocs).toBe(2);
expect(catalog.totalEndpoints).toBe(2);
const roleGroupDoc = catalog.items.find((i) => i.id === 1001);
expect(roleGroupDoc).toBeDefined();
expect(roleGroupDoc?.title).toBe("查询角色组和角色");
expect(roleGroupDoc?.category).toBe("企业通讯录");
expect(roleGroupDoc?.method).toBe("GET");
expect(roleGroupDoc?.path).toBe("/api/openapi/v1/roleDefGroups");
expect(roleGroupDoc?.curl).toContain("/api/openapi/v1/roleDefGroups");
expect(roleGroupDoc?.parameters).toContain("accessToken");
const attachmentDoc = catalog.items.find((i) => i.id === 1002);
expect(attachmentDoc?.method).toBe("POST");
expect(attachmentDoc?.path).toBe("/api/openapi/v2/attachment/downloadurls");
});
it("getDocsCatalog 在测试环境中能安全加载预编译/内嵌规格", async () => {
const catalog = await getDocsCatalog();
expect(catalog.items.length).toBeGreaterThan(100);
expect(catalog.totalEndpoints).toBeGreaterThan(100);
});
it("searchDocs 按关键词检索接口并正确排序", async () => {
const res = await searchDocs({ keyword: "roleDefGroups" });
expect(res.total).toBeGreaterThanOrEqual(1);
expect(res.items[0]?.path).toBe("/api/openapi/v1/roleDefGroups");
expect(res.items[0]?.title).toBe("查询角色组和角色");
expect(res.items[0]?.matchReason).toBeDefined();
});
it("searchDocs 支持多词分词与跨正文/参数模糊检索", async () => {
const res = await searchDocs({ keyword: "员工 手机" });
expect(res.total).toBeGreaterThanOrEqual(1);
expect(res.items.some((i) => i.title.includes("员工"))).toBe(true);
expect(res.items[0]?.matchReason).toBeDefined();
});
it("searchDocs 支持按分类与 HTTP 方法精确过滤", async () => {
const res = await searchDocs({
category: "企业通讯录",
method: "GET",
});
expect(res.items.length).toBeGreaterThanOrEqual(1);
for (const item of res.items) {
expect(item.category).toBe("企业通讯录");
expect(item.method).toBe("GET");
}
});
it("getDocDetail 能按路径、URL、标题或 ID 精准定位文档", async () => {
const byPath = await getDocDetail("/api/openapi/v1/roleDefGroups");
expect(byPath).toBeDefined();
expect(byPath?.title).toBe("查询角色组和角色");
const byUrl = await getDocDetail("/docs/open-api/corporation/get-roles-group");
expect(byUrl).toBeDefined();
expect(byUrl?.id).toBe(byPath?.id);
const byTitle = await getDocDetail("查询角色组和角色");
expect(byTitle?.path).toBe("/api/openapi/v1/roleDefGroups");
});
it("listCategories 能够统计分类列表与端点总数", async () => {
const cats = await listCategories();
expect(cats.length).toBeGreaterThanOrEqual(10);
const corpCat = cats.find((c) => c.name === "企业通讯录");
expect(corpCat).toBeDefined();
expect(corpCat?.count).toBeGreaterThan(0);
expect(corpCat?.endpointsCount).toBeGreaterThan(0);
});
});