| | |
| | | use std::collections::HashSet; |
| | | |
| | | #[test] |
| | | fn production_vad_session_boundary_reads_updated_attributes() { |
| | | let config = SimpleVadConfig { |
| | | rms_threshold: 0.001, |
| | | peak_threshold: 0.01, |
| | | start_frames: 2, |
| | | end_silence_ms: 100, |
| | | min_speech_ms: 1, |
| | | max_turn_ms: 1_000, |
| | | initial_ignore_ms: 0, |
| | | }; |
| | | let mut vad = SimpleVad::new(config); |
| | | let samples = vec![1_000i16; 160]; |
| | | let frame = AudioFrame { |
| | | data: samples.as_slice().into(), |
| | | sample_rate: 16_000, |
| | | num_channels: 1, |
| | | samples_per_channel: 160, |
| | | }; |
| | | let mut attributes = std::collections::HashMap::from([ |
| | | ( |
| | | "inputSourceCategory".to_string(), |
| | | "controlled_fixture".to_string(), |
| | | ), |
| | | ( |
| | | "clientFixtureSequence".to_string(), |
| | | "fixture-01".to_string(), |
| | | ), |
| | | ]); |
| | | let mut starts = Vec::new(); |
| | | for (session_index, sequence) in [(1, "fixture-01"), (2, "fixture-02")] { |
| | | let was_in_speech = vad.in_speech; |
| | | vad.observe_frame( |
| | | "call-001", |
| | | "trace-001", |
| | | "participant", |
| | | "track", |
| | | session_index * 2 - 1, |
| | | 1_000 * session_index, |
| | | &frame, |
| | | ); |
| | | vad.observe_frame( |
| | | "call-001", |
| | | "trace-001", |
| | | "participant", |
| | | "track", |
| | | session_index * 2, |
| | | 1_000 * session_index + 10, |
| | | &frame, |
| | | ); |
| | | let is_in_speech = vad.in_speech; |
| | | assert!(!was_in_speech && is_in_speech); |
| | | attributes.insert("clientFixtureSequence".to_string(), sequence.to_string()); |
| | | let metadata = AudioIngressMetadata::from_participant(&attributes) |
| | | .expect("valid participant attributes") |
| | | .expect("controlled fixture metadata"); |
| | | let session_line = asr_realtime::session_start_line( |
| | | "call-001", |
| | | "trace-001", |
| | | &format!("turn-{session_index:04}"), |
| | | "nonce-001", |
| | | Some(&metadata), |
| | | ) |
| | | .expect("session start line"); |
| | | let session_json: serde_json::Value = |
| | | serde_json::from_slice(&session_line).expect("session start json"); |
| | | assert_eq!(sequence, session_json["clientFixtureSequence"]); |
| | | starts.push(metadata.client_fixture_sequence); |
| | | vad.reset_current_turn(); |
| | | } |
| | | assert_eq!(vec!["fixture-01", "fixture-02"], starts); |
| | | attributes.insert("inputSourceCategory".to_string(), "other".to_string()); |
| | | assert!(AudioIngressMetadata::from_participant(&attributes).is_err()); |
| | | assert!( |
| | | AudioIngressMetadata::from_participant(&std::collections::HashMap::new()) |
| | | .expect("missing attributes is absent") |
| | | .is_none() |
| | | ); |
| | | attributes.insert( |
| | | "clientFixtureSequence".to_string(), |
| | | "fixture-01".to_string(), |
| | | ); |
| | | assert!(AudioIngressMetadata::from_participant(&attributes).is_err()); |
| | | } |
| | | |
| | | #[test] |
| | | fn reply_chunk_marker_state_emits_turn_first_once_and_later_segment_first_once() { |
| | | let mut state = ReplyChunkMarkerState::default(); |
| | | |