#[test]
fn native_h3_driver_schedules_receive_flow_control_updates() {
    let driver =
        std::fs::read_to_string("src/transport/h3/native_driver.rs").expect("native driver source");

    assert!(
        driver.contains("build_client_receive_flow_control_update_packets"),
        "native H3 driver must automatically send receive-window MAX_DATA/MAX_STREAM_DATA updates"
    );
}

#[test]
fn native_h3_driver_defers_receive_credit_while_streaming_bodies_are_backpressured() {
    let driver =
        std::fs::read_to_string("src/transport/h3/native_driver.rs").expect("native driver source");
    let process_datagram = driver
        .split("async fn process_datagram_from")
        .nth(1)
        .expect("driver must have process_datagram_from")
        .split("fn sync_path_set_with_handshake")
        .next()
        .expect("process_datagram_from section");
    let event_index = process_datagram
        .find("for event in events")
        .expect("process_datagram must apply H3 events");
    let update_index = process_datagram
        .find("send_receive_flow_control_updates().await?")
        .expect("process_datagram must flush receive-window updates");

    assert!(
        event_index < update_index,
        "native H3 driver must apply response DATA to bounded body queues before advertising more receive credit"
    );
    assert!(
        process_datagram.contains("!self.receive_backpressured()"),
        "native H3 driver must not advertise more receive credit while streaming bodies or tunnels are backpressured"
    );
    assert!(
        driver
            .split("_ = self.body_progress_notify.notified() =>")
            .nth(1)
            .expect("body progress branch")
            .split('}')
            .next()
            .expect("body progress branch body")
            .contains("send_receive_flow_control_updates().await?"),
        "body progress must retry deferred receive-credit updates when user reads open body capacity"
    );
}

#[test]
fn native_h3_tunnel_backpressure_waits_for_all_tunnels_before_pausing_receive() {
    let driver =
        std::fs::read_to_string("src/transport/h3/native_driver.rs").expect("native driver source");
    let tunnel_backpressure = driver
        .split("fn tunnel_inbound_backpressured(&self) -> bool")
        .nth(1)
        .expect("driver must have tunnel_inbound_backpressured")
        .split("fn receive_backpressured")
        .next()
        .expect("tunnel_inbound_backpressured section");

    assert!(
        tunnel_backpressure.contains(".all(|tunnel|"),
        "one slow RFC9220 tunnel must not pause socket reads while a sibling tunnel still has inbound capacity"
    );
    assert!(
        !tunnel_backpressure.contains(".any(|tunnel|"),
        "H3 tunnel receive backpressure must mirror streaming response sibling fairness, not any-tunnel blocking"
    );
}

#[test]
fn native_h3_receive_backpressure_waits_for_all_active_receive_classes() {
    let driver =
        std::fs::read_to_string("src/transport/h3/native_driver.rs").expect("native driver source");
    let receive_backpressure = driver
        .split("fn receive_backpressured(&self) -> bool")
        .nth(1)
        .expect("driver must have receive_backpressured")
        .split("async fn send_preface")
        .next()
        .expect("receive_backpressured section");

    assert!(
        receive_backpressure.contains("has_streaming_responses"),
        "receive backpressure must account for whether streaming response queues are active"
    );
    assert!(
        receive_backpressure.contains("has_tunnels"),
        "receive backpressure must account for whether RFC9220 tunnel queues are active"
    );
    assert!(
        receive_backpressure.contains("streaming_responses_backpressured && tunnels_backpressured"),
        "native H3 receive should pause only when every active response/tunnel receive class is backpressured"
    );
    assert!(
        !receive_backpressure
            .trim()
            .contains("self.streaming_response_body_backpressured() || self.tunnel_inbound_backpressured()"),
        "one blocked receive class must not pause socket reads while another active class still has capacity"
    );
}

