diff --git a/crates/voelin-core/Cargo.toml b/crates/voelin-core/Cargo.toml index 5b507cb..886cf3c 100644 --- a/crates/voelin-core/Cargo.toml +++ b/crates/voelin-core/Cargo.toml @@ -43,7 +43,7 @@ tsclientlib = { path = "../proto/tsclientlib" } tsproto-packets = { path = "../proto/tsproto-packets" } [dev-dependencies] -tokio = { version = "1", features = ["macros", "rt-multi-thread", "test-util"] } +tokio = { version = "1", features = ["macros", "net", "rt-multi-thread", "test-util"] } [lints] workspace = true diff --git a/crates/voelin-core/src/history.rs b/crates/voelin-core/src/history.rs index 0ce7ed4..4af91fa 100644 --- a/crates/voelin-core/src/history.rs +++ b/crates/voelin-core/src/history.rs @@ -211,7 +211,11 @@ impl Drop for Inner { fn drop(&mut self) { // Closing the queue ends the writer after the pending jobs. self.jobs.get_mut().unwrap_or_else(PoisonError::into_inner).take(); - if let Some(thread) = self.thread.get_mut().unwrap_or_else(PoisonError::into_inner).take() { + if let Some(thread) = self.thread.get_mut().unwrap_or_else(PoisonError::into_inner).take() + // The last handle may go with a job's callback, on the writer + // itself: it ends on its own once the queue is empty. + && thread.thread().id() != std::thread::current().id() + { let _ = thread.join(); } } @@ -731,3 +735,72 @@ pub(crate) fn now_ms() -> i64 { .map(|d| d.as_millis() as i64) .unwrap_or_default() } + +#[cfg(test)] +mod tests { + use std::sync::mpsc as std_mpsc; + + use super::*; + + fn live(text: &str, ts_ms: i64) -> NewMessage { + let msg = ChatMessage { + target: ChatTarget::Channel(1), + author_name: "a".into(), + author_uid: Some("uid-a".into()), + author_id: None, + text: text.into(), + ts_ms, + via_relay: false, + }; + new_message("srv", &msg, MessageSource::Voice) + } + + /// Writes queued together run in one batch, each answered with its own + /// rows, in order; reads see them. + #[tokio::test] + async fn writes_are_batched_and_answered_in_order() { + let path = std::env::temp_dir() + .join(format!("voelin-history-{}", std::process::id())) + .join("client.db"); + let _ = std::fs::remove_dir_all(path.parent().unwrap()); + let history = History::open(&path).unwrap(); + let (tx, rx) = std_mpsc::channel(); + for i in 0..50 { + let tx = tx.clone(); + let batch = vec![live(&format!("m{i}"), i), live(&format!("n{i}"), i)]; + history.write(false, batch, 5000, move |r| { + let texts: Vec = r.unwrap().into_iter().map(|w| w.message.text).collect(); + tx.send((i, texts)).unwrap(); + }); + } + history.flush(); + let answers: Vec<_> = rx.try_iter().collect(); + assert_eq!(answers.len(), 50); + for (n, (i, texts)) in answers.into_iter().enumerate() { + assert_eq!(i, n as i64); + assert_eq!(texts, [format!("m{i}"), format!("n{i}")]); + } + let page = history + .page( + "srv", + &ChatTarget::Channel(1), + PageQuery { limit: Some(2), ..Default::default() }, + false, + ) + .await + .unwrap(); + assert_eq!( + page.iter().map(|m| m.message.text.as_str()).collect::>(), + ["m49", "n49"] + ); + assert!(page[0].id > 0); + // Kept in memory only: negative ids, not in the file. + let kept = history.write_async(true, vec![live("secret", 99)], 5000).await.unwrap(); + assert!(kept[0].message.id < 0); + let stored = + history.page("srv", &ChatTarget::Channel(1), PageQuery::default(), false).await; + assert_eq!(stored.unwrap().len(), 100); + drop(history); + std::fs::remove_dir_all(path.parent().unwrap()).unwrap(); + } +} diff --git a/crates/voelin-core/src/session.rs b/crates/voelin-core/src/session.rs index a07dee9..31ec212 100644 --- a/crates/voelin-core/src/session.rs +++ b/crates/voelin-core/src/session.rs @@ -512,6 +512,12 @@ impl Session { }) } + /// `chat.store_history` off: the history (and what it remembers about + /// servers) stays in memory. + fn memory(&self) -> bool { + !self.settings.current().get(&crate::settings::CHAT_STORE_HISTORY) + } + /// Show a chat's stored messages, then sync it with the gateway. fn open_history(&self, target: &ChatTarget) { if let Some(ctx) = self.chat_ctx() { @@ -536,7 +542,7 @@ impl Session { } let tx = self.sources_tx.clone(); self.history.current().run_then( - false, + self.memory(), move |s| s.server_alias(&alias), move |uid| { if let Ok(Some(uid)) = uid { @@ -556,7 +562,7 @@ impl Session { let aliases = self.aliases.clone(); let key = uid.clone(); self.history.current().run_then( - false, + self.memory(), move |s| aliases.iter().try_for_each(|a| s.set_server_alias(a, &key)), |r| { if let Err(e) = r { diff --git a/crates/voelin-core/src/stream.rs b/crates/voelin-core/src/stream.rs index ec2ae21..4373cf1 100644 --- a/crates/voelin-core/src/stream.rs +++ b/crates/voelin-core/src/stream.rs @@ -664,6 +664,25 @@ mod tests { audio: Option, settings: &Settings, ) -> (Handles, broadcast::Receiver, broadcast::Receiver) { + let (handles, rx, frames, _) = pair_full(audio, settings, false); + (handles, rx, frames) + } + + /// [`pair_with`]; with `late`, the server does not tell the viewer + /// (session 2) about streams, as for a client that joined after the + /// stream started, and does not answer lookups. Also returns what the + /// streamer (session 1) reports about its own stream. + fn pair_full( + audio: Option, + settings: &Settings, + late: bool, + ) -> ( + Handles, + broadcast::Receiver, + broadcast::Receiver, + mpsc::UnboundedReceiver, + ) { + let (own_tx, own_rx) = mpsc::unbounded_channel(); let (events, rx) = broadcast::channel(4096); let (frames, frames_rx) = broadcast::channel(4096); let mut voices = Vec::new(); @@ -682,7 +701,7 @@ mod tests { frames.clone(), if session == 2 { audio.take() } else { None }, SharedSettings::new(settings.clone()), - mpsc::unbounded_channel().0, + if session == 1 { own_tx.clone() } else { mpsc::unbounded_channel().0 }, )); voices.push(voice_rx); } @@ -694,6 +713,10 @@ mod tests { while let Some(cmd) = voice_rx.recv().await { if let VoiceCmd::Stream(request) = cmd { for (to, n) in relay(CLIENTS[i], request) { + let announcement = matches!(n, StreamNotification::Started { .. }); + if late && to == CLIENTS[1] && announcement { + continue; + } let index = CLIENTS.iter().position(|c| *c == to).unwrap(); handles[index].send(StreamInput::Notification(n)); } @@ -701,7 +724,7 @@ mod tests { } }); } - (handles, rx, frames_rx) + (handles, rx, frames_rx, own_rx) } /// Session 1 streams (auto-accepting), session 2 watches; returns the sink @@ -880,6 +903,92 @@ mod tests { feeder.await.unwrap(); } + /// A viewer that was not told about a running stream, and whose server + /// lookup goes unanswered, finds it in the gateway directory and + /// watches it; the streamer reports its stream's life for the directory. + #[tokio::test(flavor = "multi_thread")] + async fn late_viewer_finds_the_stream_in_the_gateway_directory() { + let (handles, mut rx, _frames, mut own) = pair_full(None, &Settings::in_memory(), true); + let setup = StreamSetup { + name: "directory test".into(), + kind: StreamKind::Window, + ..Default::default() + }; + handles[0].send(StreamInput::Start { setup, auto_accept: true }); + wait(&mut rx, |e| match e { + Event::StreamState { session: 1, state: StreamState::Live { .. } } => Some(()), + _ => None, + }) + .await; + let live = timeout(Duration::from_secs(5), own.recv()).await.unwrap().unwrap(); + assert_eq!( + live, + OwnStreamEvent::Live { + id: "s-1".into(), + title: "directory test".into(), + kind: StreamKind::Window + } + ); + // Both in channel 1, the streamer streaming: the viewer asks the + // server, which does not answer. + let clients = BTreeMap::from([ + (CLIENTS[0], ClientState { channel: 1, streaming: Some(true) }), + (CLIENTS[1], ClientState { channel: 1, streaming: Some(false) }), + ]); + handles[1].send(StreamInput::Clients(clients)); + tokio::time::sleep(Duration::from_millis(200)).await; + // The gateway's directory has it. + let entry = StreamInfo { + id: "s-1".into(), + streamer: ClientId(CLIENTS[0]), + name: "directory test".into(), + kind: parse_kind(&kind_name(StreamKind::Window)), + bitrate: 0, + viewer_limit: 0, + audio: true, + }; + handles[1].send(StreamInput::Directory(vec![entry])); + wait(&mut rx, |e| match e { + Event::StreamsChanged { session: 2, streams } + if streams.iter().any(|s| s.id == "s-1") => + { + Some(()) + } + _ => None, + }) + .await; + handles[1].send(StreamInput::Watch { stream_id: "s-1".into() }); + wait(&mut rx, |e| match e { + Event::WatchState { session: 2, state: WatchState::Connected, .. } => Some(()), + _ => None, + }) + .await; + // One connected viewer, then the end. + let viewers = timeout(Duration::from_secs(5), own.recv()).await.unwrap().unwrap(); + assert_eq!(viewers, OwnStreamEvent::Viewers(1)); + handles[0].send(StreamInput::Stop); + loop { + match timeout(Duration::from_secs(5), own.recv()).await.unwrap().unwrap() { + OwnStreamEvent::Ended { id } => break assert_eq!(id, "s-1"), + OwnStreamEvent::Viewers(_) => {} + other => panic!("{other:?}"), + } + } + for h in handles.iter() { + h.send(StreamInput::Shutdown("done".into())); + } + } + + #[test] + fn kind_names_round_trip() { + for kind in + [StreamKind::Camera, StreamKind::Screen, StreamKind::Window, StreamKind::Other(9)] + { + assert_eq!(parse_kind(&kind_name(kind)), kind); + } + assert_eq!(parse_kind("game"), StreamKind::Screen); + } + /// `stream.permissions` answers join requests unless it is `friends`. #[tokio::test(flavor = "multi_thread")] async fn permissions_answer_join_requests() { diff --git a/crates/voelin-core/tests/gateway.rs b/crates/voelin-core/tests/gateway.rs new file mode 100644 index 0000000..9fb294f --- /dev/null +++ b/crates/voelin-core/tests/gateway.rs @@ -0,0 +1,707 @@ +//! Chat history and gateway features through the engine, against a fake +//! gateway (a WebSocket server speaking the tsgw protocol from memory). + +use std::collections::{BTreeMap, BTreeSet, HashSet}; +use std::sync::{Arc, Mutex}; +use std::time::Duration; + +use futures::{SinkExt, StreamExt}; +use serde_json::json; +use tokio::net::TcpListener; +use tokio::sync::broadcast::Receiver; +use tokio::sync::mpsc; +use tokio::time::timeout; +use tokio_tungstenite::tungstenite::Message; +use tokio_tungstenite::tungstenite::handshake::server::{Request, Response}; +use voelin_core::gateway::{GatewayRequest, GatewayUpdate}; +use voelin_core::history::MessageSource; +use voelin_core::settings::Settings; +use voelin_core::{Command, Engine, Event, History, HistoryMessage, HistorySource, ObserveState}; +use voelin_gateway_proto::{ + ClientMsg, Envelope, ErrorCode, HistoryEntry, HistoryPage, PinInfo, ReactionCount, ServerMsg, + TopicInfo, UserRef, feature, +}; +use voelin_model::{ChatMessage, ChatTarget, PresenceSnapshot}; + +const LOBBY: ChatTarget = ChatTarget::Channel(1); +const ME: &str = "me-uid"; + +/// A connected session: its pushes and open chats. +type Session = (mpsc::UnboundedSender, Arc>>); + +/// The gateway's data. +#[derive(Default)] +struct State { + messages: Vec, + rev: i64, + /// (message, emoji) → who reacted. + reactions: BTreeMap<(i64, String), BTreeSet>, + topics: Vec, + /// Push channels of the connected sessions, with their open chats. + sessions: Vec, + /// Types of the requests received. + requests: Vec, + /// Close connections at once (the gateway is down). + refuse: bool, +} + +impl State { + fn next_rev(&mut self) -> i64 { + self.rev += 1; + self.rev + } + + /// A message as `uid` sees it. + fn view(&self, entry: &HistoryEntry, uid: &str) -> HistoryEntry { + let mut e = entry.clone(); + e.reactions = self + .reactions + .iter() + .filter(|((id, _), users)| *id == entry.id && !users.is_empty()) + .map(|((_, emoji), users)| ReactionCount { + emoji: emoji.clone(), + count: users.len() as u32, + me: users.contains(uid), + }) + .collect(); + e + } + + fn add(&mut self, target: ChatTarget, author: &str, text: &str, ts_ms: i64) -> HistoryEntry { + let rev = self.next_rev(); + let message = ChatMessage { + target, + author_name: author.into(), + author_uid: Some(format!("{author}-uid")), + author_id: None, + text: text.into(), + ts_ms, + via_relay: true, + }; + let entry = + HistoryEntry { rev, ..HistoryEntry::new(self.messages.len() as i64 + 1, message) }; + self.messages.push(entry.clone()); + self.push_open(&entry.message.target, ServerMsg::Message { entry: entry.clone() }); + entry + } + + fn push_open(&self, target: &ChatTarget, msg: ServerMsg) { + for (tx, open) in &self.sessions { + if open.lock().unwrap().contains(target) { + let _ = tx.send(msg.clone()); + } + } + } + + fn page(&self, q: &voelin_gateway_proto::HistoryQuery) -> HistoryPage { + let mut all: Vec<&HistoryEntry> = self + .messages + .iter() + .filter(|e| e.message.target == q.target) + .filter(|e| q.topic.is_none_or(|t| e.topic_id == Some(t))) + .filter(|e| q.before.is_none_or(|b| e.id < b)) + .filter(|e| q.before_ms.is_none_or(|b| e.message.ts_ms < b)) + .filter(|e| q.after.is_none_or(|a| e.id > a)) + .collect(); + let mut has_more = false; + if let Some(limit) = q.limit.map(|l| l as usize) + && all.len() > limit + { + has_more = true; + all = all.split_off(all.len() - limit); + } + HistoryPage { + target: q.target.clone(), + topic: q.topic, + messages: all.into_iter().map(|e| self.view(e, ME)).collect(), + has_more, + } + } +} + +struct FakeGateway { + state: Arc>, + url: String, +} + +impl FakeGateway { + async fn start() -> Self { + let listener = TcpListener::bind("127.0.0.1:0").await.unwrap(); + let url = format!("ws://{}/v1", listener.local_addr().unwrap()); + let state: Arc> = Arc::default(); + let st = state.clone(); + tokio::spawn(async move { + while let Ok((tcp, _)) = listener.accept().await { + tokio::spawn(serve(tcp, st.clone())); + } + }); + Self { state, url } + } + + fn add(&self, target: ChatTarget, author: &str, text: &str, ts_ms: i64) -> HistoryEntry { + self.state.lock().unwrap().add(target, author, text, ts_ms) + } + + fn requests(&self) -> Vec { + self.state.lock().unwrap().requests.clone() + } +} + +fn caps() -> Vec { + [ + feature::PRESENCE, + feature::RELAY, + feature::HISTORY, + feature::PINS, + feature::REACTIONS, + feature::TOPICS, + feature::EVENTS, + ] + .map(String::from) + .to_vec() +} + +async fn serve(tcp: tokio::net::TcpStream, state: Arc>) { + if state.lock().unwrap().refuse { + return; + } + #[allow(clippy::result_large_err)] // tungstenite's callback type + let callback = |_: &Request, mut response: Response| { + response + .headers_mut() + .insert("Sec-WebSocket-Protocol", voelin_gateway_proto::SUBPROTOCOL.parse().unwrap()); + Ok(response) + }; + let ws = tokio_tungstenite::accept_hdr_async(tcp, callback).await.unwrap(); + let (mut sink, mut stream) = ws.split(); + let (tx, mut rx) = mpsc::unbounded_channel::>(); + tokio::spawn(async move { + while let Some(env) = rx.recv().await { + let text = serde_json::to_string(&env).unwrap(); + if sink.send(Message::Text(text.into())).await.is_err() { + break; + } + } + let _ = sink.close().await; + }); + let _ = tx.send(Envelope::new(ServerMsg::Hello { + gateway_id: "fake-gw".into(), + server_uid: "fake-server".into(), + server_name: "Fake".into(), + nonce: "n".into(), + capabilities: caps(), + })); + // Pushes of this session. + let (push_tx, mut push_rx) = mpsc::unbounded_channel(); + let open: Arc>> = Arc::default(); + let forward = tx.clone(); + tokio::spawn(async move { + while let Some(msg) = push_rx.recv().await { + if forward.send(Envelope::new(msg)).is_err() { + break; + } + } + }); + let mut logged_in = false; + while let Some(Ok(frame)) = stream.next().await { + let Message::Text(text) = frame else { continue }; + let env: Envelope = serde_json::from_str(text.as_str()).unwrap(); + let id = env.id.unwrap_or(0); + let reply = { + let mut st = state.lock().unwrap(); + let name = serde_json::to_value(&env.msg).unwrap()["type"].as_str().unwrap().to_owned(); + st.requests.push(name); + answer(&mut st, env.msg, &open, &push_tx, &mut logged_in) + }; + if tx.send(Envelope::with_id(id, reply)).is_err() { + break; + } + } + let mut st = state.lock().unwrap(); + st.sessions.retain(|(t, _)| !t.same_channel(&push_tx)); +} + +fn answer( + st: &mut State, + msg: ClientMsg, + open: &Arc>>, + push: &mpsc::UnboundedSender, + logged_in: &mut bool, +) -> ServerMsg { + match msg { + ClientMsg::Auth { .. } => { + *logged_in = true; + st.sessions.push((push.clone(), open.clone())); + ServerMsg::AuthOk { + uid: ME.into(), + token: "t".into(), + token_expires: i64::MAX, + capabilities: caps(), + } + } + _ if !*logged_in => { + ServerMsg::Error { code: ErrorCode::NotAuthenticated, message: "".into() } + } + ClientMsg::Enable { .. } => ServerMsg::Enabled { features: caps() }, + ClientMsg::SubscribePresence => { + let snapshot = PresenceSnapshot { + server_name: "Fake".into(), + channels: Vec::new(), + clients: Vec::new(), + }; + let _ = push.send(ServerMsg::PresenceSnapshot { seq: 0, snapshot }); + ServerMsg::Ok + } + ClientMsg::OpenChat { target } => { + open.lock().unwrap().insert(target); + ServerMsg::Ok + } + ClientMsg::CloseChat { target } => { + open.lock().unwrap().remove(&target); + ServerMsg::Ok + } + ClientMsg::SendChat { target, text } => { + // Posted as "me" (uid `me-uid`). + st.add(target, "me", &text, now_ms()); + ServerMsg::Ok + } + ClientMsg::Post { target, text, topic } => { + let rev = st.next_rev(); + let message = ChatMessage { + target: target.clone(), + author_name: "me".into(), + author_uid: Some(ME.into()), + author_id: None, + text, + ts_ms: now_ms(), + via_relay: true, + }; + let entry = HistoryEntry { + rev, + topic_id: topic, + ..HistoryEntry::new(st.messages.len() as i64 + 1, message) + }; + st.messages.push(entry.clone()); + st.push_open(&target, ServerMsg::Message { entry: entry.clone() }); + ServerMsg::Posted { entry } + } + ClientMsg::QueryHistory(q) => ServerMsg::HistoryPage(st.page(&q)), + ClientMsg::Sync { target, since_rev, limit } => { + let mut changed: Vec = st + .messages + .iter() + .filter(|e| e.message.target == target && e.rev > since_rev) + .map(|e| st.view(e, ME)) + .collect(); + changed.sort_by_key(|e| e.rev); + let has_more = limit.is_some_and(|l| changed.len() > l as usize); + if let Some(l) = limit { + changed.truncate(l as usize); + } + let rev = changed.last().map_or(since_rev, |e| e.rev); + ServerMsg::SyncPage { target, messages: changed, rev, has_more } + } + ClientMsg::Pin { message_id } => { + let rev = st.next_rev(); + let Some(e) = st.messages.iter_mut().find(|e| e.id == message_id) else { + return ServerMsg::Error { code: ErrorCode::NotFound, message: "".into() }; + }; + e.pinned = true; + e.rev = rev; + let entry = e.clone(); + let pin = PinInfo { entry: st.view(&entry, ME), by: me(), ts_ms: now_ms() }; + st.push_open( + &entry.message.target, + ServerMsg::Pinned { target: entry.message.target.clone(), pin }, + ); + ServerMsg::Ok + } + ClientMsg::ListPins { target } => ServerMsg::Pins { + pins: st + .messages + .iter() + .filter(|e| e.pinned && e.message.target == target) + .map(|e| PinInfo { entry: st.view(e, ME), by: me(), ts_ms: 1 }) + .collect(), + target, + }, + ClientMsg::React { message_id, emoji } => { + let rev = st.next_rev(); + let Some(e) = st.messages.iter_mut().find(|e| e.id == message_id) else { + return ServerMsg::Error { code: ErrorCode::NotFound, message: "".into() }; + }; + e.rev = rev; + let target = e.message.target.clone(); + let users = st.reactions.entry((message_id, emoji.clone())).or_default(); + users.insert(ME.into()); + let count = users.len() as u32; + let push = ServerMsg::Reaction { + target: target.clone(), + message_id, + emoji, + user: me(), + added: true, + count, + }; + st.push_open(&target, push); + ServerMsg::Ok + } + ClientMsg::CreateTopic { target, title, message_id } => { + let topic = TopicInfo { + id: st.topics.len() as i64 + 1, + target, + title, + creator: me(), + created_ms: 1, + root_message_id: message_id, + last_activity_ms: 1, + message_count: 0, + archived: false, + }; + st.topics.push(topic.clone()); + ServerMsg::Topic { topic } + } + ClientMsg::SubscribeEvents | ClientMsg::Ping => ServerMsg::Ok, + _ => ServerMsg::Error { code: ErrorCode::UnknownType, message: "not in the fake".into() }, + } +} + +fn me() -> UserRef { + UserRef { uid: ME.into(), name: "me".into() } +} + +fn now_ms() -> i64 { + std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_millis() as i64 +} + +/// The next event matching `f`, within 10 s. +async fn wait(rx: &mut Receiver, what: &str, mut f: impl FnMut(Event) -> Option) -> T { + timeout(Duration::from_secs(10), async { + loop { + match rx.recv().await { + Ok(e) => { + if let Some(t) = f(e) { + return t; + } + } + Err(tokio::sync::broadcast::error::RecvError::Lagged(_)) => {} + Err(e) => panic!("{e}"), + } + } + }) + .await + .unwrap_or_else(|_| panic!("timed out waiting for {what}")) +} + +type Batch = (Vec, HistorySource, bool); + +async fn batch(rx: &mut Receiver, source: HistorySource) -> Batch { + wait(rx, &format!("{source:?} history"), |e| match e { + Event::ChatHistory { messages, source: s, complete, target, .. } + if s == source && target == LOBBY => + { + Some((messages, s, complete)) + } + _ => None, + }) + .await +} + +fn texts(messages: &[HistoryMessage]) -> Vec<&str> { + messages.iter().map(|m| m.message.text.as_str()).collect() +} + +async fn update( + rx: &mut Receiver, + what: &str, + mut f: impl FnMut(&GatewayUpdate) -> bool, +) -> GatewayUpdate { + wait(rx, what, |e| match e { + Event::Gateway { update, .. } if f(&update) => Some(update), + _ => None, + }) + .await +} + +fn observe(engine: &Engine, session: u64, gw: &FakeGateway) { + let identity = tsclientlib::Identity::create(); + engine.send(Command::ObserveGateway { + session, + url: gw.url.clone(), + identity: Box::new(identity), + }); +} + +fn temp_db(tag: &str) -> std::path::PathBuf { + let dir = std::env::temp_dir().join(format!("voelin-core-{tag}-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + dir.join("client.db") +} + +/// Opening a chat shows the stored page, then the gateway's; older pages +/// come from the gateway; what was said while we were away arrives on the +/// next connect; the database keeps it all. +#[tokio::test(flavor = "multi_thread")] +async fn history_on_open_paging_and_offline_messages() { + let gw = FakeGateway::start().await; + for i in 1..=5 { + gw.add(LOBBY, "alice", &format!("m{i}"), i * 1000); + } + gw.add(ChatTarget::Server, "alice", "elsewhere", 500); + let path = temp_db("history"); + let settings = Settings::in_memory(); + settings.set_json("chat.history_page", json!(3)).unwrap(); + let engine = Engine::start_with(settings.clone(), History::open(&path).unwrap()); + let mut rx = engine.subscribe(); + + engine.send(Command::OpenChat { session: 1, target: LOBBY }); + observe(&engine, 1, &gw); + // Nothing stored yet; then the gateway's latest page; then in sync. + let (local, _, complete) = batch(&mut rx, HistorySource::Local).await; + assert!(local.is_empty() && !complete); + let (page, _, complete) = batch(&mut rx, HistorySource::Gateway).await; + assert_eq!(texts(&page), ["m3", "m4", "m5"]); + assert!(!complete, "older messages exist"); + assert!(page.iter().all(|m| m.remote_id.is_some() && m.source == MessageSource::Gateway)); + let (synced, _, _) = batch(&mut rx, HistorySource::Gateway).await; + assert!(synced.is_empty(), "nothing new: {synced:?}"); + + // Older: the gateway's page before m3. + engine.send(Command::LoadOlderHistory { session: 1, target: LOBBY, before: Some(page[0].id) }); + let (older, _, complete) = batch(&mut rx, HistorySource::Gateway).await; + assert_eq!(texts(&older), ["m1", "m2"]); + assert!(complete, "the beginning"); + + // Away: two messages and a pin on m2. + engine.send(Command::StopObserving { session: 1 }); + wait(&mut rx, "gateway stopped", |e| match e { + Event::State { state, .. } if state.observe == ObserveState::Off => Some(()), + _ => None, + }) + .await; + gw.add(LOBBY, "bob", "while away 1", 6000); + gw.add(LOBBY, "bob", "while away 2", 7000); + { + let mut st = gw.state.lock().unwrap(); + let rev = st.next_rev(); + let m2 = st.messages.iter_mut().find(|e| e.message.text == "m2").unwrap(); + m2.pinned = true; + m2.rev = rev; + } + // Back: stored page at once, then what we missed. + observe(&engine, 1, &gw); + let (local, _, _) = batch(&mut rx, HistorySource::Local).await; + assert_eq!(texts(&local), ["m3", "m4", "m5"]); + let mut missed = Vec::new(); + while missed.len() < 3 { + missed.extend(batch(&mut rx, HistorySource::Gateway).await.0); + } + missed.sort_by_key(|m| (m.message.ts_ms, m.id)); + assert_eq!(texts(&missed), ["m2", "while away 1", "while away 2"]); + assert!(missed[0].pinned); + assert_eq!(missed[0].id, older[1].id, "the stored row was updated"); + + // It is all in the database, oldest first. + engine.send(Command::CloseSession { session: 1 }); + let history = engine.history(); + let stored = history.page("fake-server", &LOBBY, Default::default(), false).await.unwrap(); + assert_eq!(texts(&stored), ["m1", "m2", "m3", "m4", "m5", "while away 1", "while away 2"]); + history.flush(); + drop((engine, history)); + + // After a restart, with the gateway down: the stored page, found by the + // gateway's URL, which is remembered with the server's id. + gw.state.lock().unwrap().refuse = true; + let engine = Engine::start_with(settings, History::open(&path).unwrap()); + let mut rx = engine.subscribe(); + engine.send(Command::OpenChat { session: 7, target: LOBBY }); + observe(&engine, 7, &gw); + let (local, source) = wait(&mut rx, "history without the gateway", |e| match e { + Event::ChatHistory { session: 7, messages, source, .. } => Some((messages, source)), + _ => None, + }) + .await; + assert_eq!(source, HistorySource::Local); + assert_eq!(texts(&local), ["m5", "while away 1", "while away 2"]); + drop(engine); + let _ = std::fs::remove_dir_all(path.parent().unwrap()); +} + +/// Live messages, our own included, are stored once; pins, reactions, +/// topics and other requests go through Command::Gateway. +#[tokio::test(flavor = "multi_thread")] +async fn live_messages_and_gateway_requests() { + let gw = FakeGateway::start().await; + let engine = Engine::start(); + let mut rx = engine.subscribe(); + engine.send(Command::OpenChat { session: 1, target: LOBBY }); + observe(&engine, 1, &gw); + let connected = + update(&mut rx, "connected", |u| matches!(u, GatewayUpdate::Connected { .. })).await; + let GatewayUpdate::Connected { server_uid, uid, capabilities, .. } = connected else { + unreachable!() + }; + assert_eq!((server_uid.as_str(), uid.as_str()), ("fake-server", ME)); + assert!(capabilities.iter().any(|c| c == feature::PINS)); + // In sync (the chat is empty). + batch(&mut rx, HistorySource::Gateway).await; + + // Our message: stored at once, then merged with the gateway's copy. + engine.send(Command::SendChat { session: 1, target: LOBBY, text: "hello".into() }); + let (sent, _, _) = batch(&mut rx, HistorySource::Live).await; + assert_eq!((sent[0].source, sent[0].remote_id), (MessageSource::Local, None)); + let (merged, _, _) = batch(&mut rx, HistorySource::Live).await; + assert_eq!(merged[0].id, sent[0].id, "one message"); + let remote = merged[0].remote_id.expect("the gateway's id"); + + // A message from someone else arrives live. + gw.add(LOBBY, "bob", "hi me", now_ms()); + let (live, _, _) = batch(&mut rx, HistorySource::Live).await; + assert_eq!(texts(&live), ["hi me"]); + assert_ne!(live[0].id, sent[0].id); + + // Pin: done, pushed, the row is pinned. + engine + .send(Command::Gateway { session: 1, request: GatewayRequest::Pin { message_id: remote } }); + let (mut pinned_row, mut pinned_push, mut done) = (None, None, false); + wait(&mut rx, "pin", |e| { + match e { + Event::ChatHistory { messages, source: HistorySource::Live, .. } + if messages[0].pinned => + { + pinned_row = Some(messages[0].clone()); + } + Event::Gateway { update: GatewayUpdate::Pinned { pin, .. }, .. } => { + pinned_push = Some(pin) + } + Event::Gateway { update: GatewayUpdate::Done { request }, .. } if request == "pin" => { + done = true + } + _ => {} + } + (pinned_row.is_some() && pinned_push.is_some() && done).then_some(()) + }) + .await; + assert_eq!(pinned_row.unwrap().id, sent[0].id); + assert_eq!(pinned_push.unwrap().message.id, sent[0].id); + + // React: the row carries the count and that it was us. + engine.send(Command::Gateway { + session: 1, + request: GatewayRequest::React { message_id: remote, emoji: "👍".into() }, + }); + let row = wait(&mut rx, "reaction", |e| match e { + Event::ChatHistory { messages, source: HistorySource::Live, .. } + if !messages[0].reactions.is_empty() => + { + Some(messages[0].clone()) + } + _ => None, + }) + .await; + assert_eq!(row.reactions, [ReactionCount { emoji: "👍".into(), count: 1, me: true }]); + update(&mut rx, "reaction push", |u| { + matches!(u, GatewayUpdate::Reaction { added: true, count: 1, .. }) + }) + .await; + + // A topic from the message, and a post into it. + engine.send(Command::Gateway { + session: 1, + request: GatewayRequest::CreateTopic { + target: LOBBY, + title: "Plans".into(), + message_id: Some(remote), + }, + }); + let GatewayUpdate::Topic { topic } = + update(&mut rx, "topic", |u| matches!(u, GatewayUpdate::Topic { .. })).await + else { + unreachable!() + }; + assert_eq!((topic.title.as_str(), topic.root_message_id), ("Plans", Some(remote))); + engine.send(Command::Gateway { + session: 1, + request: GatewayRequest::Post { + target: LOBBY, + text: "in topic".into(), + topic: Some(topic.id), + }, + }); + let GatewayUpdate::Posted { message } = + update(&mut rx, "posted", |u| matches!(u, GatewayUpdate::Posted { .. })).await + else { + unreachable!() + }; + assert_eq!((message.topic_id, message.message.text.as_str()), (Some(topic.id), "in topic")); + assert_ne!(message.id, 0, "stored (in memory: negative ids)"); + + // Pins list with local ids. + engine.send(Command::Gateway { session: 1, request: GatewayRequest::Pins { target: LOBBY } }); + let GatewayUpdate::Pins { pins, .. } = + update(&mut rx, "pins", |u| matches!(u, GatewayUpdate::Pins { .. })).await + else { + unreachable!() + }; + assert_eq!(pins.iter().map(|p| p.message.id).collect::>(), [sent[0].id]); + + // Refusals come as Failed, with the gateway's code. + engine.send(Command::Gateway { session: 1, request: GatewayRequest::ConfigList }); + let failed = update(&mut rx, "failed", |u| matches!(u, GatewayUpdate::Failed { .. })).await; + assert!( + matches!(failed, GatewayUpdate::Failed { request, code: Some(ErrorCode::UnknownType), .. } if request == "config_list") + ); + // Without a gateway, too. + engine.send(Command::Gateway { session: 2, request: GatewayRequest::Streams }); + let failed = wait(&mut rx, "no gateway", |e| match e { + Event::Gateway { session: 2, update } => Some(update), + _ => None, + }) + .await; + assert!(matches!(failed, GatewayUpdate::Failed { code: None, .. })); + + // Everything is one row per message in the (in-memory) history. + let stored = engine.history().page("fake-server", &LOBBY, Default::default(), false).await; + assert_eq!(texts(&stored.unwrap()), ["hello", "hi me", "in topic"]); + // The engine enabled the extensions and subscribed to events. + let requests = gw.requests(); + assert!(requests.contains(&"enable".to_owned()), "{requests:?}"); + assert!(requests.contains(&"subscribe_events".to_owned()), "{requests:?}"); + // Requests serialize for tools. + assert_eq!( + serde_json::to_value(GatewayRequest::Pin { message_id: 7 }).unwrap(), + json!({"pin": {"message_id": 7}}) + ); + let parsed: GatewayRequest = + serde_json::from_value(json!({"topics": {"target": {"kind": "channel", "id": 1}}})) + .unwrap(); + assert_eq!(parsed, GatewayRequest::Topics { target: LOBBY, include_archived: false }); +} + +/// With `chat.store_history` off nothing reaches the database; messages +/// are kept in memory with negative ids. +#[tokio::test(flavor = "multi_thread")] +async fn store_history_off_keeps_the_database_empty() { + let gw = FakeGateway::start().await; + gw.add(LOBBY, "alice", "secret", 1000); + let path = temp_db("nostore"); + let settings = Settings::in_memory(); + settings.set_json("chat.store_history", json!(false)).unwrap(); + let engine = Engine::start_with(settings, History::open(&path).unwrap()); + let mut rx = engine.subscribe(); + engine.send(Command::OpenChat { session: 1, target: LOBBY }); + observe(&engine, 1, &gw); + let (page, _, _) = batch(&mut rx, HistorySource::Gateway).await; + assert_eq!(texts(&page), ["secret"]); + assert!(page[0].id < 0, "{}", page[0].id); + let history = engine.history(); + assert!( + history.page("fake-server", &LOBBY, Default::default(), false).await.unwrap().is_empty() + ); + assert_eq!( + history.page("fake-server", &LOBBY, Default::default(), true).await.unwrap().len(), + 1 + ); + drop((engine, history)); + let _ = std::fs::remove_dir_all(path.parent().unwrap()); +}