1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
import { mkdtemp, rm } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { createRunShellTool } from "../../src/tools/run-shell.js";
describe("run_shell tool", () => {
let workDir: string;
beforeEach(async () => {
workDir = await mkdtemp(join(tmpdir(), "dispatch-test-"));
});
afterEach(async () => {
await rm(workDir, { recursive: true, force: true });
});
it("executes a simple echo command", async () => {
const tool = createRunShellTool(workDir);
const raw = await tool.execute({ command: "echo hello" });
const result = JSON.parse(raw);
expect(result.stdout.trim()).toBe("hello");
expect(result.exitCode).toBe(0);
});
it("returns non-zero exit code on failure", async () => {
const tool = createRunShellTool(workDir);
const raw = await tool.execute({ command: "exit 42" });
const result = JSON.parse(raw);
expect(result.exitCode).toBe(42);
});
it("captures stderr", async () => {
const tool = createRunShellTool(workDir);
const raw = await tool.execute({ command: "echo errormsg >&2" });
const result = JSON.parse(raw);
expect(result.stderr.trim()).toBe("errormsg");
});
it("handles timeout", async () => {
const tool = createRunShellTool(workDir);
const raw = await tool.execute({ command: "sleep 10", timeout: 100 });
const result = JSON.parse(raw);
// Either times out (non-zero exit) or returns an error
expect(result.exitCode !== 0 || result.error !== undefined).toBe(true);
}, 5000);
it("executes in the working directory", async () => {
const tool = createRunShellTool(workDir);
const raw = await tool.execute({ command: "pwd" });
const result = JSON.parse(raw);
// On macOS /tmp is symlinked; use includes check
expect(result.stdout.trim()).toContain(workDir.replace(/^\/private/, ""));
});
it("calls onOutput callback with stdout chunks", async () => {
const tool = createRunShellTool(workDir);
const onOutput = vi.fn();
const raw = await tool.execute({ command: "echo streaming" }, { onOutput });
const result = JSON.parse(raw);
expect(result.stdout.trim()).toBe("streaming");
expect(onOutput).toHaveBeenCalledWith(expect.stringContaining("streaming"), "stdout");
});
it("calls onOutput callback with stderr chunks", async () => {
const tool = createRunShellTool(workDir);
const onOutput = vi.fn();
await tool.execute({ command: "echo errdata >&2" }, { onOutput });
expect(onOutput).toHaveBeenCalledWith(expect.stringContaining("errdata"), "stderr");
});
it("works without context (backward compatible)", async () => {
const tool = createRunShellTool(workDir);
const raw = await tool.execute({ command: "echo nocontext" });
const result = JSON.parse(raw);
expect(result.stdout.trim()).toBe("nocontext");
});
});
|