#[test]
fn native_h3_driver_flushes_receive_credit_from_consumed_body_bytes() {
    let driver =
        std::fs::read_to_string("src/transport/h3/native_driver.rs").expect("native driver source");
    let body_progress = driver
        .split("_ = self.body_progress_notify.notified() =>")
        .nth(1)
        .expect("body progress branch")
        .split("}")
        .next()
        .expect("body progress branch body");
    let released_body_credits = driver
        .split("async fn apply_released_body_credits")
        .nth(1)
        .expect("driver must have apply_released_body_credits")
        .split("fn apply_released_tunnel_credits")
        .next()
        .expect("apply_released_body_credits section");
    let released_tunnel_credits = driver
        .split("fn apply_released_tunnel_credits")
        .nth(1)
        .expect("driver must have apply_released_tunnel_credits")
        .split("async fn send_stream_cancel")
        .next()
        .expect("apply_released_tunnel_credits section");

    assert!(
        driver.contains("apply_released_body_credits"),
        "native H3 driver must collect public body-consumed bytes before advertising receive credit"
    );
    assert!(
        driver.contains("take_released_recv_bytes"),
        "native H3 driver must read byte-precise H3BodyShared release counters"
    );
    assert!(
        body_progress.contains("apply_released_body_credits().await?"),
        "body progress must apply consumed body-byte credit before flushing receive-window updates"
    );
    assert!(
        released_body_credits.contains("record_client_stream_consumed(stream_id, released as u64)"),
        "body byte release must update QUIC receive credit for the exact stream that the user consumed"
    );
    assert!(
        released_tunnel_credits
            .contains("record_client_stream_consumed(stream_id, released as u64)"),
        "RFC9220 tunnel byte release must update QUIC receive credit for the exact CONNECT stream"
    );
}

#[test]
fn native_h3_connect_wires_client_initial_pto_retransmission() {
    let connection = std::fs::read_to_string("src/transport/h3/connection.rs")
        .expect("native H3 connection source");
    let connect_native = connection
        .split("async fn connect_native")
        .nth(1)
        .expect("native H3 connection must have connect_native")
        .split("fn random_connection_id")
        .next()
        .expect("connect_native section");

    assert!(
        connect_native.contains("record_client_initial_sent_at"),
        "connect_native must record Initial sends so client Initial PTO can arm"
    );
    assert!(
        connect_native.contains("on_loss_detection_timeout")
            && connect_native.contains("retransmit_pto_client_initial_crypto_packets"),
        "connect_native must retransmit client Initial CRYPTO when the loss-detection timer fires"
    );
}

#[test]
fn native_mock_h3_server_schedules_receive_flow_control_updates() {
    let mock_server = std::fs::read_to_string("tests/helpers/mock_h3_server.rs")
        .expect("native mock H3 server source");

    assert!(
        mock_server.contains("build_server_receive_flow_control_update_packets"),
        "native mock H3 server must automatically send receive-window MAX_DATA/MAX_STREAM_DATA updates"
    );
}

#[test]
fn native_h3_driver_schedules_lost_application_stream_retransmits() {
    let driver =
        std::fs::read_to_string("src/transport/h3/native_driver.rs").expect("native driver source");

    assert!(
        driver.contains("retransmit_lost_client_application_stream_packets"),
        "native H3 driver must automatically send lost application STREAM retransmits after ACK/loss updates"
    );
}

#[test]
fn native_h3_driver_retains_connection_close_for_draining_replay() {
    let driver =
        std::fs::read_to_string("src/transport/h3/native_driver.rs").expect("native driver source");
    let driver_fields = driver
        .split("struct NativeH3Driver {")
        .nth(1)
        .expect("driver must have NativeH3Driver")
        .split("struct NativeDriverStreamingResponseState")
        .next()
        .expect("driver field section");
    let send_connection_close = driver
        .split("async fn send_connection_close")
        .nth(1)
        .expect("driver must have send_connection_close")
        .split("async fn send_receive_flow_control_updates")
        .next()
        .expect("send_connection_close section");
    let process_datagram = driver
        .split("async fn process_datagram_from")
        .nth(1)
        .expect("driver must have process_datagram_from")
        .split("fn sync_path_set_with_handshake")
        .next()
        .expect("process_datagram_from section");
    let drive_loop = driver
        .split("async fn drive_loop")
        .nth(1)
        .expect("driver must have drive_loop")
        .split("fn has_pending_work")
        .next()
        .expect("drive_loop section");

    assert!(
        driver_fields.contains("closing_connection_close_packet: Option<Bytes>"),
        "native H3 driver must retain the protected CONNECTION_CLOSE packet for drain replays"
    );
    assert!(
        send_connection_close
            .contains("self.closing_connection_close_packet = Some(close_packet.clone())"),
        "send_connection_close must remember the protected close packet before entering drain"
    );
    assert!(
        send_connection_close.contains("self.is_draining"),
        "send_connection_close must put the public handle into draining state"
    );
    assert!(
        process_datagram.contains("replay_connection_close().await?"),
        "draining native H3 driver must replay CONNECTION_CLOSE on inbound peer packets instead of processing them"
    );
    assert!(
        drive_loop.matches("run_close_window(&mut buf).await?").count() >= 2,
        "local idle/client-shutdown closes must remain in a bounded drain window and replay CONNECTION_CLOSE before driver exit"
    );
}

