Skip to content

Commit

Permalink
sync: add unit tests for marks_room_as_unread
Browse files Browse the repository at this point in the history
  • Loading branch information
bnjbvr committed Dec 15, 2023
1 parent a72e9db commit afe3dbf
Showing 1 changed file with 142 additions and 0 deletions.
142 changes: 142 additions & 0 deletions crates/matrix-sdk-base/src/read_receipts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,3 +243,145 @@ async fn find_latest_read_receipt_timestamp(

Ok(latest)
}

#[cfg(test)]
mod tests {
use std::ops::Not as _;

use matrix_sdk_test::sync_timeline_event;
use ruma::{event_id, uint, user_id, MilliSecondsSinceUnixEpoch};

use crate::read_receipts::marks_as_unread;

#[test]
fn test_room_message_marks_as_unread() {
let user_id = user_id!("@alice:example.org");
let other_user_id = user_id!("@bob:example.org");

// A message from somebody else marks the room as unread...
let ev = sync_timeline_event!({
"sender": other_user_id,
"type": "m.room.message",
"event_id": "$ida",
"origin_server_ts": 12344446,
"content": { "body":"A", "msgtype": "m.text" },
});
assert!(marks_as_unread(&ev, user_id, None));

// A message older than our latest receipt doesn't mark the room as unread.
assert!(
marks_as_unread(&ev, user_id, Some(MilliSecondsSinceUnixEpoch(uint!(12344447)))).not()
);

// A message more recent than our latest receipt marks the room as unread.
assert!(marks_as_unread(&ev, user_id, Some(MilliSecondsSinceUnixEpoch(uint!(12344445)))));

// ... but a message from ourselves doesn't.
let ev = sync_timeline_event!({
"sender": user_id,
"type": "m.room.message",
"event_id": "$ida",
"origin_server_ts": 12344446,
"content": { "body":"A", "msgtype": "m.text" },
});
assert!(marks_as_unread(&ev, user_id, None).not());
}

#[test]
fn test_room_edit_doesnt_mark_as_unread() {
let user_id = user_id!("@alice:example.org");
let other_user_id = user_id!("@bob:example.org");

// An edit to a message from somebody else doesn't mark the room as unread.
let ev = sync_timeline_event!({
"sender": other_user_id,
"type": "m.room.message",
"event_id": "$ida",
"origin_server_ts": 12344446,
"content": {
"body": " * edited message",
"m.new_content": {
"body": "edited message",
"msgtype": "m.text"
},
"m.relates_to": {
"event_id": "$someeventid:localhost",
"rel_type": "m.replace"
},
"msgtype": "m.text"
},
});
assert!(marks_as_unread(&ev, user_id, None).not());
}

#[test]
fn test_redaction_doesnt_mark_room_as_unread() {
let user_id = user_id!("@alice:example.org");
let other_user_id = user_id!("@bob:example.org");

// A redact of a message from somebody else doesn't mark the room as unread.
let ev = sync_timeline_event!({
"content": {
"reason": "🛑"
},
"event_id": "$151957878228ssqrJ:localhost",
"origin_server_ts": 151957878000000_u64,
"sender": other_user_id,
"type": "m.room.redaction",
"redacts": "$151957878228ssqrj:localhost",
"unsigned": {
"age": 85
}
});

assert!(marks_as_unread(&ev, user_id, None).not());
}

#[test]
fn test_reaction_doesnt_mark_room_as_unread() {
let user_id = user_id!("@alice:example.org");
let other_user_id = user_id!("@bob:example.org");

// A reaction from somebody else to a message doesn't mark the room as unread.
let ev = sync_timeline_event!({
"content": {
"m.relates_to": {
"event_id": "$15275047031IXQRi:localhost",
"key": "👍",
"rel_type": "m.annotation"
}
},
"event_id": "$15275047031IXQRi:localhost",
"origin_server_ts": 159027581000000_u64,
"sender": other_user_id,
"type": "m.reaction",
"unsigned": {
"age": 85
}
});

assert!(marks_as_unread(&ev, user_id, None).not());
}

#[test]
fn test_state_event_doesnt_mark_as_unread() {
let user_id = user_id!("@alice:example.org");
let event_id = event_id!("$1");
let ev = sync_timeline_event!({
"content": {
"displayname": "Alice",
"membership": "join",
},
"event_id": event_id,
"origin_server_ts": 1432135524678u64,
"sender": user_id,
"state_key": user_id,
"type": "m.room.member",
});

assert!(marks_as_unread(&ev, user_id, None).not());

let other_user_id = user_id!("@bob:example.org");
assert!(marks_as_unread(&ev, other_user_id, None).not());
}
}

0 comments on commit afe3dbf

Please sign in to comment.