| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293 |
- // @vitest-environment jsdom
- import { createElement, type ReactNode } from "react";
- import { cleanup, fireEvent, render, screen, waitFor } from "@testing-library/react";
- import { afterEach, describe, expect, it, vi } from "vitest";
- import type { HookInvocationRow } from "@zcode/shared/zcode-protocol-v4";
- import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
- import { ConversationHookDetailsAction } from "@/v4/ConversationHookDetailsAction.js";
- const tooltipCapture = vi.hoisted(() => ({ opens: [] as Array<boolean | undefined> }));
- vi.mock("@/components/ui/tooltip.js", () => ({
- Tooltip: ({ children, open }: { children: ReactNode; open?: boolean }) => {
- tooltipCapture.opens.push(open);
- return createElement("div", { "data-tooltip-open": String(open) }, children);
- },
- TooltipTrigger: ({ children }: { children: ReactNode }) => children,
- TooltipContent: ({ children }: { children: ReactNode }) =>
- createElement("span", { "data-testid": "hook-tooltip" }, children),
- }));
- function hookRow(
- rowId: number,
- options: {
- didExecute?: boolean;
- displayName: string;
- durationMs?: number;
- event?: HookInvocationRow["hookEventName"];
- outcome?: HookInvocationRow["executions"][number]["outcome"];
- blockReason?: string;
- pluginName?: string;
- sourceKind: HookInvocationRow["executions"][number]["sourceKind"];
- toolName?: string;
- },
- ): HookInvocationRow {
- const didExecute = options.didExecute ?? true;
- const durationMs = options.durationMs ?? 25;
- const outcome = options.outcome ?? "success";
- return {
- rowId,
- turnId: "turn-1",
- entityId: `hook-invocation-${rowId}`,
- productTurnId: "turn-1",
- visibility: "visible",
- createdAt: 1_700_000_000_000 + rowId,
- createdAtSeq: rowId,
- kind: "hookInvocation",
- hookInvocationId: `hook-invocation-${rowId}`,
- hookEventName: options.event ?? "UserPromptSubmit",
- hookCount: 1,
- state: outcome === "failed" || outcome === "timed_out" ? "failed" : "completed",
- startedAt: 1_700_000_000_000 + rowId,
- endedAt: 1_700_000_000_000 + rowId + durationMs,
- durationMs,
- lane: options.toolName ? "toolBefore" : "assistantWork",
- ...(options.toolName ? { anchorToolCallId: `tool-${rowId}` } : {}),
- executions: [
- {
- hookRunId: `hook-run-${rowId}`,
- hookIndex: 0,
- didExecute,
- state: outcome === "failed" || outcome === "timed_out" ? "failed" : "completed",
- outcome,
- startedAt: 1_700_000_000_000 + rowId,
- endedAt: 1_700_000_000_000 + rowId + durationMs,
- durationMs,
- displayName: options.displayName,
- sourceKind: options.sourceKind,
- ...(options.blockReason ? { blockReason: options.blockReason } : {}),
- ...(options.pluginName ? { pluginName: options.pluginName } : {}),
- ...(options.toolName ? { toolName: options.toolName } : {}),
- },
- ],
- };
- }
- function renderAction(rows: readonly HookInvocationRow[], locale: "zh-CN" | "en-US" = "zh-CN") {
- return render(actionElement(rows, locale));
- }
- function actionElement(rows: readonly HookInvocationRow[], locale: "zh-CN" | "en-US" = "zh-CN") {
- return createElement(
- ZCodeIntlProvider,
- { initialLocale: locale },
- createElement(ConversationHookDetailsAction, {
- rows,
- turnId: "turn-1",
- }),
- );
- }
- afterEach(() => {
- cleanup();
- tooltipCapture.opens.length = 0;
- });
- describe("ConversationHookDetailsAction", () => {
- it("shows executed Hooks in order without exposing implementation details or aggregating repeats", async () => {
- renderAction([
- hookRow(1, { displayName: "prepare-context", sourceKind: "user" }),
- hookRow(4, { displayName: "prepare-context", sourceKind: "user" }),
- hookRow(2, {
- displayName: "validate-write",
- event: "PreToolUse",
- outcome: "failed",
- sourceKind: "project",
- toolName: "Write",
- }),
- hookRow(5, {
- displayName: "validate-write",
- event: "PreToolUse",
- outcome: "failed",
- sourceKind: "project",
- toolName: "Read",
- }),
- hookRow(3, {
- displayName: "plugin-check",
- event: "PostToolUse",
- pluginName: "Quality Plugin",
- sourceKind: "plugin",
- toolName: "Write",
- }),
- ]);
- expect(screen.getByTestId("hook-tooltip").textContent).toBe("钩子");
- expect(screen.queryByTestId("v4-hook-details-content-turn-1")).toBeNull();
- fireEvent.click(screen.getByTestId("v4-hook-details-trigger-turn-1"));
- const content = await screen.findByTestId("v4-hook-details-content-turn-1");
- expect(content.className).toContain("max-w-[calc(100vw-2rem)]");
- expect(content.style.maxHeight).toBe(
- "min(20rem, var(--radix-popover-content-available-height))",
- );
- expect(content.textContent).toContain("钩子");
- expect(content.textContent).not.toContain("本轮钩子");
- expect(content.textContent).not.toContain("共 ");
- expect([...content.querySelectorAll("li")].map((item) => item.textContent)).toEqual([
- "UserPromptSubmit用户25ms",
- "UserPromptSubmit用户25ms",
- "PreToolUse工作区25ms失败",
- "PreToolUse工作区25ms失败",
- "PostToolUse插件25ms",
- ]);
- expect(content.textContent).not.toContain("prepare-context");
- expect(content.textContent).not.toContain("validate-write");
- expect(content.textContent).not.toContain("Quality Plugin");
- expect(content.textContent).not.toContain("Write");
- expect(content.textContent).not.toContain("Read");
- expect(content.textContent).not.toContain("×");
- expect(content.textContent).not.toContain("项目");
- expect(content.textContent).not.toContain("已完成");
- expect(content.textContent).toContain("25ms");
- await waitFor(() => expect(tooltipCapture.opens.at(-1)).toBe(false));
- });
- it("does not render the action when no client-visible Hook actually executed", () => {
- const { container } = renderAction([
- hookRow(1, {
- didExecute: false,
- displayName: "blocked-project-hook",
- outcome: "blocked",
- sourceKind: "project",
- }),
- ]);
- expect(container.innerHTML).toBe("");
- });
- it("keeps the block reason in Hook details while the chat error owns the primary message", async () => {
- renderAction([
- hookRow(1, {
- displayName: "prompt-block",
- event: "UserPromptSubmit",
- outcome: "blocked",
- blockReason: "HOOK_PROMPT_BLOCK_REASON",
- sourceKind: "user",
- }),
- ]);
- expect(screen.queryByText(":HOOK_PROMPT_BLOCK_REASON")).toBeNull();
- fireEvent.click(screen.getByTestId("v4-hook-details-trigger-turn-1"));
- const content = await screen.findByTestId("v4-hook-details-content-turn-1");
- const row = content.querySelector("li");
- expect(screen.getAllByText(":HOOK_PROMPT_BLOCK_REASON")).toHaveLength(1);
- expect(row?.textContent).toContain("已阻止");
- expect(row?.textContent).toContain("HOOK_PROMPT_BLOCK_REASON");
- expect(screen.getByTestId("hook-tooltip").textContent).toBe("钩子");
- });
- it("lists executions from one invocation by hookIndex without deduplicating them", async () => {
- const row = hookRow(1, {
- displayName: "late-plugin-hook",
- event: "PreToolUse",
- sourceKind: "plugin",
- });
- row.executions = [
- { ...row.executions[0]!, hookIndex: 2, hookRunId: "hook-run-plugin" },
- {
- ...row.executions[0]!,
- displayName: "first-user-hook",
- hookIndex: 0,
- hookRunId: "hook-run-user",
- sourceKind: "user",
- },
- {
- ...row.executions[0]!,
- displayName: "failed-workspace-hook",
- hookIndex: 1,
- hookRunId: "hook-run-workspace",
- outcome: "failed",
- sourceKind: "project",
- state: "failed",
- },
- ];
- renderAction([row]);
- fireEvent.click(screen.getByTestId("v4-hook-details-trigger-turn-1"));
- const content = await screen.findByTestId("v4-hook-details-content-turn-1");
- expect([...content.querySelectorAll("li")].map((item) => item.textContent)).toEqual([
- "PreToolUse用户25ms",
- "PreToolUse工作区25ms失败",
- "PreToolUse插件25ms",
- ]);
- });
- it("uses localized English source and state labels", async () => {
- renderAction(
- [
- hookRow(1, {
- displayName: "cancelled-check",
- durationMs: 1_250,
- outcome: "cancelled",
- sourceKind: "user",
- }),
- hookRow(2, {
- displayName: "workspace-check",
- event: "PreToolUse",
- sourceKind: "project",
- }),
- ],
- "en-US",
- );
- fireEvent.click(screen.getByTestId("v4-hook-details-trigger-turn-1"));
- const content = await screen.findByTestId("v4-hook-details-content-turn-1");
- expect(content.textContent).toContain("Hooks");
- expect(content.textContent).not.toContain("Hooks in this turn");
- expect(content.textContent).toContain("User");
- expect(content.textContent).toContain("Workspace");
- expect(content.textContent).toContain("1.25s");
- expect(content.textContent).toContain("Cancelled");
- });
- it("HK13 keeps the Popover open while an async Hook terminal updates in place", async () => {
- const running = hookRow(1, {
- displayName: "async-check",
- sourceKind: "user",
- });
- running.state = "running";
- const runningExecution = {
- ...running.executions[0]!,
- state: "running",
- };
- delete runningExecution.outcome;
- delete runningExecution.durationMs;
- delete runningExecution.endedAt;
- running.executions[0] = runningExecution;
- const view = renderAction([running]);
- fireEvent.click(screen.getByTestId("v4-hook-details-trigger-turn-1"));
- expect((await screen.findByTestId("v4-hook-details-content-turn-1")).textContent).toContain(
- "运行中",
- );
- expect(screen.getByTestId("v4-hook-details-content-turn-1").textContent).not.toContain("25ms");
- const completed = hookRow(1, {
- displayName: "async-check",
- sourceKind: "user",
- });
- view.rerender(actionElement([completed]));
- const content = await screen.findByTestId("v4-hook-details-content-turn-1");
- expect(content.textContent).not.toContain("运行中");
- expect(content.textContent).not.toContain("已完成");
- expect(content.textContent).toContain("25ms");
- expect(screen.getByTestId("v4-hook-details-trigger-turn-1").getAttribute("aria-expanded")).toBe(
- "true",
- );
- });
- });
|