Skip to content

Commit afe3dbf

Browse files
committed
sync: add unit tests for marks_room_as_unread
1 parent a72e9db commit afe3dbf

File tree

1 file changed

+142
-0
lines changed

1 file changed

+142
-0
lines changed

crates/matrix-sdk-base/src/read_receipts.rs

+142
Original file line numberDiff line numberDiff line change
@@ -243,3 +243,145 @@ async fn find_latest_read_receipt_timestamp(
243243

244244
Ok(latest)
245245
}
246+
247+
#[cfg(test)]
248+
mod tests {
249+
use std::ops::Not as _;
250+
251+
use matrix_sdk_test::sync_timeline_event;
252+
use ruma::{event_id, uint, user_id, MilliSecondsSinceUnixEpoch};
253+
254+
use crate::read_receipts::marks_as_unread;
255+
256+
#[test]
257+
fn test_room_message_marks_as_unread() {
258+
let user_id = user_id!("@alice:example.org");
259+
let other_user_id = user_id!("@bob:example.org");
260+
261+
// A message from somebody else marks the room as unread...
262+
let ev = sync_timeline_event!({
263+
"sender": other_user_id,
264+
"type": "m.room.message",
265+
"event_id": "$ida",
266+
"origin_server_ts": 12344446,
267+
"content": { "body":"A", "msgtype": "m.text" },
268+
});
269+
assert!(marks_as_unread(&ev, user_id, None));
270+
271+
// A message older than our latest receipt doesn't mark the room as unread.
272+
assert!(
273+
marks_as_unread(&ev, user_id, Some(MilliSecondsSinceUnixEpoch(uint!(12344447)))).not()
274+
);
275+
276+
// A message more recent than our latest receipt marks the room as unread.
277+
assert!(marks_as_unread(&ev, user_id, Some(MilliSecondsSinceUnixEpoch(uint!(12344445)))));
278+
279+
// ... but a message from ourselves doesn't.
280+
let ev = sync_timeline_event!({
281+
"sender": user_id,
282+
"type": "m.room.message",
283+
"event_id": "$ida",
284+
"origin_server_ts": 12344446,
285+
"content": { "body":"A", "msgtype": "m.text" },
286+
});
287+
assert!(marks_as_unread(&ev, user_id, None).not());
288+
}
289+
290+
#[test]
291+
fn test_room_edit_doesnt_mark_as_unread() {
292+
let user_id = user_id!("@alice:example.org");
293+
let other_user_id = user_id!("@bob:example.org");
294+
295+
// An edit to a message from somebody else doesn't mark the room as unread.
296+
let ev = sync_timeline_event!({
297+
"sender": other_user_id,
298+
"type": "m.room.message",
299+
"event_id": "$ida",
300+
"origin_server_ts": 12344446,
301+
"content": {
302+
"body": " * edited message",
303+
"m.new_content": {
304+
"body": "edited message",
305+
"msgtype": "m.text"
306+
},
307+
"m.relates_to": {
308+
"event_id": "$someeventid:localhost",
309+
"rel_type": "m.replace"
310+
},
311+
"msgtype": "m.text"
312+
},
313+
});
314+
assert!(marks_as_unread(&ev, user_id, None).not());
315+
}
316+
317+
#[test]
318+
fn test_redaction_doesnt_mark_room_as_unread() {
319+
let user_id = user_id!("@alice:example.org");
320+
let other_user_id = user_id!("@bob:example.org");
321+
322+
// A redact of a message from somebody else doesn't mark the room as unread.
323+
let ev = sync_timeline_event!({
324+
"content": {
325+
"reason": "🛑"
326+
},
327+
"event_id": "$151957878228ssqrJ:localhost",
328+
"origin_server_ts": 151957878000000_u64,
329+
"sender": other_user_id,
330+
"type": "m.room.redaction",
331+
"redacts": "$151957878228ssqrj:localhost",
332+
"unsigned": {
333+
"age": 85
334+
}
335+
});
336+
337+
assert!(marks_as_unread(&ev, user_id, None).not());
338+
}
339+
340+
#[test]
341+
fn test_reaction_doesnt_mark_room_as_unread() {
342+
let user_id = user_id!("@alice:example.org");
343+
let other_user_id = user_id!("@bob:example.org");
344+
345+
// A reaction from somebody else to a message doesn't mark the room as unread.
346+
let ev = sync_timeline_event!({
347+
"content": {
348+
"m.relates_to": {
349+
"event_id": "$15275047031IXQRi:localhost",
350+
"key": "👍",
351+
"rel_type": "m.annotation"
352+
}
353+
},
354+
"event_id": "$15275047031IXQRi:localhost",
355+
"origin_server_ts": 159027581000000_u64,
356+
"sender": other_user_id,
357+
"type": "m.reaction",
358+
"unsigned": {
359+
"age": 85
360+
}
361+
});
362+
363+
assert!(marks_as_unread(&ev, user_id, None).not());
364+
}
365+
366+
#[test]
367+
fn test_state_event_doesnt_mark_as_unread() {
368+
let user_id = user_id!("@alice:example.org");
369+
let event_id = event_id!("$1");
370+
let ev = sync_timeline_event!({
371+
"content": {
372+
"displayname": "Alice",
373+
"membership": "join",
374+
},
375+
"event_id": event_id,
376+
"origin_server_ts": 1432135524678u64,
377+
"sender": user_id,
378+
"state_key": user_id,
379+
"type": "m.room.member",
380+
});
381+
382+
assert!(marks_as_unread(&ev, user_id, None).not());
383+
384+
let other_user_id = user_id!("@bob:example.org");
385+
assert!(marks_as_unread(&ev, other_user_id, None).not());
386+
}
387+
}

0 commit comments

Comments
 (0)