// UI contract for upstream webcam transport truth.
//
// Scope: source-level assertions around the HEVC/MJPEG selector and status
// chips without opening a GTK session.
// Targets: `client/src/launcher/state/*`,
// `client/src/launcher/ui_components/build_device_controls.rs`, and
// `client/src/launcher/ui_runtime/status_details.rs`.
// Why: the launcher must not show a decorative codec selector; the selected
// transport has calibrated server timing consequences and must match runtime.

const SELECTION_MODELS_SRC: &str = include_str!(concat!(
    env!("CARGO_MANIFEST_DIR"),
    "/client/src/launcher/state/selection_models.rs"
));
const LAUNCHER_MOD_SRC: &str = include_str!(concat!(
    env!("CARGO_MANIFEST_DIR"),
    "/client/src/launcher/mod.rs"
));
const BUILD_DEVICE_CONTROLS_SRC: &str = include_str!(concat!(
    env!("CARGO_MANIFEST_DIR"),
    "/client/src/launcher/ui_components/build_device_controls.rs"
));
const STATUS_REFRESH_SRC: &str = include_str!(concat!(
    env!("CARGO_MANIFEST_DIR"),
    "/client/src/launcher/ui_runtime/status_refresh.rs"
));
const STATUS_DETAILS_SRC: &str = include_str!(concat!(
    env!("CARGO_MANIFEST_DIR"),
    "/client/src/launcher/ui_runtime/status_details.rs"
));
const STAGE_DEVICE_BINDINGS_SRC: &str = include_str!(concat!(
    env!("CARGO_MANIFEST_DIR"),
    "/client/src/launcher/ui/stage_device_bindings.rs"
));

#[test]
fn webcam_transport_selector_exposes_real_hevc_and_mjpeg_choices() {
    for marker in [
        "pub enum WebcamTransport",
        "#[default]\n    Mjpeg",
        "Self::Hevc => \"hevc\"",
        "Self::Mjpeg => \"mjpeg\"",
        "Self::Hevc => \"HEVC\"",
        "Self::Mjpeg => \"MJPEG\"",
        "LESAVKA_CAM_CODEC",
        "state.effective_webcam_transport().env_value().to_string()",
    ] {
        assert!(
            SELECTION_MODELS_SRC.contains(marker) || LAUNCHER_MOD_SRC.contains(marker),
            "webcam transport contract should preserve marker {marker}"
        );
    }

    for marker in [
        "for transport in [WebcamTransport::Mjpeg, WebcamTransport::Hevc]",
        "webcam_transport_combo.append(Some(transport.as_id()), transport.label());",
        "webcam_transport_combo.set_active_id(Some(state.effective_webcam_transport().as_id()));",
        "webcam_transport_combo.set_sensitive(true);",
        "MJPEG is the safe calibrated default; HEVC is selectable for hardware-accelerated testing",
    ] {
        assert!(
            BUILD_DEVICE_CONTROLS_SRC.contains(marker),
            "webcam transport dropdown should preserve marker {marker}"
        );
    }
}

#[test]
fn webcam_transport_changes_are_staged_when_relay_is_live() {
    for marker in [
        "if widgets.webcam_transport_syncing.get()",
        "state.try_borrow_mut()",
        "state_mut.select_webcam_transport(selected);",
        "child_proc.borrow().is_some()",
        "apply_live_media_device_change(",
        "\"Webcam transport\"",
        "Webcam transport set to {} for the next relay launch.",
    ] {
        assert!(
            STAGE_DEVICE_BINDINGS_SRC.contains(marker),
            "live transport selection should preserve marker {marker}"
        );
    }

    for marker in [
        "widgets.webcam_transport_syncing.set(true);",
        "widgets.webcam_transport_syncing.set(false);",
        ".webcam_transport_combo\n        .set_sensitive(state.channels.camera);",
        "Changing upstream webcam transport restarts the live camera path; the picture may pause briefly.",
        "Choose upstream webcam transport for the next relay launch; MJPEG is the safe calibrated default.",
    ] {
        assert!(
            STATUS_REFRESH_SRC.contains(marker),
            "transport sensitivity/tooltip should preserve marker {marker}"
        );
    }
}

#[test]
fn uvc_chip_reports_selected_transport_not_stale_server_codec() {
    for marker in [
        "let codec = state.effective_webcam_transport().label().to_string();",
        "if !relay_live {\n        return (StatusLightState::Live, codec);",
        "if matches!(health, StatusLightState::Live) {\n        (health, codec)",
        "state.select_webcam_transport(WebcamTransport::Mjpeg);",
        "(StatusLightState::Live, \"MJPEG\".to_string())",
    ] {
        assert!(
            STATUS_DETAILS_SRC.contains(marker)
                || include_str!(concat!(
                    env!("CARGO_MANIFEST_DIR"),
                    "/client/src/launcher/tests/ui_runtime.rs"
                ))
                .contains(marker),
            "UVC chip should preserve selected transport marker {marker}"
        );
    }
}
