Files
telegram-tui/tests/copy.rs
Mikhail Kilin 56bddabbe1 feat: add optional feature flags for clipboard and url-open (P5.15)
Implemented feature flags to make dependencies optional:
- clipboard feature: controls arboard dependency
- url-open feature: controls open dependency
- Both enabled by default for backward compatibility

Changes:
- Cargo.toml: Added [features] section with optional deps
- src/input/main_input.rs: Conditional compilation for open::that() and copy_to_clipboard()
- tests/copy.rs: Clipboard tests only compile with feature enabled
- Graceful degradation: user-friendly error messages when features disabled

Benefits:
- Smaller binary size when features disabled
- Modular functionality
- Better platform compatibility

Progress: Priority 5: 1/3 tasks, Total: 17/20 (85%)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-02-01 02:23:17 +03:00

176 lines
6.9 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Integration tests for copy message flow
mod helpers;
use helpers::test_data::TestMessageBuilder;
/// Test: Форматирование простого сообщения для копирования
#[test]
fn test_format_plain_message() {
let msg = TestMessageBuilder::new("Hello, world!", 1)
.sender("Alice")
.outgoing()
.build();
// Простое сообщение должно содержать только текст
let formatted = format_message_for_test(&msg);
assert_eq!(formatted, "Hello, world!");
}
/// Test: Форматирование сообщения с forward контекстом
#[test]
fn test_format_message_with_forward() {
let msg = TestMessageBuilder::new("Forwarded message", 1)
.sender("Bob")
.forwarded_from("Alice")
.build();
// Сообщение с forward должно содержать контекст
let formatted = format_message_for_test(&msg);
assert!(formatted.contains("↪ Переслано от Alice"));
assert!(formatted.contains("Forwarded message"));
}
/// Test: Форматирование сообщения с reply контекстом
#[test]
fn test_format_message_with_reply() {
let reply_msg = TestMessageBuilder::new("Reply text", 2)
.sender("Bob")
.reply_to(1, "Alice", "Original message")
.build();
// Сообщение с reply должно содержать контекст оригинала
let formatted = format_message_for_test(&reply_msg);
assert!(formatted.contains("┌ Alice: Original message"));
assert!(formatted.contains("Reply text"));
}
/// Test: Форматирование сообщения с forward и reply одновременно
#[test]
fn test_format_message_with_both_contexts() {
// Создаём сообщение с reply и forward
let msg = TestMessageBuilder::new("Complex message", 2)
.sender("Bob")
.reply_to(1, "Alice", "Original")
.forwarded_from("Charlie")
.build();
let formatted = format_message_for_test(&msg);
// Должны быть оба контекста
assert!(formatted.contains("↪ Переслано от Charlie"));
assert!(formatted.contains("┌ Alice: Original"));
assert!(formatted.contains("Complex message"));
}
/// Test: Форматирование длинного сообщения
#[test]
fn test_format_long_message() {
let long_text = "This is a very long message that spans multiple lines. ".repeat(10);
let msg = TestMessageBuilder::new(&long_text, 1)
.sender("Alice")
.build();
let formatted = format_message_for_test(&msg);
assert_eq!(formatted, long_text);
}
/// Test: Форматирование сообщения с markdown entities
#[test]
fn test_format_message_with_markdown() {
// Этот тест проверяет что entities сохраняются при копировании
// В реальном коде entities конвертируются в markdown
let msg = TestMessageBuilder::new("Bold text", 1)
.sender("Alice")
.build();
let formatted = format_message_for_test(&msg);
// Для простоты проверяем что текст присутствует
// В реальности здесь должна быть конвертация entities в markdown
assert!(formatted.contains("Bold text"));
}
// Helper функция для форматирования (упрощённая версия)
// В реальном коде это делается в src/input/main_input.rs::format_message_for_clipboard
fn format_message_for_test(msg: &tele_tui::tdlib::MessageInfo) -> String {
let mut result = String::new();
// Добавляем forward контекст если есть
if let Some(forward) = &msg.forward_from() {
result.push_str(&format!("↪ Переслано от {}\n", forward.sender_name));
}
// Добавляем reply контекст если есть
if let Some(reply) = &msg.reply_to() {
result.push_str(&format!("{}: {}\n", reply.sender_name, reply.text));
}
// Добавляем основной текст
result.push_str(msg.text());
result
}
#[cfg(all(test, feature = "clipboard"))]
mod clipboard_tests {
use super::*;
/// Test: Проверка что clipboard функции не падают
/// Примечание: Реальное тестирование clipboard требует GUI окружения
/// и может быть ненадёжным в CI. Этот тест просто проверяет что
/// arboard::Clipboard инициализируется без ошибок.
#[test]
#[ignore] // Игнорируем в CI, так как может не быть GUI окружения
fn test_clipboard_initialization() {
use arboard::Clipboard;
// Проверяем что можем создать clipboard
let result = Clipboard::new();
// В headless окружении может вернуть ошибку - это нормально
// Главное что не паникует
match result {
Ok(_) => {
// Clipboard доступен - отлично!
}
Err(_) => {
// Clipboard недоступен - ожидаемо в headless окружении
// Тест всё равно проходит
}
}
}
/// Test: Копирование в реальный clipboard (только для локального тестирования)
#[test]
#[ignore] // Игнорируем по умолчанию, запускать вручную: cargo test --ignored
fn test_copy_to_real_clipboard() {
use arboard::Clipboard;
let test_text = "Test message for clipboard";
// Пытаемся скопировать
if let Ok(mut clipboard) = Clipboard::new() {
let copy_result = clipboard.set_text(test_text);
assert!(copy_result.is_ok(), "Failed to copy to clipboard");
// Пытаемся прочитать обратно
if let Ok(content) = clipboard.get_text() {
assert_eq!(content, test_text, "Clipboard content mismatch");
}
}
}
/// Test: Кроссплатформенность clipboard
#[test]
fn test_clipboard_availability() {
use arboard::Clipboard;
// Этот тест просто проверяет что arboard доступен на всех платформах
// arboard поддерживает: Linux (X11/Wayland), Windows, macOS
let _clipboard_available = Clipboard::new().is_ok();
// Тест всегда проходит - мы просто проверяем что код компилируется
// и не паникует на разных платформах
}
}