use pretty_assertions::assert_eq;
use rho_protocol::{self as wire, EntryId, ErrorCode, HistoryEvent};
use super::fit_message;
use crate::app::{
daemon_host::sessions::feed::tests::{idle_feed, notice, saved, shown},
session_events::Boundary,
};
fn id(entry: &str) -> EntryId {
EntryId::new(entry)
}
fn stamped(event: wire::SessionEvent, after: Option<&str>) -> HistoryEvent {
let mut page = HistoryEvent::new(event);
page.after_entry = after.map(id);
page
}
fn page(response: wire::SessionHistoryResponse) -> (Vec<HistoryEvent>, Option<EntryId>) {
let events = response
.events
.into_iter()
.map(|mut event| {
event.at = None;
event
})
.collect();
(events, response.earlier)
}
#[test]
fn history_pages_back_through_the_saved_entries() {
let feed = idle_feed();
feed.end_turn(saved(&["one", "two"]));
feed.end_turn(saved(&["three"]));
feed.publish([notice("live")]);
let (two, three) = {
let state = feed.state();
(
state.transcript.bytes(1, 2, None),
state.transcript.bytes(2, 3, None),
)
};
for (name, through, max_bytes, expected) in [
(
"the newest entry alone",
"three/0",
three,
(
vec![stamped(shown("three"), Some("two/0"))],
Some(id("two/0")),
),
),
(
"none fits: still one",
"three/0",
1,
(
vec![stamped(shown("three"), Some("two/0"))],
Some(id("two/0")),
),
),
(
"two entries",
"three/0",
two + three,
(
vec![
stamped(shown("two"), Some("one/0")),
stamped(shown("three"), Some("two/0")),
],
Some(id("one/0")),
),
),
(
"the start",
"one/0",
u64::MAX,
(vec![stamped(shown("one"), None)], None),
),
(
"all",
"three/0",
u64::MAX,
(
vec![
stamped(shown("one"), None),
stamped(shown("two"), Some("one/0")),
stamped(shown("three"), Some("two/0")),
],
None,
),
),
] {
let response = feed
.history(&id(through), max_bytes, None)
.unwrap()
.render();
assert_eq!(page(response), expected, "{name}");
}
let Err(moved) = feed.history(&id("gone/0"), u64::MAX, None) else {
panic!("a page through an entry the conversation does not hold");
};
assert_eq!(moved.error_code(), Some(ErrorCode::ConversationMoved));
}
#[test]
fn a_page_fits_one_message() {
let limit = super::PAGE_LIMIT;
let cut = |index, entry: &str| Boundary {
index,
entry: id(entry),
};
let cuts = [cut(1, "a"), cut(1, "x"), cut(2, "b")];
let page = |sizes: [usize; 3]| {
["a", "b", "c"]
.into_iter()
.zip(sizes)
.map(|(text, bytes)| (stamped(notice(text), None), bytes))
.collect()
};
for (name, sizes, expected) in [
(
"all fit",
[1, 1, 1],
(
vec![notice("a"), notice("b"), notice("c")],
Some(id("before")),
),
),
(
"the oldest cut",
[limit, 1, 1],
(vec![notice("b"), notice("c")], Some(id("x"))),
),
("two cut", [1, limit, 1], (vec![notice("c")], Some(id("b")))),
] {
let response = fit_message(page(sizes), &cuts, Some(id("before")));
let events = response
.events
.into_iter()
.map(|event| event.event)
.collect();
assert_eq!((events, response.earlier), expected, "{name}");
}
let alone = fit_message(page([1, 1, limit]), &cuts, None);
assert_eq!(
(
alone.events.len(),
matches!(alone.events[0].event, wire::SessionEvent::Notice { .. }),
alone.events[0].after_entry.clone(),
alone.earlier,
),
(1, true, Some(id("b")), Some(id("b"))),
"an entry too large alone"
);
}