File size: 5,996 Bytes
fd2c364 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | import { render, screen } from "@testing-library/react";
import type { ReactNode } from "react";
import { describe, expect, it, vi } from "vitest";
import { HookExecutionEventMessage } from "#/components/shared/hook-execution-event-message";
import { I18nKey } from "#/i18n/declaration";
import type {
HookExecutionEvent,
OpenHandsEvent,
} from "#/types/agent-server/core";
vi.mock("react-i18next", () => ({
useTranslation: (namespace: string) => ({
t: (key: string) =>
namespace === "openhands" ? key : `wrong-namespace:${key}`,
}),
}));
vi.mock("#/components/features/chat/generic-event-message", () => ({
GenericEventMessage: ({
title,
details,
success,
}: {
title: ReactNode;
details: ReactNode;
success?: "success" | "error";
}) => (
<section
data-testid="generic-event-message"
data-success={success ?? "none"}
>
<header>{title}</header>
<div>{details}</div>
</section>
),
}));
const makeHookEvent = (
overrides: Partial<HookExecutionEvent> = {},
): HookExecutionEvent => ({
id: "hook-event-1",
timestamp: "2026-07-12T00:00:00.000Z",
source: "hook",
kind: "HookExecutionEvent",
hook_event_type: "PreToolUse",
hook_command: "npm test",
success: true,
blocked: false,
exit_code: null,
reason: null,
tool_name: null,
action_id: null,
message_id: null,
stdout: null,
stderr: null,
error: null,
additional_context: null,
hook_input: null,
...overrides,
});
const renderHookEvent = (overrides: Partial<HookExecutionEvent> = {}) =>
render(<HookExecutionEventMessage event={makeHookEvent(overrides)} />);
describe("hook execution event message", () => {
it("renders nothing for an event from another event family", () => {
const event = {
id: "state-event-1",
timestamp: "2026-07-12T00:00:00.000Z",
source: "agent",
kind: "ConversationStateUpdateEvent",
key: "execution_status",
value: "idle",
} as unknown as OpenHandsEvent;
const { container } = render(<HookExecutionEventMessage event={event} />);
expect(container).toBeEmptyDOMElement();
});
it.each([
["PreToolUse", "โณ"],
["PostToolUse", "โ
"],
["UserPromptSubmit", "๐"],
["SessionStart", "๐"],
["SessionEnd", "๐"],
["Stop", "โน๏ธ"],
["FutureHook", "๐"],
])("uses the expected icon for %s hooks", (hookType, icon) => {
renderHookEvent({
hook_event_type: hookType as HookExecutionEvent["hook_event_type"],
});
const message = screen.getByTestId("generic-event-message");
expect(message).toHaveTextContent(
`${icon} ${I18nKey.HOOK$HOOK_LABEL}: ${hookType}`,
);
expect(message).toHaveAttribute("data-success", "success");
expect(screen.getByText("ok")).toHaveClass(
"bg-green-900/50",
"text-green-300",
);
});
it("shows all available metadata for a blocked hook", () => {
renderHookEvent({
hook_event_type: "PostToolUse",
hook_command: "check-policy",
success: false,
blocked: true,
exit_code: 13,
reason: "Command denied by policy",
tool_name: "terminal",
stdout: "policy output",
stderr: "policy warning",
error: "hook failed",
additional_context: "workspace is protected",
});
const message = screen.getByTestId("generic-event-message");
expect(message).toHaveAttribute("data-success", "none");
expect(message).toHaveTextContent(
`๐ซ ${I18nKey.HOOK$HOOK_LABEL}: PostToolUse`,
);
expect(screen.getByText("(terminal)")).toBeInTheDocument();
expect(screen.getByText("blocked")).toHaveClass(
"ml-2",
"px-1",
"py-0.5",
"rounded",
"text-xs",
"bg-amber-900/50",
"text-amber-300",
);
expect(message).toHaveTextContent(`${I18nKey.HOOK$COMMAND}: check-policy`);
expect(message).toHaveTextContent(`${I18nKey.HOOK$EXIT_CODE}: 13`);
expect(message).toHaveTextContent(
`${I18nKey.HOOK$BLOCKED_REASON}: Command denied by policy`,
);
expect(message).toHaveTextContent(
`${I18nKey.HOOK$CONTEXT}: workspace is protected`,
);
expect(message).toHaveTextContent(`${I18nKey.HOOK$ERROR}: hook failed`);
expect(message).toHaveTextContent(`${I18nKey.HOOK$OUTPUT}:policy output`);
expect(message).toHaveTextContent(`${I18nKey.HOOK$STDERR}:policy warning`);
});
it("shows a failed status and truncates commands longer than 80 characters", () => {
const longCommand = "x".repeat(100);
renderHookEvent({
hook_command: longCommand,
success: false,
reason: "not shown unless blocked",
});
const message = screen.getByTestId("generic-event-message");
expect(message).toHaveAttribute("data-success", "error");
expect(screen.getByText("failed")).toHaveClass(
"bg-red-900/50",
"text-red-300",
);
expect(screen.getByText(`${"x".repeat(77)}...`)).toBeInTheDocument();
expect(message).not.toHaveTextContent(longCommand);
expect(message).not.toHaveTextContent(I18nKey.HOOK$BLOCKED_REASON);
});
it("keeps an exactly 80-character command intact", () => {
const command = "y".repeat(80);
renderHookEvent({ hook_command: command });
expect(screen.getByText(command)).toBeInTheDocument();
expect(screen.getByTestId("generic-event-message")).not.toHaveTextContent(
`${"y".repeat(77)}...`,
);
});
it("omits a blocked-reason row when a blocked hook has no reason", () => {
renderHookEvent({ blocked: true, reason: null });
const message = screen.getByTestId("generic-event-message");
expect(message).toHaveAttribute("data-success", "none");
expect(message).not.toHaveTextContent(I18nKey.HOOK$BLOCKED_REASON);
});
it("omits the exit-code row when no exit code is available", () => {
renderHookEvent({ exit_code: null });
expect(screen.getByTestId("generic-event-message")).not.toHaveTextContent(
I18nKey.HOOK$EXIT_CODE,
);
});
});
|