#[test]
fn native_h3_driver_requeues_flow_control_blocked_new_stream_commands() {
    let driver =
        std::fs::read_to_string("src/transport/h3/native_driver.rs").expect("native driver source");

    assert!(
        driver.contains("queue_flow_control_blocked_command"),
        "native H3 driver must queue new request/tunnel commands that hit QUIC stream limits"
    );
    assert!(
        driver.contains("build_client_flow_control_blocked_packet"),
        "native H3 driver must emit DATA_BLOCKED/STREAM_DATA_BLOCKED/STREAMS_BLOCKED when queueing blocked sends"
    );
}

#[test]
fn native_h3_driver_retries_flow_control_blocked_open_stream_data() {
    let driver =
        std::fs::read_to_string("src/transport/h3/native_driver.rs").expect("native driver source");

    assert!(
        driver.contains("flush_pending_tunnel_data"),
        "native H3 driver must retry queued RFC9220 tunnel DATA after MAX_DATA/MAX_STREAM_DATA"
    );
    assert!(
        driver.contains("send_flow_control_blocked_packet"),
        "native H3 driver must emit BLOCKED frames instead of failing open-stream DATA sends"
    );
}

#[test]
fn native_h3_driver_schedules_request_body_and_tunnel_data_fairly() {
    let driver =
        std::fs::read_to_string("src/transport/h3/native_driver.rs").expect("native driver source");
    let scheduler = driver
        .split("struct H3SendScheduler")
        .nth(1)
        .expect("native H3 driver must have a send scheduler")
        .split("struct H3ReleasedReceiveCredit")
        .next()
        .expect("send scheduler section");
    let flush = driver
        .split("async fn flush_scheduled_send_work")
        .nth(1)
        .expect("native H3 driver must flush through the scheduler")
        .split("async fn flush_pending_tunnel_data_once")
        .next()
        .expect("scheduled send flush section");

    assert!(
        scheduler.contains("next_classes"),
        "native H3 scheduler must arbitrate request-body and tunnel DATA classes"
    );
    assert!(
        scheduler.contains("ordered_streams"),
        "native H3 scheduler must rotate streams within each DATA class"
    );
    assert!(
        flush.contains("flush_request_stream_bodies_once")
            && flush.contains("flush_pending_tunnel_data_once"),
        "native H3 driver must route both request-body and tunnel DATA through the fair scheduler"
    );
    assert!(
        driver.contains("data_budget") && driver.contains("record_data_sent"),
        "native H3 sends must use a bounded, adaptive per-turn DATA budget"
    );
}

#[test]
fn native_h3_driver_keeps_tunnel_data_off_control_command_queue() {
    let command = std::fs::read_to_string("src/transport/h3/command.rs").expect("command source");
    let driver =
        std::fs::read_to_string("src/transport/h3/native_driver.rs").expect("native driver source");
    let spawn = driver
        .split("pub fn spawn_native_h3_driver")
        .nth(1)
        .expect("native H3 driver spawn")
        .split("struct NativeH3Driver")
        .next()
        .expect("native H3 driver spawn section");
    let select_loop = driver
        .split("tokio::select!")
        .nth(1)
        .expect("native H3 driver select loop")
        .split("fn has_pending_work")
        .next()
        .expect("native H3 driver select section");
    let tunnel_event = driver
        .split("fn apply_tunnel_event")
        .nth(1)
        .expect("native H3 driver tunnel event path")
        .split("fn apply_reset_event")
        .next()
        .expect("native H3 tunnel event section");
    let handle_command = driver
        .split("async fn handle_command")
        .nth(1)
        .expect("native H3 driver command handler")
        .split("async fn queue_flow_control_blocked_command(&mut self")
        .next()
        .expect("native H3 command handler section");

    assert!(
        !command.contains("SendTunnelData"),
        "RFC 9220 tunnel DATA must not be a DriverCommand variant"
    );
    assert!(
        spawn.contains("mpsc::channel::<(u64, H3TunnelOutbound)>(32)"),
        "native H3 driver must allocate a dedicated tunnel outbound channel"
    );
    assert!(
        select_loop.contains("tunnel_outbound_rx.recv()")
            && select_loop.contains("send_tunnel_data(stream_id, outbound)"),
        "driver select loop must drain tunnel DATA from the dedicated channel"
    );
    assert!(
        tunnel_event.contains(".send((stream_id, outbound))"),
        "public tunnel writer must forward DATA through the dedicated tunnel outbound channel"
    );
    assert!(
        handle_command.contains("pending_control_packets")
            && handle_command.contains("push_back((packet.stream_id, packet.packet))"),
        "fresh request and tunnel-open packets must enqueue on the strict-priority control queue"
    );
}

