use axum::extract::{Json, Path, State}; use futures::stream::StreamExt; use crate::{ channel::routes, events::{app, types}, repo::channel, test::fixtures::{self, future::Immediately as _}, }; #[tokio::test] async fn messages_in_order() { // Set up the environment let app = fixtures::scratch_app().await; let sender = fixtures::login::create(&app).await; let channel = fixtures::channel::create(&app, &fixtures::now()).await; // Call the endpoint (twice) let requests = vec![ (fixtures::now(), fixtures::message::propose()), (fixtures::now(), fixtures::message::propose()), ]; for (sent_at, message) in &requests { let request = routes::SendRequest { message: message.clone(), }; routes::on_send( State(app.clone()), Path(channel.id.clone()), sent_at.clone(), sender.clone(), Json(request.clone()), ) .await .expect("sending to a valid channel"); } // Verify the semantics let subscribed_at = fixtures::now(); let events = app .events() .subscribe(&subscribed_at, types::ResumePoint::default()) .await .expect("subscribing to a valid channel") .take(requests.len()); let events = events.collect::>().immediately().await; for ((sent_at, message), types::ResumableEvent(_, event)) in requests.into_iter().zip(events) { assert_eq!(*sent_at, event.at); assert!(matches!( event.data, types::ChannelEventData::Message(event_message) if event_message.sender == sender && event_message.message.body == message )); } } #[tokio::test] async fn nonexistent_channel() { // Set up the environment let app = fixtures::scratch_app().await; let login = fixtures::login::create(&app).await; // Call the endpoint let sent_at = fixtures::now(); let channel = channel::Id::generate(); let request = routes::SendRequest { message: fixtures::message::propose(), }; let routes::ErrorResponse(error) = routes::on_send( State(app), Path(channel.clone()), sent_at, login, Json(request), ) .await .expect_err("sending to a nonexistent channel"); // Verify the structure of the response assert!(matches!( error, app::EventsError::ChannelNotFound(error_channel) if channel == error_channel )); }