elicitationDialogFocus.test.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // @vitest-environment jsdom
  2. import { act, cleanup, fireEvent, render, waitFor } from "@testing-library/react";
  3. import { createElement } from "react";
  4. import { afterEach, describe, expect, it, vi } from "vitest";
  5. import type { ZCodeElicitationRequest } from "@zcode/shared";
  6. import { ElicitationDialog } from "@/ElicitationDialog.js";
  7. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  8. afterEach(() => {
  9. cleanup();
  10. vi.unstubAllGlobals();
  11. });
  12. describe("ElicitationDialog custom answer focus", () => {
  13. it.each(["single", "multi", "plan"])(
  14. "%s: 上下键可进入及离开自定义输入框,保留草稿且不隐式提交",
  15. async (kind) => {
  16. vi.stubGlobal("requestAnimationFrame", (callback: FrameRequestCallback) =>
  17. window.setTimeout(() => callback(performance.now()), 0),
  18. );
  19. vi.stubGlobal("cancelAnimationFrame", (id: number) => window.clearTimeout(id));
  20. const request: ZCodeElicitationRequest = {
  21. type: "elicitation_request",
  22. taskId: "task-focus",
  23. traceId: "trace-focus",
  24. requestId: "ask-focus",
  25. message: "请选择方案",
  26. options: [],
  27. questions: [
  28. {
  29. question: "请选择方案",
  30. multiSelect: kind === "multi",
  31. options: [
  32. { value: "approve", label: "方案一" },
  33. { value: "second", label: "方案二" },
  34. ],
  35. },
  36. ],
  37. ...(kind === "plan"
  38. ? { schema: { interaction: "plan_approval", toolName: "ExitPlanMode" } }
  39. : {}),
  40. };
  41. const onRespond = vi.fn();
  42. const { container } = render(
  43. createElement(
  44. ZCodeIntlProvider,
  45. { initialLocale: "zh-CN" },
  46. createElement(ElicitationDialog, { request, onRespond }),
  47. ),
  48. );
  49. const options = container.querySelectorAll<HTMLButtonElement>(
  50. 'button[role="option"],button[role="checkbox"]',
  51. );
  52. const input = container.querySelector<HTMLTextAreaElement>("textarea")!;
  53. const card = container.querySelector<HTMLElement>('[data-elicitation-dialog-card="true"]')!;
  54. await waitFor(() => expect(document.activeElement).toBe(kind === "plan" ? options[0] : card));
  55. fireEvent.keyDown(document.activeElement!, { key: "ArrowUp" });
  56. await waitFor(() => expect(document.activeElement).toBe(input));
  57. fireEvent.change(input, { target: { value: "保留我的自定义答案\n第二行" } });
  58. // 输入法上下选词不能误切到选项;同时覆盖 native 和 composition ref 两种保护。
  59. fireEvent.keyDown(input, { key: "ArrowUp", isComposing: true });
  60. await act(async () => {
  61. await new Promise((resolve) => window.setTimeout(resolve, 0));
  62. });
  63. expect(document.activeElement).toBe(input);
  64. fireEvent.compositionStart(input);
  65. fireEvent.keyDown(input, { key: "ArrowDown" });
  66. await act(async () => {
  67. await new Promise((resolve) => window.setTimeout(resolve, 0));
  68. });
  69. expect(document.activeElement).toBe(input);
  70. fireEvent.compositionEnd(input);
  71. fireEvent.keyDown(input, { key: "ArrowUp" });
  72. await waitFor(() => expect(document.activeElement).toBe(options[1]));
  73. fireEvent.keyDown(options[1]!, { key: "ArrowDown" });
  74. await waitFor(() => expect(document.activeElement).toBe(input));
  75. fireEvent.keyDown(input, { key: "ArrowDown" });
  76. await waitFor(() => expect(document.activeElement).toBe(options[0]));
  77. fireEvent.keyDown(options[0]!, { key: "ArrowUp" });
  78. await waitFor(() => expect(document.activeElement).toBe(input));
  79. expect(input.value).toBe("保留我的自定义答案\n第二行");
  80. expect(onRespond).not.toHaveBeenCalled();
  81. expect(options[0]!.getAttribute(kind === "multi" ? "aria-checked" : "aria-selected")).toBe(
  82. "false",
  83. );
  84. if (kind !== "multi") {
  85. expect(input.previousElementSibling?.textContent).toBe("3.");
  86. expect(input.previousElementSibling?.classList.contains("self-start")).toBe(true);
  87. expect(input.previousElementSibling?.classList.contains("mt-px")).toBe(true);
  88. expect(input.previousElementSibling?.classList.contains("leading-normal")).toBe(true);
  89. expect(input.previousElementSibling?.classList.contains("md:leading-relaxed")).toBe(true);
  90. }
  91. },
  92. );
  93. });