// Include-based coverage for camera capture timing helpers.
//
// Scope: include `client/src/input/camera.rs` and exercise timestamp logging
// branches without requiring a physical webcam.
// Targets: `client/src/input/camera.rs`.
// Why: upstream freshness diagnostics depend on stable first-packet and
// periodic timing traces while the HEVC transport path is being tuned.

#[allow(warnings)]
mod live_capture_clock {
    include!(concat!(
        env!("CARGO_MANIFEST_DIR"),
        "/tests/helpers/support/live_capture_clock_shim.rs"
    ));
}

#[allow(warnings)]
mod camera_timing_contract {
    include!(env!("LESAVKA_CLIENT_CAMERA_SRC"));

    use temp_env::with_var;
    const CAMERA_CAPTURE_SRC: &str = include_str!(concat!(
        env!("CARGO_MANIFEST_DIR"),
        "/client/src/input/camera/capture_pipeline.rs"
    ));

    #[test]
    fn camera_timing_helpers_cover_first_packet_and_trace_enabled_paths() {
        log_camera_first_packet(0, 128, 42_000);
        assert!(!should_log_camera_timing_sample(11));

        with_var("LESAVKA_UPSTREAM_TIMING_TRACE", Some("1"), || {
            assert!(should_log_camera_timing_sample(0));
            log_camera_timing_sample(
                0,
                crate::live_capture_clock::RebasedSourcePts {
                    packet_pts_us: 12_345,
                    capture_now_us: 12_999,
                    source_pts_us: Some(5_000),
                    source_base_us: Some(5_000),
                    capture_base_us: Some(7_345),
                    used_source_pts: true,
                    lag_clamped: false,
                    lead_clamped: false,
                },
                256,
            );
        });

        log_camera_lag_clamped_source(
            crate::live_capture_clock::RebasedSourcePts {
                packet_pts_us: 1,
                capture_now_us: 1_000_000,
                source_pts_us: Some(1),
                source_base_us: Some(1),
                capture_base_us: Some(999_999),
                used_source_pts: true,
                lag_clamped: true,
                lead_clamped: false,
            },
            512,
        );
    }

    #[test]
    fn lag_clamped_camera_timestamp_still_sends_the_frame() {
        assert!(CAMERA_CAPTURE_SRC.contains("log_camera_lag_clamped_source("));
        assert!(CAMERA_CAPTURE_SRC.contains("let pts = timing.packet_pts_us;"));
        assert!(
            !CAMERA_CAPTURE_SRC.contains(
                "log_camera_lag_clamped_source(timing, map.as_slice().len());\n            return None;"
            ) && !CAMERA_CAPTURE_SRC.contains(
                "log_camera_lag_clamped_source(\n                timing,\n                map.as_slice().len(),\n            );\n            return None;"
            ),
            "lag-clamped webcam packets should use the repaired live timestamp, not freeze UVC by being dropped"
        );
    }
}
