|
| 1 | +import { describe, expect, it, vi } from "vitest"; |
| 2 | +import type { EmbeddedPiSubscribeContext } from "./pi-embedded-subscribe.handlers.types.js"; |
| 3 | +import { handleToolExecutionEnd } from "./pi-embedded-subscribe.handlers.tools.js"; |
| 4 | + |
| 5 | +// Minimal mock context factory. Only the fields needed for the media emission path. |
| 6 | +function createMockContext(overrides?: { |
| 7 | + shouldEmitToolOutput?: boolean; |
| 8 | + onToolResult?: ReturnType<typeof vi.fn>; |
| 9 | +}): EmbeddedPiSubscribeContext { |
| 10 | + const onToolResult = overrides?.onToolResult ?? vi.fn(); |
| 11 | + return { |
| 12 | + params: { |
| 13 | + runId: "test-run", |
| 14 | + onToolResult, |
| 15 | + onAgentEvent: vi.fn(), |
| 16 | + }, |
| 17 | + state: { |
| 18 | + toolMetaById: new Map(), |
| 19 | + toolMetas: [], |
| 20 | + toolSummaryById: new Set(), |
| 21 | + pendingMessagingTexts: new Map(), |
| 22 | + pendingMessagingTargets: new Map(), |
| 23 | + messagingToolSentTexts: [], |
| 24 | + messagingToolSentTextsNormalized: [], |
| 25 | + messagingToolSentTargets: [], |
| 26 | + }, |
| 27 | + log: { debug: vi.fn(), warn: vi.fn() }, |
| 28 | + shouldEmitToolResult: vi.fn(() => false), |
| 29 | + shouldEmitToolOutput: vi.fn(() => overrides?.shouldEmitToolOutput ?? false), |
| 30 | + emitToolSummary: vi.fn(), |
| 31 | + emitToolOutput: vi.fn(), |
| 32 | + trimMessagingToolSent: vi.fn(), |
| 33 | + hookRunner: undefined, |
| 34 | + // Fill in remaining required fields with no-ops. |
| 35 | + blockChunker: null, |
| 36 | + noteLastAssistant: vi.fn(), |
| 37 | + stripBlockTags: vi.fn((t: string) => t), |
| 38 | + emitBlockChunk: vi.fn(), |
| 39 | + flushBlockReplyBuffer: vi.fn(), |
| 40 | + emitReasoningStream: vi.fn(), |
| 41 | + consumeReplyDirectives: vi.fn(() => null), |
| 42 | + consumePartialReplyDirectives: vi.fn(() => null), |
| 43 | + resetAssistantMessageState: vi.fn(), |
| 44 | + resetForCompactionRetry: vi.fn(), |
| 45 | + finalizeAssistantTexts: vi.fn(), |
| 46 | + ensureCompactionPromise: vi.fn(), |
| 47 | + noteCompactionRetry: vi.fn(), |
| 48 | + resolveCompactionRetry: vi.fn(), |
| 49 | + maybeResolveCompactionWait: vi.fn(), |
| 50 | + recordAssistantUsage: vi.fn(), |
| 51 | + incrementCompactionCount: vi.fn(), |
| 52 | + getUsageTotals: vi.fn(() => undefined), |
| 53 | + getCompactionCount: vi.fn(() => 0), |
| 54 | + } as unknown as EmbeddedPiSubscribeContext; |
| 55 | +} |
| 56 | + |
| 57 | +describe("handleToolExecutionEnd media emission", () => { |
| 58 | + it("emits media when verbose is off and tool result has MEDIA: path", async () => { |
| 59 | + const onToolResult = vi.fn(); |
| 60 | + const ctx = createMockContext({ shouldEmitToolOutput: false, onToolResult }); |
| 61 | + |
| 62 | + await handleToolExecutionEnd(ctx, { |
| 63 | + type: "tool_execution_end", |
| 64 | + toolName: "browser", |
| 65 | + toolCallId: "tc-1", |
| 66 | + isError: false, |
| 67 | + result: { |
| 68 | + content: [ |
| 69 | + { type: "text", text: "MEDIA:/tmp/screenshot.png" }, |
| 70 | + { type: "image", data: "base64", mimeType: "image/png" }, |
| 71 | + ], |
| 72 | + details: { path: "/tmp/screenshot.png" }, |
| 73 | + }, |
| 74 | + }); |
| 75 | + |
| 76 | + expect(onToolResult).toHaveBeenCalledWith({ |
| 77 | + mediaUrls: ["/tmp/screenshot.png"], |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + it("does NOT emit media when verbose is full (emitToolOutput handles it)", async () => { |
| 82 | + const onToolResult = vi.fn(); |
| 83 | + const ctx = createMockContext({ shouldEmitToolOutput: true, onToolResult }); |
| 84 | + |
| 85 | + await handleToolExecutionEnd(ctx, { |
| 86 | + type: "tool_execution_end", |
| 87 | + toolName: "browser", |
| 88 | + toolCallId: "tc-1", |
| 89 | + isError: false, |
| 90 | + result: { |
| 91 | + content: [ |
| 92 | + { type: "text", text: "MEDIA:/tmp/screenshot.png" }, |
| 93 | + { type: "image", data: "base64", mimeType: "image/png" }, |
| 94 | + ], |
| 95 | + details: { path: "/tmp/screenshot.png" }, |
| 96 | + }, |
| 97 | + }); |
| 98 | + |
| 99 | + // onToolResult should NOT be called by the new media path (emitToolOutput handles it). |
| 100 | + // It may be called by emitToolOutput, but the new block should not fire. |
| 101 | + // Verify emitToolOutput was called instead. |
| 102 | + expect(ctx.emitToolOutput).toHaveBeenCalled(); |
| 103 | + // The direct media emission should not have been called with just mediaUrls. |
| 104 | + const directMediaCalls = onToolResult.mock.calls.filter( |
| 105 | + (call: unknown[]) => |
| 106 | + call[0] && |
| 107 | + typeof call[0] === "object" && |
| 108 | + "mediaUrls" in (call[0] as Record<string, unknown>) && |
| 109 | + !("text" in (call[0] as Record<string, unknown>)), |
| 110 | + ); |
| 111 | + expect(directMediaCalls).toHaveLength(0); |
| 112 | + }); |
| 113 | + |
| 114 | + it("does NOT emit media for error results", async () => { |
| 115 | + const onToolResult = vi.fn(); |
| 116 | + const ctx = createMockContext({ shouldEmitToolOutput: false, onToolResult }); |
| 117 | + |
| 118 | + await handleToolExecutionEnd(ctx, { |
| 119 | + type: "tool_execution_end", |
| 120 | + toolName: "browser", |
| 121 | + toolCallId: "tc-1", |
| 122 | + isError: true, |
| 123 | + result: { |
| 124 | + content: [ |
| 125 | + { type: "text", text: "MEDIA:/tmp/screenshot.png" }, |
| 126 | + { type: "image", data: "base64", mimeType: "image/png" }, |
| 127 | + ], |
| 128 | + details: { path: "/tmp/screenshot.png" }, |
| 129 | + }, |
| 130 | + }); |
| 131 | + |
| 132 | + expect(onToolResult).not.toHaveBeenCalled(); |
| 133 | + }); |
| 134 | + |
| 135 | + it("does NOT emit when tool result has no media", async () => { |
| 136 | + const onToolResult = vi.fn(); |
| 137 | + const ctx = createMockContext({ shouldEmitToolOutput: false, onToolResult }); |
| 138 | + |
| 139 | + await handleToolExecutionEnd(ctx, { |
| 140 | + type: "tool_execution_end", |
| 141 | + toolName: "bash", |
| 142 | + toolCallId: "tc-1", |
| 143 | + isError: false, |
| 144 | + result: { |
| 145 | + content: [{ type: "text", text: "Command executed successfully" }], |
| 146 | + }, |
| 147 | + }); |
| 148 | + |
| 149 | + expect(onToolResult).not.toHaveBeenCalled(); |
| 150 | + }); |
| 151 | + |
| 152 | + it("emits media from details.path fallback when no MEDIA: text", async () => { |
| 153 | + const onToolResult = vi.fn(); |
| 154 | + const ctx = createMockContext({ shouldEmitToolOutput: false, onToolResult }); |
| 155 | + |
| 156 | + await handleToolExecutionEnd(ctx, { |
| 157 | + type: "tool_execution_end", |
| 158 | + toolName: "canvas", |
| 159 | + toolCallId: "tc-1", |
| 160 | + isError: false, |
| 161 | + result: { |
| 162 | + content: [ |
| 163 | + { type: "text", text: "Rendered canvas" }, |
| 164 | + { type: "image", data: "base64", mimeType: "image/png" }, |
| 165 | + ], |
| 166 | + details: { path: "/tmp/canvas-output.png" }, |
| 167 | + }, |
| 168 | + }); |
| 169 | + |
| 170 | + expect(onToolResult).toHaveBeenCalledWith({ |
| 171 | + mediaUrls: ["/tmp/canvas-output.png"], |
| 172 | + }); |
| 173 | + }); |
| 174 | +}); |
0 commit comments