Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

10.4 KB
Raw Blame
use anyhow::Result;
use clap::{Parser, Subcommand};
use std::path::PathBuf;
use tracing_subscriber::{EnvFilter, fmt, prelude::*};

mod config;
mod discovery;
mod matrix;
mod results;
mod suites;

use config::TestConfig;
use discovery::DiscoveredHardware;
use matrix::{CompatMatrixRunner, MatrixRunner};
#[allow(unused_imports)]
use results::{ResultsSummary, TestResults};

#[derive(Parser)]
#[command(name = "cap-test")]
#[command(about = "Unified testing harness for Cap", long_about = None)]
struct Cli {
    #[command(subcommand)]
    command: Commands,

    #[arg(long, global = true, default_value = "info")]
    log_level: String,
}

#[derive(Subcommand)]
enum Commands {
    Discover {
        #[arg(long)]
        json: bool,
    },

    Matrix {
        #[arg(short, long)]
        config: Option<PathBuf>,

        #[arg(long)]
        quick: bool,

        #[arg(long)]
        exhaustive: bool,

        #[arg(short, long)]
        output: Option<PathBuf>,
    },

    Suite {
        name: String,

        #[arg(short, long)]
        output: Option<PathBuf>,

        #[arg(long, default_value = "10")]
        duration: u64,

        #[arg(long)]
        recording_path: Option<PathBuf>,

        #[arg(long)]
        gate: bool,
    },

    Synthetic {
        #[arg(short, long)]
        config: Option<PathBuf>,

        #[arg(short, long)]
        output: Option<PathBuf>,
    },

    Benchmark {
        #[arg(long, default_value = "30")]
        duration: u64,

        #[arg(long, default_value = "5")]
        warmup: u64,

        #[arg(short, long)]
        output: Option<PathBuf>,
    },

    CompatMatrix {
        #[arg(short, long)]
        config: Option<PathBuf>,

        #[arg(short, long)]
        output: Option<PathBuf>,

        #[arg(long)]
        interactive: bool,

        #[arg(long)]
        gate: bool,
    },

    Validate {
        path: PathBuf,

        #[arg(long)]
        json: bool,
    },

    Compare {
        current: PathBuf,
        baseline: PathBuf,
    },

    Report {
        results: PathBuf,
    },

    RecordHarness {
        #[arg(long)]
        output: PathBuf,

        #[arg(long)]
        ready_file: Option<PathBuf>,

        #[arg(long)]
        display: Option<String>,

        #[arg(long, default_value = "30")]
        fps: u32,

        #[arg(long, default_value = "600")]
        max_duration: u64,

        #[arg(long, default_value = "true")]
        include_mic: bool,

        #[arg(long, default_value = "false")]
        include_system_audio: bool,
    },
}

#[tokio::main]
async fn main() -> Result<()> {
    let cli = Cli::parse();

    tracing_subscriber::registry()
        .with(fmt::layer())
        .with(EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new(&cli.log_level)))
        .init();

    match cli.command {
        Commands::Discover { json } => {
            cmd_discover(json).await?;
        }

        Commands::Matrix {
            config,
            quick,
            exhaustive,
            output,
        } => {
            cmd_matrix(config, quick, exhaustive, output).await?;
        }

        Commands::Suite {
            name,
            output,
            duration,
            recording_path,
            gate,
        } => {
            cmd_suite(&name, duration, recording_path, output, gate).await?;
        }

        Commands::Synthetic { config, output } => {
            cmd_synthetic(config, output).await?;
        }

        Commands::Benchmark {
            duration,
            warmup,
            output,
        } => {
            cmd_benchmark(duration, warmup, output).await?;
        }

        Commands::CompatMatrix {
            config,
            output,
            interactive,
            gate,
        } => {
            let passed = cmd_compat_matrix(config, output, interactive).await?;
            if gate && !passed {
                std::process::exit(1);
            }
        }

        Commands::Validate { path, json } => {
            cmd_validate(path, json).await?;
        }

        Commands::Compare { current, baseline } => {
            cmd_compare(current, baseline).await?;
        }

        Commands::Report { results } => {
            cmd_report(results).await?;
        }

        Commands::RecordHarness {
            output,
            ready_file,
            display,
            fps,
            max_duration,
            include_mic,
            include_system_audio,
        } => {
            suites::kill9::run_record_harness(suites::kill9::RecordHarnessArgs {
                output,
                ready_file,
                display_id: display,
                fps,
                max_duration_secs: max_duration,
                include_mic,
                include_system_audio,
            })
            .await?;
        }
    }

    Ok(())
}

async fn cmd_discover(json_output: bool) -> Result<()> {
    let hardware = DiscoveredHardware::discover().await?;

    if json_output {
        println!("{}", serde_json::to_string_pretty(&hardware)?);
    } else {
        hardware.print_summary();
    }

    Ok(())
}

