From c8464e075cc66704c0f9210113a3b852121c5d2a Mon Sep 17 00:00:00 2001
From: cai <cai@nbcai.cc>
Date: Sat, 08 Aug 2026 18:55:07 +0800
Subject: [PATCH] test: cover participant binding rejection

---
 src/main.rs |  131 +++++++++++++++++++++++++++++++++++++------
 1 files changed, 111 insertions(+), 20 deletions(-)

diff --git a/src/main.rs b/src/main.rs
index c3fa805..9b08934 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -30,7 +30,7 @@
     options::TrackPublishOptions,
     prelude::{
         DataPacket, LocalAudioTrack, LocalTrack, ParticipantIdentity, RemoteAudioTrack,
-        RemoteTrack, Room, RoomEvent, RoomOptions,
+        RemoteParticipant, RemoteTrack, Room, RoomEvent, RoomOptions,
     },
 };
 use reqwest::Client;
@@ -737,6 +737,10 @@
     })
 }
 
+fn is_bound_user_participant(identity: &str, expected: Option<&str>) -> bool {
+    expected.is_none_or(|value| identity == value)
+}
+
 async fn observe_user_audio_events(
     mut events: UnboundedReceiver<RoomEvent>,
     call_id: String,
@@ -771,25 +775,15 @@
                 publication: _,
                 participant,
             } => {
-                if user_participant_identity
-                    .as_deref()
-                    .is_some_and(|expected| participant.identity().to_string() != expected)
-                {
+                if !is_bound_user_participant(
+                    &participant.identity().to_string(),
+                    user_participant_identity.as_deref(),
+                ) {
                     warn!(call_id = %call_id, trace_id = %trace_id,
                         metadata_status = "wrong_participant",
                         "runtime helper ignored non-user audio participant");
                     continue;
                 }
-                let ingress_metadata =
-                    match AudioIngressMetadata::from_participant(&participant.attributes()) {
-                        Ok(value) => value,
-                        Err(reason) => {
-                            warn!(call_id = %call_id, trace_id = %trace_id,
-                            metadata_status = "invalid", reason = reason,
-                            "runtime helper ignored invalid audio ingress metadata");
-                            None
-                        }
-                    };
                 let participant_alias = redact(&participant.identity().to_string());
                 let track_sid_alias = redact(&track.sid().to_string());
                 let track_name = track.name();
@@ -801,9 +795,6 @@
                     track_sid_alias = %track_sid_alias,
                     track_name = %track_name,
                     track_source = %track_source,
-                    metadata_status = if ingress_metadata.is_some() { "bound" } else { "absent" },
-                    metadata_source = ingress_metadata.as_ref().map(|_| "controlled_fixture"),
-                    metadata_sequence_present = ingress_metadata.is_some(),
                     "runtime helper user_track_subscribed"
                 );
                 spawn_user_audio_frame_observer(
@@ -818,6 +809,7 @@
                     turn_bridge_config.clone(),
                     http.clone(),
                     sink.clone(),
+                    participant,
                 );
             }
             RoomEvent::TrackSubscribed {
@@ -2929,6 +2921,7 @@
     turn_bridge_config: TurnBridgeConfig,
     http: Client,
     sink: Arc<BotAudioOutputSink>,
+    participant: RemoteParticipant,
 ) -> JoinHandle<()> {
     tokio::spawn(async move {
         let mut stream = NativeAudioStream::new(
@@ -3054,14 +3047,14 @@
 
                     if !was_in_speech && is_in_speech {
                         let turn_id = format!("turn-{:04}", vad.turn_index);
-                        match RealtimeAsrUpload::start(
+                        match RealtimeAsrUpload::start_with_participant_attributes(
                             http.clone(),
                             turn_bridge_config.realtime_asr_config(),
                             &call_id,
                             &trace_id,
                             &turn_id,
                             &vad.speech_samples,
-                            ingress_metadata.as_ref(),
+                            || participant.attributes(),
                         ) {
                             Ok(upload) => {
                                 info!(
@@ -3962,6 +3955,104 @@
     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 production_observer_rejects_wrong_participant_before_vad_session() {
+        assert!(!is_bound_user_participant(
+            "participant-other",
+            Some("participant-user")
+        ));
+        assert!(is_bound_user_participant(
+            "participant-user",
+            Some("participant-user")
+        ));
+        assert!(is_bound_user_participant("participant-any", None));
+    }
+
+    #[test]
     fn reply_chunk_marker_state_emits_turn_first_once_and_later_segment_first_once() {
         let mut state = ReplyChunkMarkerState::default();
 

--
Gitblit v1.9.3