Files
client/core/tests/embedded.rs
T

83 lines
4.1 KiB
Rust

use std::{path::PathBuf, sync::atomic::{AtomicU64, Ordering}};
use teamspeach_core::{protocol::Command, runtime::{spawn, CoreConfig}};
static NEXT: AtomicU64 = AtomicU64::new(0);
struct Profile(PathBuf);
impl Profile {
fn new() -> Self {
Self(std::env::temp_dir().join(format!("teamspeach-embedded-{}-{}", std::process::id(), NEXT.fetch_add(1, Ordering::Relaxed))))
}
fn config(&self, slot: usize) -> CoreConfig {
CoreConfig { data_dir: self.0.join("core"), connection_dir: self.0.join("core/connections").join(slot.to_string()) }
}
}
impl Drop for Profile { fn drop(&mut self) { let _ = std::fs::remove_dir_all(&self.0); } }
#[tokio::test(flavor = "current_thread")]
async fn embedded_identity_survives_repeated_worker_lifetimes() {
let profile = Profile::new();
let (first, _events) = spawn(profile.config(1)).unwrap();
let created = first.request(Command::ProfileCreate { nickname: "Native owner".into() }).await.unwrap();
let uid = created["uid"].clone();
assert!(uid.as_str().is_some());
for _ in 0..100 {
let (other, _events) = spawn(profile.config(2)).unwrap();
let state = other.request(Command::ProfileGet).await.unwrap();
assert_eq!(state["uid"], uid);
other.shutdown().await.unwrap();
}
assert_eq!(first.request(Command::ProfileGet).await.unwrap()["uid"], uid);
first.shutdown().await.unwrap();
let (again, _events) = spawn(profile.config(1)).unwrap();
assert_eq!(again.request(Command::ProfileGet).await.unwrap()["uid"], uid);
again.shutdown().await.unwrap();
}
#[tokio::test(flavor = "current_thread")]
async fn transmit_mode_invalidates_frames_only_on_detector_change() {
use teamspeach_core::{audio::CapturedFrame, protocol::FRAME_SAMPLES, vad::GateMode};
let profile = Profile::new();
let (core, _events) = spawn(profile.config(1)).unwrap();
let direct = core.audio_epoch();
let frame = |capture_epoch| CapturedFrame { pcm: [1; FRAME_SAMPLES], captured_at: std::time::Instant::now(),
connection_generation: core.connection_generation(), capture_epoch, whisper: None };
let result = core.request(Command::SetTransmit { vad: None }).await.unwrap();
assert_eq!(result["mode"], "direct");
assert!(core.push_audio(frame(direct)));
core.request(Command::SetTransmit { vad: Some(GateMode::VolumeGate { level_db: -30. }) }).await.unwrap();
let vad = core.audio_epoch();
assert!(!core.push_audio(frame(direct)));
core.request(Command::SetTransmit { vad: Some(GateMode::VolumeGate { level_db: -25. }) }).await.unwrap();
assert!(core.push_audio(frame(vad)));
core.request(Command::SetTransmit { vad: None }).await.unwrap();
assert!(!core.push_audio(frame(vad)));
core.shutdown().await.unwrap();
}
#[tokio::test(flavor = "current_thread")]
async fn native_requests_validate_before_session_dispatch() {
use teamspeach_core::protocol::{ErrorCode, TextTarget};
let profile = Profile::new();
let (core, _events) = spawn(profile.config(1)).unwrap();
for (id, expected) in [(0, ErrorCode::BadRequest), (1, ErrorCode::NotConnected)] {
let error = core.request(Command::JoinChannel { channel_id: id, password: None }).await.unwrap_err();
assert_eq!(error.code, expected);
}
let error = core.request(Command::SendText { target: TextTarget::Client, client_id: None,
expected_client_uid: None, expected_channel_id: None, message: "private".into() }).await.unwrap_err();
assert_eq!(error.code, ErrorCode::BadRequest);
let error = core.request(Command::StreamSignal { stream_id: "s1".into(), client_id: 1, json: "[]".into() }).await.unwrap_err();
assert_eq!(error.code, ErrorCode::BadRequest);
core.shutdown().await.unwrap();
}
#[tokio::test(flavor = "current_thread")]
async fn duplicate_connection_slot_is_refused_and_released_after_shutdown() {
let profile = Profile::new();
let (core, _events) = spawn(profile.config(1)).unwrap();
assert!(spawn(profile.config(1)).is_err());
core.shutdown().await.unwrap();
let (next, _events) = spawn(profile.config(1)).unwrap();
next.shutdown().await.unwrap();
}