Files
kandev/apps/web/components/task/chat/messages/kandev-tool-message.test.tsx
T
Luan Martins 1a467b969f fix(web): wire permission approval UI for Kandev MCP tools (#1037)
* fix(web): wire permission approval UI for Kandev MCP tools

KandevToolMessage adapter ignored the permissionMessage prop, so MCP tools
that block on a permission request rendered the result row but no Approve/
Deny buttons — users could not unblock the agent. Extract the shared
permission handlers into use-permission-handlers.ts and reuse them from
both ToolCallMessage and KandevToolMessage. KandevRow now also shows an
amber pending clock icon and hides the result-count summary while waiting
for approval so the row reads correctly before the result arrives.

Adds a mock-agent scenarioKandevMCPPermission emitting
mcp__kandev__list_workspaces_kandev plus a Playwright e2e and unit tests
covering the pending UI and the absence of "0 workspaces" while pending.

* fix(web): collapse tool + permission status into resolved Kandev row

After approve/reject the orchestrator stamps the decision onto the
permission_request message immediately, but the tool_call's own
meta.status stays "pending" until the next tool_call update lands.
KandevToolMessage previously passed meta.status straight to the renderer,
so the row kept the amber pending clock and the hidden-summary state for
the brief window between the two events. resolveKandevStatus now prefers
the permission decision once present, mapping approved -> complete and
rejected -> error.

Drops a redundant Playwright `.first()` after a toHaveCount(1) assertion
on the same locator. Adds unit coverage for the resolved-decision state.

* fix(web): keep Kandev tool status independent of permission decision

The previous fix collapsed an approved permission into tool status
"complete" and a rejected permission into "error". That was wrong on two
counts: an approved tool that's still running rendered as already done
(green check + visible result summary built from an empty payload), and
any later tool_call error was silently masked because the status had
already been forced to "complete".

Replace the status override with a separate display overlay. KandevRow
now reads a `KandevPermissionUIContext` ("pending" | "rejected" |
undefined) and uses it only to drive the waiting-on-user affordances —
amber clock, hidden summary — and a rejection mark that wins over the
tool's own success state. The renderer always receives the real
tool_call status, so a tool that's approved-but-pending still reads as
pending and a tool that errors after approval still renders the red X.

Adds explicit regression tests for the two race-window failure modes
plus the new rejected overlay.

* test(web): tolerate duplicate-render backend race in Kandev MCP e2e

CI shard 1 sometimes renders two approve buttons for the same Kandev MCP
permission because the orchestrator can create a second generic tool_call
message for the same tool_call_id alongside the kandev-routed one. Both
buttons resolve the same pending_id, so clicking the first unblocks the
agent either way.

The bug the test guards against is "no approve button at all" — the
strict toHaveCount(1) assertion was tying the test to that separate
backend issue. Relax to at-least-one and click the first.

* fix: persist Deny clicks as rejected, not approved

Backend RespondToPermission previously derived status purely from cancelled:
cancelled=true -> "rejected", otherwise "approved". Deny clicks sent
cancelled=false with a reject_* option_id, so they were recorded as
"approved". Add an explicit rejected flag from the WS request so the
backend can record "rejected" without taking the cancellation path (which
races against permission_cancelled -> "expired").

Also refactor permission-request-message.tsx to consume the shared
usePermissionResponseHandlers hook (the duplicated inline handler had the
same bug) and introduce a typed models.PermissionStatus enum on the
backend so status strings are not scattered literals.

* test(web): scope Kandev MCP permission e2e to the Kandev row

Previously the test clicked the first matching permission-approve button,
which could be a duplicate generic ToolCallMessage fallback rendered for
the same pending_id by a backend race -- a green test no longer proved the
custom Kandev approval UI rendered.

Add a `data-testid="kandev-tool-permission"` wrapper around the Kandev
action row and a `kandevPermissionApproveButtons` page-object locator that
scopes through it, then assert `toHaveCount(1)` so the test only passes
when the Kandev custom row actually mounts.

* chore(orchestrator): log rejected flag in RespondToPermission

Debug log was missing the new rejected param, making it impossible to
distinguish explicit Deny from dialog cancellation when triaging
permission-flow issues.
2026-05-26 00:22:45 +01:00

435 lines
14 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { renderToStaticMarkup } from "react-dom/server";
import { KandevToolMessage, hasKandevRenderer } from "./kandev-tool-message";
import { sessionId as toSessionId, taskId as toTaskId, type Message } from "@/lib/types/http";
// kandevToolCall constructs the kandev Message shape produced by the
// orchestrator for an MCP tool_call. The shape mirrors live production data
// observed on a real session:
// - `metadata.tool_name` — NOT set
// - `metadata.title` — raw tool name (`mcp__kandev__list_*_kandev`)
// - `comment.content` — same raw tool name
// - `normalized.generic.name` — the ACP adapter's *category* ("other"),
// NOT the tool name
// The matcher must therefore look at title/content, not generic.name —
// matching on the wrong field is exactly the bug that shipped initially.
function kandevToolCall(opts: {
toolName: string;
input?: Record<string, unknown>;
resultJson?: unknown;
status?: "pending" | "running" | "complete" | "error";
}): Message {
return {
id: "msg-1",
session_id: toSessionId("s1"),
task_id: toTaskId("t1"),
author_type: "agent",
content: opts.toolName,
type: "tool_call",
created_at: "2026-05-21T10:00:00Z",
metadata: {
tool_call_id: "tc-1",
title: opts.toolName,
status: opts.status ?? "complete",
normalized: {
kind: "generic",
generic: {
name: "other",
input: opts.input,
output:
opts.resultJson !== undefined
? [{ type: "text", text: JSON.stringify(opts.resultJson) }]
: undefined,
},
},
},
};
}
describe("hasKandevRenderer", () => {
it("matches a registered kandev tool", () => {
expect(hasKandevRenderer(kandevToolCall({ toolName: "mcp__kandev__list_tasks_kandev" }))).toBe(
true,
);
});
it("does not match unrelated tools", () => {
expect(hasKandevRenderer(kandevToolCall({ toolName: "Edit" }))).toBe(false);
expect(hasKandevRenderer(kandevToolCall({ toolName: "mcp__github__list_issues" }))).toBe(false);
});
it("does not match kandev tools with no registered renderer", () => {
expect(
hasKandevRenderer(kandevToolCall({ toolName: "mcp__kandev__never_heard_of_it_kandev" })),
).toBe(false);
});
});
// State labels and titles reused across multiple tool-DTOs. Pulled to
// constants to satisfy the "no duplicate string" lint rule.
const COMPLETED = "COMPLETED";
const FIX_LOGIN_BUG = "Fix login bug";
// The expandable row only renders its body when expanded. For tests that
// need to inspect the body, set status to "running" which auto-expands the
// row — the same trigger we rely on at runtime to keep an in-progress tool
// call visible to the user.
describe("KandevToolMessage list renderers", () => {
it("renders the workflow-steps list with structured fields, not raw JSON", () => {
const html = renderToStaticMarkup(
<KandevToolMessage
comment={kandevToolCall({
status: "running",
toolName: "mcp__kandev__list_workflow_steps_kandev",
input: { workflow_id: "f058586c-8a32-474b-ac02-eef47c938b41" },
resultJson: {
steps: [
{ id: "4aad62c5", name: "Backlog", position: 0, color: "bg-neutral-400" },
{
id: "step-2",
name: "Doing",
position: 1,
color: "bg-blue-500",
is_start_step: true,
},
],
total: 2,
},
})}
/>,
);
expect(html).toContain("Kandev: List Workflow Steps");
expect(html).toContain("Backlog");
expect(html).toContain("Doing");
expect(html).toContain("2 steps");
// The raw "\n" escape sequences from the broken JSON dump must not be
// present any more.
expect(html).not.toMatch(/\\n/);
expect(html).not.toContain('"steps":');
});
it("renders list_tasks with task state badges", () => {
const html = renderToStaticMarkup(
<KandevToolMessage
comment={kandevToolCall({
status: "running",
toolName: "list_tasks_kandev",
input: { workflow_id: "wf-1" },
resultJson: {
tasks: [
{ id: "t1", title: FIX_LOGIN_BUG, state: COMPLETED },
{ id: "t2", title: "Add dashboard", state: "RUNNING" },
],
total: 2,
},
})}
/>,
);
expect(html).toContain("Kandev: List Tasks");
expect(html).toContain(FIX_LOGIN_BUG);
expect(html).toContain("Add dashboard");
expect(html).toContain(COMPLETED);
expect(html).toContain("RUNNING");
});
it("renders list_workspaces with the count and item names", () => {
const html = renderToStaticMarkup(
<KandevToolMessage
comment={kandevToolCall({
status: "running",
toolName: "mcp__kandev__list_workspaces_kandev",
resultJson: {
workspaces: [
{ id: "w1", name: "Main" },
{ id: "w2", name: "Side project" },
],
total: 2,
},
})}
/>,
);
expect(html).toContain("Kandev: List Workspaces");
expect(html).toContain("2 workspaces");
expect(html).toContain("Main");
expect(html).toContain("Side project");
});
});
describe("KandevToolMessage task renderers", () => {
it("shows the title argument inline for create_task", () => {
const html = renderToStaticMarkup(
<KandevToolMessage
comment={kandevToolCall({
status: "running",
toolName: "kandev/create_task_kandev",
input: { title: FIX_LOGIN_BUG, description: "..." },
resultJson: { id: "new-id", title: FIX_LOGIN_BUG, state: "CREATED" },
})}
/>,
);
expect(html).toContain("Kandev: Create Task");
expect(html).toContain(FIX_LOGIN_BUG);
expect(html).toContain("CREATED");
});
it("shows updated fields inline for update_task", () => {
const html = renderToStaticMarkup(
<KandevToolMessage
comment={kandevToolCall({
toolName: "mcp__kandev__update_task_kandev",
input: { task_id: "t1", state: COMPLETED },
resultJson: { id: "t1", title: "Old title", state: COMPLETED },
})}
/>,
);
expect(html).toContain("Kandev: Update Task");
expect(html).toContain(`state=${COMPLETED}`);
});
});
describe("KandevToolMessage document & question renderers", () => {
it("renders task plan markdown content for get_task_plan", () => {
const html = renderToStaticMarkup(
<KandevToolMessage
comment={kandevToolCall({
status: "running",
toolName: "mcp__kandev__get_task_plan_kandev",
input: { task_id: "t1" },
resultJson: {
id: "p1",
task_id: "t1",
title: "Migration plan",
content: "# Heading\n\nSome **bold** content.",
},
})}
/>,
);
expect(html).toContain("Kandev: Get Task Plan");
expect(html).toContain("Migration plan");
// Markdown is rendered through ReactMarkdown so the heading and bold
// mark up into proper HTML tags rather than raw text.
expect(html).toContain("<h1");
expect(html).toContain("<strong>bold</strong>");
});
it("renders ask_user_question with the prompt and options", () => {
const html = renderToStaticMarkup(
<KandevToolMessage
comment={kandevToolCall({
status: "running",
toolName: "mcp__kandev__ask_user_question_kandev",
input: {
questions: [
{
id: "q1",
prompt: "Which database should we use?",
options: [{ label: "Postgres" }, { label: "SQLite" }],
},
],
},
resultJson: {
pending_id: "pend-1",
responses: { q1: { question_id: "q1", selected: "Postgres" } },
},
})}
/>,
);
expect(html).toContain("Kandev: Ask User Question");
expect(html).toContain("Which database should we use?");
expect(html).toContain("Postgres");
expect(html).toContain("SQLite");
});
// Regression guard for the array-shape response branch in
// matchAnswerForQuestion. Positional arrays come from older backends or
// single-question calls; the renderer must match the answer by index.
it("renders ask_user_question with positional array responses", () => {
const html = renderToStaticMarkup(
<KandevToolMessage
comment={kandevToolCall({
status: "running",
toolName: "mcp__kandev__ask_user_question_kandev",
input: {
questions: [
{ id: "q1", prompt: "Pick one", options: [{ label: "A" }, { label: "B" }] },
],
},
resultJson: { responses: [{ question_id: "q1", selected: "A" }] },
})}
/>,
);
expect(html).toContain("Pick one");
// Selected option gets the "default" Badge variant; unselected stays "outline".
expect(html).toMatch(/data-variant="default"[^>]*>A<\/span>/);
});
it("does not render anything when the renderer is missing", () => {
const html = renderToStaticMarkup(
<KandevToolMessage
comment={kandevToolCall({ toolName: "mcp__kandev__unknown_tool_kandev" })}
/>,
);
expect(html).toBe("");
});
});
// Unset status is treated as pending by parsePermission.
function pendingPermissionMessage(toolCallId: string): Message {
return {
id: "perm-1",
session_id: toSessionId("s1"),
task_id: toTaskId("t1"),
author_type: "agent",
content: "permission",
type: "permission_request",
created_at: "2026-05-21T10:00:01Z",
metadata: {
pending_id: "pend-1",
tool_call_id: toolCallId,
action_type: "mcp_tool",
action_details: {},
options: [
{ option_id: "allow", name: "Allow", kind: "allow_once" },
{ option_id: "reject", name: "Reject", kind: "reject_once" },
],
},
};
}
describe("KandevToolMessage permission UI", () => {
it("renders Approve / Deny buttons when permissionMessage is pending", () => {
const html = renderToStaticMarkup(
<KandevToolMessage
comment={kandevToolCall({
status: "pending",
toolName: "mcp__kandev__list_workspaces_kandev",
})}
permissionMessage={pendingPermissionMessage("tc-1")}
/>,
);
expect(html).toContain('data-testid="permission-action-row"');
expect(html).toContain('data-testid="permission-approve"');
expect(html).toContain('data-testid="permission-reject"');
});
it("does not render permission row when permissionMessage is absent", () => {
const html = renderToStaticMarkup(
<KandevToolMessage
comment={kandevToolCall({
status: "complete",
toolName: "mcp__kandev__list_workspaces_kandev",
})}
/>,
);
expect(html).not.toContain('data-testid="permission-action-row"');
expect(html).not.toContain('data-testid="permission-approve"');
});
it("renders an amber pending clock icon while waiting for approval", () => {
const html = renderToStaticMarkup(
<KandevToolMessage
comment={kandevToolCall({
status: "pending",
toolName: "mcp__kandev__list_workspaces_kandev",
})}
permissionMessage={pendingPermissionMessage("tc-1")}
/>,
);
expect(html).toContain("tabler-icon-clock");
expect(html).toContain("text-amber");
});
it("hides the result-count summary while pending (no misleading '0 workspaces')", () => {
const html = renderToStaticMarkup(
<KandevToolMessage
comment={kandevToolCall({
status: "pending",
toolName: "mcp__kandev__list_workspaces_kandev",
})}
permissionMessage={pendingPermissionMessage("tc-1")}
/>,
);
expect(html).not.toContain("0 workspaces");
});
});
describe("KandevToolMessage resolved-permission overlay", () => {
function resolvedPermissionMessage(toolCallId: string, status: "approved" | "rejected"): Message {
const msg = pendingPermissionMessage(toolCallId);
return {
...msg,
metadata: { ...(msg.metadata as object), status },
} as Message;
}
it("drops the amber pending clock once the permission resolves to approved", () => {
const html = renderToStaticMarkup(
<KandevToolMessage
comment={kandevToolCall({
status: "pending",
toolName: "mcp__kandev__list_workspaces_kandev",
})}
permissionMessage={resolvedPermissionMessage("tc-1", "approved")}
/>,
);
expect(html).not.toContain("tabler-icon-clock");
expect(html).not.toContain('data-testid="permission-action-row"');
});
it("does NOT mark the tool complete just because the permission was approved", () => {
const html = renderToStaticMarkup(
<KandevToolMessage
comment={kandevToolCall({
status: "pending",
toolName: "mcp__kandev__list_workspaces_kandev",
})}
permissionMessage={resolvedPermissionMessage("tc-1", "approved")}
/>,
);
expect(html).not.toContain("tabler-icon-check");
});
it("still renders an error when the tool_call errors after an approved permission", () => {
const html = renderToStaticMarkup(
<KandevToolMessage
comment={kandevToolCall({
status: "error",
toolName: "mcp__kandev__list_workspaces_kandev",
})}
permissionMessage={resolvedPermissionMessage("tc-1", "approved")}
/>,
);
expect(html).toContain("tabler-icon-x");
});
it("shows the result summary once permission resolves even if meta.status still pending", () => {
const html = renderToStaticMarkup(
<KandevToolMessage
comment={kandevToolCall({
status: "pending",
toolName: "mcp__kandev__list_workspaces_kandev",
resultJson: { workspaces: [{ id: "w1", name: "Main" }], total: 1 },
})}
permissionMessage={resolvedPermissionMessage("tc-1", "approved")}
/>,
);
expect(html).toContain("1 workspace");
});
it("renders a red X overlay when the permission is rejected", () => {
const html = renderToStaticMarkup(
<KandevToolMessage
comment={kandevToolCall({
status: "pending",
toolName: "mcp__kandev__list_workspaces_kandev",
})}
permissionMessage={resolvedPermissionMessage("tc-1", "rejected")}
/>,
);
expect(html).toContain("tabler-icon-x");
expect(html).toContain("text-red-500");
expect(html).not.toContain("tabler-icon-clock");
});
});