test(acp): cover live shell output over a real acp subprocess

This commit is contained in:
Filip 2026-11-05 21:38:00 +00:00
parent fddab0b6df
commit dc0279a2e4
2 changed files with 51 additions and 0 deletions

View file

@ -0,0 +1,46 @@
import type { PromptResponse, SessionNotification } from "@agentclientprotocol/sdk"
import { describe, expect, test } from "bun:test"
import { createAcpFixture, expectOk, initialize, newSession, toolCallStream } from "./subprocess"
// Each line is printed seconds apart, so the live output is read before the command ends.
const command = "echo line 1; sleep 2; echo line 2; sleep 2; echo line 3"
// The first completion runs the command; the follow-up completion ends the turn.
function shellModel(request: unknown) {
if (JSON.stringify(request).includes('"role":"tool"')) return "done"
return toolCallStream("call_shell", "shell", { command })
}
describe("acp shell output subprocess", () => {
test("streams a running shell command's output on in_progress tool call updates", async () => {
await using fixture = await createAcpFixture({ respond: shellModel })
const acp = fixture.spawn()
await initialize(acp)
const session = await newSession(acp, fixture.home)
const result = expectOk(
await acp.request<PromptResponse>("session/prompt", {
sessionId: session.sessionId,
prompt: [{ type: "text", text: `Run this and report the output: ${command}` }],
}),
)
expect(result.stopReason).toBe("end_turn")
const updates = acp
.received()
.filter((message) => message.method === "session/update")
.map((message) => (message.params as SessionNotification).update)
.filter((update) => update.sessionUpdate === "tool_call_update" && update.toolCallId === "call_shell")
const running = updates.flatMap((update) => {
if (update.sessionUpdate !== "tool_call_update" || update.status !== "in_progress") return []
const part = update.content?.[0]
return part?.type === "content" && part.content.type === "text" ? [part.content.text] : []
})
expect(running.some((text) => text.startsWith("line 1\n") && !text.includes("line 3"))).toBe(true)
expect(running.every((text) => "line 1\nline 2\nline 3\n".startsWith(text))).toBe(true)
expect(updates.at(-1)).toMatchObject({
status: "completed",
content: [{ type: "content", content: { type: "text", text: "line 1\nline 2\nline 3\n" } }],
})
}, 60_000)
})

View file

@ -37,6 +37,8 @@ type AcpProcess = {
readonly pid: number
readonly exited: Promise<number>
readonly request: <T>(method: string, params?: unknown) => Promise<JsonRpcResponse<T>>
/** Every request and notification the agent sent, in order. */
readonly received: () => readonly JsonRpcMessage[]
readonly close: () => Promise<number>
readonly stderr: () => string
readonly [Symbol.asyncDispose]: () => Promise<void>
@ -197,6 +199,7 @@ function spawnAcp(input: { readonly env: Record<string, string | undefined> }):
const decoder = new TextDecoder()
const errorDecoder = new TextDecoder()
const messages: JsonRpcMessage[] = []
const received: JsonRpcMessage[] = []
const waiters: Waiter[] = []
let nextID = 1
let failure: Error | undefined
@ -214,6 +217,7 @@ function spawnAcp(input: { readonly env: Record<string, string | undefined> }):
}
const dispatch = (message: JsonRpcMessage) => {
if ("method" in message) received.push(message)
const index = waiters.findIndex((waiter) => waiter.predicate(message))
if (index === -1) {
messages.push(message)
@ -294,6 +298,7 @@ function spawnAcp(input: { readonly env: Record<string, string | undefined> }):
pid: child.pid,
exited: child.exited,
request,
received: () => received,
async close() {
if (!inputClosed) {
inputClosed = true