|
1 | 1 | use assert_matches2::assert_matches;
|
2 | 2 | use matrix_sdk::{room::ThreadSubscription, test_utils::mocks::MatrixMockServer};
|
3 |
| -use matrix_sdk_test::async_test; |
4 |
| -use ruma::{owned_event_id, room_id}; |
| 3 | +use matrix_sdk_test::{async_test, event_factory::EventFactory, JoinedRoomBuilder, ALICE}; |
| 4 | +use ruma::{event_id, owned_event_id, room_id}; |
5 | 5 |
|
6 | 6 | #[async_test]
|
7 | 7 | async fn test_subscribe_thread() {
|
@@ -78,3 +78,64 @@ async fn test_subscribe_thread() {
|
78 | 78 | let subscription = room.fetch_thread_subscription(root_id).await.unwrap();
|
79 | 79 | assert_matches!(subscription, None);
|
80 | 80 | }
|
| 81 | + |
| 82 | +#[async_test] |
| 83 | +async fn test_thread_push_rule_is_triggered_for_subscribed_threads() { |
| 84 | + // This test checks that the evaluation of push rules for threads will correctly |
| 85 | + // call `Room::fetch_thread_subscription` for threads. |
| 86 | + |
| 87 | + let server = MatrixMockServer::new().await; |
| 88 | + let client = server.client_builder().build().await; |
| 89 | + |
| 90 | + let room_id = room_id!("!test:example.org"); |
| 91 | + let room = server.sync_joined_room(&client, room_id).await; |
| 92 | + |
| 93 | + let thread_root_id = owned_event_id!("$root"); |
| 94 | + let f = EventFactory::new().room(room_id).sender(*ALICE); |
| 95 | + |
| 96 | + // Make it so that the client has a member event for the current user. |
| 97 | + server |
| 98 | + .sync_room( |
| 99 | + &client, |
| 100 | + JoinedRoomBuilder::new(room_id).add_state_event(f.member(client.user_id().unwrap())), |
| 101 | + ) |
| 102 | + .await; |
| 103 | + |
| 104 | + // Sanity check: we can get a push context. |
| 105 | + let push_context = room |
| 106 | + .push_context() |
| 107 | + .await |
| 108 | + .expect("getting a push context works") |
| 109 | + .expect("the push context should exist"); |
| 110 | + |
| 111 | + // Mock the thread subscriptions endpoint so the user is subscribed to the |
| 112 | + // thread. |
| 113 | + server |
| 114 | + .mock_get_thread_subscription() |
| 115 | + .match_room_id(room_id.to_owned()) |
| 116 | + .match_thread_id(thread_root_id.clone()) |
| 117 | + .ok(true) |
| 118 | + .mock_once() |
| 119 | + .mount() |
| 120 | + .await; |
| 121 | + |
| 122 | + // Given an event in the thread I'm subscribed to, the push rule evaluation will |
| 123 | + // trigger the thread subscription endpoint, |
| 124 | + let event = |
| 125 | + f.text_msg("hello to you too!").in_thread(&thread_root_id, &thread_root_id).into_raw_sync(); |
| 126 | + |
| 127 | + // And the event will trigger a notification. |
| 128 | + let actions = push_context.for_event(&event).await; |
| 129 | + assert!(actions.iter().any(|action| action.should_notify())); |
| 130 | + |
| 131 | + // But for a thread that I haven't subscribed to (i.e. the endpoint returns 404, |
| 132 | + // because it's not set up), no actions are returned. |
| 133 | + let another_thread_root_id = event_id!("$another_root"); |
| 134 | + let event = f |
| 135 | + .text_msg("bonjour à vous également !") |
| 136 | + .in_thread(another_thread_root_id, another_thread_root_id) |
| 137 | + .into_raw_sync(); |
| 138 | + |
| 139 | + let actions = push_context.for_event(&event).await; |
| 140 | + assert!(actions.is_empty()); |
| 141 | +} |
0 commit comments