#[test]
fn native_h3_driver_serves_control_frames_ahead_of_in_flight_tunnel_data() {
    let driver =
        std::fs::read_to_string("src/transport/h3/native_driver.rs").expect("native driver source");

    assert!(
        driver.contains("enum H3SendClass")
            && driver.contains("Control")
            && driver.contains("RequestBody")
            && driver.contains("TunnelData"),
        "H3SendClass must carry a strict-priority Control variant so HEADERS frames cannot \
         sit behind in-flight tunnel DATA"
    );

    let next_classes = driver
        .split("fn next_classes")
        .nth(1)
        .expect("scheduler must define next_classes")
        .split("fn ordered_streams")
        .next()
        .expect("next_classes section");
    let control_pos = next_classes
        .find("H3SendClass::Control")
        .expect("next_classes must push Control");
    let request_pos = next_classes
        .find("H3SendClass::RequestBody")
        .unwrap_or(usize::MAX);
    let tunnel_pos = next_classes
        .find("H3SendClass::TunnelData")
        .unwrap_or(usize::MAX);
    assert!(
        control_pos < request_pos && control_pos < tunnel_pos,
        "next_classes must push Control ahead of both data classes for strict priority"
    );

    let flush_scheduled = driver
        .split("async fn flush_scheduled_send_work")
        .nth(1)
        .expect("driver must have flush_scheduled_send_work")
        .split("async fn flush_pending_control_packet_once")
        .next()
        .expect("flush_scheduled_send_work section");
    assert!(
        flush_scheduled.contains("has_control_work"),
        "flush_scheduled_send_work must consult has_control_work() so pending HEADERS are flushed"
    );
    assert!(
        flush_scheduled.contains("flush_pending_control_packet_once"),
        "flush_scheduled_send_work must drain pending control packets via \
         flush_pending_control_packet_once"
    );

    let flush_control = driver
        .split("async fn flush_pending_control_packet_once")
        .nth(1)
        .expect("driver must define flush_pending_control_packet_once")
        .split("async fn flush_pending_tunnel_data_once")
        .next()
        .expect("flush_pending_control_packet_once section");
    assert!(
        flush_control.contains("pending_control_packets.pop_front()")
            && flush_control.contains("send_packet_to_peer"),
        "flush_pending_control_packet_once must pop a queued control packet and emit it to the peer"
    );

    let handle_command = driver
        .split("async fn handle_command")
        .nth(1)
        .expect("native H3 driver command handler")
        .split("async fn queue_flow_control_blocked_command(&mut self")
        .next()
        .expect("native H3 command handler section");

    for (command, next_delim) in [
        (
            "DriverCommand::SendRequest",
            "DriverCommand::SendStreamingRequest",
        ),
        (
            "DriverCommand::SendStreamingRequest",
            "DriverCommand::OpenWebSocketTunnel",
        ),
        (
            "DriverCommand::OpenWebSocketTunnel",
            "\n        }\n        Ok(())",
        ),
    ] {
        let arm_section = handle_command
            .splitn(2, command)
            .nth(1)
            .unwrap_or_else(|| panic!("handle_command must include arm for {command}"))
            .split(next_delim)
            .next()
            .expect("command arm section");
        assert!(
            arm_section.contains("pending_control_packets")
                && arm_section.contains("push_back((packet.stream_id, packet.packet))"),
            "{command} arm must queue its HEADERS packet on pending_control_packets instead of \
             sending synchronously"
        );
    }
}

#[test]
fn native_h3_driver_routes_tunnel_outbound_off_command_channel() {
    let driver =
        std::fs::read_to_string("src/transport/h3/native_driver.rs").expect("native driver source");

    assert!(
        driver.contains("tunnel_outbound_tx: mpsc::Sender<(u64, H3TunnelOutbound)>")
            && driver.contains("tunnel_outbound_rx: mpsc::Receiver<(u64, H3TunnelOutbound)>"),
        "driver must own a dedicated tunnel-outbound mpsc so SendTunnelData traffic does not \
         queue behind control commands on command_rx"
    );

    let drive_loop = driver
        .split("async fn drive_loop")
        .nth(1)
        .expect("driver must have drive_loop")
        .split("fn has_pending_work")
        .next()
        .expect("drive_loop section");
    let select_idx = drive_loop
        .find("tokio::select!")
        .expect("drive_loop must use tokio::select!");
    let after_select = &drive_loop[select_idx..];
    let biased_idx = after_select
        .find("biased")
        .expect("drive_loop select must be biased so command_rx wins");
    let command_idx = after_select
        .find("self.command_rx.recv()")
        .expect("drive_loop must poll command_rx in its select");
    let tunnel_idx = after_select
        .find("self.tunnel_outbound_rx.recv()")
        .expect("drive_loop must poll tunnel_outbound_rx in its select");
    assert!(
        biased_idx < command_idx && command_idx < tunnel_idx,
        "biased select must place command_rx ahead of tunnel_outbound_rx so a streaming-request \
         command is dequeued before any further tunnel DATA"
    );

    let apply_tunnel = driver
        .split("fn apply_tunnel_event")
        .nth(1)
        .expect("driver must apply tunnel events")
        .split("fn push_streaming_body")
        .next()
        .expect("apply_tunnel_event section");
    assert!(
        apply_tunnel.contains(".send((stream_id, outbound))"),
        "per-tunnel forwarder must push outbound DATA onto tunnel_outbound_tx, not command_tx"
    );
    assert!(
        !apply_tunnel.contains("DriverCommand::SendTunnelData"),
        "per-tunnel forwarder must not wrap outbound DATA in a DriverCommand (removed in suite-gate refactor)"
    );
}

