The AI-native screen recorder. Record your screen and talk; the people you share it with and your AI agents both get it.
Rust agent-readableai-agentsarecclaude-codecodexcursorlocal-firstmacosprivacyrustscreen-recorderscreen-recordingtauritranscriptiontypescriptwindows
import { chmodSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { delimiter, join } from "node:path";
import { afterEach, describe, expect, it } from "vitest";
import { checkCapture } from "../src/check.js";
import { createRunner, ffmpegPath, ffprobePath, findExecutable, realRunner } from "../src/proc.js";
const tempDirs: string[] = [];
afterEach(() => {
for (const dir of tempDirs.splice(0)) rmSync(dir, { recursive: true, force: true });
});
/** An empty folder, so no real PATH entry can answer a lookup. */
function tempDir(): string {
const dir = mkdtempSync(join(tmpdir(), "clipy-proc-"));
tempDirs.push(dir);
return dir;
}
/** A fake executable in `dir`, or a plain file with `mode`, and its path. */
function stub(dir: string, name: string, mode = 0o755): string {
const path = join(dir, name);
writeFileSync(path, "#!/bin/sh\nexit 0\n");
chmodSync(path, mode);
return path;
}
describe("findExecutable", () => {
it("finds a name on PATH and returns null when it is missing", () => {
const dir = tempDir();
const ffmpeg = stub(dir, "ffmpeg");
expect(findExecutable("ffmpeg", "linux", { PATH: dir })).toBe(ffmpeg);
expect(findExecutable("ffprobe", "linux", { PATH: dir })).toBeNull();
});
it("searches every PATH entry, ignoring empty ones", () => {
const empty = tempDir();
const dir = tempDir();
const tool = stub(dir, "tool");
// proc.ts splits PATH with the separator of the platform running the tests.
expect(findExecutable("tool", "linux", { PATH: [empty, dir].join(delimiter) })).toBe(tool);
expect(findExecutable("tool", "linux", { PATH: [empty, "", dir, ""].join(delimiter) })).toBe(tool);
});
it("takes an explicit path, with slashes, instead of searching PATH", () => {
const dir = tempDir();
stub(dir, "tool");
// Forward slashes work on every OS and are what this branch looks for.
const explicit = join(dir, "tool").replace(/\\/g, "/");
expect(findExecutable(explicit, "linux", { PATH: "" })).toBe(explicit);
expect(findExecutable("/definitely/not/here/tool", "linux", { PATH: dir })).toBeNull();
});
it.skipIf(process.platform === "win32")("skips a file that is not executable on Linux and macOS", () => {
// Windows has no execute bit, so this can only be checked on a POSIX host.
for (const platform of ["linux", "darwin"] as const) {
const dir = tempDir();
stub(dir, "tool", 0o644);
expect(findExecutable("tool", platform, { PATH: dir })).toBeNull();
}
});
it("skips a folder that carries the program's name", () => {
const dir = tempDir();
mkdirSync(join(dir, "tool"));
expect(findExecutable("tool", "linux", { PATH: dir })).toBeNull();
});
});
describe("findExecutable on Windows", () => {
it("resolves .exe from PATH with the default PATHEXT", () => {
const dir = tempDir();
const ffmpeg = stub(dir, "ffmpeg.EXE");
expect(findExecutable("ffmpeg", "win32", { PATH: dir })).toBe(ffmpeg);
});
it("resolves FFmpeg and FFprobe from a quoted Windows PATH entry", () => {
const dir = join(tempDir(), "Program Files");
mkdirSync(dir);
const ffmpeg = stub(dir, "ffmpeg.exe");
const ffprobe = stub(dir, "ffprobe.exe");
const env = { PATH: `"";${tempDir()};"${dir}"`, PATHEXT: ".exe" };
expect(ffmpegPath("win32", env)).toBe(ffmpeg);
expect(ffprobePath("win32", env)).toBe(ffprobe);
});
it("honours PATHEXT and does not double an extension already there", () => {
const withCmd = tempDir();
const cmd = stub(withCmd, "tool.CMD");
expect(findExecutable("tool", "win32", { PATH: withCmd, PATHEXT: ".CMD" })).toBe(cmd);
const withExe = tempDir();
const exe = stub(withExe, "ffmpeg.exe");
expect(findExecutable("ffmpeg.exe", "win32", { PATH: withExe })).toBe(exe);
});
it("reads the Path spelling of the variable", () => {
const dir = tempDir();
const tool = stub(dir, "tool.EXE");
expect(findExecutable("tool", "win32", { Path: dir })).toBe(tool);
});
it("resolves PATHEXT for an explicit path without an extension", () => {
const dir = tempDir();
const ffmpeg = stub(dir, "ffmpeg.EXE");
const ffprobe = stub(dir, "ffprobe.EXE");
const cmd = stub(dir, "tool.CMD");
const explicit = join(dir, "ffmpeg");
expect(findExecutable(explicit, "win32", { PATH: "" })).toBe(ffmpeg);
expect(findExecutable(explicit.replace(/\\/g, "/"), "win32", { PATH: "" })).toBe(
`${explicit.replace(/\\/g, "/")}.EXE`,
);
expect(findExecutable(join(dir, "tool"), "win32", { PATH: "", PATHEXT: ".CMD" })).toBe(cmd);
expect(findExecutable(join(dir, "missing"), "win32", { PATH: "" })).toBeNull();
expect(ffmpegPath("win32", { PATH: "", CLIPY_FFMPEG: explicit })).toBe(ffmpeg);
expect(ffprobePath("win32", { PATH: "", CLIPY_FFMPEG: explicit })).toBe(ffprobe);
});
});
describe("realRunner", () => {
it("answers a null status when the command cannot be spawned at all", async () => {
// A NUL byte makes execFile throw on every OS.
await expect(realRunner(process.execPath, ["\0"])).resolves.toEqual({ status: null, stdout: "", stderr: "" });
});
it.skipIf(process.platform !== "win32")("answers a null status for a Windows batch file", async () => {
const cmd = stub(tempDir(), "ffmpeg.cmd");
expect((await realRunner(cmd, [])).status).toBeNull();
});
});
describe("ffmpegPath", () => {
it("prefers CLIPY_FFMPEG over a copy on PATH", () => {
const onPath = tempDir();
stub(onPath, "ffmpeg");
const chosen = stub(tempDir(), "mine-ffmpeg").replace(/\\/g, "/");
expect(ffmpegPath("linux", { PATH: onPath, CLIPY_FFMPEG: chosen })).toBe(chosen);
});
it("finds a bare CLIPY_FFMPEG on PATH", () => {
const dir = tempDir();
const ffmpeg = stub(dir, "ffmpeg-custom");
expect(ffmpegPath("linux", { PATH: dir, CLIPY_FFMPEG: "ffmpeg-custom" })).toBe(ffmpeg);
});
it("returns null for a CLIPY_FFMPEG that does not exist, rather than falling back to PATH", () => {
const onPath = tempDir();
stub(onPath, "ffmpeg");
expect(ffmpegPath("linux", { PATH: onPath, CLIPY_FFMPEG: "/nope/ffmpeg" })).toBeNull();
});
it("treats a blank CLIPY_FFMPEG as unset and ignores spaces around a path", () => {
const onPath = tempDir();
const ffmpeg = stub(onPath, "ffmpeg");
for (const blank of ["", " ", "\n"]) expect(ffmpegPath("linux", { PATH: onPath, CLIPY_FFMPEG: blank })).toBe(ffmpeg);
const chosen = stub(tempDir(), "mine-ffmpeg").replace(/\\/g, "/");
expect(ffmpegPath("linux", { PATH: onPath, CLIPY_FFMPEG: ` ${chosen}\n` })).toBe(chosen);
});
it("falls back to ffmpeg on PATH", () => {
const dir = tempDir();
const ffmpeg = stub(dir, "ffmpeg");
expect(ffmpegPath("linux", { PATH: dir })).toBe(ffmpeg);
expect(ffmpegPath("linux", { PATH: tempDir() })).toBeNull();
});
});
describe("Windows batch files", () => {
const PATHEXT = ".COM;.EXE;.BAT;.CMD";
it("keeps looking for an ffmpeg.exe later on PATH", () => {
for (const shim of ["ffmpeg.CMD", "ffmpeg.BAT"]) {
const shims = tempDir();
stub(shims, shim);
const dir = tempDir();
const exe = stub(dir, "ffmpeg.EXE");
const PATH = [shims, dir].join(";");
expect(ffmpegPath("win32", { PATH, PATHEXT })).toBe(exe);
expect(ffmpegPath("win32", { PATH, PATHEXT, CLIPY_FFMPEG: "ffmpeg" })).toBe(exe);
expect(ffmpegPath("win32", { PATH: shims, PATHEXT })).toBeNull();
}
});
it("does the same for ffprobe", () => {
const shims = tempDir();
stub(shims, "ffprobe.CMD");
const dir = tempDir();
const exe = stub(dir, "ffprobe.EXE");
const PATH = [shims, dir].join(";");
expect(ffprobePath("win32", { PATH, PATHEXT })).toBe(exe);
expect(ffprobePath("win32", { PATH, PATHEXT, CLIPY_FFPROBE: "ffprobe" })).toBe(exe);
});
it.skipIf(process.platform !== "win32")("reports a CLIPY_FFMPEG batch file as a problem instead of throwing", async () => {
const cmd = stub(tempDir(), "ffmpeg.cmd");
const check = await checkCapture("win32", { env: { ...process.env, CLIPY_FFMPEG: cmd } });
expect(check.ok).toBe(false);
expect(check.problems).toEqual([`ffmpeg at ${cmd} could not be run`]);
});
});
describe("ffprobePath", () => {
it("prefers CLIPY_FFPROBE over everything else", () => {
const onPath = tempDir();
stub(onPath, "ffmpeg");
stub(onPath, "ffprobe");
const chosen = stub(tempDir(), "mine-ffprobe").replace(/\\/g, "/");
expect(ffprobePath("linux", { PATH: onPath, CLIPY_FFPROBE: chosen })).toBe(chosen);
expect(ffprobePath("linux", { PATH: onPath, CLIPY_FFPROBE: "/nope/ffprobe" })).toBeNull();
});
it("treats a blank CLIPY_FFPROBE as unset and ignores spaces around a path", () => {
const onPath = tempDir();
const ffprobe = stub(onPath, "ffprobe");
expect(ffprobePath("linux", { PATH: onPath, CLIPY_FFPROBE: " " })).toBe(ffprobe);
const chosen = stub(tempDir(), "mine-ffprobe").replace(/\\/g, "/");
expect(ffprobePath("linux", { PATH: onPath, CLIPY_FFPROBE: ` ${chosen}\n` })).toBe(chosen);
});
it("uses the ffprobe next to the ffmpeg it found, before the one on PATH", () => {
const elsewhere = tempDir();
const onPath = stub(elsewhere, "ffprobe");
const dir = tempDir();
stub(dir, "ffmpeg");
const sibling = stub(dir, "ffprobe");
expect(ffprobePath("linux", { PATH: [dir, elsewhere].join(delimiter) })).toBe(sibling);
const lone = tempDir();
stub(lone, "ffmpeg");
expect(ffprobePath("linux", { PATH: [lone, elsewhere].join(delimiter) })).toBe(onPath);
});
it("falls back to ffprobe on PATH when there is no ffmpeg at all", () => {
const dir = tempDir();
const ffprobe = stub(dir, "ffprobe");
expect(ffprobePath("linux", { PATH: dir })).toBe(ffprobe);
expect(ffprobePath("linux", { PATH: tempDir() })).toBeNull();
});
});
describe("createRunner", () => {
it.skipIf(process.platform === "win32")("returns null status on timeout and keeps numeric status on normal non-zero exit", async () => {
const dir = tempDir();
const hung = join(dir, "hung");
writeFileSync(hung, "#!/bin/sh\nexec sleep 10\n");
chmodSync(hung, 0o755);
expect((await createRunner(50)(hung, [])).status).toBeNull();
expect((await createRunner(1000)(join(dir, "missing"), [])).status).toBeNull();
const fail = join(dir, "fail");
writeFileSync(fail, "#!/bin/sh\necho err >&2\nexit 1\n");
chmodSync(fail, 0o755);
expect(await createRunner(1000)(fail, [])).toEqual({ status: 1, stdout: "", stderr: "err\n" });
});
});