use libremetaverse::types::UUID;
use libremetaverse_voice_webrtc::{
    CloseRequest, Jsep, ProvisionRequest, SignalingComplete, SignalingCompleteRequest, VoiceSecret,
    encode_pcm_48k_mono,
};

#[test]
fn local_provision_matches_web_rtc_capability_wire_shape() {
    let request = ProvisionRequest {
        jsep: Jsep {
            kind: "offer".into(),
            sdp: "v=0\r\n".into(),
        },
        channel_type: "local".into(),
        voice_server_type: "webrtc".into(),
        parcel_local_id: Some(42),
    };
    let value = serde_json::to_value(request).unwrap();
    assert_eq!(value["jsep"]["type"], "offer");
    assert_eq!(value["parcel_local_id"], 42);
    assert_eq!(value["channel_type"], "local");
    assert_eq!(value["voice_server_type"], "webrtc");
}

#[test]
fn completion_uses_singular_candidate_and_close_is_explicit() {
    let session = UUID::new_with_string("11111111-2222-4333-8444-555555555555".into()).unwrap();
    let completion = serde_json::to_value(SignalingCompleteRequest {
        voice_server_type: "webrtc".into(),
        viewer_session: session.to_string(),
        candidate: SignalingComplete { completed: true },
    })
    .unwrap();
    assert_eq!(completion["candidate"]["completed"], true);
    assert!(completion.get("candidates").is_none());

    let close = serde_json::to_value(CloseRequest {
        logout: true,
        voice_server_type: "webrtc".into(),
        viewer_session: session.to_string(),
        channel: None,
        credentials: None,
    })
    .unwrap();
    assert_eq!(close["logout"], true);
}

#[test]
fn native_opus_frames_are_nonempty_and_credentials_are_redacted() {
    let frames = encode_pcm_48k_mono(&vec![0_i16; 1_920]).unwrap();
    assert_eq!(frames.len(), 2);
    assert!(frames.iter().all(|frame| !frame.0.is_empty()));
    assert_eq!(
        format!("{:?}", VoiceSecret::new("capability-token").unwrap()),
        "VoiceSecret(<redacted>)"
    );
}