async fn cmd_matrix(
    config_path: Option<PathBuf>,
    quick: bool,
    exhaustive: bool,
    output: Option<PathBuf>,
) -> Result<()> {
    let config = if quick {
        TestConfig::quick()
    } else if exhaustive {
        TestConfig::exhaustive()
    } else if let Some(path) = config_path {
        TestConfig::load(&path)?
    } else {
        TestConfig::standard()
    };

    let hardware = DiscoveredHardware::discover().await?;
    let runner = MatrixRunner::new(config, hardware);
    let results = runner.run().await?;

    results.print_summary();

    if let Some(path) = output {
        results.save_json(&path)?;
        println!("\nResults saved to: {}", path.display());
    }

    Ok(())
}

async fn cmd_suite(
    name: &str,
    duration: u64,
    recording_path: Option<PathBuf>,
    output: Option<PathBuf>,
    gate: bool,
) -> Result<()> {
    let hardware = DiscoveredHardware::discover().await?;

    let results = match name {
        "recording" => suites::run_recording_suite(&hardware, duration).await?,
        "encoding" => suites::run_encoding_suite(&hardware, duration).await?,
        "playback" => suites::run_playback_suite(&hardware, duration).await?,
        "performance" => {
            let recording_path = resolve_recording_path(recording_path)?;
            suites::run_performance_suite(&hardware, &recording_path, duration).await?
        }
        "sync" => suites::run_sync_suite(&hardware, duration).await?,
        "av-alignment" => suites::run_av_alignment_suite(&hardware, duration).await?,
        "av-step" => suites::run_av_step_suite(&hardware, duration).await?,
        "av-step-instant" => suites::run_av_step_instant_suite(&hardware, duration).await?,
        "drift" => suites::run_drift_suite(&hardware, duration).await?,
        "kill9-crash-test" | "kill9" => suites::run_kill9_crash_suite(&hardware, duration).await?,
        _ => {
            anyhow::bail!(
                "Unknown suite: {}. Available: recording, encoding, playback, performance, sync, av-alignment, av-step, av-step-instant, drift, kill9-crash-test",
                name
            );
        }
    };

    results.print_summary();

    if let Some(path) = output {
        results.save_json(&path)?;
    }

    if gate && (results.summary.failed > 0 || results.summary.errors > 0) {
        anyhow::bail!(
            "Suite gate failed: {} failed, {} errors",
            results.summary.failed,
            results.summary.errors
        );
    }

    Ok(())
}

fn resolve_recording_path(explicit: Option<PathBuf>) -> Result<PathBuf> {
    if let Some(path) = explicit {
        return Ok(path);
    }

    if let Some(path) = std::env::var_os("CAP_TEST_RECORDING_PATH") {
        return Ok(PathBuf::from(path));
    }

    let candidates = [
        PathBuf::from("performance-fixtures/reference-recording.cap"),
        PathBuf::from("crates/cap-test/fixtures/reference-recording.cap"),
    ];

    for candidate in candidates {
        if candidate.exists() {
            return Ok(candidate);
        }
    }

    anyhow::bail!(
        "Performance suite requires a recording fixture. Pass --recording-path, set CAP_TEST_RECORDING_PATH, or place the fixture at performance-fixtures/reference-recording.cap"
    )
}

async fn cmd_synthetic(config_path: Option<PathBuf>, output: Option<PathBuf>) -> Result<()> {
    let config = if let Some(path) = config_path {
        TestConfig::load(&path)?
    } else {
        TestConfig::synthetic()
    };

    let runner = MatrixRunner::new_synthetic(config);
    let results = runner.run().await?;

    results.print_summary();

    if let Some(path) = output {
        results.save_json(&path)?;
    }

    Ok(())
}

async fn cmd_benchmark(duration: u64, warmup: u64, output: Option<PathBuf>) -> Result<()> {
    let hardware = DiscoveredHardware::discover().await?;
    let results = suites::run_benchmark(&hardware, duration, warmup).await?;

    results.print_summary();

    if let Some(path) = output {
        results.save_json(&path)?;
    }

    Ok(())
}

async fn cmd_compat_matrix(
    config_path: Option<PathBuf>,
    output: Option<PathBuf>,
    interactive: bool,
) -> Result<bool> {
    let config = if let Some(path) = config_path {
        TestConfig::load(&path)?
    } else {
        TestConfig::compatibility()
    };

    let hardware = DiscoveredHardware::discover().await?;
    let runner = CompatMatrixRunner::new(config, hardware, interactive);
    let report = runner.run().await?;

    report.print_summary();

    if let Some(path) = output {
        report.save_json(&path)?;
        println!("Compatibility report saved to: {}", path.display());
    }

    Ok(report.release_gate_passed)
}

async fn cmd_validate(path: PathBuf, json_output: bool) -> Result<()> {
    let validation = suites::validate_recording(&path).await?;

    if json_output {
        println!("{}", serde_json::to_string_pretty(&validation)?);
    } else {
        validation.print_summary();
    }

    Ok(())
}

async fn cmd_compare(current: PathBuf, baseline: PathBuf) -> Result<()> {
    let current_results = TestResults::load(&current)?;
    let baseline_results = TestResults::load(&baseline)?;

    let comparison = results::compare(&current_results, &baseline_results);
    comparison.print_summary();

    Ok(())
}

async fn cmd_report(results_path: PathBuf) -> Result<()> {
    let results = TestResults::load(&results_path)?;
    results.print_detailed_report();

    Ok(())
}