Add more integration tests for client requests

This commit is contained in:
Dominik Nakamura 2020-12-31 14:47:54 +09:00
parent 5077adac0b
commit 0bdaccbf42
No known key found for this signature in database
GPG key ID: E4C6A749B2491910
7 changed files with 139 additions and 7 deletions

View file

@ -1,15 +1,17 @@
use std::env;
use std::sync::Once;
use std::time::Duration;
use anyhow::{ensure, Result};
use obws::{
responses::{Output, Scene, SceneCollection, SourceListItem},
responses::{Output, Profile, Scene, SceneCollection, SourceListItem},
Client,
};
use tokio::time;
pub const TEST_OUTPUT: &str = "virtualcam_output";
pub const TEST_COLLECTION: &str = "OBWS-TEST";
pub const TEST_PROFILE: &str = "OBWS-TEST";
pub const TEST_SCENE: &str = "OBWS-TEST-Scene";
pub const TEXT_SOURCE: &str = "OBWS-TEST-Text";
@ -21,8 +23,9 @@ pub async fn new_client() -> Result<Client> {
pretty_env_logger::init();
});
let client = Client::connect("localhost", 4444).await?;
client.login(std::env::var("OBS_PASSWORD").ok()).await?;
let host = env::var("OBS_HOST").unwrap_or_else(|_| "localhost".to_owned());
let client = Client::connect(host, 4444).await?;
client.login(env::var("OBS_PASSWORD").ok()).await?;
let collections = client.scene_collections().list_scene_collections().await?;
ensure!(
@ -37,7 +40,7 @@ pub async fn new_client() -> Result<Client> {
.await?;
// Give OBS some time to load the scene collection
time::sleep(Duration::from_millis(500)).await;
time::sleep(Duration::from_secs(1)).await;
ensure_obs_setup(&client).await?;
@ -72,6 +75,13 @@ async fn ensure_obs_setup(client: &Client) -> Result<()> {
"desktop audio device required for sources tests"
);
let profiles = client.profiles().list_profiles().await?;
ensure!(
profiles.iter().any(is_required_profile),
"profile `{}` not found, required for profiles tests",
TEST_PROFILE
);
Ok(())
}
@ -90,3 +100,18 @@ fn is_required_scene(scene: &Scene) -> bool {
fn is_required_source(source: &SourceListItem) -> bool {
source.name == TEXT_SOURCE && source.ty == "input" && source.type_id == "text_ft2_source_v2"
}
fn is_required_profile(profile: &Profile) -> bool {
profile.profile_name == TEST_PROFILE
}
#[allow(unused_macros)]
macro_rules! wait_for {
($expression:expr, $pattern:pat) => {
while let Some(Event { ty, .. }) = $expression.next().await {
if matches!(ty, $pattern) {
break;
}
}
};
}

View file

@ -7,7 +7,7 @@ use serde_json::json;
mod common;
#[tokio::test]
async fn general() -> Result<()> {
async fn main() -> Result<()> {
let client = common::new_client().await?;
let client = client.general();

View file

@ -7,7 +7,7 @@ use common::TEST_OUTPUT;
mod common;
#[tokio::test]
async fn general() -> Result<()> {
async fn main() -> Result<()> {
let client = common::new_client().await?;
let client = client.outputs();

28
tests/profiles.rs Normal file
View file

@ -0,0 +1,28 @@
#![cfg(feature = "test-integration")]
use std::time::Duration;
use anyhow::Result;
use tokio::time;
use common::TEST_PROFILE;
mod common;
#[tokio::test]
async fn main() -> Result<()> {
let client = common::new_client().await?;
let client = client.profiles();
client.list_profiles().await?;
let original = client.get_current_profile().await?;
client.set_current_profile(TEST_PROFILE).await?;
// Give OBS some time to switch profiles
time::sleep(Duration::from_millis(200)).await;
client.set_current_profile(&original).await?;
Ok(())
}

45
tests/recording.rs Normal file
View file

@ -0,0 +1,45 @@
#![cfg(feature = "test-integration")]
use std::path::Path;
use std::time::Duration;
use anyhow::Result;
use futures_util::{pin_mut, StreamExt};
use obws::events::{Event, EventType};
use tokio::time;
#[macro_use]
mod common;
#[tokio::test]
async fn main() -> Result<()> {
let client = common::new_client().await?;
let events = client.events();
let client = client.recording();
pin_mut!(events);
client.start_stop_recording().await?;
wait_for!(events, EventType::RecordingStarted);
client.start_stop_recording().await?;
wait_for!(events, EventType::RecordingStopped);
// Wait a little more as recording sometimes doesn't start when started/stopped frequently.
time::sleep(Duration::from_secs(1)).await;
client.start_recording().await?;
wait_for!(events, EventType::RecordingStarted);
// Pausing doesn't seem to work currently
// client.pause_recording().await?;
// wait_for!(events, EventType::RecordingPaused);
// client.resume_recording().await?;
// wait_for!(events, EventType::RecordingResumed);
client.stop_recording().await?;
wait_for!(events, EventType::RecordingStopped);
let original = client.get_recording_folder().await?;
client.set_recording_folder(Path::new("test")).await?;
client.set_recording_folder(&original).await?;
Ok(())
}

View file

@ -0,0 +1,34 @@
#![cfg(feature = "test-integration")]
use std::time::Duration;
use anyhow::{Context, Result};
use tokio::time;
use common::TEST_COLLECTION;
mod common;
#[tokio::test]
async fn main() -> Result<()> {
let client = common::new_client().await?;
let client = client.scene_collections();
let other = client
.list_scene_collections()
.await?
.into_iter()
.find(|sc| sc.sc_name != TEST_COLLECTION)
.context("only the test scene collection exists, but another is needed for tests")?
.sc_name;
let original = client.get_current_scene_collection().await?;
client.set_current_scene_collection(&other).await?;
// Give OBS some time to load the scene collection
time::sleep(Duration::from_secs(1)).await;
client.set_current_scene_collection(&original).await?;
Ok(())
}

View file

@ -16,7 +16,7 @@ use common::TEXT_SOURCE;
mod common;
#[tokio::test]
async fn sources() -> Result<()> {
async fn main() -> Result<()> {
let client = common::new_client().await?;
let client = client.sources();