//! Integration tests for gpu-trace-perf run

use crate::GpuTracePerfRun;
#[allow(unused)]
use insta_cmd::assert_cmd_snapshot;

#[allow(unused)]
pub fn single_trace_test(filename: &str, samples: usize) {
    let mut run = GpuTracePerfRun::new();
    run.setup_run(samples);
    run.env_reproducible_outputs("before", 30.0, 2.0);
    run.env_reproducible_outputs("after", 40.0, 1.0);
    run.add_trace(filename);

    run.snapshot_output(&format!("run__{filename}"));
}

#[test]
pub fn run_bad_script() {
    let mut run = GpuTracePerfRun::new();
    run.setup_run(1);
    run.env_passthrough("before");
    run.env_script("after", "echo this script is so bad\nexit 1");
    run.add_trace("vkcube.rdc");

    run.snapshot_output("run_bad_script");
}

#[test]
pub fn run_no_traces() {
    let mut run = GpuTracePerfRun::new();
    run.setup_run(1);
    run.env_passthrough("before");
    run.env_passthrough("after");

    run.snapshot_output("run_no_traces");
}

#[test]
pub fn mock_simple() {
    single_trace_test("glxgears.mock-trace", 6);
}

#[test]
#[cfg(feature = "testgfxr")]
pub fn gfxr_simple() {
    single_trace_test("vkcube-10-frames.gfxr", 6);
}

#[test]
#[cfg(feature = "testrdc")]
pub fn rdoc_simple() {
    single_trace_test("vkcube.rdc", 6);
}

#[test]
#[cfg(feature = "testapitrace")]
pub fn apitrace_file_not_found() {
    single_trace_test("typoed.trace", 3);
}

#[test]
#[cfg(feature = "testgfxr")]
pub fn gfxr_file_not_found() {
    single_trace_test("typoed.gfxr", 3);
}

#[test]
#[cfg(feature = "testrdc")]
pub fn rdoc_file_not_found() {
    single_trace_test("typoed.rdc", 3);
}

// This one throws a backtrace that would be impossible to filter, but maybe we
// could make apitrace throw a nicer error if we had a better corrupted trace
// file.
#[ignore]
#[test]
#[cfg(feature = "testapitrace")]
pub fn apitrace_corrupted() {
    single_trace_test("corrupted.trace", 3);
}

#[test]
#[cfg(feature = "testgfxr")]
pub fn gfxr_corrupted() {
    single_trace_test("corrupted.gfxr", 3);
}

// This one intermittently segfaults, producing unreliable results.
#[allow(unused)]
#[cfg(feature = "testrdc")]
pub fn rdoc_corrupted() {
    single_trace_test("corrupted.rdc", 3);
}
