grr-cli 2.4.1

Google tools from the terminal, at maximum performance: 401 methods across 14 Google APIs in one generated command tree, zero-config, over HTTP/3
Documentation
//! Transport introspection CLI command.
//!
//! Reports the HTTP version negotiated during a real probe, the HTTP/3
//! request/effective/fallback flags, and detected runtime features.
//! Everything shown is auto-tuned — there is no user-facing transport
//! configuration, this command exists to prove it.
//!
//! The probe targets the Gmail API (base URL + `users/me/profile`),
//! issued through a plain `HttpCore`. The report's shape is quoted
//! verbatim in the site docs and must not change.

use crate::core::RuntimeFeatures;
use crate::core::auth::GoogleAuth;
use crate::core::http::{HttpCore, TransportInfo};
use anyhow::Result;
use clap::Args;

/// The Gmail base URL and probe path used for the probe — keeping them
/// identical keeps the report's meaning identical.
const PROBE_BASE_URL: &str = "https://gmail.googleapis.com/gmail/v1/";
const PROBE_PATH: &str = "users/me/profile";

#[derive(Args, Debug)]
pub struct TransportArgs {}

/// Normalize a raw `reqwest::Version` debug string into the CLI's
/// canonical protocol labels. Unknown values (including `not-probed`)
/// are passed through unchanged.
fn normalize_version(raw: &str) -> &str {
    match raw {
        "HTTP/1.1" => "HTTP_11",
        "HTTP/2" | "HTTP/2.0" => "HTTP_2",
        "HTTP/3" | "HTTP/3.0" => "HTTP_3",
        other => other,
    }
}

/// Build the human-readable transport report.
fn format_transport_report(info: &TransportInfo, features: &RuntimeFeatures) -> String {
    format!(
        "negotiated_protocol: {}\n\
         http3_requested: {}\n\
         http3_effective: {}\n\
         fell_back: {}\n\
         cpus: {}\n\
         io_uring: {}\n\
         http3_always_compiled: true\n\
         runtime: tokio multi-thread (auto-sized to cores)",
        normalize_version(&info.negotiated_version),
        info.http3_requested,
        info.http3_effective,
        info.fell_back,
        features.num_cpus,
        features.io_uring,
    )
}

pub async fn handle_transport_cmd(auth: GoogleAuth, _args: TransportArgs) -> Result<()> {
    let base = crate::core::http::parse_url(PROBE_BASE_URL, "transport probe base")?;
    // Consuming the auth handle: this command's only job is the probe.
    let core = HttpCore::connect(auth, &base, PROBE_PATH).await?;
    let report = format_transport_report(
        core.transport_info(),
        &crate::core::runtime::detect_runtime_features().await,
    );
    println!("{report}");
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    fn features() -> RuntimeFeatures {
        RuntimeFeatures {
            io_uring: false,
            num_cpus: 8,
        }
    }

    #[test]
    fn normalizes_known_versions() {
        assert_eq!(normalize_version("HTTP/3"), "HTTP_3");
        assert_eq!(normalize_version("HTTP/3.0"), "HTTP_3");
        assert_eq!(normalize_version("HTTP/2"), "HTTP_2");
        assert_eq!(normalize_version("HTTP/2.0"), "HTTP_2");
        assert_eq!(normalize_version("HTTP/1.1"), "HTTP_11");
        assert_eq!(normalize_version("not-probed"), "not-probed");
    }

    #[test]
    fn report_h3_effective() {
        let info = TransportInfo {
            negotiated_version: "HTTP/3".into(),
            http3_requested: true,
            http3_effective: true,
            fell_back: false,
        };
        let report = format_transport_report(&info, &features());
        for line in [
            "negotiated_protocol: HTTP_3",
            "http3_requested: true",
            "http3_effective: true",
            "fell_back: false",
            "cpus: 8",
            "io_uring: false",
            "http3_always_compiled: true",
        ] {
            assert!(report.contains(line), "missing `{line}` in:\n{report}");
        }
    }

    #[test]
    fn report_h3_fell_back_to_h2() {
        let info = TransportInfo {
            negotiated_version: "HTTP/2".into(),
            http3_requested: true,
            http3_effective: false,
            fell_back: true,
        };
        let report = format_transport_report(&info, &features());
        assert!(report.contains("negotiated_protocol: HTTP_2"));
        assert!(report.contains("fell_back: true"));
        assert!(report.contains("http3_effective: false"));
    }

    #[test]
    fn report_not_probed() {
        let info = TransportInfo {
            negotiated_version: "not-probed".into(),
            ..TransportInfo::default()
        };
        let report = format_transport_report(&info, &features());
        assert!(report.contains("negotiated_protocol: not-probed"));
        assert!(report.contains("http3_requested: true"));
        assert!(report.contains("http3_always_compiled: true"));
    }
}