use nihility_listener::asr::{AutomaticSpeechRecognition, AutomaticSpeechRecognitionConfig};
use tracing::debug;

#[tokio::test]
async fn test_asr() {
    nihility_log::init().expect("Failed to initialize logger");

    let (speech_sender, speech_receiver) = tokio::sync::broadcast::channel::<Vec<f32>>(10);

    let mut reader =
        hound::WavReader::open("tests/samples/test.wav").expect("Failed to open samples/test.wav");
    let spec = reader.spec();
    debug!("Spec: {:?}", &spec);

    let asr_config =
        nihility_config::get_config::<AutomaticSpeechRecognitionConfig>("nihility-asr")
            .expect("asr config");
    let mut asr = AutomaticSpeechRecognition::init(asr_config, speech_receiver)
        .expect("Failed to initialize asr");

    let speech = reader
        .samples::<i16>()
        .flatten()
        .map(|sample| f32::from(sample) / i16::MAX as f32)
        .collect::<Vec<_>>();
    speech_sender
        .send(speech)
        .expect("Failed to send asr speech");
    drop(speech_sender);
    debug!("Waiting for Automatic Speech Recognition");
    let mut text_receiver = asr.get_text_receiver();
    asr.run().await.expect("Failed to run asr");
    drop(asr);
    debug!("Automatic Speech Recognition done");
    while let Ok(text) = text_receiver.recv().await {
        debug!("Got text: {}", text);
    }
    debug!("successfully completed automatic Speech Recognition");
}