#[test]
fn native_h3_driver_releases_tunnel_outbound_credit_per_wire_chunk() {
    let driver =
        std::fs::read_to_string("src/transport/h3/native_driver.rs").expect("native driver source");
    let send_tunnel_data = driver
        .split("async fn send_tunnel_data")
        .nth(1)
        .expect("native H3 driver must enqueue tunnel outbound data")
        .split("async fn flush_scheduled_send_work")
        .next()
        .expect("send_tunnel_data section");
    let flush_once = driver
        .split("async fn flush_tunnel_data_once")
        .nth(1)
        .expect("native H3 driver must flush tunnel data")
        .split("async fn flush_request_stream_bodies_once")
        .next()
        .expect("flush_tunnel_data_once section");

    assert!(
        send_tunnel_data.contains("DriverPendingTunnelOutbound::from_outbound"),
        "driver must wrap outbound tunnel chunks with per-outbound byte-credit accounting"
    );
    assert!(
        flush_once.contains("record_chunk_sent"),
        "driver must release tunnel outbound byte credit as each wire chunk is sent"
    );
    assert!(
        flush_once.contains("drain_remaining_credit"),
        "driver must drain any remaining tunnel outbound credit when a queued outbound is fully consumed"
    );
    assert!(
        !flush_once.contains("release_send_bytes(outbound.bytes.len())"),
        "driver must not release a whole queued outbound's credit after only a partial DATA write"
    );
}

#[test]
fn h3_client_slow_path_uses_origin_fair_dispatcher() {
    let h3_client = std::fs::read_to_string("src/transport/h3/mod.rs").expect("h3 module source");
    let pooled_handle = h3_client
        .split("async fn pooled_handle")
        .nth(1)
        .expect("H3Client must have pooled_handle")
        .split("fn pool_key")
        .next()
        .expect("pooled_handle section");
    let dispatcher_index = pooled_handle
        .find("self.dispatcher.acquire")
        .expect("H3Client slow path must acquire an origin-fair dispatcher ticket");
    let connect_index = pooled_handle
        .find("H3Connection::connect")
        .expect("H3Client slow path must establish fresh connections");

    assert!(
        dispatcher_index < connect_index,
        "H3Client must enter origin-fair admission before opening a fresh native H3 connection"
    );
    assert!(
        pooled_handle.contains("OriginKey"),
        "H3Client dispatcher admission must be keyed by origin, not by full fingerprint/pool key"
    );
}

#[test]
fn native_h3_driver_schedules_timer_driven_delayed_application_acks() {
    let driver =
        std::fs::read_to_string("src/transport/h3/native_driver.rs").expect("native driver source");
    let drive_loop = driver
        .split("async fn drive_loop")
        .nth(1)
        .expect("driver must have drive_loop")
        .split("fn has_pending_work")
        .next()
        .expect("drive_loop section");

    assert!(
        drive_loop.contains("client_application_ack_deadline"),
        "native H3 driver must derive a delayed ACK deadline from max_ack_delay_ms"
    );
    assert!(
        drive_loop.contains("send_delayed_application_ack().await?"),
        "native H3 driver must wake on the delayed ACK timer even when ack_eliciting_threshold is not reached"
    );
    assert!(
        driver.contains("ack_delay_exponent"),
        "native H3 delayed ACKs must encode ACK Delay using the configured ack_delay_exponent"
    );
}

#[test]
fn native_h3_driver_treats_pending_delayed_ack_as_pending_work() {
    let driver =
        std::fs::read_to_string("src/transport/h3/native_driver.rs").expect("native driver source");
    let has_pending_work = driver
        .split("fn has_pending_work")
        .nth(1)
        .expect("driver must have has_pending_work")
        .split("fn streaming_response_body_backpressured")
        .next()
        .expect("has_pending_work section");

    assert!(
        has_pending_work.contains("client_application_ack_deadline().is_some()"),
        "native H3 idle handling must not close while a delayed ACK is pending"
    );
    assert!(
        driver.contains("_ = tokio::time::sleep(remaining_idle), if !has_pending_work =>"),
        "native H3 idle sleep must be disabled while delayed ACK or other work is pending"
    );
}

