// E2E contract for the blind client->server->RCT synthetic media route.
//
// Scope: protect the black-box HEVC/audio route that generates media locally,
// sends bundled media to the server, and measures final RCT capture.
// Targets: scripts/manual/run_client_to_rct_transport_probe.sh and
// client/src/sync_probe/runner/bundled_transport.rs.
// Why: before deeper introspection, the product needs a stable blind test that
// proves the same path a real call uses can be measured end-to-end.

const CLIENT_RCT_SCRIPT: &str = include_str!(concat!(
    env!("CARGO_MANIFEST_DIR"),
    "/scripts/manual/run_client_to_rct_transport_probe.sh"
));
const BUNDLED_TRANSPORT: &str = include_str!(concat!(
    env!("CARGO_MANIFEST_DIR"),
    "/client/src/sync_probe/runner/bundled_transport.rs"
));
const SYNC_RUNNER: &str = include_str!(concat!(
    env!("CARGO_MANIFEST_DIR"),
    "/client/src/sync_probe/runner.rs"
));

#[test]
fn blind_route_uses_synthetic_bundled_media_and_final_rct_capture() {
    for marker in [
        "client-to-RCT bundled transport probe",
        "lesavka-sync-probe",
        "PROBE_EVENT_WIDTH_CODES=${PROBE_EVENT_WIDTH_CODES:-1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}",
        "stream_webcam_media",
        "UpstreamMediaBundle",
        "starting RCT UVC/UAC capture on ${TETHYS_HOST}",
        "analyzing RCT capture",
        "client-transport-timeline.json",
    ] {
        assert!(
            CLIENT_RCT_SCRIPT.contains(marker)
                || BUNDLED_TRANSPORT.contains(marker)
                || SYNC_RUNNER.contains(marker),
            "blind client->server->RCT route should preserve marker {marker}"
        );
    }
}

#[test]
fn blind_route_refuses_split_streams_and_requires_bundled_server_support() {
    for marker in [
        "server does not advertise bundled webcam media",
        "refusing to measure split upstream",
        "bundled_webcam_media",
    ] {
        assert!(
            BUNDLED_TRANSPORT.contains(marker) || SYNC_RUNNER.contains(marker),
            "blind route should preserve bundled-media guard {marker}"
        );
    }

    for forbidden in [
        ".stream_camera(Request::new(outbound))",
        ".stream_microphone(Request::new(outbound))",
    ] {
        assert!(
            !BUNDLED_TRANSPORT.contains(forbidden) && !SYNC_RUNNER.contains(forbidden),
            "blind route must not fall back to split-stream RPC {forbidden}"
        );
    }
}

#[test]
fn blind_route_has_formal_pass_fail_budgets_and_artifacts() {
    for marker in [
        "LESAVKA_CLIENT_RCT_MAX_AGE_MS=${LESAVKA_CLIENT_RCT_MAX_AGE_MS:-1000}",
        "LESAVKA_CLIENT_RCT_MIN_PAIRS=${LESAVKA_CLIENT_RCT_MIN_PAIRS:-13}",
        "LESAVKA_CLIENT_RCT_REQUIRE_SMOOTHNESS=${LESAVKA_CLIENT_RCT_REQUIRE_SMOOTHNESS:-0}",
        "client-rct-transport-summary.json",
        "client-rct-transport-summary.txt",
        "upstream-sync-samples.jsonl",
        "client-send-bundles.jsonl",
        "uvc-frame-meta-summary.json",
    ] {
        assert!(
            CLIENT_RCT_SCRIPT.contains(marker),
            "blind route should preserve budget/artifact marker {marker}"
        );
    }
}
