2025-04-16 12:30:43 -07:00
|
|
|
|
import { describe, it, expect, vi } from "vitest";
|
|
|
|
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
// Low‑level rawExec test ------------------------------------------------------
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
import { exec as rawExec } from "../src/utils/agent/sandbox/raw-exec.js";
|
2025-05-05 22:56:55 +05:30
|
|
|
|
import type { AppConfig } from "../src/utils/config.js";
|
2025-04-16 12:30:43 -07:00
|
|
|
|
describe("rawExec – invalid command handling", () => {
|
|
|
|
|
|
it("resolves with non‑zero exit code when executable is missing", async () => {
|
|
|
|
|
|
const cmd = ["definitely-not-a-command-1234567890"];
|
2025-05-05 22:56:55 +05:30
|
|
|
|
const config = { model: "any", instructions: "" } as AppConfig;
|
|
|
|
|
|
const result = await rawExec(cmd, {}, config);
|
2025-04-16 12:30:43 -07:00
|
|
|
|
|
|
|
|
|
|
expect(result.exitCode).not.toBe(0);
|
|
|
|
|
|
expect(result.stderr.length).toBeGreaterThan(0);
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
// Higher‑level handleExecCommand test ----------------------------------------
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
// Mock approvals and logging helpers so the test focuses on execution flow.
|
2025-04-16 14:16:53 -07:00
|
|
|
|
vi.mock("../src/approvals.js", () => {
|
2025-04-16 12:30:43 -07:00
|
|
|
|
return {
|
|
|
|
|
|
__esModule: true,
|
|
|
|
|
|
canAutoApprove: () =>
|
2025-04-25 22:21:50 +08:00
|
|
|
|
({ type: "auto-approve", runInSandbox: false }) as any,
|
2025-04-16 12:30:43 -07:00
|
|
|
|
isSafeCommand: () => null,
|
|
|
|
|
|
};
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2025-04-16 14:16:53 -07:00
|
|
|
|
vi.mock("../src/format-command.js", () => {
|
2025-04-16 12:30:43 -07:00
|
|
|
|
return {
|
|
|
|
|
|
__esModule: true,
|
|
|
|
|
|
formatCommandForDisplay: (cmd: Array<string>) => cmd.join(" "),
|
|
|
|
|
|
};
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
vi.mock("../src/utils/agent/log.js", () => ({
|
|
|
|
|
|
__esModule: true,
|
|
|
|
|
|
log: () => {},
|
|
|
|
|
|
isLoggingEnabled: () => false,
|
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
|
|
import { handleExecCommand } from "../src/utils/agent/handle-exec-command.js";
|
|
|
|
|
|
|
|
|
|
|
|
describe("handleExecCommand – invalid executable", () => {
|
|
|
|
|
|
it("returns non‑zero exit code for 'git show' as a single argv element", async () => {
|
|
|
|
|
|
const execInput = { cmd: ["git show"] } as any;
|
|
|
|
|
|
const config = { model: "any", instructions: "" } as any;
|
|
|
|
|
|
const policy = { mode: "auto" } as any;
|
2025-04-25 22:21:50 +08:00
|
|
|
|
const getConfirmation = async () => ({ review: "yes" }) as any;
|
2025-04-16 12:30:43 -07:00
|
|
|
|
|
2025-04-17 15:39:26 -07:00
|
|
|
|
const additionalWritableRoots: Array<string> = [];
|
2025-04-16 12:30:43 -07:00
|
|
|
|
const { outputText, metadata } = await handleExecCommand(
|
|
|
|
|
|
execInput,
|
|
|
|
|
|
config,
|
|
|
|
|
|
policy,
|
2025-04-17 15:39:26 -07:00
|
|
|
|
additionalWritableRoots,
|
2025-04-16 12:30:43 -07:00
|
|
|
|
getConfirmation,
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
expect(metadata["exit_code"]).not.toBe(0);
|
|
|
|
|
|
expect(String(outputText).length).toBeGreaterThan(0);
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|