mpvipc-async/tests/events.rs
h7x4 7eec34ce00
All checks were successful
Build and test / build (pull_request) Successful in 1m55s
Build and test / check (pull_request) Successful in 1m52s
Build and test / docs (pull_request) Successful in 2m21s
Build and test / test (pull_request) Successful in 4m9s
Build and test / check (push) Successful in 1m55s
Build and test / build (push) Successful in 1m57s
Build and test / docs (push) Successful in 2m43s
Build and test / test (push) Successful in 5m33s
split property parsing from event parsing:
High-level properties are now optional, considering there are about a
thousand of them to parse. The high-level properties are a few chosen
ones that I suspect might be useful for most people, with catch-all enum
variants for the less common ones.
2024-05-02 21:16:23 +02:00

72 lines
2.1 KiB
Rust

use std::panic;
use futures::{stream::StreamExt, SinkExt};
use mpvipc::{parse_event_property, Mpv, MpvDataType, MpvExt, Property};
use serde_json::json;
use test_log::test;
use tokio::{net::UnixStream, task::JoinHandle};
use tokio_util::codec::{Framed, LinesCodec, LinesCodecError};
fn test_socket(
answers: Vec<(bool, String)>,
) -> (UnixStream, JoinHandle<Result<(), LinesCodecError>>) {
let (socket, server) = UnixStream::pair().unwrap();
let join_handle = tokio::spawn(async move {
let mut framed = Framed::new(socket, LinesCodec::new());
for (unsolicited, answer) in answers {
if !unsolicited {
framed.next().await;
}
framed.send(answer).await?;
}
Ok(())
});
(server, join_handle)
}
#[test(tokio::test)]
async fn test_observe_event_successful() {
let (server, join_handle) = test_socket(vec![
(
false,
json!({ "request_id": 0, "error": "success" }).to_string(),
),
(
false,
json!({ "request_id": 0, "error": "success" }).to_string(),
),
(
true,
json!({ "data": 64.0, "event": "property-change", "id": 1, "name": "volume" })
.to_string(),
),
]);
let mpv = Mpv::connect_socket(server).await.unwrap();
mpv.observe_property(1, "volume").await.unwrap();
let mpv2 = mpv.clone();
tokio::spawn(async move {
let event = mpv2.get_event_stream().await.next().await.unwrap().unwrap();
let data = match parse_event_property(event) {
Ok((_, Property::Unknown { name, data })) => {
assert_eq!(name, "volume");
data
}
Ok((_, property)) => panic!("{:?}", property),
Err(err) => panic!("{:?}", err),
};
match data {
MpvDataType::Double(data) => assert_eq!(data, 64.0),
err => panic!("{:?}", err),
}
});
mpv.set_property("volume", 64.0).await.unwrap();
join_handle.await.unwrap().unwrap();
}