Skip to content

Commit

Permalink
Add tests to cover editing messages (#5699)
Browse files Browse the repository at this point in the history
  • Loading branch information
hpeebles authored Apr 19, 2024
1 parent d33710a commit 8dbf806
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 0 deletions.
28 changes: 28 additions & 0 deletions backend/integration_tests/src/client/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,34 @@ pub mod happy_path {
}
}

pub fn edit_text_message(
env: &mut PocketIc,
sender: &User,
user_id: UserId,
message_id: MessageId,
text: impl ToString,
block_level_markdown: Option<bool>,
) {
let response = super::edit_message_v2(
env,
sender.principal,
sender.canister(),
&user_canister::edit_message_v2::Args {
user_id,
thread_root_message_index: None,
message_id,
content: MessageContentInitial::Text(TextContent { text: text.to_string() }),
block_level_markdown,
correlation_id: 0,
},
);

match response {
user_canister::edit_message_v2::Response::Success => {}
response => panic!("'edit_message_v2' error: {response:?}"),
}
}

pub fn create_group(
env: &mut PocketIc,
sender: &User,
Expand Down
103 changes: 103 additions & 0 deletions backend/integration_tests/src/edit_message_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
use crate::env::ENV;
use crate::rng::random_message_id;
use crate::{client, TestEnv};
use std::ops::Deref;
use test_case::test_case;
use types::{ChatEvent, MessageContentInitial, TextContent};

#[test]
fn edit_message_succeeds() {
let mut wrapper = ENV.deref().get();
let TestEnv { env, canister_ids, .. } = wrapper.env();

let user1 = client::register_user(env, canister_ids);
let user2 = client::register_user(env, canister_ids);

let message_id = random_message_id();

let send_message_result = client::user::happy_path::send_text_message(env, &user1, user2.user_id, "TEXT", Some(message_id));

let new_text = "TEXT!";
client::user::happy_path::edit_text_message(env, &user1, user2.user_id, message_id, new_text, None);

env.tick();

let user1_event =
client::user::happy_path::events_by_index(env, &user1, user2.user_id, vec![send_message_result.event_index])
.events
.pop()
.unwrap();

let user2_event =
client::user::happy_path::events_by_index(env, &user2, user1.user_id, vec![send_message_result.event_index])
.events
.pop()
.unwrap();

let ChatEvent::Message(m1) = user1_event.event else { panic!() };
assert!(m1.edited);
assert_eq!(m1.content.text().unwrap(), new_text);

let ChatEvent::Message(m2) = user2_event.event else { panic!() };
assert!(m2.edited);
assert_eq!(m2.content.text().unwrap(), new_text);
}

#[test_case(true)]
#[test_case(false)]
fn update_block_level_markdown_succeeds(starting_value: bool) {
let mut wrapper = ENV.deref().get();
let TestEnv { env, canister_ids, .. } = wrapper.env();

let user1 = client::register_user(env, canister_ids);
let user2 = client::register_user(env, canister_ids);

let message_id = random_message_id();

let user_canister::send_message_v2::Response::Success(send_message_result) = client::user::send_message_v2(
env,
user1.principal,
user1.canister(),
&user_canister::send_message_v2::Args {
recipient: user2.user_id,
thread_root_message_index: None,
message_id,
content: MessageContentInitial::Text(TextContent {
text: "TEXT".to_string(),
}),
replies_to: None,
forwarding: false,
block_level_markdown: starting_value,
message_filter_failed: None,
pin: None,
correlation_id: 0,
},
) else {
panic!()
};

let new_value = !starting_value;
client::user::happy_path::edit_text_message(env, &user1, user2.user_id, message_id, "TEXT", Some(new_value));

env.tick();

let user1_event =
client::user::happy_path::events_by_index(env, &user1, user2.user_id, vec![send_message_result.event_index])
.events
.pop()
.unwrap();

let user2_event =
client::user::happy_path::events_by_index(env, &user2, user1.user_id, vec![send_message_result.event_index])
.events
.pop()
.unwrap();

let ChatEvent::Message(m1) = user1_event.event else { panic!() };
assert!(m1.edited);
assert_eq!(m1.block_level_markdown, new_value);

let ChatEvent::Message(m2) = user2_event.event else { panic!() };
assert!(m2.edited);
assert_eq!(m2.block_level_markdown, new_value);
}
1 change: 1 addition & 0 deletions backend/integration_tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ mod delete_group_tests;
mod delete_message_tests;
mod diamond_membership_tests;
mod disappearing_message_tests;
mod edit_message_tests;
mod env;
mod escrow_tests;
mod fire_and_forget_handler_tests;
Expand Down

0 comments on commit 8dbf806

Please sign in to comment.