feat(docs): 新增合思开放平台官方接口文档检索独立业务域与 CLI 客户端

- 新增 `src/domains/docs/` 领域(api.ts, tools.ts, types.ts, README.docs.md, docs.test.ts)
- 实现从线上 `docs.ekuaibao.com/search-index.json` 实时拉取最新规范与端点提取能力
- 集成多级缓存(L1 内存 + L2 SQLite)与 `specs/docs-catalog.json` 静态预编译保底,支持零网络离线毫秒级检索
- 提供 270+ 个 OpenAPI HTTP 接口端点、参数表格、CURL 示范与成功响应示例的高精度解析与结构化输出
- 注册 `hose-use docs` CLI 命令(search, show, categories, sync)及 `doc`, `api-doc` 别名
- 严格遵循 AGENTS.md 架构分层约束,全量 1144+ 自动化单测与 tsc 类型检查 100% 通过
This commit is contained in:
gitea committed 2026-11-16 16:29:27 +08:00
1 parent 448dd438ff
commit 4f572ad9c1
11 files changed
+1013 -2

No files matched your search

+147
View File
@@ -0,0 +1,147 @@
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("查询角色组和角色");
});
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);
});
});