workflowToolSummary.test.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // @vitest-environment jsdom
  2. import { createElement } from "react";
  3. import { cleanup, fireEvent, render } from "@testing-library/react";
  4. import { afterEach, describe, expect, it, vi } from "vitest";
  5. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  6. import { WorkflowToolSummary } from "@/v4/WorkflowToolSummary.js";
  7. const summary = {
  8. runId: "run-1",
  9. toolCallId: "tool-1",
  10. status: "running" as const,
  11. nodesTotal: 3,
  12. nodesSettled: 1,
  13. agents: 3,
  14. };
  15. afterEach(cleanup);
  16. describe("WorkflowToolSummary", () => {
  17. it.each(["en-US", "zh-CN"] as const)("本地化子代理数并通过普通摘要打开详情:%s", (locale) => {
  18. const onOpen = vi.fn();
  19. const view = render(
  20. createElement(
  21. ZCodeIntlProvider,
  22. { initialLocale: locale },
  23. createElement(WorkflowToolSummary, { toolCallId: "tool-1", summary, onOpen }),
  24. ),
  25. );
  26. expect(view.container.textContent).toContain(locale === "en-US" ? "3 agents" : "3 个子代理");
  27. expect(view.container.textContent).not.toContain("step");
  28. const trigger = view.getByRole("button");
  29. expect(trigger.className).toContain("group/tool-summary");
  30. fireEvent.click(trigger);
  31. fireEvent.keyDown(trigger, { key: "Enter" });
  32. fireEvent.keyDown(trigger, { key: " " });
  33. expect(onOpen).toHaveBeenCalledTimes(3);
  34. expect(view.container.querySelector("[aria-expanded]")).toBeNull();
  35. expect(view.queryByTestId("workflow-timeline-station")).toBeNull();
  36. });
  37. it.each([
  38. ["en-US", "Workflow amended"],
  39. ["zh-CN", "工作流已调整"],
  40. ] as const)("修订行的种类词是「工作流已调整」:%s", (locale, word) => {
  41. const view = render(
  42. createElement(
  43. ZCodeIntlProvider,
  44. { initialLocale: locale },
  45. createElement(WorkflowToolSummary, { toolCallId: "tool-1", summary, amend: true }),
  46. ),
  47. );
  48. expect(view.container.textContent).toContain(word);
  49. expect(view.container.textContent).toContain(locale === "en-US" ? "3 agents" : "3 个子代理");
  50. });
  51. it("宿主缺少打开能力时只展示文字", () => {
  52. const view = render(
  53. createElement(
  54. ZCodeIntlProvider,
  55. { initialLocale: "en-US" },
  56. createElement(WorkflowToolSummary, { toolCallId: "tool-1", summary }),
  57. ),
  58. );
  59. expect(view.queryByRole("button")).toBeNull();
  60. expect(view.container.textContent).toContain("3 agents");
  61. });
  62. it("摘要没带子代理数:不写计数,也不写步数", () => {
  63. const { agents: _dropped, ...bare } = summary;
  64. const view = render(
  65. createElement(
  66. ZCodeIntlProvider,
  67. { initialLocale: "en-US" },
  68. createElement(WorkflowToolSummary, { toolCallId: "tool-1", summary: bare }),
  69. ),
  70. );
  71. expect(view.getByTestId("workflow-summary-agents").textContent).toBe("");
  72. expect(view.container.textContent).not.toContain("step");
  73. });
  74. });