#[test]
fn native_h3_driver_schedules_application_loss_detection_timer() {
    let driver =
        std::fs::read_to_string("src/transport/h3/native_driver.rs").expect("native driver source");
    let drive_loop = driver
        .split("async fn drive_loop")
        .nth(1)
        .expect("driver must have drive_loop")
        .split("fn has_pending_work")
        .next()
        .expect("drive_loop section");
    let has_pending_work = driver
        .split("fn has_pending_work")
        .nth(1)
        .expect("driver must have has_pending_work")
        .split("fn has_outbound_send_work")
        .next()
        .expect("has_pending_work section");

    assert!(
        drive_loop.contains("client_loss_detection_deadline"),
        "native H3 driver must derive a post-handshake QUIC loss-detection deadline"
    );
    assert!(
        drive_loop.contains("handle_loss_detection_timeout().await?"),
        "native H3 driver must wake on the QUIC loss-detection timer"
    );
    assert!(
        has_pending_work.contains("client_loss_detection_deadline().is_some()"),
        "native H3 idle handling must not close while application PTO/recovery work is pending"
    );
    assert!(
        driver.contains("LossDetectionOutcome::Pto {")
            && driver.contains("PacketNumberSpace::Application")
            && driver.contains("retransmit_pto_client_application_stream_packets"),
        "native H3 driver must retransmit application STREAM data on application-space PTO"
    );
}

#[test]
fn native_h3_driver_decays_send_window_on_ack_ecn_congestion() {
    let driver =
        std::fs::read_to_string("src/transport/h3/native_driver.rs").expect("native driver source");
    let observe_recovery_signals = driver
        .split("fn observe_recovery_signals")
        .nth(1)
        .expect("driver must have observe_recovery_signals")
        .split("async fn handle_command")
        .next()
        .expect("observe_recovery_signals section");

    assert!(
        observe_recovery_signals.contains("take_client_application_ecn_congestion()"),
        "native H3 driver must consume ACK_ECN CE congestion signals from recovery"
    );
    assert!(
        observe_recovery_signals.contains("send_scheduler.observe_loss()"),
        "ACK_ECN CE congestion must decay the adaptive send window just like loss"
    );
}

#[test]
fn native_h3_threads_socket_received_ecn_marks_into_ack_ecn_generation() {
    let connection =
        std::fs::read_to_string("src/transport/h3/connection.rs").expect("connection source");
    let driver =
        std::fs::read_to_string("src/transport/h3/native_driver.rs").expect("native driver source");
    let handshake =
        std::fs::read_to_string("src/transport/h3/handshake.rs").expect("handshake source");
    let udp_ecn = std::fs::read_to_string("src/transport/h3/udp_ecn.rs")
        .expect("native UDP ECN helper source");

    assert!(
        connection.contains("enable_udp_ecn_receive")
            && connection.contains("recv_from_with_ecn")
            && connection.contains("process_server_datagram_with_ecn"),
        "native H3 connection setup must enable socket-level ECN receive metadata and pass marks into handshake ACK tracking"
    );
    assert!(
        driver.contains("recv_from_with_ecn")
            && driver.contains("open_server_h3_event_packet_from_with_ecn"),
        "native H3 driver must keep ECN marks attached to application datagrams after the handshake"
    );
    assert!(
        handshake.contains("observe_packet_with_ecn")
            && handshake.contains("observe_ecn_at")
            && handshake.contains("QuicEcnMark"),
        "native QUIC handshake must feed received ECN marks into QuicAckTracker so ACK_ECN counters are generated"
    );
    assert!(
        udp_ecn.contains("set_recv_tos_v4")
            && udp_ecn.contains("set_recv_tclass_v6")
            && udp_ecn.contains("IP_TOS")
            && udp_ecn.contains("IPV6_TCLASS"),
        "native UDP sockets must request and parse IPv4/IPv6 traffic-class ancillary data"
    );
}

