fix: mark incoming messages as read when opening chat and load all history
Fixes two critical bugs: 1. Unread badge not clearing when opening chat - incoming messages weren't marked as viewed 2. Only last 2-3 messages loaded instead of full history due to incorrect break condition Changes: - Add incoming message IDs to pending_view_messages queue on chat open - Remove premature break in get_chat_history() that stopped after 2 messages - Add FakeTdClient.pending_view_messages field for testing - Implement process_pending_view_messages() in FakeTdClient Tests added: - test_incoming_message_shows_unread_badge: verify "(1)" appears for unread - test_opening_chat_clears_unread_badge: verify badge clears after opening - test_opening_chat_loads_many_messages: verify all 50 messages load, not just last few All 28 chat_list tests pass.
This commit is contained in:
@@ -54,6 +54,169 @@ fn snapshot_chat_with_unread_count() {
|
||||
assert_snapshot!("chat_with_unread", output);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_incoming_message_shows_unread_badge() {
|
||||
use tele_tui::tdlib::ChatInfo;
|
||||
use tele_tui::types::ChatId;
|
||||
|
||||
// Создаём чат БЕЗ непрочитанных сообщений
|
||||
let chat = TestChatBuilder::new("Friend", 999)
|
||||
.unread_count(0)
|
||||
.last_message("Как дела?")
|
||||
.build();
|
||||
|
||||
let mut app = TestAppBuilder::new()
|
||||
.with_chat(chat)
|
||||
.build();
|
||||
|
||||
// Рендерим UI - должно быть без "(1)"
|
||||
let buffer_before = render_to_buffer(80, 24, |f| {
|
||||
tele_tui::ui::chat_list::render(f, f.area(), &mut app);
|
||||
});
|
||||
let output_before = buffer_to_string(&buffer_before);
|
||||
|
||||
// Проверяем что нет "(1)" в первой строке чата
|
||||
assert!(!output_before.contains("(1)"), "Before: should not contain (1)");
|
||||
|
||||
// Симулируем входящее сообщение - обновляем unread_count
|
||||
app.chats[0].unread_count = 1;
|
||||
app.chats[0].last_message = "Привет!".to_string();
|
||||
|
||||
// Рендерим UI снова - теперь должно быть "(1)"
|
||||
let buffer_after = render_to_buffer(80, 24, |f| {
|
||||
tele_tui::ui::chat_list::render(f, f.area(), &mut app);
|
||||
});
|
||||
let output_after = buffer_to_string(&buffer_after);
|
||||
|
||||
// Проверяем что появилось "(1)" в первой строке чата
|
||||
assert!(output_after.contains("(1)"), "After: should contain (1)\nActual output:\n{}", output_after);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_opening_chat_clears_unread_badge() {
|
||||
use helpers::test_data::TestMessageBuilder;
|
||||
use tele_tui::tdlib::TdClientTrait;
|
||||
use tele_tui::types::{ChatId, MessageId};
|
||||
|
||||
// Создаём чат с 3 непрочитанными сообщениями
|
||||
let chat = TestChatBuilder::new("Friend", 999)
|
||||
.unread_count(3)
|
||||
.last_message("У тебя 3 новых сообщения")
|
||||
.build();
|
||||
|
||||
// Создаём 3 входящих сообщения (по умолчанию is_outgoing = false)
|
||||
let messages = vec![
|
||||
TestMessageBuilder::new("Привет!", 1)
|
||||
.sender("Friend")
|
||||
.build(),
|
||||
TestMessageBuilder::new("Как дела?", 2)
|
||||
.sender("Friend")
|
||||
.build(),
|
||||
TestMessageBuilder::new("Ответь мне!", 3)
|
||||
.sender("Friend")
|
||||
.build(),
|
||||
];
|
||||
|
||||
let mut app = TestAppBuilder::new()
|
||||
.with_chat(chat)
|
||||
.with_messages(999, messages)
|
||||
.build();
|
||||
|
||||
// Рендерим UI - должно быть "(3)"
|
||||
let buffer_before = render_to_buffer(80, 24, |f| {
|
||||
tele_tui::ui::chat_list::render(f, f.area(), &mut app);
|
||||
});
|
||||
let output_before = buffer_to_string(&buffer_before);
|
||||
|
||||
// Проверяем что есть "(3)" в списке чатов
|
||||
assert!(output_before.contains("(3)"), "Before opening: should contain (3)\nActual output:\n{}", output_before);
|
||||
|
||||
// Симулируем открытие чата - загружаем историю
|
||||
let chat_id = ChatId::new(999);
|
||||
let loaded_messages = app.td_client.get_chat_history(chat_id, 100).await.unwrap();
|
||||
|
||||
// Собираем ID входящих сообщений (как в реальном коде)
|
||||
let incoming_message_ids: Vec<MessageId> = loaded_messages
|
||||
.iter()
|
||||
.filter(|msg| !msg.is_outgoing())
|
||||
.map(|msg| msg.id())
|
||||
.collect();
|
||||
|
||||
// Проверяем что нашли 3 входящих сообщения
|
||||
assert_eq!(incoming_message_ids.len(), 3, "Should have 3 incoming messages");
|
||||
|
||||
// Добавляем в очередь для отметки как прочитанные (напрямую через Mutex)
|
||||
app.td_client.pending_view_messages
|
||||
.lock()
|
||||
.unwrap()
|
||||
.push((chat_id, incoming_message_ids));
|
||||
|
||||
// Обрабатываем очередь (как в main loop)
|
||||
app.td_client.process_pending_view_messages().await;
|
||||
|
||||
// В FakeTdClient это должно записаться в viewed_messages
|
||||
let viewed = app.td_client.get_viewed_messages();
|
||||
assert_eq!(viewed.len(), 1, "Should have one batch of viewed messages");
|
||||
assert_eq!(viewed[0].0, 999, "Should be for chat 999");
|
||||
assert_eq!(viewed[0].1.len(), 3, "Should have viewed 3 messages");
|
||||
|
||||
// В реальном приложении TDLib отправит Update::ChatReadInbox
|
||||
// который обновит unread_count в чате. Симулируем это:
|
||||
app.chats[0].unread_count = 0;
|
||||
|
||||
// Рендерим UI снова - "(3)" должно пропасть
|
||||
let buffer_after = render_to_buffer(80, 24, |f| {
|
||||
tele_tui::ui::chat_list::render(f, f.area(), &mut app);
|
||||
});
|
||||
let output_after = buffer_to_string(&buffer_after);
|
||||
|
||||
// Проверяем что "(3)" больше нет
|
||||
assert!(!output_after.contains("(3)"), "After opening: should not contain (3)\nActual output:\n{}", output_after);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_opening_chat_loads_many_messages() {
|
||||
use helpers::test_data::TestMessageBuilder;
|
||||
use tele_tui::tdlib::TdClientTrait;
|
||||
use tele_tui::types::ChatId;
|
||||
|
||||
// Создаём чат с 50 сообщениями
|
||||
let chat = TestChatBuilder::new("History Chat", 888)
|
||||
.last_message("Message 50")
|
||||
.build();
|
||||
|
||||
// Создаём 50 сообщений
|
||||
let messages: Vec<_> = (1..=50)
|
||||
.map(|i| {
|
||||
TestMessageBuilder::new(&format!("Message {}", i), i)
|
||||
.sender("Friend")
|
||||
.build()
|
||||
})
|
||||
.collect();
|
||||
|
||||
let mut app = TestAppBuilder::new()
|
||||
.with_chat(chat)
|
||||
.with_messages(888, messages)
|
||||
.build();
|
||||
|
||||
// Открываем чат - загружаем историю (запрашиваем 100 сообщений)
|
||||
let chat_id = ChatId::new(888);
|
||||
let loaded_messages = app.td_client.get_chat_history(chat_id, 100).await.unwrap();
|
||||
|
||||
// Проверяем что загрузились ВСЕ 50 сообщений, а не только последние 2-3
|
||||
assert_eq!(
|
||||
loaded_messages.len(),
|
||||
50,
|
||||
"Should load all 50 messages, not just last few. Got: {}",
|
||||
loaded_messages.len()
|
||||
);
|
||||
|
||||
// Проверяем что сообщения в правильном порядке (от старых к новым)
|
||||
assert_eq!(loaded_messages[0].text(), "Message 1");
|
||||
assert_eq!(loaded_messages[24].text(), "Message 25");
|
||||
assert_eq!(loaded_messages[49].text(), "Message 50");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn snapshot_chat_with_pinned() {
|
||||
let chat = TestChatBuilder::new("Important Chat", 123)
|
||||
|
||||
Reference in New Issue
Block a user