Skip to content

Commit

Permalink
continue
Browse files Browse the repository at this point in the history
  • Loading branch information
Hywan committed Dec 6, 2024
1 parent 7d94f46 commit 0059ab7
Showing 1 changed file with 144 additions and 36 deletions.
180 changes: 144 additions & 36 deletions crates/matrix-sdk-ui/src/timeline/controller/observable_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,29 +384,77 @@ mod observable_items_tests {
)
}

fn event_meta(event_id: &str, timeline_item_index: Option<usize>) -> EventMeta {
EventMeta { event_id: event_id.parse().unwrap(), timeline_item_index, visible: false }
fn read_marker() -> Arc<TimelineItem> {
TimelineItem::read_marker()
}

fn event_meta(event_id: &str) -> EventMeta {
EventMeta { event_id: event_id.parse().unwrap(), timeline_item_index: None, visible: false }
}

macro_rules! assert_event_id {
($timeline_item:expr, $event_id:literal) => {
assert_eq!($timeline_item.as_event().unwrap().event_id().unwrap().as_str(), $event_id);
( $timeline_item:expr, $event_id:literal $( , $message:expr )? $(,)? ) => {
assert_eq!($timeline_item.as_event().unwrap().event_id().unwrap().as_str(), $event_id $( , $message)? );
};
}

macro_rules! assert_events {
( $events:expr, [ $( ( $event_id:literal, $timeline_item_index:expr ) ),* $(,)? ] ) => {
let events = $events;
let mut iter = events.iter();
macro_rules! assert_mapping {
( on $transaction:ident:
| event_id | event_index | timeline_item_index |
| $( - )+ | $( - )+ | $( - )+ |
$(
| $event_id:literal | $event_index:literal | $( $timeline_item_index:literal )? |
)+
) => {
let all_remote_events = $transaction .all_remote_events();

$(
assert_matches!(iter.next(), Some(EventMeta { event_id, timeline_item_index, .. }) => {
assert_eq!(event_id.as_str(), $event_id );
assert_eq!(*timeline_item_index, $timeline_item_index );
// Remote event exists at this index…
assert_matches!(all_remote_events.0.get( $event_index ), Some(EventMeta { event_id, timeline_item_index, .. }) => {
// … this is the remote event with the expected event ID
assert_eq!(
event_id.as_str(),
$event_id ,
concat!("event #", $event_index, " should have ID ", $event_id)
);


// (tiny hack to handle the case where `$timeline_item_index` is absent)
#[allow(unused_variables)]
let timeline_item_index_is_expected = false;
$(
let timeline_item_index_is_expected = true;
let _ = $timeline_item_index;
)?

if timeline_item_index_is_expected.not() {
// … this remote event does NOT map to a timeline item index
assert!(
timeline_item_index.is_none(),
concat!("event #", $event_index, " with ID ", $event_id, " should NOT map to a timeline item index" )
);
}

$(
// … this remote event maps to a timeline item index
assert_eq!(
*timeline_item_index,
Some( $timeline_item_index ),
concat!("event #", $event_index, " with ID ", $event_id, " should map to timeline item #", $timeline_item_index )
);

// … this timeline index exists
assert_matches!( $transaction .get( $timeline_item_index ), Some(timeline_item) => {
// … this timelime item has the expected event ID
assert_event_id!(
timeline_item,
$event_id ,
concat!("timeline item #", $timeline_item_index, " should map to event ID ", $event_id )
);
});
)?
});
)*

assert!(iter.next().is_none(), "Not all events have been asserted");
}
}

Expand Down Expand Up @@ -606,32 +654,92 @@ mod observable_items_tests {

let mut transaction = items.transaction();

// Push the remote events to assert mapping is working as expected.
transaction.push_back_remote_event(event_meta("$ev0", Some(0)));
transaction.push_back_remote_event(event_meta("$ev1", Some(1)));
transaction.push_back_remote_event(event_meta("$ev2", None));
transaction.push_back_remote_event(event_meta("$ev3", Some(2)));
// Remote event with its timeline item.
transaction.push_back_remote_event(event_meta("$ev0"));
transaction.push_back(item("$ev0"), Some(0));

// Assert initial state.
assert_events!(
transaction.all_remote_events(),
[("$ev0", Some(0)), ("$ev1", Some(1)), ("$ev2", None), ("$ev3", Some(2))]
);
// Timeline item without a remote event (for example a read marker).
transaction.push_back(read_marker(), None);

/*
// Remove two events.
events.remove(2); // $ev2 has no `timeline_item_index`
events.remove(1); // $ev1 has a `timeline_item_index`
// Remote event with its timeline item.
transaction.push_back_remote_event(event_meta("$ev1"));
transaction.push_back(item("$ev1"), Some(1));

assert_events!(
events,
[
("$ev0", Some(0)),
// `timeline_item_index` has shifted once
("$ev3", Some(1)),
]
);
*/
// Remote event without a timeline item (for example a state event).
transaction.push_back_remote_event(event_meta("$ev2"));

// Remote event with its timeline item.
transaction.push_back_remote_event(event_meta("$ev3"));
transaction.push_back(item("$ev3"), Some(3));

assert_mapping! {
on transaction:

| event_id | event_index | timeline_item_index |
|----------|-------------|---------------------|
| "$ev0" | 0 | 0 |
| "$ev1" | 1 | 2 |
| "$ev2" | 2 | |
| "$ev3" | 3 | 3 |
}

// Remove the timeline item that has no event.
transaction.remove(1);

assert_mapping! {
on transaction:

| event_id | event_index | timeline_item_index |
|----------|-------------|---------------------|
| "$ev0" | 0 | 0 |
| "$ev1" | 1 | 1 | // has shifted
| "$ev2" | 2 | |
| "$ev3" | 3 | 2 | // has shifted
}

// Remove an timeline item that has an event.
transaction.remove(1);

assert_mapping! {
on transaction:

| event_id | event_index | timeline_item_index |
|----------|-------------|---------------------|
| "$ev0" | 0 | 0 |
| "$ev1" | 1 | | // has been removed
| "$ev2" | 2 | |
| "$ev3" | 3 | 1 | // has shifted
}

// Remove the last timeline item to test off by 1 error.
transaction.remove(1);

assert_mapping! {
on transaction:

| event_id | event_index | timeline_item_index |
|----------|-------------|---------------------|
| "$ev0" | 0 | 0 |
| "$ev1" | 1 | |
| "$ev2" | 2 | |
| "$ev3" | 3 | | // has been removed
}

// Remove all the items \o/
transaction.remove(0);

assert_mapping! {
on transaction:

| event_id | event_index | timeline_item_index |
|----------|-------------|---------------------|
| "$ev0" | 0 | | // has been removed
| "$ev1" | 1 | |
| "$ev2" | 2 | |
| "$ev3" | 3 | |
}

assert!(transaction.is_empty());
}
}

Expand Down

0 comments on commit 0059ab7

Please sign in to comment.