api-openapi-spec.test.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /**
  2. * OpenAPI 规范验证测试
  3. *
  4. * 目的:
  5. * - 验证生成的 OpenAPI 文档符合 3.1.0 规范
  6. * - 确保所有端点都有必要的文档字段(summary, description, tags)
  7. * - 验证 schema 定义完整性
  8. *
  9. * 用法:
  10. * bun run test:api
  11. */
  12. import { beforeAll, describe, expect, test } from "vitest";
  13. import { callActionsRoute } from "../test-utils";
  14. // 类型定义(避免引入 OpenAPI 类型包)
  15. type OpenAPIDocument = {
  16. openapi: string;
  17. info: {
  18. title: string;
  19. version: string;
  20. description?: string;
  21. contact?: { name: string; url: string };
  22. license?: { name: string; url: string };
  23. };
  24. servers: Array<{ url: string; description: string }>;
  25. paths: Record<
  26. string,
  27. Record<
  28. string,
  29. {
  30. summary?: string;
  31. description?: string;
  32. tags?: string[];
  33. parameters?: unknown[];
  34. requestBody?: unknown;
  35. responses?: Record<string, unknown>;
  36. security?: unknown[];
  37. }
  38. >
  39. >;
  40. tags?: Array<{ name: string; description?: string }>;
  41. components?: {
  42. securitySchemes?: Record<string, unknown>;
  43. schemas?: Record<string, unknown>;
  44. };
  45. };
  46. describe("OpenAPI 规范验证", () => {
  47. let openApiDoc: OpenAPIDocument;
  48. beforeAll(async () => {
  49. const { response, json } = await callActionsRoute({
  50. method: "GET",
  51. pathname: "/api/actions/openapi.json",
  52. });
  53. expect(response.ok).toBe(true);
  54. expect(response.headers.get("content-type")).toContain("application/json");
  55. openApiDoc = json as OpenAPIDocument;
  56. expect(openApiDoc).toBeDefined();
  57. });
  58. test("应该符合 OpenAPI 3.1.0 规范", () => {
  59. expect(openApiDoc.openapi).toBe("3.1.0");
  60. expect(openApiDoc.info).toBeDefined();
  61. expect(openApiDoc.info.title).toBe("Claude Code Hub API");
  62. expect(openApiDoc.info.version).toBeDefined();
  63. });
  64. test("应该包含项目元信息", () => {
  65. expect(openApiDoc.info.contact).toBeDefined();
  66. expect(openApiDoc.info.contact?.name).toBe("项目维护团队");
  67. expect(openApiDoc.info.contact?.url).toContain("github.com");
  68. expect(openApiDoc.info.license).toBeDefined();
  69. expect(openApiDoc.info.license?.name).toBe("MIT License");
  70. });
  71. test("应该定义 servers 配置", () => {
  72. expect(openApiDoc.servers).toBeDefined();
  73. expect(openApiDoc.servers.length).toBeGreaterThan(0);
  74. for (const server of openApiDoc.servers) {
  75. expect(server.url).toBeDefined();
  76. expect(server.description).toBeDefined();
  77. }
  78. });
  79. test("应该定义所有标签分组", () => {
  80. const expectedTags = [
  81. "用户管理",
  82. "密钥管理",
  83. "供应商管理",
  84. "模型价格",
  85. "统计分析",
  86. "使用日志",
  87. "概览",
  88. "敏感词管理",
  89. "Session 管理",
  90. "通知管理",
  91. ];
  92. expect(openApiDoc.tags).toBeDefined();
  93. expect(openApiDoc.tags!.length).toBe(expectedTags.length);
  94. for (const tagName of expectedTags) {
  95. const tag = openApiDoc.tags!.find((t) => t.name === tagName);
  96. expect(tag).toBeDefined();
  97. expect(tag!.description).toBeDefined();
  98. expect(tag!.description!.length).toBeGreaterThan(10); // 确保描述足够详细
  99. }
  100. });
  101. test("应该定义 Cookie 认证方案", () => {
  102. expect(openApiDoc.components?.securitySchemes).toBeDefined();
  103. expect(openApiDoc.components!.securitySchemes!.cookieAuth).toBeDefined();
  104. });
  105. test("所有端点都应该有 summary 字段", () => {
  106. const paths = openApiDoc.paths;
  107. const pathsWithoutSummary: string[] = [];
  108. for (const [path, methods] of Object.entries(paths)) {
  109. for (const [method, operation] of Object.entries(methods)) {
  110. if (!operation.summary) {
  111. pathsWithoutSummary.push(`${method.toUpperCase()} ${path}`);
  112. }
  113. }
  114. }
  115. expect(pathsWithoutSummary).toEqual([]);
  116. });
  117. test("所有端点都应该有 description 字段", () => {
  118. const paths = openApiDoc.paths;
  119. const pathsWithoutDescription: string[] = [];
  120. for (const [path, methods] of Object.entries(paths)) {
  121. for (const [method, operation] of Object.entries(methods)) {
  122. if (!operation.description) {
  123. pathsWithoutDescription.push(`${method.toUpperCase()} ${path}`);
  124. }
  125. }
  126. }
  127. expect(pathsWithoutDescription).toEqual([]);
  128. });
  129. test("所有端点都应该有 tags 分组", () => {
  130. const paths = openApiDoc.paths;
  131. const pathsWithoutTags: string[] = [];
  132. for (const [path, methods] of Object.entries(paths)) {
  133. for (const [method, operation] of Object.entries(methods)) {
  134. if (!operation.tags || operation.tags.length === 0) {
  135. pathsWithoutTags.push(`${method.toUpperCase()} ${path}`);
  136. }
  137. }
  138. }
  139. expect(pathsWithoutTags).toEqual([]);
  140. });
  141. test("所有 POST 端点都应该定义响应 schema", () => {
  142. const paths = openApiDoc.paths;
  143. const pathsWithoutResponses: string[] = [];
  144. for (const [path, methods] of Object.entries(paths)) {
  145. const postOperation = methods.post;
  146. if (postOperation && !postOperation.responses) {
  147. pathsWithoutResponses.push(`POST ${path}`);
  148. }
  149. }
  150. expect(pathsWithoutResponses).toEqual([]);
  151. });
  152. test("应该包含标准错误响应定义", () => {
  153. const paths = openApiDoc.paths;
  154. const firstPath = Object.values(paths)[0];
  155. const firstOperation = Object.values(firstPath)[0];
  156. expect(firstOperation.responses).toBeDefined();
  157. expect(firstOperation.responses!["200"]).toBeDefined();
  158. expect(firstOperation.responses!["400"]).toBeDefined();
  159. expect(firstOperation.responses!["401"]).toBeDefined();
  160. expect(firstOperation.responses!["500"]).toBeDefined();
  161. });
  162. test("端点数量应该符合预期", () => {
  163. const totalPaths = Object.keys(openApiDoc.paths).length;
  164. // 端点数量会随着功能模块增长而变化:这里只做“合理范围”约束
  165. expect(totalPaths).toBeGreaterThanOrEqual(40);
  166. expect(totalPaths).toBeLessThanOrEqual(80);
  167. });
  168. test("summary 和 description 应该不同", () => {
  169. const paths = openApiDoc.paths;
  170. const violations: string[] = [];
  171. const totalPaths = Object.keys(paths).length;
  172. for (const [path, methods] of Object.entries(paths)) {
  173. for (const [method, operation] of Object.entries(methods)) {
  174. if (operation.summary && operation.description) {
  175. // summary 应该是简短版本,description 可以包含更多信息
  176. // 如果完全相同,可能需要优化
  177. if (operation.summary === operation.description) {
  178. violations.push(`${method.toUpperCase()} ${path}`);
  179. }
  180. }
  181. }
  182. }
  183. // 允许部分端点 summary 和 description 相同(简单操作)
  184. // 但不应该太多(允许 35% 以内)
  185. expect(violations.length).toBeLessThan(totalPaths * 0.35);
  186. });
  187. });