@@ -1,31 +1,29 @@
11 import { describe, expect, it, mock } from "bun:test";
22 import { shellTool } from "./shell.ts";
33
4-describe("shellTool", () => {
5- it("appends a newline to streamed output when the command does not print one", async () => {
6- const onOutput = mock(() => {});
4+function collectOutput(mockFn: ReturnType<typeof mock>): string {
5+ return mockFn.mock.calls.map((call) => String(call[0] ?? "")).join("");
6+}
7+
8+async function expectStreamedOutput(command: string, expectedOutput: string) {
9+ const onOutput = mock(() => {});
10+ const result = await shellTool.execute({
11+ command,
12+ timeout: 30_000,
13+ onOutput,
14+ });
715
8- const result = await shellTool.execute({
9- command: "printf ok",
10- timeout: 30_000,
11- onOutput,
12- });
16+ expect(result.stdout).toBe(expectedOutput.trimEnd());
17+ expect(collectOutput(onOutput)).toBe(expectedOutput);
18+}
1319
14- expect(result.stdout).toBe("ok");
15- expect(onOutput.mock.calls.map(([chunk]) => chunk).join("")).toBe("ok\n");
20+describe("shellTool", () => {
21+ it("appends a newline to streamed output when the command does not print one", async () => {
22+ await expectStreamedOutput("printf ok", "ok\n");
1623 });
1724
1825 it("does not duplicate a trailing newline in streamed output", async () => {
19- const onOutput = mock(() => {});
20-
21- const result = await shellTool.execute({
22- command: "printf 'ok\\n'",
23- timeout: 30_000,
24- onOutput,
25- });
26-
27- expect(result.stdout).toBe("ok");
28- expect(onOutput.mock.calls.map(([chunk]) => chunk).join("")).toBe("ok\n");
26+ await expectStreamedOutput("printf 'ok\\n'", "ok\n");
2927 });
3028 });