#[test]
fn native_h3_driver_schedules_pmtu_probes_after_handshake() {
    let driver =
        std::fs::read_to_string("src/transport/h3/native_driver.rs").expect("native driver source");
    let drive_loop = driver
        .split("async fn drive_loop")
        .nth(1)
        .expect("driver must have drive_loop")
        .split("async fn send_preface")
        .next()
        .expect("drive_loop section");

    assert!(
        drive_loop.contains("send_client_pmtu_probe_if_available().await?"),
        "native H3 driver must schedule PMTU probes once application keys are available"
    );
    assert!(
        driver.contains("build_client_pmtu_probe_packet")
            && driver.contains("client_pmtu_pending_probe_size"),
        "native H3 driver must use the handshake PMTU policy rather than ad-hoc packet sizing"
    );
}

#[test]
fn native_h3_driver_propagates_tls_handshake_status_to_handle() {
    let driver =
        std::fs::read_to_string("src/transport/h3/native_driver.rs").expect("native driver source");
    let spawn_driver = driver
        .split("pub fn spawn_native_h3_driver")
        .nth(1)
        .expect("driver must have spawn_native_h3_driver")
        .split("struct NativeH3Driver")
        .next()
        .expect("spawn_native_h3_driver section");

    assert!(
        spawn_driver.contains("handshake.handshake_status()"),
        "native H3 must snapshot TLS resumption / 0-RTT status before moving the handshake into the driver"
    );
    assert!(
        spawn_driver.contains("NativeH3HandshakeReport"),
        "native H3 handle must receive a structured handshake report for caller replay policy"
    );
    assert!(
        spawn_driver.contains("new_with_transport_config_and_native_handshake_report"),
        "native H3 driver must attach the handshake report to the returned H3Handle"
    );
}

#[test]
fn native_h3_client_has_safe_zero_rtt_request_policy() {
    let h3_client = std::fs::read_to_string("src/transport/h3/mod.rs").expect("H3 client source");
    let send_request = h3_client
        .split("pub async fn send_request")
        .nth(1)
        .expect("H3Client must expose send_request")
        .split("pub async fn send_streaming")
        .next()
        .expect("send_request section");

    assert!(
        h3_client.contains("is_zero_rtt_safe_request"),
        "native H3 must centralize the anti-replay policy for 0-RTT request eligibility"
    );
    assert!(
        send_request.contains("try_send_request_with_zero_rtt"),
        "H3Client::send_request must attempt 0-RTT only for safe fresh-connection requests"
    );
    let zero_rtt = std::fs::read_to_string("src/transport/zero_rtt.rs")
        .expect("shared transport::zero_rtt source");
    assert!(
        zero_rtt.contains("matches!(method, \"GET\" | \"HEAD\" | \"OPTIONS\")"),
        "0-RTT policy must be stricter than pooled-retry idempotency and exclude unsafe PUT/DELETE replays"
    );
}

#[test]
fn native_h3_connection_replays_rejected_zero_rtt_once() {
    let connection =
        std::fs::read_to_string("src/transport/h3/connection.rs").expect("connection source");
    let handshake =
        std::fs::read_to_string("src/transport/h3/handshake.rs").expect("handshake source");

    assert!(
        connection.contains("connect_with_zero_rtt_request"),
        "connection establishment must accept a first request for end-to-end 0-RTT"
    );
    assert!(
        handshake.contains("build_client_h3_zero_rtt_request_packet"),
        "native QUIC must be able to packetize the first H3 request as QUIC 0-RTT"
    );
    assert!(
        connection.contains("early_data_rejected()")
            && connection.contains("build_client_h3_replay_request_packet"),
        "rejected 0-RTT must replay exactly through the 1-RTT request packet path"
    );
}

#[test]
fn native_h3_zero_rtt_acceptance_propagates_with_pending_response() {
    let driver =
        std::fs::read_to_string("src/transport/h3/native_driver.rs").expect("native driver source");
    let spawn_driver = driver
        .split("pub fn spawn_native_h3_driver")
        .nth(1)
        .expect("driver must have spawn_native_h3_driver")
        .split("struct NativeH3Driver")
        .next()
        .expect("spawn_native_h3_driver section");

    assert!(
        spawn_driver.contains("pending_zero_rtt_response"),
        "driver spawn must inherit the response waiter for a request sent during the handshake"
    );
    assert!(
        spawn_driver.contains("native_handshake_report"),
        "driver spawn must preserve the EarlyAccepted/EarlyRejected status for H3Handle and H3Client"
    );
}

#[test]
fn native_mock_h3_server_schedules_timer_driven_delayed_application_acks() {
    let mock_server = std::fs::read_to_string("tests/helpers/mock_h3_server.rs")
        .expect("native mock H3 server source");

    assert!(
        mock_server.contains("server_application_ack_deadline"),
        "native mock H3 server must derive a delayed ACK deadline from max_ack_delay_ms"
    );
    assert!(
        mock_server.contains("send_delayed_application_ack().await"),
        "native mock H3 server must wake on the delayed ACK timer below ack_eliciting_threshold"
    );
    assert!(
        mock_server.contains("build_server_application_ack_packet_after_or_delay"),
        "native mock H3 server must use threshold-or-delay ACK emission instead of immediate ACKs"
    );
}

