bash-process-observation.test.ts 1.4 KB

123456789101112131415161718192021222324252627
  1. import { tmpdir } from "node:os";
  2. import { describe, expect, it, vi } from "vitest";
  3. import { createLocalBashOperations } from "../src/core/tools/bash.js";
  4. describe("local Bash process observations", () => {
  5. it("observes a real shell spawn without changing a nonzero exit", async () => {
  6. const onProcessEvent = vi.fn();
  7. const result = await createLocalBashOperations().exec("exit 7", tmpdir(), { onData: () => {}, onProcessEvent });
  8. expect(result.exitCode).toBe(7);
  9. expect(onProcessEvent).toHaveBeenCalledWith({ type: "spawned", processRole: "shell" });
  10. });
  11. it("does not fabricate a spawn before cancellation or cwd validation", async () => {
  12. const onProcessEvent = vi.fn();
  13. await expect(createLocalBashOperations().exec("exit 0", tmpdir(), {
  14. onData: () => {}, signal: AbortSignal.abort(), onProcessEvent,
  15. })).rejects.toThrow("aborted");
  16. expect(onProcessEvent).not.toHaveBeenCalled();
  17. });
  18. it("reports timeout from the timer and ignores throwing observers", async () => {
  19. const onProcessEvent = vi.fn(() => { throw new Error("observer failed"); });
  20. const command = process.platform === "win32" ? "Start-Sleep -Seconds 3" : "sleep 3";
  21. await expect(createLocalBashOperations().exec(command, tmpdir(), {
  22. onData: () => {}, timeout: 0.05, onProcessEvent,
  23. })).rejects.toThrow("timeout:");
  24. expect(onProcessEvent).toHaveBeenCalledWith({ type: "timeout", processRole: "shell" });
  25. });
  26. });