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 |  110 +++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 106 insertions(+), 4 deletions(-)

diff --git a/src/main.rs b/src/main.rs
index d357e4a..9b08934 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -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,10 +775,10 @@
                 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");
@@ -3951,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