v4ConversationHookDetailsAction.test.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. // @vitest-environment jsdom
  2. import { createElement, type ReactNode } from "react";
  3. import { cleanup, fireEvent, render, screen, waitFor } from "@testing-library/react";
  4. import { afterEach, describe, expect, it, vi } from "vitest";
  5. import type { HookInvocationRow } from "@zcode/shared/zcode-protocol-v4";
  6. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  7. import { ConversationHookDetailsAction } from "@/v4/ConversationHookDetailsAction.js";
  8. const tooltipCapture = vi.hoisted(() => ({ opens: [] as Array<boolean | undefined> }));
  9. vi.mock("@/components/ui/tooltip.js", () => ({
  10. Tooltip: ({ children, open }: { children: ReactNode; open?: boolean }) => {
  11. tooltipCapture.opens.push(open);
  12. return createElement("div", { "data-tooltip-open": String(open) }, children);
  13. },
  14. TooltipTrigger: ({ children }: { children: ReactNode }) => children,
  15. TooltipContent: ({ children }: { children: ReactNode }) =>
  16. createElement("span", { "data-testid": "hook-tooltip" }, children),
  17. }));
  18. function hookRow(
  19. rowId: number,
  20. options: {
  21. didExecute?: boolean;
  22. displayName: string;
  23. durationMs?: number;
  24. event?: HookInvocationRow["hookEventName"];
  25. outcome?: HookInvocationRow["executions"][number]["outcome"];
  26. blockReason?: string;
  27. pluginName?: string;
  28. sourceKind: HookInvocationRow["executions"][number]["sourceKind"];
  29. toolName?: string;
  30. },
  31. ): HookInvocationRow {
  32. const didExecute = options.didExecute ?? true;
  33. const durationMs = options.durationMs ?? 25;
  34. const outcome = options.outcome ?? "success";
  35. return {
  36. rowId,
  37. turnId: "turn-1",
  38. entityId: `hook-invocation-${rowId}`,
  39. productTurnId: "turn-1",
  40. visibility: "visible",
  41. createdAt: 1_700_000_000_000 + rowId,
  42. createdAtSeq: rowId,
  43. kind: "hookInvocation",
  44. hookInvocationId: `hook-invocation-${rowId}`,
  45. hookEventName: options.event ?? "UserPromptSubmit",
  46. hookCount: 1,
  47. state: outcome === "failed" || outcome === "timed_out" ? "failed" : "completed",
  48. startedAt: 1_700_000_000_000 + rowId,
  49. endedAt: 1_700_000_000_000 + rowId + durationMs,
  50. durationMs,
  51. lane: options.toolName ? "toolBefore" : "assistantWork",
  52. ...(options.toolName ? { anchorToolCallId: `tool-${rowId}` } : {}),
  53. executions: [
  54. {
  55. hookRunId: `hook-run-${rowId}`,
  56. hookIndex: 0,
  57. didExecute,
  58. state: outcome === "failed" || outcome === "timed_out" ? "failed" : "completed",
  59. outcome,
  60. startedAt: 1_700_000_000_000 + rowId,
  61. endedAt: 1_700_000_000_000 + rowId + durationMs,
  62. durationMs,
  63. displayName: options.displayName,
  64. sourceKind: options.sourceKind,
  65. ...(options.blockReason ? { blockReason: options.blockReason } : {}),
  66. ...(options.pluginName ? { pluginName: options.pluginName } : {}),
  67. ...(options.toolName ? { toolName: options.toolName } : {}),
  68. },
  69. ],
  70. };
  71. }
  72. function renderAction(rows: readonly HookInvocationRow[], locale: "zh-CN" | "en-US" = "zh-CN") {
  73. return render(actionElement(rows, locale));
  74. }
  75. function actionElement(rows: readonly HookInvocationRow[], locale: "zh-CN" | "en-US" = "zh-CN") {
  76. return createElement(
  77. ZCodeIntlProvider,
  78. { initialLocale: locale },
  79. createElement(ConversationHookDetailsAction, {
  80. rows,
  81. turnId: "turn-1",
  82. }),
  83. );
  84. }
  85. afterEach(() => {
  86. cleanup();
  87. tooltipCapture.opens.length = 0;
  88. });
  89. describe("ConversationHookDetailsAction", () => {
  90. it("shows executed Hooks in order without exposing implementation details or aggregating repeats", async () => {
  91. renderAction([
  92. hookRow(1, { displayName: "prepare-context", sourceKind: "user" }),
  93. hookRow(4, { displayName: "prepare-context", sourceKind: "user" }),
  94. hookRow(2, {
  95. displayName: "validate-write",
  96. event: "PreToolUse",
  97. outcome: "failed",
  98. sourceKind: "project",
  99. toolName: "Write",
  100. }),
  101. hookRow(5, {
  102. displayName: "validate-write",
  103. event: "PreToolUse",
  104. outcome: "failed",
  105. sourceKind: "project",
  106. toolName: "Read",
  107. }),
  108. hookRow(3, {
  109. displayName: "plugin-check",
  110. event: "PostToolUse",
  111. pluginName: "Quality Plugin",
  112. sourceKind: "plugin",
  113. toolName: "Write",
  114. }),
  115. ]);
  116. expect(screen.getByTestId("hook-tooltip").textContent).toBe("钩子");
  117. expect(screen.queryByTestId("v4-hook-details-content-turn-1")).toBeNull();
  118. fireEvent.click(screen.getByTestId("v4-hook-details-trigger-turn-1"));
  119. const content = await screen.findByTestId("v4-hook-details-content-turn-1");
  120. expect(content.className).toContain("max-w-[calc(100vw-2rem)]");
  121. expect(content.style.maxHeight).toBe(
  122. "min(20rem, var(--radix-popover-content-available-height))",
  123. );
  124. expect(content.textContent).toContain("钩子");
  125. expect(content.textContent).not.toContain("本轮钩子");
  126. expect(content.textContent).not.toContain("共 ");
  127. expect([...content.querySelectorAll("li")].map((item) => item.textContent)).toEqual([
  128. "UserPromptSubmit用户25ms",
  129. "UserPromptSubmit用户25ms",
  130. "PreToolUse工作区25ms失败",
  131. "PreToolUse工作区25ms失败",
  132. "PostToolUse插件25ms",
  133. ]);
  134. expect(content.textContent).not.toContain("prepare-context");
  135. expect(content.textContent).not.toContain("validate-write");
  136. expect(content.textContent).not.toContain("Quality Plugin");
  137. expect(content.textContent).not.toContain("Write");
  138. expect(content.textContent).not.toContain("Read");
  139. expect(content.textContent).not.toContain("×");
  140. expect(content.textContent).not.toContain("项目");
  141. expect(content.textContent).not.toContain("已完成");
  142. expect(content.textContent).toContain("25ms");
  143. await waitFor(() => expect(tooltipCapture.opens.at(-1)).toBe(false));
  144. });
  145. it("does not render the action when no client-visible Hook actually executed", () => {
  146. const { container } = renderAction([
  147. hookRow(1, {
  148. didExecute: false,
  149. displayName: "blocked-project-hook",
  150. outcome: "blocked",
  151. sourceKind: "project",
  152. }),
  153. ]);
  154. expect(container.innerHTML).toBe("");
  155. });
  156. it("keeps the block reason in Hook details while the chat error owns the primary message", async () => {
  157. renderAction([
  158. hookRow(1, {
  159. displayName: "prompt-block",
  160. event: "UserPromptSubmit",
  161. outcome: "blocked",
  162. blockReason: "HOOK_PROMPT_BLOCK_REASON",
  163. sourceKind: "user",
  164. }),
  165. ]);
  166. expect(screen.queryByText(":HOOK_PROMPT_BLOCK_REASON")).toBeNull();
  167. fireEvent.click(screen.getByTestId("v4-hook-details-trigger-turn-1"));
  168. const content = await screen.findByTestId("v4-hook-details-content-turn-1");
  169. const row = content.querySelector("li");
  170. expect(screen.getAllByText(":HOOK_PROMPT_BLOCK_REASON")).toHaveLength(1);
  171. expect(row?.textContent).toContain("已阻止");
  172. expect(row?.textContent).toContain("HOOK_PROMPT_BLOCK_REASON");
  173. expect(screen.getByTestId("hook-tooltip").textContent).toBe("钩子");
  174. });
  175. it("lists executions from one invocation by hookIndex without deduplicating them", async () => {
  176. const row = hookRow(1, {
  177. displayName: "late-plugin-hook",
  178. event: "PreToolUse",
  179. sourceKind: "plugin",
  180. });
  181. row.executions = [
  182. { ...row.executions[0]!, hookIndex: 2, hookRunId: "hook-run-plugin" },
  183. {
  184. ...row.executions[0]!,
  185. displayName: "first-user-hook",
  186. hookIndex: 0,
  187. hookRunId: "hook-run-user",
  188. sourceKind: "user",
  189. },
  190. {
  191. ...row.executions[0]!,
  192. displayName: "failed-workspace-hook",
  193. hookIndex: 1,
  194. hookRunId: "hook-run-workspace",
  195. outcome: "failed",
  196. sourceKind: "project",
  197. state: "failed",
  198. },
  199. ];
  200. renderAction([row]);
  201. fireEvent.click(screen.getByTestId("v4-hook-details-trigger-turn-1"));
  202. const content = await screen.findByTestId("v4-hook-details-content-turn-1");
  203. expect([...content.querySelectorAll("li")].map((item) => item.textContent)).toEqual([
  204. "PreToolUse用户25ms",
  205. "PreToolUse工作区25ms失败",
  206. "PreToolUse插件25ms",
  207. ]);
  208. });
  209. it("uses localized English source and state labels", async () => {
  210. renderAction(
  211. [
  212. hookRow(1, {
  213. displayName: "cancelled-check",
  214. durationMs: 1_250,
  215. outcome: "cancelled",
  216. sourceKind: "user",
  217. }),
  218. hookRow(2, {
  219. displayName: "workspace-check",
  220. event: "PreToolUse",
  221. sourceKind: "project",
  222. }),
  223. ],
  224. "en-US",
  225. );
  226. fireEvent.click(screen.getByTestId("v4-hook-details-trigger-turn-1"));
  227. const content = await screen.findByTestId("v4-hook-details-content-turn-1");
  228. expect(content.textContent).toContain("Hooks");
  229. expect(content.textContent).not.toContain("Hooks in this turn");
  230. expect(content.textContent).toContain("User");
  231. expect(content.textContent).toContain("Workspace");
  232. expect(content.textContent).toContain("1.25s");
  233. expect(content.textContent).toContain("Cancelled");
  234. });
  235. it("HK13 keeps the Popover open while an async Hook terminal updates in place", async () => {
  236. const running = hookRow(1, {
  237. displayName: "async-check",
  238. sourceKind: "user",
  239. });
  240. running.state = "running";
  241. const runningExecution = {
  242. ...running.executions[0]!,
  243. state: "running",
  244. };
  245. delete runningExecution.outcome;
  246. delete runningExecution.durationMs;
  247. delete runningExecution.endedAt;
  248. running.executions[0] = runningExecution;
  249. const view = renderAction([running]);
  250. fireEvent.click(screen.getByTestId("v4-hook-details-trigger-turn-1"));
  251. expect((await screen.findByTestId("v4-hook-details-content-turn-1")).textContent).toContain(
  252. "运行中",
  253. );
  254. expect(screen.getByTestId("v4-hook-details-content-turn-1").textContent).not.toContain("25ms");
  255. const completed = hookRow(1, {
  256. displayName: "async-check",
  257. sourceKind: "user",
  258. });
  259. view.rerender(actionElement([completed]));
  260. const content = await screen.findByTestId("v4-hook-details-content-turn-1");
  261. expect(content.textContent).not.toContain("运行中");
  262. expect(content.textContent).not.toContain("已完成");
  263. expect(content.textContent).toContain("25ms");
  264. expect(screen.getByTestId("v4-hook-details-trigger-turn-1").getAttribute("aria-expanded")).toBe(
  265. "true",
  266. );
  267. });
  268. });