| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364 |
- // @vitest-environment jsdom
- import { createElement } from "react";
- import { renderToStaticMarkup } from "react-dom/server";
- import { cleanup, fireEvent, render, screen } from "@testing-library/react";
- import { afterEach, describe, expect, it, vi } from "vitest";
- import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
- import { TooltipProvider } from "@/components/ui/tooltip.js";
- import type { ToolCallRow } from "@zcode/shared/zcode-protocol-v4";
- import { ToolCallBlock } from "../src/ToolCallBlocks.js";
- import { ToolCallBody } from "../src/ToolCallBlocks/ToolCallBody.js";
- import { readRawToolCallFileSummaries } from "../src/ToolCallBlocks/shared.js";
- import { isExploreToolCall } from "../src/lib/exploreToolCall.js";
- import { buildToolDisplayModel } from "../src/lib/toolDisplay.js";
- import { toolCallRowToLegacyNode } from "../src/v4/toolCallRowAdapter.js";
- vi.mock("@/ToolCallBlocks/renderers/switch-mode.js", () => ({
- SwitchModeToolCallBlock: ({
- toolCallNode,
- }: {
- toolCallNode: {
- toolCall: {
- toolId: string;
- title?: string;
- kind: string;
- };
- };
- }) =>
- createElement(
- "div",
- { "data-testid": "mock-switch-mode-panel" },
- `switch-mode:${toolCallNode.toolCall.toolId}:${toolCallNode.toolCall.title ?? ""}:${toolCallNode.toolCall.kind}`,
- ),
- }));
- // CreateWorkflow renderer 只回显它**收到的上下文**:这条 mock 存在的理由是一个真实 bug——
- // onOpenWorkflowRun 曾经被透传给子工具卡却从未进 renderContext,于是聊天卡永远拿不到
- // run 详情入口(表现是「入口找不到」,成因却在这层装配里)。
- vi.mock("@/ToolCallBlocks/renderers/create-workflow.js", () => ({
- CreateWorkflowToolCallBlock: ({
- onOpenWorkflowRun,
- workflowRun,
- }: {
- onOpenWorkflowRun?: () => void;
- workflowRun?: { runId: string };
- }) =>
- createElement("div", {
- "data-testid": "mock-create-workflow-panel",
- "data-has-open-run": String(typeof onOpenWorkflowRun === "function"),
- "data-run-id": workflowRun?.runId ?? "",
- }),
- }));
- vi.mock("@/ToolCallBlocks/renderers/plan-guidance.js", () => ({
- PlanGuidanceToolCallBlock: ({
- toolCallNode,
- }: {
- toolCallNode: {
- toolCall: {
- toolId: string;
- title?: string;
- kind: string;
- };
- };
- }) =>
- createElement(
- "div",
- { "data-testid": "mock-plan-guidance-panel" },
- `plan-guidance:${toolCallNode.toolCall.toolId}:${toolCallNode.toolCall.title ?? ""}:${toolCallNode.toolCall.kind}`,
- ),
- }));
- vi.mock("@/ToolCallBlocks/renderers/agent.js", () => ({
- AgentToolCallBlock: ({
- toolCallNode,
- sourceLabel,
- }: {
- toolCallNode: {
- toolCall: {
- toolId: string;
- title?: string;
- kind: string;
- };
- };
- sourceLabel?: string;
- }) =>
- createElement(
- "div",
- { "data-testid": "mock-agent-panel" },
- `agent:${toolCallNode.toolCall.toolId}:${toolCallNode.toolCall.title ?? ""}:${toolCallNode.toolCall.kind}:source:${sourceLabel ?? "none"}`,
- ),
- }));
- afterEach(cleanup);
- describe("isExploreToolCall", () => {
- it("does not classify redirect write commands as explore", () => {
- const result = isExploreToolCall({
- kind: "execute",
- input: {
- command: ["/bin/zsh", "-lc", "cat > index.html <<'EOF' <!doctype html>"],
- },
- });
- expect(result).toBe(false);
- });
- });
- describe("ToolCallBlock agent routing", () => {
- it("renders compact trusted MCP labels from the real plugin-scoped payload", () => {
- const row: ToolCallRow = {
- rowId: 3,
- turnId: "turn-1",
- createdAt: 1_700_000_000_002,
- createdAtSeq: 3,
- kind: "toolCall",
- toolCallId: "call-mcp",
- toolName: "mcp__plugin_firebase_firebase__firebase_get_environment",
- status: "success",
- inputText: '{"query":"is:open"}',
- input: { query: "is:open" },
- output: { text: "12 issues" },
- display: {
- kind: "mcp_tool",
- serverName: "plugin:firebase:firebase",
- toolName: "firebase_get_environment",
- description: "Retrieve the current Firebase environment configuration.",
- },
- };
- const html = renderToStaticMarkup(
- createElement(
- ZCodeIntlProvider,
- { initialLocale: "zh-CN" },
- createElement(
- TooltipProvider,
- null,
- createElement(ToolCallBlock, {
- toolCallNode: toolCallRowToLegacyNode(row),
- workspacePath: "/workspace",
- }),
- ),
- ),
- );
- expect(html).toContain(">MCP<");
- expect(html).toContain("Get environment");
- expect(html).toContain('class="text-foreground-subtle">Firebase</span>');
- expect(html).not.toContain("Plugin:firebase:firebase");
- expect(html).not.toContain("Firebase get environment");
- expect(html).not.toContain(">mcp__plugin_firebase_firebase__firebase_get_environment<");
- expect(html).not.toContain(""toolId"");
- expect(html).not.toContain("已执行");
- });
- it("renders historical MCP rows without display through the MCP summary and detail", () => {
- const syntheticName = "mcp__plugin_firebase_firebase__firebase_get_environment";
- const row: ToolCallRow = {
- rowId: 31,
- turnId: "turn-legacy",
- createdAt: 1_700_000_000_031,
- createdAtSeq: 31,
- kind: "toolCall",
- toolCallId: "call-mcp-legacy-stopped",
- toolName: syntheticName,
- status: "cancelled",
- inputText: "{}",
- input: {},
- };
- render(
- createElement(
- ZCodeIntlProvider,
- { initialLocale: "zh-CN" },
- createElement(ToolCallBlock, {
- toolCallNode: toolCallRowToLegacyNode(row),
- workspacePath: "/workspace",
- }),
- ),
- );
- const summary = screen.getByTestId("tool-summary-trigger-call-mcp-legacy-stopped");
- expect(summary.textContent).toContain("MCP");
- expect(summary.textContent).toContain("Firebase");
- expect(summary.textContent).toContain("Get environment");
- expect(summary.textContent).toContain("已停止");
- expect(summary.innerHTML).toContain(">·</span>");
- expect(summary.textContent).not.toContain(syntheticName);
- fireEvent.click(summary);
- expect(screen.queryByTestId("mcp-expanded-content")).toBeNull();
- expect(screen.queryByRole("button", { name: "查看调用详情" })).toBeNull();
- expect(screen.queryByText("PARAMETERS")).toBeNull();
- expect(screen.queryByText(syntheticName)).toBeNull();
- expect(screen.queryByText(/toolId/u)).toBeNull();
- });
- it("shows only the result first and keeps MCP metadata in secondary call details", () => {
- const description = "Retrieve the current Firebase environment configuration.";
- const row: ToolCallRow = {
- rowId: 4,
- turnId: "turn-1",
- createdAt: 1_700_000_000_003,
- createdAtSeq: 4,
- kind: "toolCall",
- toolCallId: "call-mcp-detail",
- toolName: "mcp__plugin_firebase_firebase__firebase_get_environment",
- status: "success",
- inputText: '{"projectId":"demo"}',
- input: { projectId: "demo" },
- output: { text: "No firebase.json file was found." },
- display: {
- kind: "mcp_tool",
- serverName: "plugin:firebase:firebase",
- toolName: "firebase_get_environment",
- description,
- },
- };
- render(
- createElement(
- ZCodeIntlProvider,
- { initialLocale: "zh-CN" },
- createElement(ToolCallBlock, {
- toolCallNode: toolCallRowToLegacyNode(row),
- workspacePath: "/workspace",
- }),
- ),
- );
- expect(screen.queryByText(description)).toBeNull();
- const summary = screen.getByTestId("tool-summary-trigger-call-mcp-detail");
- fireEvent.click(summary);
- expect(screen.queryByText("Result")).toBeNull();
- expect(screen.queryByText("Parameters")).toBeNull();
- expect(screen.queryByText(description)).toBeNull();
- fireEvent.click(screen.getByRole("button", { name: "查看调用详情" }));
- expect(screen.getByText(description)).toBeTruthy();
- expect(screen.getByText("调用参数")).toBeTruthy();
- });
- it.each([
- ["inputStreaming", "等待中", false],
- ["pendingApproval", "等待中", false],
- ["running", "执行中", true],
- ["cancelled", "已停止", false],
- ] as const)("controls %s MCP detail availability", (status, label, canExpand) => {
- const row: ToolCallRow = {
- rowId: 5,
- turnId: "turn-1",
- createdAt: 1_700_000_000_004,
- createdAtSeq: 5,
- kind: "toolCall",
- toolCallId: `call-mcp-${status}`,
- toolName: "opaque-provider-name",
- status,
- inputText: "{}",
- input: {},
- display: {
- kind: "mcp_tool",
- serverName: "firebase",
- toolName: "get_environment",
- },
- };
- render(
- createElement(
- ZCodeIntlProvider,
- { initialLocale: "zh-CN" },
- createElement(ToolCallBlock, {
- toolCallNode: toolCallRowToLegacyNode(row),
- workspacePath: "/workspace",
- }),
- ),
- );
- const summary = screen.getByTestId(`tool-summary-trigger-call-mcp-${status}`);
- if (status === "inputStreaming" || status === "pendingApproval" || status === "running") {
- expect(summary.textContent).not.toContain(label);
- } else {
- expect(summary.textContent).toContain(label);
- expect(
- Array.from(summary.querySelectorAll("span")).filter(
- (element) => element.textContent === "·",
- ),
- ).toHaveLength(2);
- }
- fireEvent.click(summary);
- if (canExpand) {
- expect(screen.getByTestId("mcp-expanded-content").textContent).toContain(label);
- } else {
- expect(screen.queryByTestId("mcp-expanded-content")).toBeNull();
- }
- });
- it("shows the MCP failure result before call details", () => {
- const row: ToolCallRow = {
- rowId: 6,
- turnId: "turn-1",
- createdAt: 1_700_000_000_005,
- createdAtSeq: 6,
- kind: "toolCall",
- toolCallId: "call-mcp-failed",
- toolName: "opaque-provider-name",
- status: "error",
- inputText: "{}",
- input: {},
- error: { code: "tool_failed", message: "Firebase authentication failed." },
- display: {
- kind: "mcp_tool",
- serverName: "firebase",
- toolName: "get_environment",
- },
- };
- render(
- createElement(
- ZCodeIntlProvider,
- { initialLocale: "zh-CN" },
- createElement(ToolCallBlock, {
- toolCallNode: toolCallRowToLegacyNode(row),
- workspacePath: "/workspace",
- }),
- ),
- );
- const summary = screen.getByTestId("tool-summary-trigger-call-mcp-failed");
- expect(
- Array.from(summary.querySelectorAll("span")).filter((element) => element.textContent === "·"),
- ).toHaveLength(2);
- fireEvent.click(summary);
- const detail = screen.getByTestId("mcp-expanded-content");
- expect(detail.textContent).toContain("Firebase authentication failed.");
- expect(detail.textContent).toContain("查看调用详情");
- expect(screen.getByTestId("mcp-error-surface").className).toContain("bg-destructive/10");
- });
- it("renders multiline MCP result actions", () => {
- const row: ToolCallRow = {
- rowId: 7,
- turnId: "turn-1",
- createdAt: 1_700_000_000_006,
- createdAtSeq: 7,
- kind: "toolCall",
- toolCallId: "call-mcp-long-result",
- toolName: "opaque-provider-name",
- status: "success",
- inputText: "{}",
- input: {},
- output: { text: '{\n "project": "demo"\n}' },
- display: {
- kind: "mcp_tool",
- serverName: "firebase",
- toolName: "get_environment",
- },
- };
- render(
- createElement(
- ZCodeIntlProvider,
- { initialLocale: "zh-CN" },
- createElement(
- TooltipProvider,
- null,
- createElement(ToolCallBlock, {
- toolCallNode: toolCallRowToLegacyNode(row),
- workspacePath: "/workspace",
- }),
- ),
- ),
- );
- fireEvent.click(screen.getByTestId("tool-summary-trigger-call-mcp-long-result"));
- screen.getByTestId("mcp-result-surface");
- expect(screen.getByRole("button", { name: "复制结果" })).toBeTruthy();
- expect(screen.getByRole("button", { name: "切换自动换行" })).toBeTruthy();
- });
- it("renders adapted v4 output-only tool failures as an error surface", () => {
- const row: ToolCallRow = {
- rowId: 1,
- turnId: "turn-1",
- createdAt: 1_700_000_000_000,
- createdAtSeq: 1,
- kind: "toolCall",
- toolCallId: "call-failed",
- toolName: "Bash",
- status: "error",
- inputText: '{"command":"cat /workspace/secret.txt"}',
- input: { command: "cat /workspace/secret.txt" },
- output: {
- text: "<tool_use_error>Permission denied: /workspace/secret.txt</tool_use_error>",
- },
- };
- const toolCall = toolCallRowToLegacyNode(row).toolCall;
- const html = renderToStaticMarkup(
- createElement(
- ZCodeIntlProvider,
- { initialLocale: "zh-CN" },
- createElement(ToolCallBody, {
- childToolList: null,
- displayModel: buildToolDisplayModel(toolCall),
- toolCall,
- workspacePath: "/workspace",
- }),
- ),
- );
- expect(html).toContain("Error");
- expect(html).toContain("Permission denied: /workspace/secret.txt");
- expect(html).not.toContain("<tool_use_error>");
- });
- it("keeps adapted v4 successful tool output on the normal result surface", () => {
- const row: ToolCallRow = {
- rowId: 2,
- turnId: "turn-1",
- createdAt: 1_700_000_000_001,
- createdAtSeq: 2,
- kind: "toolCall",
- toolCallId: "call-success",
- toolName: "Bash",
- status: "success",
- inputText: '{"command":"echo ok"}',
- input: { command: "echo ok" },
- output: { text: "ok" },
- };
- const toolCall = toolCallRowToLegacyNode(row).toolCall;
- const html = renderToStaticMarkup(
- createElement(
- ZCodeIntlProvider,
- { initialLocale: "zh-CN" },
- createElement(ToolCallBody, {
- childToolList: null,
- displayModel: buildToolDisplayModel(toolCall),
- toolCall,
- workspacePath: "/workspace",
- }),
- ),
- );
- expect(html).toContain("Result");
- expect(html).not.toContain("Error");
- });
- it("routes Task tool calls to the agent renderer", () => {
- const html = renderToStaticMarkup(
- createElement(
- ZCodeIntlProvider,
- { initialLocale: "zh-CN" },
- createElement(ToolCallBlock, {
- toolCallNode: {
- toolCall: {
- toolId: "tool-task-agent",
- kind: "think",
- title: "Task",
- input: {
- prompt: "搜索今日新闻",
- run_in_background: true,
- },
- output: [],
- status: "completed",
- raw: {},
- },
- childToolCalls: [],
- },
- workspacePath: "/workspace",
- }),
- ),
- );
- expect(html).toContain("agent:tool-task-agent:Task:think:source:none");
- });
- it("routes GLM Agent tool calls to the agent renderer", () => {
- const html = renderToStaticMarkup(
- createElement(
- ZCodeIntlProvider,
- { initialLocale: "zh-CN" },
- createElement(ToolCallBlock, {
- toolCallNode: {
- toolCall: {
- toolId: "tool-glm-agent",
- kind: "think",
- title: "Agent",
- input: {
- description: "Find runtime loop",
- prompt: "Find where the runtime injects tool results.",
- },
- output: {
- status: "completed",
- agentId: "agent-glm-explore",
- agentType: "Explore",
- },
- status: "completed",
- raw: {},
- },
- childToolCalls: [],
- },
- workspacePath: "/workspace",
- }),
- ),
- );
- expect(html).toContain("agent:tool-glm-agent:Agent:think:source:none");
- });
- it("routes spawn_agent tool calls to the agent renderer", () => {
- const html = renderToStaticMarkup(
- createElement(
- ZCodeIntlProvider,
- { initialLocale: "zh-CN" },
- createElement(ToolCallBlock, {
- toolCallNode: {
- toolCall: {
- toolId: "tool-spawn-agent",
- kind: "spawn_agent",
- title: "spawn_agent",
- input: {
- agent_type: "explorer",
- message: "检查当前项目结构",
- },
- output: {
- agent_id: "agent-1",
- nickname: "Locke",
- },
- status: "completed",
- raw: {
- name: "spawn_agent",
- _meta: {
- zcode: {
- toolName: "spawn_agent",
- nickname: "Locke",
- },
- },
- },
- },
- childToolCalls: [],
- },
- workspacePath: "/workspace",
- }),
- ),
- );
- expect(html).toContain("agent:tool-spawn-agent:spawn_agent:spawn_agent:source:none");
- });
- });
- describe("readRawToolCallFileSummaries", () => {
- it("does not infer edit summaries from read tool content keywords", () => {
- const summaries = readRawToolCallFileSummaries(
- {
- kind: "read",
- status: "completed",
- rawInput: {
- file_path: "/Users/dev/ZCodeProject/demo/snake/index.html",
- },
- content: [
- {
- type: "content",
- content: {
- type: "text",
- text: "function deleteFood() { return writeScore(); }",
- },
- },
- ],
- },
- {
- kind: "read",
- title: "Read snake/index.html",
- input: {
- file_path: "/Users/dev/ZCodeProject/demo/snake/index.html",
- },
- },
- );
- expect(summaries).toEqual([]);
- });
- it("routes read tools to the read renderer even if raw summaries exist", () => {
- const html = renderToStaticMarkup(
- createElement(
- ZCodeIntlProvider,
- { initialLocale: "zh-CN" },
- createElement(ToolCallBlock, {
- toolCallNode: {
- toolCall: {
- toolId: "tool-read-weird",
- kind: "read",
- title: "Read snake/index.html",
- input: {
- file_path: "/Users/dev/ZCodeProject/demo/snake/index.html",
- },
- output: {
- content: "function deleteFood() { return writeScore(); }",
- },
- status: "completed",
- raw: {
- kind: "read",
- changes: {
- "/Users/dev/ZCodeProject/demo/snake/index.html": {
- type: "add",
- content: "unexpected",
- },
- },
- rawInput: {
- file_path: "/Users/dev/ZCodeProject/demo/snake/index.html",
- },
- },
- },
- childToolCalls: [],
- },
- workspacePath: "/workspace",
- }),
- ),
- );
- expect(html).toContain("读取");
- expect(html).not.toContain("主 Agent");
- expect(html).not.toContain("SubAgent");
- expect(html).toContain("index.html");
- expect(html).not.toContain("已写入");
- expect(html).not.toContain("已删除");
- expect(html).not.toContain("已编辑");
- });
- it("does not infer edit summaries for explore nodes backed by Read tool metadata", () => {
- const summaries = readRawToolCallFileSummaries(
- {
- _meta: {
- claudeCode: {
- toolName: "Read",
- },
- },
- content: [
- {
- type: "content",
- content: {
- type: "text",
- text: "function deleteFood() { return writeScore(); }",
- },
- },
- ],
- rawOutput: "...",
- status: "completed",
- sessionUpdate: "tool_call_update",
- },
- {
- kind: "explore",
- title: "Read snake/game.ts",
- input: {
- file_path: "/Users/dev/ZCodeProject/demo/snake/game.ts",
- },
- },
- );
- expect(summaries).toEqual([]);
- });
- it("reads multi-file ZCode tool display summaries", () => {
- const summaries = readRawToolCallFileSummaries(
- {
- display: {
- kind: "file_diffs",
- files: [
- {
- filePath: "/workspace/src/a.ts",
- additions: 2,
- deletions: 1,
- structuredPatch: [
- {
- oldStart: 1,
- oldLines: 1,
- newStart: 1,
- newLines: 1,
- lines: ["-old", "+new"],
- },
- ],
- },
- {
- filePath: "/workspace/src/b.ts",
- additions: 3,
- deletions: 0,
- structuredPatch: [
- {
- oldStart: 1,
- oldLines: 0,
- newStart: 1,
- newLines: 1,
- lines: ["+created"],
- },
- ],
- },
- ],
- },
- },
- {
- kind: "ApplyPatch",
- title: "Edited 2 files",
- input: {},
- },
- );
- expect(summaries).toHaveLength(2);
- expect(summaries.map((summary) => summary.changeStat)).toEqual([
- { added: 2, removed: 1 },
- { added: 3, removed: 0 },
- ]);
- expect(summaries[1]?.patch).toContain("+created");
- });
- it("does not build delete summaries for non-writable execute tools even with raw changes", () => {
- const summaries = readRawToolCallFileSummaries(
- {
- kind: "execute",
- changes: {
- "/Users/dev/ZCodeProject/demo/snake/game.ts": {
- type: "delete",
- oldText: "console.log('legacy');",
- },
- },
- rawInput: {
- path: "/Users/dev/ZCodeProject/demo/snake/game.ts",
- },
- },
- {
- kind: "execute",
- title: "Run cleanup command",
- input: {
- command: "rm game.ts",
- },
- },
- );
- expect(summaries).toEqual([]);
- });
- it("does not infer delete from explore text without writable tool kind", () => {
- const summaries = readRawToolCallFileSummaries(
- {
- status: "completed",
- rawInput: {
- file_path: "/Users/dev/ZCodeProject/demo/snake/game.ts",
- description: "Delete legacy fallback after inspection",
- },
- },
- {
- kind: "explore",
- title: "Inspect game.ts",
- input: {
- file_path: "/Users/dev/ZCodeProject/demo/snake/game.ts",
- description: "Delete legacy fallback after inspection",
- },
- },
- );
- expect(summaries).toEqual([]);
- });
- it("normalizes hunk-only add patches as newly created files", () => {
- const summaries = readRawToolCallFileSummaries(
- {
- changes: {
- "src/demo.ts": {
- type: "add",
- unified_diff: "@@ -0,0 +1,2 @@\n+export const value = 1;\n+export { value };",
- },
- },
- },
- {
- kind: "write",
- title: "Write src/demo.ts",
- input: {
- path: "src/demo.ts",
- },
- },
- );
- expect(summaries).toHaveLength(1);
- expect(summaries[0]?.patch).toContain("--- /dev/null");
- expect(summaries[0]?.patch).toContain("+++ b/demo.ts");
- });
- it("builds write summaries from ZCode protocol input payloads", () => {
- const summaries = readRawToolCallFileSummaries(
- {
- toolName: "Write",
- input: {
- file_path: "src/quicksort.js",
- content: "export const quicksort = () => [];\n",
- },
- },
- {
- kind: "Write",
- title: "Write",
- raw: {
- toolName: "Write",
- input: {
- file_path: "src/quicksort.js",
- content: "export const quicksort = () => [];\n",
- },
- },
- },
- );
- expect(summaries).toHaveLength(1);
- expect(summaries[0]?.path).toBe("src/quicksort.js");
- expect(summaries[0]?.operationKind).toBe("write");
- expect(summaries[0]?.changeStat).toEqual({ added: 1, removed: 0 });
- });
- it("builds edit summaries from ZCode protocol input payloads", () => {
- const summaries = readRawToolCallFileSummaries(
- {
- toolName: "Edit",
- input: {
- file_path: "src/quicksort.js",
- old_string: "return left.concat(pivot, right);",
- new_string: "return [...left, pivot, ...right];",
- },
- },
- {
- kind: "Edit",
- title: "Edit",
- raw: {
- toolName: "Edit",
- input: {
- file_path: "src/quicksort.js",
- old_string: "return left.concat(pivot, right);",
- new_string: "return [...left, pivot, ...right];",
- },
- },
- },
- );
- expect(summaries).toHaveLength(1);
- expect(summaries[0]?.path).toBe("src/quicksort.js");
- expect(summaries[0]?.operationKind).toBe("edit");
- expect(summaries[0]?.changeStat).toEqual({ added: 1, removed: 1 });
- });
- it("drops multi-file unified diff payloads and falls back to single-file patch", () => {
- const summaries = readRawToolCallFileSummaries(
- {
- changes: {
- "src/demo.ts": {
- type: "update",
- oldText: "const value = 1;\n",
- newText: "const value = 2;\n",
- unified_diff:
- "diff --git a/src/demo.ts b/src/demo.ts\n--- a/src/demo.ts\n+++ b/src/demo.ts\n@@ -1 +1 @@\n-const value = 1;\n+const value = 2;\n" +
- "diff --git a/src/other.ts b/src/other.ts\n--- a/src/other.ts\n+++ b/src/other.ts\n@@ -1 +1 @@\n-console.log('a');\n+console.log('b');",
- },
- },
- },
- {
- kind: "edit",
- title: "Edit src/demo.ts",
- input: {
- path: "src/demo.ts",
- },
- },
- );
- expect(summaries).toHaveLength(1);
- expect(summaries[0]?.patch).toContain("--- a/demo.ts");
- expect(summaries[0]?.patch).toContain("+++ b/demo.ts");
- expect(summaries[0]?.patch).not.toContain("src/other.ts");
- });
- it("drops truncated second-file hunks in multi-file unified diff payloads", () => {
- const summaries = readRawToolCallFileSummaries(
- {
- changes: {
- "src/demo.ts": {
- type: "update",
- oldText: "const value = 1;\n",
- newText: "const value = 2;\n",
- // 回归场景:第二个文件 hunk 被流式截断时,仍应识别为多文件并触发 fallback。
- unified_diff:
- "--- a/src/demo.ts\n+++ b/src/demo.ts\n@@ -1 +1 @@\n-const value = 1;\n+const value = 2;\n" +
- "--- a/src/other.ts\n+++ b/src/other.ts\n@@ -1 +1 @@\n-console.log('a');",
- },
- },
- },
- {
- kind: "edit",
- title: "Edit src/demo.ts",
- input: {
- path: "src/demo.ts",
- },
- },
- );
- expect(summaries).toHaveLength(1);
- expect(summaries[0]?.patch).toContain("--- a/demo.ts");
- expect(summaries[0]?.patch).toContain("+++ b/demo.ts");
- expect(summaries[0]?.patch).not.toContain("src/other.ts");
- });
- it("keeps single-file patches when hunk content contains header-like lines", () => {
- const summaries = readRawToolCallFileSummaries(
- {
- changes: {
- "src/demo.ts": {
- type: "update",
- // 回归场景:正文里以 --- / +++ 开头的文本不应被识别成额外文件头。
- unified_diff:
- "--- a/src/demo.ts\n+++ b/src/demo.ts\n@@ -1,2 +1,2 @@\n const marker = true;\n--- header-like-old-line\n+++ header-like-new-line",
- },
- },
- },
- {
- kind: "edit",
- title: "Edit src/demo.ts",
- input: {
- path: "src/demo.ts",
- },
- },
- );
- expect(summaries).toHaveLength(1);
- expect(summaries[0]?.patch).toContain("--- a/src/demo.ts");
- expect(summaries[0]?.patch).toContain("+++ b/src/demo.ts");
- expect(summaries[0]?.patch).toContain("--- header-like-old-line");
- expect(summaries[0]?.patch).toContain("+++ header-like-new-line");
- });
- it("keeps read-like explore nodes out of edit renderer", () => {
- const html = renderToStaticMarkup(
- createElement(
- ZCodeIntlProvider,
- { initialLocale: "zh-CN" },
- createElement(ToolCallBlock, {
- toolCallNode: {
- toolCall: {
- toolId: "tool-explore-read-like",
- kind: "explore",
- title: "Read snake/game.ts",
- input: {
- file_path: "/Users/dev/ZCodeProject/demo/snake/game.ts",
- },
- output: "...",
- status: "completed",
- raw: {
- _meta: {
- claudeCode: {
- toolName: "Read",
- },
- },
- content: [
- {
- type: "content",
- content: {
- type: "text",
- text: "function deleteFood() { return writeScore(); }",
- },
- },
- ],
- rawOutput: "...",
- status: "completed",
- toolCallId: "call_bcbf3d3258d34b44b3dbc0b4",
- sessionUpdate: "tool_call_update",
- },
- },
- childToolCalls: [],
- },
- workspacePath: "/workspace",
- }),
- ),
- );
- expect(html).not.toContain("已写入");
- expect(html).not.toContain("已删除");
- expect(html).not.toContain("已编辑");
- });
- it("keeps execute tools with delete-like raw changes out of edit renderer", () => {
- const html = renderToStaticMarkup(
- createElement(
- ZCodeIntlProvider,
- { initialLocale: "zh-CN" },
- createElement(ToolCallBlock, {
- toolCallNode: {
- toolCall: {
- toolId: "tool-execute-delete-like",
- kind: "execute",
- title: "Run cleanup",
- input: {
- command: "rm snake/game.ts",
- },
- output: {
- stdout: "cleanup finished",
- },
- status: "completed",
- raw: {
- kind: "execute",
- changes: {
- "/Users/dev/ZCodeProject/demo/snake/game.ts": {
- type: "delete",
- oldText: "console.log('legacy');",
- },
- },
- },
- },
- childToolCalls: [],
- },
- workspacePath: "/workspace",
- }),
- ),
- );
- expect(html).toContain("终端");
- expect(html).not.toContain("已删除");
- expect(html).not.toContain("已编辑");
- expect(html).not.toContain("Deleting");
- expect(html).not.toContain("Deleted");
- });
- it("把 dwf run 入口与 run 摘要交到 CreateWorkflow renderer 手里", () => {
- // 回归:这两个 prop 之前只被透传给**子**工具卡,从未进过 renderContext,于是 run 态卡片
- // 和它的详情页入口在生产里根本不存在。装配这一层必须自己被钉住。
- const html = renderToStaticMarkup(
- createElement(
- ZCodeIntlProvider,
- { initialLocale: "zh-CN" },
- createElement(ToolCallBlock, {
- toolCallNode: {
- toolCall: {
- toolId: "tool-create-workflow",
- kind: "CreateWorkflow",
- title: "CreateWorkflow",
- input: { name: "Fan-out review", script: "const x = 1;" },
- status: "completed",
- },
- childToolCalls: [],
- },
- workspacePath: "/workspace",
- onOpenWorkflowRun: () => {},
- workflowRun: {
- runId: "dwfrun-42",
- status: "running" as const,
- nodesSettled: 3,
- nodesTotal: 7,
- },
- }),
- ),
- );
- expect(html).toContain("mock-create-workflow-panel");
- expect(html).toContain('data-has-open-run="true"');
- expect(html).toContain('data-run-id="dwfrun-42"');
- });
- it("routes switch_mode tool calls to the dedicated renderer", () => {
- const html = renderToStaticMarkup(
- createElement(
- ZCodeIntlProvider,
- { initialLocale: "zh-CN" },
- createElement(ToolCallBlock, {
- toolCallNode: {
- toolCall: {
- toolId: "tool-switch-mode",
- kind: "switch_mode",
- title: "Exited Plan Mode",
- input: {
- plan: "# 输出计划\n\n- 渲染 markdown",
- },
- output: "...",
- status: "completed",
- raw: {
- rawOutput: "...",
- },
- },
- childToolCalls: [],
- },
- workspacePath: "/workspace",
- }),
- ),
- );
- expect(html).toContain("mock-switch-mode-panel");
- expect(html).toContain("switch-mode:tool-switch-mode:Exited Plan Mode:switch_mode");
- });
- it("routes Exited Plan Mode titles to the switch mode renderer even when kind is other", () => {
- const html = renderToStaticMarkup(
- createElement(
- ZCodeIntlProvider,
- { initialLocale: "zh-CN" },
- createElement(ToolCallBlock, {
- toolCallNode: {
- toolCall: {
- toolId: "tool-exited-plan-title",
- kind: "other",
- title: "Exited Plan Mode",
- input: {
- plan: "# 输出计划\n\n- 渲染 markdown",
- },
- output: "...",
- status: "completed",
- raw: {
- rawOutput: "...",
- },
- },
- childToolCalls: [],
- },
- workspacePath: "/workspace",
- }),
- ),
- );
- expect(html).toContain("mock-switch-mode-panel");
- expect(html).toContain("switch-mode:tool-exited-plan-title:Exited Plan Mode:other");
- });
- it("routes current ExitPlanMode tool calls to the switch mode renderer", () => {
- const html = renderToStaticMarkup(
- createElement(
- ZCodeIntlProvider,
- { initialLocale: "zh-CN" },
- createElement(ToolCallBlock, {
- toolCallNode: {
- toolCall: {
- toolId: "tool-exit-plan-current",
- kind: "ExitPlanMode",
- title: "ExitPlanMode",
- input: {
- plan: "# 输出计划\n\n- 渲染 markdown",
- },
- output: "...",
- status: "completed",
- raw: {
- toolName: "ExitPlanMode",
- rawOutput: "...",
- },
- },
- childToolCalls: [],
- },
- workspacePath: "/workspace",
- }),
- ),
- );
- expect(html).toContain("mock-switch-mode-panel");
- expect(html).toContain("switch-mode:tool-exit-plan-current:ExitPlanMode:ExitPlanMode");
- });
- it("routes EnterPlanMode titles to the plan guidance renderer", () => {
- const html = renderToStaticMarkup(
- createElement(
- ZCodeIntlProvider,
- { initialLocale: "zh-CN" },
- createElement(ToolCallBlock, {
- toolCallNode: {
- toolCall: {
- toolId: "tool-enter-plan-guidance",
- kind: "other",
- title: "EnterPlanMode",
- input: {},
- output: "Entered plan mode.",
- status: "completed",
- raw: {
- _meta: {
- claudeCode: {
- toolName: "EnterPlanMode",
- },
- },
- rawOutput: "Entered plan mode.",
- },
- },
- childToolCalls: [],
- },
- workspacePath: "/workspace",
- }),
- ),
- );
- expect(html).toContain("mock-plan-guidance-panel");
- expect(html).toContain("plan-guidance:tool-enter-plan-guidance:EnterPlanMode:other");
- });
- it("routes TodoWrite to the todo renderer instead of the file write renderer", () => {
- const html = renderToStaticMarkup(
- createElement(
- ZCodeIntlProvider,
- { initialLocale: "zh-CN" },
- createElement(ToolCallBlock, {
- toolCallNode: {
- toolCall: {
- toolId: "tool-todo-write",
- kind: "TodoWrite",
- title: "TodoWrite",
- input: {
- todos: [
- {
- content: "更新 index.html 标题",
- priority: "high",
- status: "in_progress",
- },
- {
- content: "创建番茄时钟组件",
- priority: "medium",
- status: "pending",
- },
- ],
- },
- output: JSON.stringify({
- oldTodos: [],
- todos: [
- {
- content: "更新 index.html 标题",
- priority: "high",
- status: "in_progress",
- },
- {
- content: "创建番茄时钟组件",
- priority: "medium",
- status: "pending",
- },
- ],
- }),
- status: "completed",
- raw: {
- toolCallId: "tool-todo-write",
- toolName: "TodoWrite",
- },
- },
- childToolCalls: [],
- },
- workspacePath: "/workspace",
- }),
- ),
- );
- expect(html).toContain("待办");
- expect(html).toContain("更新 index.html 标题");
- expect(html).toContain("0/2");
- expect(html).not.toContain("已写入");
- });
- it("routes fixed AskUserQuestion tools without renaming the tool identity", () => {
- const html = renderToStaticMarkup(
- createElement(
- ZCodeIntlProvider,
- { initialLocale: "zh-CN" },
- createElement(ToolCallBlock, {
- toolCallNode: {
- toolCall: {
- toolId: "tool-ask-user-question",
- toolName: "AskUserQuestion",
- kind: "AskUserQuestion",
- title: "AskUserQuestion",
- input: {
- questions: [
- {
- id: "q1",
- question: "选择实现方向?",
- options: [
- { id: "safe", label: "稳妥" },
- { id: "fast", label: "快速" },
- ],
- },
- ],
- answers: {
- "选择实现方向?": "稳妥",
- },
- },
- status: "completed",
- raw: {
- toolName: "AskUserQuestion",
- },
- },
- childToolCalls: [],
- },
- workspacePath: "/workspace",
- }),
- ),
- );
- expect(html).toContain("已询问");
- expect(html).toContain("1 个问题");
- expect(html).toContain("AskUserQuestion");
- expect(html).not.toContain("AskQuestion");
- expect(html).not.toContain("已写入");
- });
- it("shows an automatic-continuation history label for explicit empty answers", () => {
- const html = renderToStaticMarkup(
- createElement(
- ZCodeIntlProvider,
- { initialLocale: "zh-CN" },
- createElement(ToolCallBlock, {
- toolCallNode: {
- toolCall: {
- toolId: "tool-ask-auto-continued",
- toolName: "AskUserQuestion",
- kind: "AskUserQuestion",
- title: "AskUserQuestion",
- input: {
- questions: [
- {
- id: "q1",
- question: "选择实现方向?",
- options: [
- { id: "safe", label: "稳妥" },
- { id: "fast", label: "快速" },
- ],
- },
- ],
- },
- output: { questions: [], answers: {} },
- status: "completed",
- raw: { toolName: "AskUserQuestion" },
- },
- childToolCalls: [],
- },
- workspacePath: "/workspace",
- }),
- ),
- );
- expect(html).toContain("未回答,已自动继续");
- expect(html).not.toContain("未提供回答");
- });
- it("routes Agent tool calls to the dedicated renderer", () => {
- const html = renderToStaticMarkup(
- createElement(
- ZCodeIntlProvider,
- { initialLocale: "zh-CN" },
- createElement(ToolCallBlock, {
- toolCallNode: {
- toolCall: {
- toolId: "tool-agent",
- kind: "think",
- title: "Explore project structure",
- input: {
- description: "Explore project structure",
- subagent_type: "Explore",
- },
- output: "...",
- status: "completed",
- raw: {
- _meta: {
- claudeCode: {
- toolName: "Agent",
- },
- },
- },
- },
- childToolCalls: [],
- },
- workspacePath: "/workspace",
- }),
- ),
- );
- expect(html).toContain("mock-agent-panel");
- expect(html).toContain("agent:tool-agent:Explore project structure:think");
- });
- });
|