Skip to content

armgit / clipy

Public Imported from GitHub
0

The AI-native screen recorder. Record your screen and talk; the people you share it with and your AI agents both get it.

3.9 KB
Raw Blame
import { describe, expect, it } from "vitest";
import { resolveLinuxAudio } from "../src/capture.js";
import type { Runner, RunResult } from "../src/proc.js";

const PULSE_DEVICES = " DE alsa  ALSA\n DE pulse PulseAudio\n";
const ALSA_ONLY_DEVICES = " DE alsa  ALSA\n";

function fakeRunner(answers: Record<string, Partial<RunResult>>): Runner {
  return async (cmd, args) => {
    const full = `${cmd} ${args.join(" ")}`;
    const key = Object.keys(answers).find((k) => full.includes(k));
    if (!key) return { status: null, stdout: "", stderr: "" };
    return { status: 0, stdout: "", stderr: "", ...answers[key] };
  };
}

const PACTL_LIST = [
  "0\talsa_output.pci.analog-stereo.monitor\tPipeWire\tfloat32le 2ch 48000Hz\tIDLE",
  "1\thdmi_out.monitor\tPipeWire\tfloat32le 2ch 48000Hz\tSUSPENDED",
  "2\talsa_input.pci.analog-stereo\tPipeWire\tfloat32le 2ch 48000Hz\tRUNNING",
].join("\n");

describe("resolveLinuxAudio", () => {
  it("resolves default systemAudio and combines with microphone", async () => {
    const res = await resolveLinuxAudio(
      { microphone: true, systemAudio: true },
      PULSE_DEVICES,
      fakeRunner({ "get-default-sink": { stdout: "alsa_output.pci.analog-stereo\n" } }),
    );
    expect(res).toEqual({
      audio: { backend: "pulse", device: "default" },
      systemAudio: { backend: "pulse", device: "alsa_output.pci.analog-stereo.monitor" },
      warnings: [],
    });
  });

  it("appends .monitor when a sink name is passed, and keeps an existing .monitor suffix", async () => {
    const run = fakeRunner({ "list short sources": { stdout: PACTL_LIST } });
    const fromSink = await resolveLinuxAudio({ systemAudio: "hdmi_out" }, PULSE_DEVICES, run);
    expect(fromSink).toEqual({
      audio: undefined,
      systemAudio: { backend: "pulse", device: "hdmi_out.monitor" },
      warnings: [],
    });
    const fromMonitor = await resolveLinuxAudio({ systemAudio: "hdmi_out.monitor" }, PULSE_DEVICES, run);
    expect(fromMonitor.systemAudio).toEqual({ backend: "pulse", device: "hdmi_out.monitor" });
    expect(fromMonitor.warnings).toEqual([]);
  });

  it("warns and continues without systemAudio when ffmpeg has no pulse input", async () => {
    const res = await resolveLinuxAudio({ microphone: true, systemAudio: true }, ALSA_ONLY_DEVICES, fakeRunner({}));
    expect(res.audio).toEqual({ backend: "alsa", device: "default" });
    expect(res.systemAudio).toBeUndefined();
    expect(res.warnings).toEqual(["this ffmpeg has no pulse input; recording without system audio"]);
  });

  it("warns and continues without systemAudio when pactl is missing or no default sink exists", async () => {
    const missing = await resolveLinuxAudio({ systemAudio: true }, PULSE_DEVICES, fakeRunner({}));
    expect(missing.systemAudio).toBeUndefined();
    expect(missing.warnings).toHaveLength(1);
    expect(missing.warnings[0]).toMatch(/pactl/i);

    const empty = await resolveLinuxAudio(
      { systemAudio: true },
      PULSE_DEVICES,
      fakeRunner({ "get-default-sink": { stdout: "" }, "pactl info": { stdout: "Server Name: pipewire\n" } }),
    );
    expect(empty.systemAudio).toBeUndefined();
    expect(empty.warnings).toHaveLength(1);
  });

  it("warns and continues without systemAudio when a named monitor is missing or pactl is absent", async () => {
    const notFound = await resolveLinuxAudio(
      { systemAudio: "nonexistent_sink" },
      PULSE_DEVICES,
      fakeRunner({ "list short sources": { stdout: PACTL_LIST } }),
    );
    expect(notFound.systemAudio).toBeUndefined();
    expect(notFound.warnings).toEqual(['system audio monitor "nonexistent_sink.monitor" was not found; recording without system audio']);

    const noPactl = await resolveLinuxAudio({ systemAudio: "hdmi_out" }, PULSE_DEVICES, fakeRunner({}));
    expect(noPactl.systemAudio).toBeUndefined();
    expect(noPactl.warnings).toEqual(["pactl was not found; recording without system audio"]);
  });
});