#[test]
fn native_h3_same_fixture_schedules_timer_driven_delayed_application_acks() {
    let fixture = std::fs::read_to_string("benches/native_h3_vs_rust_clients/src/main.rs")
        .expect("native H3 same-fixture benchmark source");

    assert!(
        fixture.contains("server_application_ack_deadline"),
        "native H3 same-fixture server must derive a delayed ACK deadline from max_ack_delay_ms"
    );
    assert!(
        fixture.contains("send_delayed_application_ack().await"),
        "native H3 same-fixture server must wake on the delayed ACK timer below ack_eliciting_threshold"
    );
    assert!(
        fixture.contains("build_server_application_ack_packet_after_or_delay"),
        "native H3 same-fixture server must use threshold-or-delay ACK emission instead of immediate ACKs"
    );
}

#[test]
fn native_mock_h3_server_schedules_lost_application_stream_retransmits() {
    let mock_server = std::fs::read_to_string("tests/helpers/mock_h3_server.rs")
        .expect("native mock H3 server source");

    assert!(
        mock_server.contains("retransmit_lost_server_application_stream_packets"),
        "native mock H3 server must automatically send lost application STREAM retransmits after ACK/loss updates"
    );
}

#[test]
fn native_mock_h3_server_schedules_application_loss_detection_timer() {
    let mock_server = std::fs::read_to_string("tests/helpers/mock_h3_server.rs")
        .expect("native mock H3 server source");

    assert!(
        mock_server.contains("server_loss_detection_deadline"),
        "native mock H3 server must derive an application loss-detection deadline"
    );
    assert!(
        mock_server.contains("handle_loss_detection_timeout().await"),
        "native mock H3 server must wake on the application loss-detection timer"
    );
    assert!(
        mock_server.contains("retransmit_pto_server_application_stream_packets"),
        "native mock H3 server must retransmit application STREAM data on server application PTO"
    );
}

#[test]
fn native_h3_same_fixture_schedules_application_loss_detection_timer() {
    let fixture = std::fs::read_to_string("benches/native_h3_vs_rust_clients/src/main.rs")
        .expect("native H3 same-fixture benchmark source");

    assert!(
        fixture.contains("server_loss_detection_deadline"),
        "native H3 same-fixture server must derive an application loss-detection deadline"
    );
    assert!(
        fixture.contains("handle_loss_detection_timeout().await"),
        "native H3 same-fixture server must wake on the application loss-detection timer"
    );
    assert!(
        fixture.contains("retransmit_pto_server_application_stream_packets"),
        "native H3 same-fixture server must retransmit application STREAM data on server application PTO"
    );
}

#[test]
fn native_mock_h3_server_runs_connection_close_window() {
    let mock_server = std::fs::read_to_string("tests/helpers/mock_h3_server.rs")
        .expect("native mock H3 server source");

    assert!(
        mock_server.contains("closing_connection_close_packet: Option<Bytes>"),
        "native mock H3 server must retain the protected CONNECTION_CLOSE packet for server close replays"
    );
    assert!(
        mock_server.contains("run_server_close_window"),
        "native mock H3 server must keep the server alive for a bounded close window"
    );
    assert!(
        mock_server.contains("server_should_replay_connection_close"),
        "native mock H3 server must rate-limit server CONNECTION_CLOSE replays using QuicCloseState"
    );
    assert!(
        mock_server.contains("server_close_time_until_expiry"),
        "native mock H3 server close window must be tied to the server PTO-derived close timer"
    );
}

#[test]
fn native_h3_fixture_suppresses_sends_after_peer_connection_close() {
    let mock_server = std::fs::read_to_string("tests/helpers/mock_h3_server.rs")
        .expect("native mock H3 server source");
    let fixture = std::fs::read_to_string("benches/native_h3_vs_rust_clients/src/main.rs")
        .expect("native H3 same-fixture benchmark source");

    for (name, source) in [("mock", mock_server), ("same-fixture", fixture)] {
        let process_datagram = source
            .split("open_client_h3_event_packet_from")
            .nth(1)
            .expect("server process_datagram must open client H3 events")
            .split("build_server_application_ack_packet_after_or_delay")
            .next()
            .expect("server process_datagram must decide whether to ACK");
        assert!(
            process_datagram.contains("close_state().is_draining()"),
            "{name} server must suppress ACK/flow-control/retransmit sends after peer CONNECTION_CLOSE enters draining"
        );
    }
}
