tests / exec.test.ts
import * as os from "os";
import { describe, expect, it } from "vitest";
import { runCommand, scrubbedEnv } from "../src/exec";
const base = { cwd: os.tmpdir(), timeoutMs: 5000, maxChars: 20000 };
describe("runCommand", () => {
it("captures stdout and exit code 0", async () => {
const result = await runCommand("echo hello", base);
expect(result).toMatchObject({ output: "hello\n", exitCode: 0, timedOut: false, aborted: false, truncated: false });
});
it("supports pipes and && (real shell)", async () => {
const result = await runCommand("printf 'a\\nb\\n' | wc -l && echo done", base);
expect(result.output).toContain("2");
expect(result.output).toContain("done");
});
it("captures stderr and non-zero exit codes", async () => {
const result = await runCommand("echo oops >&2; exit 3", base);
expect(result.exitCode).toBe(3);
expect(result.output).toContain("oops");
});
it("runs in the requested cwd", async () => {
const result = await runCommand("pwd", { ...base, cwd: "/" });
expect(result.output.trim()).toBe("/");
});
it("kills the process tree on timeout", async () => {
const started = Date.now();
const result = await runCommand("sleep 5 & sleep 5", { ...base, timeoutMs: 200 });
expect(result.timedOut).toBe(true);
expect(Date.now() - started).toBeLessThan(2000);
});
it("stops promptly when aborted", async () => {
const controller = new AbortController();
const pending = runCommand("sleep 5", { ...base, signal: controller.signal });
setTimeout(() => controller.abort(), 100);
const result = await pending;
expect(result.aborted).toBe(true);
});
it("does not start when the signal is already aborted", async () => {
const result = await runCommand("sleep 5", { ...base, signal: AbortSignal.abort() });
expect(result.aborted).toBe(true);
});
it("truncates output past maxChars", async () => {
const result = await runCommand("head -c 5000 /dev/zero | tr '\\0' a", { ...base, maxChars: 100 });
expect(result.output).toHaveLength(100);
expect(result.truncated).toBe(true);
});
it("rejects when the cwd does not exist", async () => {
await expect(runCommand("ls", { ...base, cwd: "/definitely/not/here" })).rejects.toThrow();
});
it("hides the jev key from the child's environment", async () => {
process.env.JEV_API_KEY = "secret-test-key";
try {
const result = await runCommand("env", base);
expect(result.output).not.toContain("secret-test-key");
expect(scrubbedEnv({ JEV_API_KEY: "x", TYPESAFE_API_KEY: "y", KEEP: "z" })).toEqual({ KEEP: "z" });
} finally {
delete process.env.JEV_API_KEY;
}
});
});
tests / exec.test.ts
import * as os from "os";
import { describe, expect, it } from "vitest";
import { runCommand, scrubbedEnv } from "../src/exec";
const base = { cwd: os.tmpdir(), timeoutMs: 5000, maxChars: 20000 };
describe("runCommand", () => {
it("captures stdout and exit code 0", async () => {
const result = await runCommand("echo hello", base);
expect(result).toMatchObject({ output: "hello\n", exitCode: 0, timedOut: false, aborted: false, truncated: false });
});
it("supports pipes and && (real shell)", async () => {
const result = await runCommand("printf 'a\\nb\\n' | wc -l && echo done", base);
expect(result.output).toContain("2");
expect(result.output).toContain("done");
});
it("captures stderr and non-zero exit codes", async () => {
const result = await runCommand("echo oops >&2; exit 3", base);
expect(result.exitCode).toBe(3);
expect(result.output).toContain("oops");
});
it("runs in the requested cwd", async () => {
const result = await runCommand("pwd", { ...base, cwd: "/" });
expect(result.output.trim()).toBe("/");
});
it("kills the process tree on timeout", async () => {
const started = Date.now();
const result = await runCommand("sleep 5 & sleep 5", { ...base, timeoutMs: 200 });
expect(result.timedOut).toBe(true);
expect(Date.now() - started).toBeLessThan(2000);
});
it("stops promptly when aborted", async () => {
const controller = new AbortController();
const pending = runCommand("sleep 5", { ...base, signal: controller.signal });
setTimeout(() => controller.abort(), 100);
const result = await pending;
expect(result.aborted).toBe(true);
});
it("does not start when the signal is already aborted", async () => {
const result = await runCommand("sleep 5", { ...base, signal: AbortSignal.abort() });
expect(result.aborted).toBe(true);
});
it("truncates output past maxChars", async () => {
const result = await runCommand("head -c 5000 /dev/zero | tr '\\0' a", { ...base, maxChars: 100 });
expect(result.output).toHaveLength(100);
expect(result.truncated).toBe(true);
});
it("rejects when the cwd does not exist", async () => {
await expect(runCommand("ls", { ...base, cwd: "/definitely/not/here" })).rejects.toThrow();
});
it("hides the jev key from the child's environment", async () => {
process.env.JEV_API_KEY = "secret-test-key";
try {
const result = await runCommand("env", base);
expect(result.output).not.toContain("secret-test-key");
expect(scrubbedEnv({ JEV_API_KEY: "x", TYPESAFE_API_KEY: "y", KEEP: "z" })).toEqual({ KEEP: "z" });
} finally {
delete process.env.JEV_API_KEY;
}
});
});