clientScenesService.test.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import { describe, expect, it, vi } from "vitest";
  2. import type { ApiClient } from "@zcode/shared";
  3. import { ZCODE_CLIENT_SCENES_URL } from "../src/providers/api/apiEndpoints.js";
  4. import { createClientScenesService } from "../src/client-scenes/clientScenesService.js";
  5. describe("ClientScenesService", () => {
  6. it("通过统一 ApiClient 读取 client scenes envelope", async () => {
  7. const request = vi.fn(
  8. async () =>
  9. new Response(
  10. JSON.stringify({
  11. code: 0,
  12. msg: "",
  13. data: [
  14. {
  15. namespace: "zcode",
  16. scene: "ai-slides",
  17. options: {
  18. template: {
  19. id: "template",
  20. type: "template",
  21. contents: { en: "Template", cn: "模板" },
  22. prompts: { en: "Create slides", cn: "制作幻灯片" },
  23. items: [
  24. {
  25. id: "business",
  26. type: "template",
  27. contents: { en: "Business", cn: "商务" },
  28. descs: { en: "Business deck", cn: "商务演示" },
  29. labels: { en: "Recommended", cn: "推荐" },
  30. on_finish: "open_presentation",
  31. img: null,
  32. imgs: { cn: "https://example.com/cn.png" },
  33. share_urls: {},
  34. },
  35. ],
  36. refer: "",
  37. cascades: {},
  38. },
  39. },
  40. created_at: 1,
  41. updated_at: 2,
  42. },
  43. ],
  44. }),
  45. { status: 200 },
  46. ),
  47. );
  48. const service = createClientScenesService({
  49. apiClient: { request } as ApiClient,
  50. });
  51. await expect(service.list()).resolves.toEqual({
  52. code: 0,
  53. msg: "",
  54. data: [
  55. expect.objectContaining({
  56. namespace: "zcode",
  57. scene: "ai-slides",
  58. created_at: 1,
  59. updated_at: 2,
  60. }),
  61. ],
  62. });
  63. expect(request).toHaveBeenCalledOnce();
  64. expect(request).toHaveBeenCalledWith(ZCODE_CLIENT_SCENES_URL, {
  65. method: "GET",
  66. });
  67. });
  68. });