// Client compatibility contracts for an optional Opus upstream audio transport.
//
// Scope: inspect client capture/bundle/install seams without requiring a live
// audio stack.
// Targets: `client/src/input/microphone.rs`,
// `client/src/app/uplink_media/uplink_queue_metadata.rs`, and
// `scripts/install/client.sh`.
// Why: Opus remains an optional compressed microphone transport, while raw PCM
// is the default known-good fallback.

const MICROPHONE: &str = include_str!(concat!(
    env!("CARGO_MANIFEST_DIR"),
    "/client/src/input/microphone.rs"
));
const MICROPHONE_CAPTURE_RUNTIME: &str = include_str!(concat!(
    env!("CARGO_MANIFEST_DIR"),
    "/client/src/input/microphone/capture_runtime.rs"
));
const AUDIO_CODEC: &str = include_str!(concat!(
    env!("CARGO_MANIFEST_DIR"),
    "/client/src/input/audio_codec.rs"
));
const AUDIO_CODEC_TESTS: &str = include_str!(concat!(
    env!("CARGO_MANIFEST_DIR"),
    "/client/src/input/audio_codec/tests/mod.rs"
));
const UPLINK_QUEUE_METADATA: &str = include_str!(concat!(
    env!("CARGO_MANIFEST_DIR"),
    "/client/src/app/uplink_media/uplink_queue_metadata.rs"
));
const CLIENT_INSTALL: &str = include_str!(concat!(
    env!("CARGO_MANIFEST_DIR"),
    "/scripts/install/client.sh"
));

#[test]
fn client_microphone_path_preserves_pcm_fallback_and_opus_selection() {
    for expected in [
        "audio_transport::mark_packet_pcm_s16le",
        "OpusPacketEncoder",
        "requested_upstream_audio_codec_from_env",
        "LESAVKA_UPLINK_AUDIO_CODEC",
        "LESAVKA_MIC_NOISE_SUPPRESSION",
        "webrtcdsp",
        "frame_duration_us",
        "MIC_SAMPLE_RATE",
        "MIC_CHANNELS",
        "MIC_PACKET_TARGET_DURATION_ENV",
        "DEFAULT_MIC_PACKET_TARGET_DURATION_US: u64 = 20_000",
        "Ok(None) => None",
    ] {
        assert!(
            MICROPHONE.contains(expected)
                || MICROPHONE_CAPTURE_RUNTIME.contains(expected)
                || AUDIO_CODEC.contains(expected),
            "microphone capture should preserve audio transport marker {expected}"
        );
    }
    assert!(
        AUDIO_CODEC.contains("mark_packet_opus") && AUDIO_CODEC.contains("PCM by default"),
        "client Opus encoder should stamp packets while documenting the safe default"
    );
    for expected in [
        "audio-type=restricted-lowdelay",
        "bitrate=96000",
        "plc=false",
        "decode_opus_payload",
    ] {
        assert!(
            AUDIO_CODEC.contains(expected) || AUDIO_CODEC_TESTS.contains(expected),
            "client Opus path should keep low-delay/non-silent evidence marker {expected}"
        );
    }
    for expected in [
        "pending_packets: VecDeque<AudioPacket>",
        "take_pending_packet(sample_pts_us)",
        "push_pending_packet(&mut self.pending_packets",
    ] {
        assert!(
            AUDIO_CODEC.contains(expected),
            "client Opus encoder must preserve timing metadata across codec delay: {expected}"
        );
    }
}

#[test]
fn bundled_uplink_envelope_carries_audio_codec_metadata_with_video() {
    for expected in [
        "packet_audio_profile",
        "mark_bundle_audio_profile",
        "has_audio",
        "UpstreamMediaBundle",
        "attach_bundle_queue_metadata",
    ] {
        assert!(
            UPLINK_QUEUE_METADATA.contains(expected),
            "bundled uplink should preserve audio profile marker {expected}"
        );
    }
    assert!(
        UPLINK_QUEUE_METADATA
            .find("mark_bundle_audio_profile")
            .unwrap()
            < UPLINK_QUEUE_METADATA
                .find("attach_bundle_queue_metadata")
                .unwrap(),
        "bundle audio codec metadata should be available before queue metadata is attached"
    );
}

#[test]
fn client_installer_reports_optional_opus_gstreamer_route() {
    for expected in [
        "Opus upstream audio transport route",
        "opusenc",
        "opusdec",
        "microphone noise suppression route",
        "webrtcdsp",
        "fall back to PCM",
    ] {
        assert!(
            CLIENT_INSTALL.contains(expected),
            "client installer should keep Opus diagnostic marker {expected}"
        );
    }
}
