Добавлены типобезопасные обёртки ChatId, MessageId, UserId для предотвращения смешивания разных типов идентификаторов на этапе компиляции. Изменения: - Создан src/types.rs с тремя newtype структурами - Реализованы методы: new(), as_i64(), From<i64>, Display - Добавлены traits: Hash, Eq, Serialize, Deserialize - Обновлены 15+ модулей для использования новых типов: * tdlib: types.rs, chats.rs, messages.rs, users.rs, reactions.rs, client.rs * app: mod.rs, chat_state.rs * input: main_input.rs * tests: app_builder.rs, test_data.rs - Исправлены 53 ошибки компиляции связанные с type conversions Преимущества: - Компилятор предотвращает смешивание разных типов ID - Улучшенная читаемость кода (явные типы вместо i64) - Самодокументирующиеся типы Статус: Priority 2 теперь 60% (3/5 задач) - ✅ Error enum - ✅ Config validation - ✅ Newtype для ID - ⏳ MessageInfo реструктуризация - ⏳ MessageBuilder pattern Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
240 lines
6.6 KiB
Rust
240 lines
6.6 KiB
Rust
// Test data builders and fixtures
|
|
|
|
use tele_tui::tdlib::{ChatInfo, ForwardInfo, MessageInfo, ProfileInfo, ReactionInfo, ReplyInfo};
|
|
use tele_tui::types::{ChatId, MessageId};
|
|
|
|
/// Builder для создания тестового чата
|
|
pub struct TestChatBuilder {
|
|
id: i64,
|
|
title: String,
|
|
username: Option<String>,
|
|
last_message: String,
|
|
last_message_date: i32,
|
|
unread_count: i32,
|
|
unread_mention_count: i32,
|
|
is_pinned: bool,
|
|
order: i64,
|
|
last_read_outbox_message_id: i64,
|
|
folder_ids: Vec<i32>,
|
|
is_muted: bool,
|
|
draft_text: Option<String>,
|
|
}
|
|
|
|
impl TestChatBuilder {
|
|
pub fn new(title: &str, id: i64) -> Self {
|
|
Self {
|
|
id,
|
|
title: title.to_string(),
|
|
username: None,
|
|
last_message: "".to_string(),
|
|
last_message_date: 1640000000,
|
|
unread_count: 0,
|
|
unread_mention_count: 0,
|
|
is_pinned: false,
|
|
order: id,
|
|
last_read_outbox_message_id: 0,
|
|
folder_ids: vec![0],
|
|
is_muted: false,
|
|
draft_text: None,
|
|
}
|
|
}
|
|
|
|
pub fn username(mut self, username: &str) -> Self {
|
|
self.username = Some(username.to_string());
|
|
self
|
|
}
|
|
|
|
pub fn last_message(mut self, text: &str) -> Self {
|
|
self.last_message = text.to_string();
|
|
self
|
|
}
|
|
|
|
pub fn unread_count(mut self, count: i32) -> Self {
|
|
self.unread_count = count;
|
|
self
|
|
}
|
|
|
|
pub fn unread_mentions(mut self, count: i32) -> Self {
|
|
self.unread_mention_count = count;
|
|
self
|
|
}
|
|
|
|
pub fn pinned(mut self) -> Self {
|
|
self.is_pinned = true;
|
|
self
|
|
}
|
|
|
|
pub fn muted(mut self) -> Self {
|
|
self.is_muted = true;
|
|
self
|
|
}
|
|
|
|
pub fn draft(mut self, text: &str) -> Self {
|
|
self.draft_text = Some(text.to_string());
|
|
self
|
|
}
|
|
|
|
pub fn folder(mut self, folder_id: i32) -> Self {
|
|
self.folder_ids = vec![folder_id];
|
|
self
|
|
}
|
|
|
|
pub fn build(self) -> ChatInfo {
|
|
ChatInfo {
|
|
id: ChatId::new(self.id),
|
|
title: self.title,
|
|
username: self.username,
|
|
last_message: self.last_message,
|
|
last_message_date: self.last_message_date,
|
|
unread_count: self.unread_count,
|
|
unread_mention_count: self.unread_mention_count,
|
|
is_pinned: self.is_pinned,
|
|
order: self.order,
|
|
last_read_outbox_message_id: MessageId::new(self.last_read_outbox_message_id),
|
|
folder_ids: self.folder_ids,
|
|
is_muted: self.is_muted,
|
|
draft_text: self.draft_text,
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Builder для создания тестового сообщения
|
|
pub struct TestMessageBuilder {
|
|
id: i64,
|
|
sender_name: String,
|
|
is_outgoing: bool,
|
|
content: String,
|
|
entities: Vec<tdlib_rs::types::TextEntity>,
|
|
date: i32,
|
|
edit_date: i32,
|
|
is_read: bool,
|
|
can_be_edited: bool,
|
|
can_be_deleted_only_for_self: bool,
|
|
can_be_deleted_for_all_users: bool,
|
|
reply_to: Option<ReplyInfo>,
|
|
forward_from: Option<ForwardInfo>,
|
|
reactions: Vec<ReactionInfo>,
|
|
}
|
|
|
|
impl TestMessageBuilder {
|
|
pub fn new(content: &str, id: i64) -> Self {
|
|
Self {
|
|
id,
|
|
sender_name: "User".to_string(),
|
|
is_outgoing: false,
|
|
content: content.to_string(),
|
|
entities: vec![],
|
|
date: 1640000000,
|
|
edit_date: 0,
|
|
is_read: true,
|
|
can_be_edited: false,
|
|
can_be_deleted_only_for_self: true,
|
|
can_be_deleted_for_all_users: false,
|
|
reply_to: None,
|
|
forward_from: None,
|
|
reactions: vec![],
|
|
}
|
|
}
|
|
|
|
pub fn outgoing(mut self) -> Self {
|
|
self.is_outgoing = true;
|
|
self.sender_name = "You".to_string();
|
|
self.can_be_edited = true;
|
|
self.can_be_deleted_for_all_users = true;
|
|
self
|
|
}
|
|
|
|
pub fn sender(mut self, name: &str) -> Self {
|
|
self.sender_name = name.to_string();
|
|
self
|
|
}
|
|
|
|
pub fn date(mut self, timestamp: i32) -> Self {
|
|
self.date = timestamp;
|
|
self
|
|
}
|
|
|
|
pub fn edited(mut self) -> Self {
|
|
self.edit_date = self.date + 60;
|
|
self
|
|
}
|
|
|
|
pub fn unread(mut self) -> Self {
|
|
self.is_read = false;
|
|
self
|
|
}
|
|
|
|
pub fn reply_to(mut self, message_id: i64, sender: &str, text: &str) -> Self {
|
|
self.reply_to = Some(ReplyInfo {
|
|
message_id: MessageId::new(message_id),
|
|
sender_name: sender.to_string(),
|
|
text: text.to_string(),
|
|
});
|
|
self
|
|
}
|
|
|
|
pub fn forwarded_from(mut self, sender: &str) -> Self {
|
|
self.forward_from = Some(ForwardInfo {
|
|
sender_name: sender.to_string(),
|
|
date: self.date - 3600,
|
|
});
|
|
self
|
|
}
|
|
|
|
pub fn reaction(mut self, emoji: &str, count: i32, chosen: bool) -> Self {
|
|
self.reactions
|
|
.push(ReactionInfo { emoji: emoji.to_string(), count, is_chosen: chosen });
|
|
self
|
|
}
|
|
|
|
pub fn build(self) -> MessageInfo {
|
|
MessageInfo {
|
|
id: MessageId::new(self.id),
|
|
sender_name: self.sender_name,
|
|
is_outgoing: self.is_outgoing,
|
|
content: self.content,
|
|
entities: self.entities,
|
|
date: self.date,
|
|
edit_date: self.edit_date,
|
|
is_read: self.is_read,
|
|
can_be_edited: self.can_be_edited,
|
|
can_be_deleted_only_for_self: self.can_be_deleted_only_for_self,
|
|
can_be_deleted_for_all_users: self.can_be_deleted_for_all_users,
|
|
reply_to: self.reply_to,
|
|
forward_from: self.forward_from,
|
|
reactions: self.reactions,
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Хелперы для быстрого создания тестовых данных
|
|
|
|
pub fn create_test_chat(title: &str, id: i64) -> ChatInfo {
|
|
TestChatBuilder::new(title, id).build()
|
|
}
|
|
|
|
pub fn create_test_message(content: &str, id: i64) -> MessageInfo {
|
|
TestMessageBuilder::new(content, id).build()
|
|
}
|
|
|
|
pub fn create_test_user(name: &str, id: i64) -> (i64, String) {
|
|
(id, name.to_string())
|
|
}
|
|
|
|
/// Хелпер для создания профиля
|
|
pub fn create_test_profile(title: &str, chat_id: i64) -> ProfileInfo {
|
|
ProfileInfo {
|
|
chat_id: ChatId::new(chat_id),
|
|
title: title.to_string(),
|
|
username: None,
|
|
bio: None,
|
|
phone_number: None,
|
|
chat_type: "Личный чат".to_string(),
|
|
member_count: None,
|
|
description: None,
|
|
invite_link: None,
|
|
is_group: false,
|
|
online_status: None,
|
|
}
|
|
}
|