// Server contract for future Opus upstream audio handoff.
//
// Scope: inspect the UAC voice sink and shared audio transport helpers.
// Targets: `server/src/audio/voice_input.rs` and `common/src/audio_transport.rs`.
// Why: compressed Opus payloads must be decoded to PCM before UAC. If decode is
// unavailable or fails, the packet should be rejected safely and visibly.

use lesavka_common::audio_transport::{
    AudioTransportProfile, packet_audio_profile, packet_is_raw_pcm_s16le,
};
use lesavka_common::lesavka::{AudioEncoding, AudioPacket};

const VOICE_INPUT: &str = include_str!(concat!(
    env!("CARGO_MANIFEST_DIR"),
    "/server/src/audio/voice_input.rs"
));
const OPUS_DECODE: &str = include_str!(concat!(
    env!("CARGO_MANIFEST_DIR"),
    "/server/src/audio/opus_decode.rs"
));
const OPUS_DECODE_TESTS: &str = include_str!(concat!(
    env!("CARGO_MANIFEST_DIR"),
    "/server/src/audio/opus_decode/tests/mod.rs"
));

#[test]
fn uac_sink_remains_raw_pcm_and_guards_compressed_packets() {
    for expected in [
        "audio/x-raw",
        "S16LE",
        "packet_is_raw_pcm_s16le",
        "AudioEncoding::Opus",
        "decode_packet(pkt)",
        "dropping Opus microphone packet because opusdec is unavailable",
        "dropping Opus microphone packet after decode failure",
    ] {
        assert!(
            VOICE_INPUT.contains(expected),
            "UAC sink should preserve compressed-audio safety marker {expected}"
        );
    }
    for expected in [
        "OpusPacketDecoder",
        "opusdec",
        "plc=false",
        "min-latency=0",
        "audio/x-opus",
        "audio/x-raw",
        "pending_packets: VecDeque<AudioPacket>",
        "take_pending_packet(sample_pts_us)",
        "push_pending_packet(&mut self.pending_packets",
    ] {
        assert!(
            OPUS_DECODE.contains(expected),
            "Opus decoder should preserve decode-to-PCM marker {expected}"
        );
    }
    for expected in ["audio-type=restricted-lowdelay", "sine_pcm_packet"] {
        assert!(
            OPUS_DECODE_TESTS.contains(expected),
            "Opus decoder tests should preserve live encode/decode evidence {expected}"
        );
    }
}

#[test]
fn opus_packets_are_detected_as_compressed_before_uac_handoff() {
    let opus_packet = AudioPacket {
        encoding: AudioEncoding::Opus as i32,
        sample_rate: 48_000,
        channels: 2,
        frame_duration_us: 20_000,
        data: vec![0x55; 240],
        ..AudioPacket::default()
    };

    assert_eq!(
        packet_audio_profile(&opus_packet),
        AudioTransportProfile::opus_voice()
    );
    assert!(!packet_is_raw_pcm_s16le(&opus_packet));
    assert!(packet_is_raw_pcm_s16le(&AudioPacket::default()));
}
