(feat) gracefully handle invalid commands (#79)

* handle invalid commands
* better test
* format
This commit is contained in:
easong-openai
2025-04-16 12:30:43 -07:00
committed by GitHub
parent a5641a0d9c
commit 75e2454d1d
2 changed files with 76 additions and 7 deletions

View File

@@ -0,0 +1,66 @@
import { describe, it, expect, vi } from "vitest";
// ---------------------------------------------------------------------------
// Lowlevel rawExec test ------------------------------------------------------
// ---------------------------------------------------------------------------
import { exec as rawExec } from "../src/utils/agent/sandbox/raw-exec.js";
describe("rawExec invalid command handling", () => {
it("resolves with nonzero exit code when executable is missing", async () => {
const cmd = ["definitely-not-a-command-1234567890"];
const result = await rawExec(cmd, {}, []);
expect(result.exitCode).not.toBe(0);
expect(result.stderr.length).toBeGreaterThan(0);
});
});
// ---------------------------------------------------------------------------
// Higherlevel handleExecCommand test ----------------------------------------
// ---------------------------------------------------------------------------
// Mock approvals and logging helpers so the test focuses on execution flow.
vi.mock("@lib/approvals.js", () => {
return {
__esModule: true,
canAutoApprove: () =>
({ type: "auto-approve", runInSandbox: false } as any),
isSafeCommand: () => null,
};
});
vi.mock("@lib/format-command.js", () => {
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 nonzero 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;
const getConfirmation = async () => ({ review: "yes" } as any);
const { outputText, metadata } = await handleExecCommand(
execInput,
config,
policy,
getConfirmation,
);
expect(metadata["exit_code"]).not.toBe(0);
expect(String(outputText).length).toBeGreaterThan(0);
});
});