documentApi.test.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import { describe, expect, it, vi } from "vitest";
  2. import { createDocumentApi } from "../documentApi";
  3. import { detail, result, summary } from "./fixtures";
  4. const page = { items: [summary], page: 1, pageSize: 20, total: 1, totalPages: 1 };
  5. describe("documentApi", () => {
  6. it("GET /documents 透传筛选、排序、分页和 AbortSignal", async () => {
  7. const client = { get: vi.fn().mockResolvedValue(result(page)) };
  8. const api = createDocumentApi(client);
  9. const signal = new AbortController().signal;
  10. const query = { documentType: "MAIN", categoryId: "2", includeDescendants: true, keyword: "应急", updatedFrom: "2026-08-04T01:00:00.000Z", updatedTo: "2026-08-04T10:00:00.000Z", sortBy: "updatedAt" as const, sortDirection: "desc" as const, page: 2, pageSize: 20 };
  11. await api.list(query, signal);
  12. expect(client.get).toHaveBeenCalledWith("documents", { query, signal });
  13. });
  14. it("GET /documents/{id} 严格解析详情", async () => {
  15. const client = { get: vi.fn().mockResolvedValue(result(detail)) };
  16. const api = createDocumentApi(client);
  17. await expect(api.detail("1")).resolves.toEqual(detail);
  18. expect(client.get).toHaveBeenCalledWith("documents/1", { signal: undefined });
  19. });
  20. it("GET /main-plans/{id}/sub-plans 使用直属子方案路径", async () => {
  21. const client = { get: vi.fn().mockResolvedValue(result(page)) };
  22. const api = createDocumentApi(client);
  23. await api.subPlans("1", { page: 1, pageSize: 20 });
  24. expect(client.get).toHaveBeenCalledWith("main-plans/1/sub-plans", {
  25. query: { page: 1, pageSize: 20 },
  26. signal: undefined,
  27. });
  28. });
  29. it("直属子方案透传状态、时间、排序和分页组合", async () => {
  30. const client = { get: vi.fn().mockResolvedValue(result(page)) };
  31. const query = {
  32. keyword: "通信",
  33. status: "PUBLISHED" as const,
  34. updatedFrom: "2026-08-01T00:00:00.000Z",
  35. updatedTo: "2026-08-04T00:00:00.000Z",
  36. page: 2,
  37. pageSize: 20,
  38. sortBy: "updatedAt" as const,
  39. sortDirection: "desc" as const,
  40. };
  41. await createDocumentApi(client).subPlans("1", query);
  42. expect(client.get).toHaveBeenCalledWith("main-plans/1/sub-plans", {
  43. query,
  44. signal: undefined,
  45. });
  46. });
  47. it("拒绝 number ID 和非 UTC 时间", async () => {
  48. const client = { get: vi.fn().mockResolvedValue(result({ ...page, items: [{ ...summary, id: 1, updatedAt: "2026-07-23 10:00:00" }] })) };
  49. await expect(createDocumentApi(client).list({})).rejects.toMatchObject({
  50. code: "INTERNAL_ERROR",
  51. requestId: "request-id",
  52. });
  53. });
  54. });