use std::process::Command;

const ANALYZER: &str = include_str!(concat!(
    env!("CARGO_MANIFEST_DIR"),
    "/scripts/manual/analyze_uvc_mjpeg_integrity.py"
));
const KERNEL_WATCHER: &str = include_str!(concat!(
    env!("CARGO_MANIFEST_DIR"),
    "/scripts/daemon/lesavka-uvc-kernel-watch.py"
));

#[test]
fn uvc_integrity_analyzer_has_machine_readable_failure_classes() {
    for expected in [
        "lesavka.uvc-mjpeg-integrity.v1",
        "intact",
        "truncated",
        "spliced",
        "stale_repeat",
        "idle_black",
        "APP4_MAGIC",
        "crc_ok",
        "report.json",
        "frames.jsonl",
    ] {
        assert!(
            ANALYZER.contains(expected),
            "missing analyzer contract {expected}"
        );
    }
}

#[test]
fn uvc_integrity_analyzer_self_test_exercises_all_classes() {
    let script = concat!(
        env!("CARGO_MANIFEST_DIR"),
        "/scripts/manual/analyze_uvc_mjpeg_integrity.py"
    );
    let output = Command::new("python3")
        .args([script, "--self-test"])
        .output()
        .expect("run analyzer self-test");
    assert!(
        output.status.success(),
        "{}",
        String::from_utf8_lossy(&output.stderr)
    );
    assert!(String::from_utf8_lossy(&output.stdout).contains("self-test: pass"));
}

#[test]
fn uvc_kernel_watcher_has_bounded_machine_readable_counters() {
    for expected in [
        "lesavka.uvc-kernel-watch.v1",
        "dwc2_errors",
        "uvc_errors",
        "udc_errors",
        "urb_errors",
        "usb_resets",
        "deque(maxlen=40)",
    ] {
        assert!(
            KERNEL_WATCHER.contains(expected),
            "missing watcher contract {expected}"
        );
    }

    let script = concat!(
        env!("CARGO_MANIFEST_DIR"),
        "/scripts/daemon/lesavka-uvc-kernel-watch.py"
    );
    let output = Command::new("python3")
        .args([script, "--self-test"])
        .output()
        .expect("run kernel watcher self-test");
    assert!(
        output.status.success(),
        "{}",
        String::from_utf8_lossy(&output.stderr)
    );
    assert!(String::from_utf8_lossy(&output.stdout).contains("self-test: pass"));
}
