workflowToolSummary.test.ts 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. fireEvent.click(trigger);
  30. fireEvent.keyDown(trigger, { key: "Enter" });
  31. fireEvent.keyDown(trigger, { key: " " });
  32. expect(onOpen).toHaveBeenCalledTimes(3);
  33. expect(view.container.querySelector("[aria-expanded]")).toBeNull();
  34. expect(view.queryByTestId("workflow-timeline-station")).toBeNull();
  35. });
  36. it.each([
  37. ["en-US", "Workflow amended"],
  38. ["zh-CN", "工作流已调整"],
  39. ] as const)("修订行的种类词是「工作流已调整」:%s", (locale, word) => {
  40. const view = render(
  41. createElement(
  42. ZCodeIntlProvider,
  43. { initialLocale: locale },
  44. createElement(WorkflowToolSummary, { toolCallId: "tool-1", summary, amend: true }),
  45. ),
  46. );
  47. expect(view.container.textContent).toContain(word);
  48. expect(view.container.textContent).toContain(locale === "en-US" ? "3 agents" : "3 个子代理");
  49. });
  50. it("宿主缺少打开能力时只展示文字", () => {
  51. const view = render(
  52. createElement(
  53. ZCodeIntlProvider,
  54. { initialLocale: "en-US" },
  55. createElement(WorkflowToolSummary, { toolCallId: "tool-1", summary }),
  56. ),
  57. );
  58. expect(view.queryByRole("button")).toBeNull();
  59. expect(view.container.textContent).toContain("3 agents");
  60. });
  61. it("摘要没带子代理数:不写计数,也不写步数", () => {
  62. const { agents: _dropped, ...bare } = summary;
  63. const view = render(
  64. createElement(
  65. ZCodeIntlProvider,
  66. { initialLocale: "en-US" },
  67. createElement(WorkflowToolSummary, { toolCallId: "tool-1", summary: bare }),
  68. ),
  69. );
  70. expect(view.getByTestId("workflow-summary-agents").textContent).toBe("");
  71. expect(view.container.textContent).not.toContain("step");
  72. });
  73. });