// Contract tests for the deterministic performance gate.
//
// Scope: inspect CI gate scripts for latency-sensitive Lesavka checks.
// Targets: `scripts/ci/performance_gate.sh`, `scripts/ci/platform_quality_gate.sh`.
// Why: A/V sync and remote-control tightness are product behavior, so the main
// quality gate must fail before release when their contracts regress.

const PERFORMANCE_GATE: &str = include_str!(concat!(
    env!("CARGO_MANIFEST_DIR"),
    "/scripts/ci/performance_gate.sh"
));
const PLATFORM_GATE: &str = include_str!(concat!(
    env!("CARGO_MANIFEST_DIR"),
    "/scripts/ci/platform_quality_gate.sh"
));
const MEDIA_GATE: &str = include_str!(concat!(
    env!("CARGO_MANIFEST_DIR"),
    "/scripts/ci/media_reliability_gate.sh"
));

#[test]
fn performance_gate_tracks_av_and_interaction_latency_contracts() {
    for expected in [
        "client_uplink_performance_contract",
        "client_uplink_freshness_contract",
        "client_log_noise_contract",
        "video_downstream_feed_contract",
        "client_app_include_contract",
        "platform_quality_gate_checks_total",
        "check=\"performance\"",
        "platform-quality-ci",
    ] {
        assert!(
            PERFORMANCE_GATE.contains(expected),
            "performance gate should include {expected}"
        );
    }
}

#[test]
fn platform_gate_runs_performance_before_media_reliability() {
    let performance = PLATFORM_GATE
        .find("scripts/ci/performance_gate.sh")
        .expect("platform gate should run performance gate");
    let media = PLATFORM_GATE
        .find("scripts/ci/media_reliability_gate.sh")
        .expect("platform gate should run media reliability gate");
    assert!(
        performance < media,
        "performance regressions should fail before broader media packaging checks"
    );
}

#[test]
fn media_reliability_gate_reports_direct_sync_probe_evidence() {
    for expected in [
        "LESAVKA_SYNC_PROBE_REPORT_JSON",
        "LESAVKA_SYNC_PROBE_REPORT_DIR",
        "LESAVKA_REQUIRE_SYNC_PROBE",
        "direct_upstream_av_sync_probe",
        "check=\"sync_probe\"",
        "lesavka_sync_probe_skew_ms",
        "lesavka_sync_probe_events_total",
        "lesavka_sync_probe_verdict_info",
        "gate-status.txt",
    ] {
        assert!(
            MEDIA_GATE.contains(expected),
            "media reliability gate should include {expected}"
        );
    }
}
