tests
This commit is contained in:
@@ -4,89 +4,88 @@ mod helpers;
|
||||
|
||||
use helpers::fake_tdclient::FakeTdClient;
|
||||
use helpers::test_data::TestMessageBuilder;
|
||||
use tele_tui::types::ChatId;
|
||||
|
||||
/// Test: Добавление реакции к сообщению
|
||||
#[test]
|
||||
fn test_add_reaction_to_message() {
|
||||
let mut client = FakeTdClient::new();
|
||||
#[tokio::test]
|
||||
async fn test_add_reaction_to_message() {
|
||||
let client = FakeTdClient::new();
|
||||
|
||||
// Отправляем сообщение
|
||||
let msg_id = client.send_message(123, "React to this!".to_string(), None);
|
||||
let msg = client.send_message(ChatId::new(123), "React to this!".to_string(), None, None).await.unwrap();
|
||||
|
||||
// Добавляем реакцию
|
||||
client.add_reaction(msg_id, "👍".to_string());
|
||||
client.toggle_reaction(ChatId::new(123), msg.id(), "👍".to_string()).await.unwrap();
|
||||
|
||||
// Проверяем что реакция записалась
|
||||
let reactions = client.reactions.get(&msg_id);
|
||||
assert!(reactions.is_some());
|
||||
assert_eq!(reactions.unwrap().len(), 1);
|
||||
assert_eq!(reactions.unwrap()[0], "👍");
|
||||
let messages = client.get_messages(123);
|
||||
assert_eq!(messages.len(), 1);
|
||||
assert_eq!(messages[0].reactions().len(), 1);
|
||||
assert_eq!(messages[0].reactions()[0].emoji, "👍");
|
||||
assert_eq!(messages[0].reactions()[0].count, 1);
|
||||
assert_eq!(messages[0].reactions()[0].is_chosen, true);
|
||||
}
|
||||
|
||||
/// Test: Удаление реакции (toggle) - вторичное нажатие
|
||||
#[test]
|
||||
fn test_toggle_reaction_removes_it() {
|
||||
let mut client = FakeTdClient::new();
|
||||
#[tokio::test]
|
||||
async fn test_toggle_reaction_removes_it() {
|
||||
let client = FakeTdClient::new();
|
||||
|
||||
// Создаём сообщение с нашей реакцией
|
||||
let msg = TestMessageBuilder::new("Message", 100)
|
||||
.reaction("👍", 1, true) // chosen=true - наша реакция
|
||||
.build();
|
||||
|
||||
client = client.with_message(123, msg);
|
||||
let client = client.with_message(123, msg);
|
||||
|
||||
// Проверяем что реакция есть
|
||||
let messages_before = client.get_messages(123);
|
||||
assert_eq!(messages_before[0].reactions().len(), 1);
|
||||
assert_eq!(messages_before[0].reactions()[0].is_chosen, true);
|
||||
|
||||
// Симулируем удаление реакции (в реальном App это toggle)
|
||||
// FakeTdClient просто записывает что реакция была "убрана"
|
||||
// Для теста можем удалить из списка вручную или расширить FakeTdClient
|
||||
let msg_id = messages_before[0].id();
|
||||
|
||||
// Создаём сообщение без реакции (после toggle)
|
||||
let msg_after = TestMessageBuilder::new("Message", 100).build();
|
||||
|
||||
// Заменяем в клиенте
|
||||
client.messages.insert(123, vec![msg_after]);
|
||||
// Toggle - удаляем свою реакцию
|
||||
client.toggle_reaction(ChatId::new(123), msg_id, "👍".to_string()).await.unwrap();
|
||||
|
||||
let messages_after = client.get_messages(123);
|
||||
assert_eq!(messages_after[0].reactions().len(), 0);
|
||||
}
|
||||
|
||||
/// Test: Множественные реакции на одно сообщение
|
||||
#[test]
|
||||
fn test_multiple_reactions_on_one_message() {
|
||||
let mut client = FakeTdClient::new();
|
||||
#[tokio::test]
|
||||
async fn test_multiple_reactions_on_one_message() {
|
||||
let client = FakeTdClient::new();
|
||||
|
||||
let msg_id = client.send_message(123, "Many reactions".to_string(), None);
|
||||
let msg = client.send_message(ChatId::new(123), "Many reactions".to_string(), None, None).await.unwrap();
|
||||
|
||||
// Добавляем несколько разных реакций
|
||||
client.add_reaction(msg_id, "👍".to_string());
|
||||
client.add_reaction(msg_id, "❤️".to_string());
|
||||
client.add_reaction(msg_id, "😂".to_string());
|
||||
client.add_reaction(msg_id, "🔥".to_string());
|
||||
client.toggle_reaction(ChatId::new(123), msg.id(), "👍".to_string()).await.unwrap();
|
||||
client.toggle_reaction(ChatId::new(123), msg.id(), "❤️".to_string()).await.unwrap();
|
||||
client.toggle_reaction(ChatId::new(123), msg.id(), "😂".to_string()).await.unwrap();
|
||||
client.toggle_reaction(ChatId::new(123), msg.id(), "🔥".to_string()).await.unwrap();
|
||||
|
||||
// Проверяем что все 4 реакции записались
|
||||
let reactions = client.reactions.get(&msg_id).unwrap();
|
||||
let messages = client.get_messages(123);
|
||||
let reactions = &messages[0].reactions();
|
||||
assert_eq!(reactions.len(), 4);
|
||||
assert_eq!(reactions[0], "👍");
|
||||
assert_eq!(reactions[1], "❤️");
|
||||
assert_eq!(reactions[2], "😂");
|
||||
assert_eq!(reactions[3], "🔥");
|
||||
assert_eq!(reactions[0].emoji, "👍");
|
||||
assert_eq!(reactions[1].emoji, "❤️");
|
||||
assert_eq!(reactions[2].emoji, "😂");
|
||||
assert_eq!(reactions[3].emoji, "🔥");
|
||||
}
|
||||
|
||||
/// Test: Реакции от разных пользователей (count > 1)
|
||||
#[test]
|
||||
fn test_reactions_from_multiple_users() {
|
||||
let mut client = FakeTdClient::new();
|
||||
#[tokio::test]
|
||||
async fn test_reactions_from_multiple_users() {
|
||||
let client = FakeTdClient::new();
|
||||
|
||||
// Создаём сообщение с реакцией от 3 пользователей
|
||||
let msg = TestMessageBuilder::new("Popular message", 100)
|
||||
.reaction("👍", 3, false) // 3 человека, но не мы
|
||||
.build();
|
||||
|
||||
client = client.with_message(123, msg);
|
||||
let client = client.with_message(123, msg);
|
||||
|
||||
let messages = client.get_messages(123);
|
||||
let reaction = &messages[0].reactions()[0];
|
||||
@@ -97,16 +96,16 @@ fn test_reactions_from_multiple_users() {
|
||||
}
|
||||
|
||||
/// Test: Своя реакция (is_chosen = true)
|
||||
#[test]
|
||||
fn test_own_reaction_is_chosen() {
|
||||
let mut client = FakeTdClient::new();
|
||||
#[tokio::test]
|
||||
async fn test_own_reaction_is_chosen() {
|
||||
let client = FakeTdClient::new();
|
||||
|
||||
// Создаём сообщение с нашей реакцией
|
||||
let msg = TestMessageBuilder::new("I reacted", 100)
|
||||
.reaction("❤️", 1, true) // chosen=true
|
||||
.build();
|
||||
|
||||
client = client.with_message(123, msg);
|
||||
let client = client.with_message(123, msg);
|
||||
|
||||
let messages = client.get_messages(123);
|
||||
let reaction = &messages[0].reactions()[0];
|
||||
@@ -116,16 +115,16 @@ fn test_own_reaction_is_chosen() {
|
||||
}
|
||||
|
||||
/// Test: Чужая реакция (is_chosen = false)
|
||||
#[test]
|
||||
fn test_other_reaction_not_chosen() {
|
||||
let mut client = FakeTdClient::new();
|
||||
#[tokio::test]
|
||||
async fn test_other_reaction_not_chosen() {
|
||||
let client = FakeTdClient::new();
|
||||
|
||||
// Создаём сообщение с чужой реакцией
|
||||
let msg = TestMessageBuilder::new("They reacted", 100)
|
||||
.reaction("😂", 2, false) // chosen=false
|
||||
.build();
|
||||
|
||||
client = client.with_message(123, msg);
|
||||
let client = client.with_message(123, msg);
|
||||
|
||||
let messages = client.get_messages(123);
|
||||
let reaction = &messages[0].reactions()[0];
|
||||
@@ -135,46 +134,50 @@ fn test_other_reaction_not_chosen() {
|
||||
}
|
||||
|
||||
/// Test: Счётчик реакций увеличивается
|
||||
#[test]
|
||||
fn test_reaction_counter_increases() {
|
||||
let mut client = FakeTdClient::new();
|
||||
#[tokio::test]
|
||||
async fn test_reaction_counter_increases() {
|
||||
let client = FakeTdClient::new();
|
||||
|
||||
// Начальное сообщение с 1 реакцией
|
||||
let msg_v1 = TestMessageBuilder::new("Growing", 100)
|
||||
// Начальное сообщение с 1 реакцией от кого-то
|
||||
let msg = TestMessageBuilder::new("Growing", 100)
|
||||
.reaction("👍", 1, false)
|
||||
.build();
|
||||
|
||||
client = client.with_message(123, msg_v1);
|
||||
let client = client.with_message(123, msg);
|
||||
|
||||
// Симулируем обновление: теперь 5 человек
|
||||
let msg_v2 = TestMessageBuilder::new("Growing", 100)
|
||||
.reaction("👍", 5, false)
|
||||
.build();
|
||||
let messages_before = client.get_messages(123);
|
||||
assert_eq!(messages_before[0].reactions()[0].count, 1);
|
||||
|
||||
client.messages.insert(123, vec![msg_v2]);
|
||||
let msg_id = messages_before[0].id();
|
||||
|
||||
// Мы добавляем свою реакцию - счётчик должен увеличиться
|
||||
client.toggle_reaction(ChatId::new(123), msg_id, "👍".to_string()).await.unwrap();
|
||||
|
||||
let messages = client.get_messages(123);
|
||||
assert_eq!(messages[0].reactions()[0].count, 5);
|
||||
assert_eq!(messages[0].reactions()[0].count, 2);
|
||||
assert_eq!(messages[0].reactions()[0].is_chosen, true);
|
||||
}
|
||||
|
||||
/// Test: Обновление реакции - мы добавили свою к существующим
|
||||
#[test]
|
||||
fn test_update_reaction_we_add_ours() {
|
||||
let mut client = FakeTdClient::new();
|
||||
#[tokio::test]
|
||||
async fn test_update_reaction_we_add_ours() {
|
||||
let client = FakeTdClient::new();
|
||||
|
||||
// Изначально: 2 человека, но не мы
|
||||
let msg_before = TestMessageBuilder::new("Update", 100)
|
||||
.reaction("🔥", 2, false)
|
||||
.build();
|
||||
|
||||
client = client.with_message(123, msg_before);
|
||||
let client = client.with_message(123, msg_before);
|
||||
|
||||
// После добавления нашей: 3 человека, в том числе мы
|
||||
let msg_after = TestMessageBuilder::new("Update", 100)
|
||||
.reaction("🔥", 3, true) // is_chosen=true теперь
|
||||
.build();
|
||||
let messages_before = client.get_messages(123);
|
||||
assert_eq!(messages_before[0].reactions()[0].count, 2);
|
||||
assert_eq!(messages_before[0].reactions()[0].is_chosen, false);
|
||||
|
||||
client.messages.insert(123, vec![msg_after]);
|
||||
let msg_id = messages_before[0].id();
|
||||
|
||||
// Добавляем нашу реакцию
|
||||
client.toggle_reaction(ChatId::new(123), msg_id, "🔥".to_string()).await.unwrap();
|
||||
|
||||
let messages = client.get_messages(123);
|
||||
let reaction = &messages[0].reactions()[0];
|
||||
@@ -184,15 +187,15 @@ fn test_update_reaction_we_add_ours() {
|
||||
}
|
||||
|
||||
/// Test: Реакция с count=1 отображается только emoji
|
||||
#[test]
|
||||
fn test_single_reaction_shows_only_emoji() {
|
||||
let mut client = FakeTdClient::new();
|
||||
#[tokio::test]
|
||||
async fn test_single_reaction_shows_only_emoji() {
|
||||
let client = FakeTdClient::new();
|
||||
|
||||
let msg = TestMessageBuilder::new("Single", 100)
|
||||
.reaction("❤️", 1, true)
|
||||
.build();
|
||||
|
||||
client = client.with_message(123, msg);
|
||||
let client = client.with_message(123, msg);
|
||||
|
||||
let messages = client.get_messages(123);
|
||||
let reaction = &messages[0].reactions()[0];
|
||||
@@ -203,9 +206,9 @@ fn test_single_reaction_shows_only_emoji() {
|
||||
}
|
||||
|
||||
/// Test: Реакции на несколько сообщений
|
||||
#[test]
|
||||
fn test_reactions_on_multiple_messages() {
|
||||
let mut client = FakeTdClient::new();
|
||||
#[tokio::test]
|
||||
async fn test_reactions_on_multiple_messages() {
|
||||
let client = FakeTdClient::new();
|
||||
|
||||
let msg1 = TestMessageBuilder::new("First", 100)
|
||||
.reaction("👍", 2, false)
|
||||
@@ -220,7 +223,7 @@ fn test_reactions_on_multiple_messages() {
|
||||
.reaction("🔥", 3, true) // Две разные реакции
|
||||
.build();
|
||||
|
||||
client = client
|
||||
let client = client
|
||||
.with_message(123, msg1)
|
||||
.with_message(123, msg2)
|
||||
.with_message(123, msg3);
|
||||
|
||||
Reference in New Issue
Block a user