Skip to content

Commit 7d0958a

Browse files
committed
test(sdk): Test RoomEvents::debug_string.
This patch moves `chunk_debug_string` from `rooms/mod.rs` to `rooms/event.rs`. In addition, it restores (and rewrites) a test, initially for `chunk_debug_string`, now for `RoomEvents::debug_string` whichh is the public API.
1 parent 1d47d4d commit 7d0958a

File tree

2 files changed

+53
-31
lines changed

2 files changed

+53
-31
lines changed

crates/matrix-sdk/src/event_cache/room/events.rs

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ use ruma::{
2929
};
3030
use tracing::{error, instrument, trace, warn};
3131

32-
use super::chunk_debug_string;
33-
3432
/// This type represents all events of a single room.
3533
#[derive(Debug)]
3634
pub struct RoomEvents {
@@ -320,11 +318,14 @@ impl RoomEvents {
320318
/// events for this room.
321319
pub fn debug_string(&self) -> Vec<String> {
322320
let mut result = Vec::new();
323-
for c in self.chunks() {
324-
let content = chunk_debug_string(c.content());
325-
let line = format!("chunk #{}: {content}", c.identifier().index());
321+
322+
for chunk in self.chunks() {
323+
let content = chunk_debug_string(chunk.content());
324+
let line = format!("chunk #{}: {content}", chunk.identifier().index());
325+
326326
result.push(line);
327327
}
328+
328329
result
329330
}
330331
}
@@ -339,12 +340,36 @@ impl RoomEvents {
339340
}
340341
}
341342

343+
/// Create a debug string for a [`ChunkContent`] for an event/gap pair.
344+
fn chunk_debug_string(content: &ChunkContent<Event, Gap>) -> String {
345+
match content {
346+
ChunkContent::Gap(Gap { prev_token }) => {
347+
format!("gap['{prev_token}']")
348+
}
349+
ChunkContent::Items(vec) => {
350+
let items = vec
351+
.iter()
352+
.map(|event| {
353+
// Limit event ids to 8 chars *after* the $.
354+
event.event_id().map_or_else(
355+
|| "<no event id>".to_owned(),
356+
|id| id.as_str().chars().take(1 + 8).collect(),
357+
)
358+
})
359+
.collect::<Vec<_>>()
360+
.join(", ");
361+
362+
format!("events[{items}]")
363+
}
364+
}
365+
}
366+
342367
#[cfg(test)]
343368
mod tests {
344369
use assert_matches::assert_matches;
345370
use assert_matches2::assert_let;
346-
use matrix_sdk_test::event_factory::EventFactory;
347-
use ruma::{user_id, EventId, OwnedEventId};
371+
use matrix_sdk_test::{event_factory::EventFactory, ALICE, DEFAULT_TEST_ROOM_ID};
372+
use ruma::{event_id, user_id, EventId, OwnedEventId};
348373

349374
use super::*;
350375

@@ -704,4 +729,25 @@ mod tests {
704729
}
705730
);
706731
}
732+
733+
#[test]
734+
fn test_debug_string() {
735+
let event_factory = EventFactory::new().room(&DEFAULT_TEST_ROOM_ID).sender(*ALICE);
736+
737+
let mut room_events = RoomEvents::new();
738+
room_events.push_events(vec![
739+
event_factory
740+
.text_msg("hey")
741+
.event_id(event_id!("$123456789101112131415617181920"))
742+
.into_event(),
743+
event_factory.text_msg("you").event_id(event_id!("$2")).into_event(),
744+
]);
745+
room_events.push_gap(Gap { prev_token: "raclette".to_owned() });
746+
747+
let output = room_events.debug_string();
748+
749+
assert_eq!(output.len(), 2);
750+
assert_eq!(&output[0], "chunk #0: events[$12345678, $2]");
751+
assert_eq!(&output[1], "chunk #1: gap['raclette']");
752+
}
707753
}

crates/matrix-sdk/src/event_cache/room/mod.rs

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ use events::Gap;
2020
use eyeball_im::VectorDiff;
2121
use matrix_sdk_base::{
2222
deserialized_responses::{AmbiguityChange, TimelineEvent},
23-
linked_chunk::ChunkContent,
2423
sync::{JoinedRoomUpdate, LeftRoomUpdate, Timeline},
2524
};
2625
use ruma::{
@@ -503,29 +502,6 @@ impl RoomEventCacheInner {
503502
}
504503
}
505504

506-
/// Create a debug string for a [`ChunkContent`] for an event/gap pair.
507-
fn chunk_debug_string(content: &ChunkContent<TimelineEvent, Gap>) -> String {
508-
match content {
509-
ChunkContent::Gap(Gap { prev_token }) => {
510-
format!("gap['{prev_token}']")
511-
}
512-
ChunkContent::Items(vec) => {
513-
let items = vec
514-
.iter()
515-
.map(|event| {
516-
// Limit event ids to 8 chars *after* the $.
517-
event.event_id().map_or_else(
518-
|| "<no event id>".to_owned(),
519-
|id| id.as_str().chars().take(1 + 8).collect(),
520-
)
521-
})
522-
.collect::<Vec<_>>()
523-
.join(", ");
524-
format!("events[{items}]")
525-
}
526-
}
527-
}
528-
529505
/// Internal type to represent the output of
530506
/// `RoomEventCacheState::load_more_events_backwards`.
531507
#[derive(Debug)]

0 commit comments

Comments
 (0)