use crate::{Authenticated, XmppConnection}; use lavina_core::player::{PlayerConnection, PlayerId}; use lavina_core::repo::{Storage, StorageConfig}; use lavina_core::LavinaCore; use prometheus::Registry as MetricsRegistry; use proto_xmpp::bind::{BindRequest, BindResponse, Jid, Name, Resource, Server}; pub(crate) struct TestServer { pub metrics: MetricsRegistry, pub storage: Storage, pub core: LavinaCore, } impl TestServer { pub async fn start() -> anyhow::Result { let _ = tracing_subscriber::fmt::try_init(); let metrics = MetricsRegistry::new(); let storage = Storage::open(StorageConfig { db_path: ":memory:".into(), }) .await?; let core = LavinaCore::new(metrics.clone(), storage.clone()).await?; Ok(TestServer { metrics, storage, core }) } pub async fn reboot(self) -> anyhow::Result { self.core.shutdown().await?; let metrics = MetricsRegistry::new(); let core = LavinaCore::new(metrics.clone(), self.storage.clone()).await?; Ok(TestServer { metrics, storage: self.storage.clone(), core, }) } pub async fn shutdown(self) -> anyhow::Result<()> { self.core.shutdown().await?; self.storage.close().await?; Ok(()) } } pub async fn expect_user_authenticated<'a>( server: &'a TestServer, user: &'a Authenticated, conn: &'a mut PlayerConnection, ) -> anyhow::Result> { let conn = XmppConnection { user: &user, user_handle: conn, rooms: &server.core.rooms, hostname: "localhost".into(), hostname_rooms: "rooms.localhost".into(), }; let result = conn.bind(&BindRequest(Resource("whatever".into()))).await; let expected = BindResponse(Jid { name: Some(Name("tester".into())), server: Server("localhost".into()), resource: Some(Resource("tester".into())), }); assert_eq!(expected, result); Ok(conn) }