Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

2.8 KB
Raw Blame
import { describe, expect, it } from "vitest";

import { shellTimeoutHint } from "./shell-timeout-hint.ts";

// Claude Code 2.1.296's error text for a Bash call killed at its limit.
const TIMED_OUT = "Exit code 143\nCommand timed out after 2m 0s";

describe("shellTimeoutHint", () => {
  it("tells a timed-out media command to rerun with a longer timeout in the foreground", () => {
    const hint = shellTimeoutHint("Bash", { command: "ffmpeg -i in.mp3 -ar 16000 out.wav" }, TIMED_OUT)!;
    expect(hint).toContain("Do not rerun it the same way");
    expect(hint).toContain("`timeout`");
    expect(hint).toContain("up to 86400000, which is 1440 minutes, unless a lower limit is set here");
    expect(hint).toContain("foreground");
    expect(hint).not.toContain("nohup");
    expect(hint).not.toContain("detached");
    expect(hint).not.toContain("mdfind");
  });

  it("says the outcome of a timed-out command is unknown and must be checked before it is run again", () => {
    const hint = shellTimeoutHint("Bash", { command: "curl -sS -X PUT https://api.example.com/repos/o/r/pulls/7/merge", timeout: 60000 }, "Exit code 143\nCommand timed out after 1m 0s")!;
    expect(hint).toContain("outcome is UNKNOWN");
    expect(hint).toContain("may already have taken effect");
    expect(hint).toContain("Check the real state first");
    expect(hint).toContain("not a failure report");
    // The check comes before the advice about a longer limit, which is for the run after it.
    expect(hint.indexOf("Check the real state first")).toBeLessThan(hint.indexOf("larger `timeout`"));
  });

  it("does not warn about outcomes for a whole-disk find, which changes nothing", () => {
    expect(shellTimeoutHint("Bash", { command: "find / -name model.bin" }, TIMED_OUT)).not.toContain("UNKNOWN");
  });

  it("points a timed-out whole-disk find at mdfind or a scoped folder", () => {
    for (const command of ["find / -iname '*.bin'", "ls; find / -name model.bin", "sudo find / -name x", "(find / -type f)"]) {
      const hint = shellTimeoutHint("Bash", { command }, TIMED_OUT)!;
      expect(hint, command).toContain("mdfind -name");
      expect(hint, command).toContain("-maxdepth");
    }
  });

  it("does not mistake a scoped find for a disk scan", () => {
    for (const command of ["find /tmp/work -name '*.wav'", "find . -name x", "find ~/Music -name x"]) {
      expect(shellTimeoutHint("Bash", { command }, TIMED_OUT), command).not.toContain("mdfind");
    }
  });

  it("stays quiet for other failures and other tools", () => {
    expect(shellTimeoutHint("Bash", { command: "false" }, "Exit code 1")).toBeNull();
    expect(shellTimeoutHint("Read", { file_path: "a" }, TIMED_OUT)).toBeNull();
    expect(shellTimeoutHint("Bash", { command: "x" }, undefined)).toBeNull();
    expect(shellTimeoutHint("Bash", undefined, TIMED_OUT)).toContain("`timeout`");
  });
});