|
| 1 | +// @vitest-environment jsdom |
| 2 | +// ABOUTME: Unit tests for ToolBlock's output section behavior. |
| 3 | +// ABOUTME: Covers visibility, collapse/expand, and preview of result_content. |
| 4 | +import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"; |
| 5 | +import { mount, unmount, tick } from "svelte"; |
| 6 | +import type { ToolCall } from "../../api/types.js"; |
| 7 | + |
| 8 | +vi.mock("./SubagentInline.svelte", () => ({ |
| 9 | + default: {}, |
| 10 | +})); |
| 11 | + |
| 12 | +// @ts-ignore |
| 13 | +import ToolBlock from "./ToolBlock.svelte"; |
| 14 | + |
| 15 | +describe("ToolBlock output section", () => { |
| 16 | + let component: ReturnType<typeof mount>; |
| 17 | + |
| 18 | + afterEach(() => { |
| 19 | + if (component) unmount(component); |
| 20 | + document.body.innerHTML = ""; |
| 21 | + }); |
| 22 | + |
| 23 | + it("does not render output-header when toolCall has no result_content", async () => { |
| 24 | + const toolCall: ToolCall = { |
| 25 | + tool_name: "Read", |
| 26 | + category: "file", |
| 27 | + }; |
| 28 | + component = mount(ToolBlock, { |
| 29 | + target: document.body, |
| 30 | + props: { content: "some input", toolCall }, |
| 31 | + }); |
| 32 | + await tick(); |
| 33 | + |
| 34 | + expect(document.querySelector(".output-header")).toBeNull(); |
| 35 | + }); |
| 36 | + |
| 37 | + it("does not render output-header when toolCall is absent", async () => { |
| 38 | + component = mount(ToolBlock, { |
| 39 | + target: document.body, |
| 40 | + props: { content: "some input" }, |
| 41 | + }); |
| 42 | + await tick(); |
| 43 | + |
| 44 | + expect(document.querySelector(".output-header")).toBeNull(); |
| 45 | + }); |
| 46 | + |
| 47 | + it("renders output-header after expanding the tool block when result_content is set", async () => { |
| 48 | + const toolCall: ToolCall = { |
| 49 | + tool_name: "Read", |
| 50 | + category: "file", |
| 51 | + result_content: "line one\nline two", |
| 52 | + }; |
| 53 | + component = mount(ToolBlock, { |
| 54 | + target: document.body, |
| 55 | + props: { content: "some input", toolCall }, |
| 56 | + }); |
| 57 | + await tick(); |
| 58 | + |
| 59 | + // Output section is inside the collapsed block — not visible yet. |
| 60 | + expect(document.querySelector(".output-header")).toBeNull(); |
| 61 | + |
| 62 | + // Expand the main tool block. |
| 63 | + const toolHeader = document.querySelector<HTMLButtonElement>(".tool-header"); |
| 64 | + expect(toolHeader).not.toBeNull(); |
| 65 | + toolHeader!.click(); |
| 66 | + await tick(); |
| 67 | + |
| 68 | + expect(document.querySelector(".output-header")).not.toBeNull(); |
| 69 | + }); |
| 70 | + |
| 71 | + it("output starts collapsed after expanding the tool block", async () => { |
| 72 | + const toolCall: ToolCall = { |
| 73 | + tool_name: "Read", |
| 74 | + category: "file", |
| 75 | + result_content: "line one\nline two", |
| 76 | + }; |
| 77 | + component = mount(ToolBlock, { |
| 78 | + target: document.body, |
| 79 | + props: { content: "some input", toolCall }, |
| 80 | + }); |
| 81 | + await tick(); |
| 82 | + |
| 83 | + document.querySelector<HTMLButtonElement>(".tool-header")!.click(); |
| 84 | + await tick(); |
| 85 | + |
| 86 | + // Output content pre block should not be present when output is collapsed. |
| 87 | + expect(document.querySelector(".output-content")).toBeNull(); |
| 88 | + }); |
| 89 | + |
| 90 | + it("expands output content on clicking output-header", async () => { |
| 91 | + const resultText = "line one\nline two\nline three"; |
| 92 | + const toolCall: ToolCall = { |
| 93 | + tool_name: "Read", |
| 94 | + category: "file", |
| 95 | + result_content: resultText, |
| 96 | + }; |
| 97 | + component = mount(ToolBlock, { |
| 98 | + target: document.body, |
| 99 | + props: { content: "some input", toolCall }, |
| 100 | + }); |
| 101 | + await tick(); |
| 102 | + |
| 103 | + document.querySelector<HTMLButtonElement>(".tool-header")!.click(); |
| 104 | + await tick(); |
| 105 | + |
| 106 | + document.querySelector<HTMLButtonElement>(".output-header")!.click(); |
| 107 | + await tick(); |
| 108 | + |
| 109 | + const outputContent = document.querySelector(".output-content"); |
| 110 | + expect(outputContent).not.toBeNull(); |
| 111 | + expect(outputContent!.textContent).toBe(resultText); |
| 112 | + }); |
| 113 | + |
| 114 | + it("shows first line as preview when output is collapsed", async () => { |
| 115 | + const toolCall: ToolCall = { |
| 116 | + tool_name: "Read", |
| 117 | + category: "file", |
| 118 | + result_content: "first line\nsecond line", |
| 119 | + }; |
| 120 | + component = mount(ToolBlock, { |
| 121 | + target: document.body, |
| 122 | + props: { content: "some input", toolCall }, |
| 123 | + }); |
| 124 | + await tick(); |
| 125 | + |
| 126 | + document.querySelector<HTMLButtonElement>(".tool-header")!.click(); |
| 127 | + await tick(); |
| 128 | + |
| 129 | + // Output is collapsed — preview should show first line. |
| 130 | + const outputHeader = document.querySelector(".output-header"); |
| 131 | + expect(outputHeader).not.toBeNull(); |
| 132 | + const preview = outputHeader!.querySelector(".tool-preview"); |
| 133 | + expect(preview).not.toBeNull(); |
| 134 | + expect(preview!.textContent).toBe("first line"); |
| 135 | + }); |
| 136 | +}); |
0 commit comments