refactor: use system timezone and harden client APIs

This commit is contained in:
Mikhail Kilin
2026-05-17 13:14:59 +03:00
parent 887892f809
commit e09b83be69
50 changed files with 172 additions and 343 deletions

View File

@@ -1,8 +1,7 @@
// Integration tests for config flow
use tele_tui::config::{
AudioConfig, ColorsConfig, Config, GeneralConfig, ImagesConfig, Keybindings,
NotificationsConfig,
AudioConfig, ColorsConfig, Config, ImagesConfig, Keybindings, NotificationsConfig,
};
/// Test: Дефолтные значения конфигурации
@@ -10,9 +9,6 @@ use tele_tui::config::{
fn test_config_default_values() {
let config = Config::default();
// Проверяем дефолтный timezone
assert_eq!(config.general.timezone, "+03:00");
// Проверяем дефолтные цвета
assert_eq!(config.colors.incoming_message, "white");
assert_eq!(config.colors.outgoing_message, "green");
@@ -25,7 +21,6 @@ fn test_config_default_values() {
#[test]
fn test_config_custom_values() {
let config = Config {
general: GeneralConfig { timezone: "+05:00".to_string() },
colors: ColorsConfig {
incoming_message: "cyan".to_string(),
outgoing_message: "blue".to_string(),
@@ -39,7 +34,6 @@ fn test_config_custom_values() {
audio: AudioConfig::default(),
};
assert_eq!(config.general.timezone, "+05:00");
assert_eq!(config.colors.incoming_message, "cyan");
assert_eq!(config.colors.outgoing_message, "blue");
}
@@ -109,7 +103,6 @@ fn test_parse_color_case_insensitive() {
#[test]
fn test_config_toml_serialization() {
let original_config = Config {
general: GeneralConfig { timezone: "-05:00".to_string() },
colors: ColorsConfig {
incoming_message: "cyan".to_string(),
outgoing_message: "blue".to_string(),
@@ -130,7 +123,6 @@ fn test_config_toml_serialization() {
let deserialized: Config = toml::from_str(&toml_string).expect("Failed to deserialize config");
// Проверяем что всё совпадает
assert_eq!(deserialized.general.timezone, "-05:00");
assert_eq!(deserialized.colors.incoming_message, "cyan");
assert_eq!(deserialized.colors.outgoing_message, "blue");
assert_eq!(deserialized.colors.selected_message, "red");
@@ -139,47 +131,19 @@ fn test_config_toml_serialization() {
/// Test: Парсинг TOML с частичными данными использует дефолты
#[test]
fn test_config_partial_toml_uses_defaults() {
// TOML только с timezone, без colors
// TOML только с colors.incoming_message
let toml_str = r#"
[general]
timezone = "+02:00"
[colors]
incoming_message = "cyan"
"#;
let config: Config = toml::from_str(toml_str).expect("Failed to parse partial TOML");
// Timezone должен быть из TOML
assert_eq!(config.general.timezone, "+02:00");
// Colors должны быть дефолтными
assert_eq!(config.colors.incoming_message, "white");
// Кастомный цвет должен примениться
assert_eq!(config.colors.incoming_message, "cyan");
// Остальные colors должны быть дефолтными
assert_eq!(config.colors.outgoing_message, "green");
}
#[cfg(test)]
mod timezone_tests {
use super::*;
/// Test: Различные форматы timezone
#[test]
fn test_timezone_formats() {
let positive = Config {
general: GeneralConfig { timezone: "+03:00".to_string() },
..Default::default()
};
assert_eq!(positive.general.timezone, "+03:00");
let negative = Config {
general: GeneralConfig { timezone: "-05:00".to_string() },
..Default::default()
};
assert_eq!(negative.general.timezone, "-05:00");
let zero = Config {
general: GeneralConfig { timezone: "+00:00".to_string() },
..Default::default()
};
assert_eq!(zero.general.timezone, "+00:00");
}
assert_eq!(config.colors.selected_message, "yellow");
}
#[cfg(test)]

View File

@@ -313,11 +313,11 @@ impl TdClientTrait for FakeTdClient {
// Not implemented
}
fn pending_view_messages_mut(&mut self) -> &mut Vec<(ChatId, Vec<MessageId>)> {
// WORKAROUND: Возвращаем мутабельную ссылку через leak
// Это безопасно так как мы единственные владельцы &mut self
let guard = self.pending_view_messages.lock().unwrap();
unsafe { &mut *(guard.as_ptr() as *mut Vec<(ChatId, Vec<MessageId>)>) }
fn enqueue_pending_view_messages(&mut self, chat_id: ChatId, message_ids: Vec<MessageId>) {
self.pending_view_messages
.lock()
.unwrap()
.push((chat_id, message_ids));
}
fn pending_user_ids_mut(&mut self) -> &mut Vec<UserId> {
@@ -333,6 +333,10 @@ impl TdClientTrait for FakeTdClient {
}
// ============ Notification methods ============
fn configure_notifications(&mut self, _config: &tele_tui::config::NotificationsConfig) {
// Not implemented for fake client (notifications are not tested)
}
fn sync_notification_muted_chats(&mut self) {
// Not implemented for fake client (notifications are not tested)
}

View File

@@ -1,12 +1,13 @@
---
source: tests/input_field.rs
assertion_line: 111
expression: output
---
┌──────────────────────────────────────────────────────────────────────────────┐
│👤 Mom │
└──────────────────────────────────────────────────────────────────────────────┘
┌──────────────────────────────────────────────────────────────────────────────┐
│ ──────── 02.01.2022 ──────── │
│ ──────── 20.12.2021 ──────── │
│ │
│ Вы ──────────────── │
│ ▶ Original message text (14:33 ✓✓) │

View File

@@ -1,12 +1,13 @@
---
source: tests/input_field.rs
assertion_line: 135
expression: output
---
┌──────────────────────────────────────────────────────────────────────────────┐
│👤 Mom │
└──────────────────────────────────────────────────────────────────────────────┘
┌──────────────────────────────────────────────────────────────────────────────┐
│ ──────── 02.01.2022 ──────── │
│ ──────── 20.12.2021 ──────── │
│ │
│Mom ──────────────── │
│ (14:33) What do you think about this? │

View File

@@ -1,12 +1,13 @@
---
source: tests/messages.rs
assertion_line: 418
expression: output
---
┌──────────────────────────────────────────────────────────────────────────────┐
│👤 Mom │
└──────────────────────────────────────────────────────────────────────────────┘
┌──────────────────────────────────────────────────────────────────────────────┐
│ ──────── 02.01.2022 ──────── │
│ ──────── 20.12.2021 ──────── │
│ │
│Alice ──────────────── │
│ (14:33) 📷 [Фото] │

View File

@@ -1,12 +1,13 @@
---
source: tests/messages.rs
assertion_line: 444
expression: output
---
┌──────────────────────────────────────────────────────────────────────────────┐
│👤 Mom │
└──────────────────────────────────────────────────────────────────────────────┘
┌──────────────────────────────────────────────────────────────────────────────┐
│ ──────── 02.01.2022 ──────── │
│ ──────── 20.12.2021 ──────── │
│ │
│ Вы ──────────────── │
│ 📷 [Фото] (14:33 ✓✓)│

View File

@@ -1,12 +1,13 @@
---
source: tests/messages.rs
assertion_line: 503
expression: output
---
┌──────────────────────────────────────────────────────────────────────────────┐
│👤 Mom │
└──────────────────────────────────────────────────────────────────────────────┘
┌──────────────────────────────────────────────────────────────────────────────┐
│ ──────── 02.01.2022 ──────── │
│ ──────── 20.12.2021 ──────── │
│ │
│Alice ──────────────── │
│ (14:33) 📷 [Фото] │

View File

@@ -1,12 +1,13 @@
---
source: tests/messages.rs
assertion_line: 476
expression: output
---
┌──────────────────────────────────────────────────────────────────────────────┐
│👤 Group Chat │
└──────────────────────────────────────────────────────────────────────────────┘
┌──────────────────────────────────────────────────────────────────────────────┐
│ ──────── 02.01.2022 ──────── │
│ ──────── 20.12.2021 ──────── │
│ │
│Alice ──────────────── │
│ (14:33) Regular message before │

View File

@@ -1,12 +1,13 @@
---
source: tests/messages.rs
assertion_line: 87
expression: output
---
┌──────────────────────────────────────────────────────────────────────────────┐
│👤 Mom │
└──────────────────────────────────────────────────────────────────────────────┘
┌──────────────────────────────────────────────────────────────────────────────┐
│ ──────── 02.01.2022 ──────── │
│ ──────── 20.12.2021 ──────── │
│ │
│User ──────────────── │
│ (14:33) Message from the past │

View File

@@ -1,12 +1,13 @@
---
source: tests/messages.rs
assertion_line: 186
expression: output
---
┌──────────────────────────────────────────────────────────────────────────────┐
│👤 Mom │
└──────────────────────────────────────────────────────────────────────────────┘
┌──────────────────────────────────────────────────────────────────────────────┐
│ ──────── 02.01.2022 ──────── │
│ ──────── 20.12.2021 ──────── │
│ │
│User ──────────────── │
│ (14:33 ✎) Edited text │

View File

@@ -1,12 +1,13 @@
---
source: tests/messages.rs
assertion_line: 325
expression: output
---
┌──────────────────────────────────────────────────────────────────────────────┐
│👤 Mom │
└──────────────────────────────────────────────────────────────────────────────┘
┌──────────────────────────────────────────────────────────────────────────────┐
│ ──────── 02.01.2022 ──────── │
│ ──────── 20.12.2021 ──────── │
│ │
│User ──────────────── │
│↪ Переслано от Alice │

View File

@@ -1,12 +1,13 @@
---
source: tests/messages.rs
assertion_line: 206
expression: output
---
┌──────────────────────────────────────────────────────────────────────────────┐
│👤 Mom │
└──────────────────────────────────────────────────────────────────────────────┘
┌──────────────────────────────────────────────────────────────────────────────┐
│ ──────── 02.01.2022 ──────── │
│ ──────── 20.12.2021 ──────── │
│ │
│User ──────────────── │
│ (14:33) This is a very long message that should wrap across multiple lines │

View File

@@ -1,12 +1,13 @@
---
source: tests/messages.rs
assertion_line: 225
expression: output
---
┌──────────────────────────────────────────────────────────────────────────────┐
│👤 Mom │
└──────────────────────────────────────────────────────────────────────────────┘
┌──────────────────────────────────────────────────────────────────────────────┐
│ ──────── 02.01.2022 ──────── │
│ ──────── 20.12.2021 ──────── │
│ │
│User ──────────────── │
│ (14:33) **bold** *italic* `code` │

View File

@@ -1,12 +1,13 @@
---
source: tests/messages.rs
assertion_line: 245
expression: output
---
┌──────────────────────────────────────────────────────────────────────────────┐
│👤 Mom │
└──────────────────────────────────────────────────────────────────────────────┘
┌──────────────────────────────────────────────────────────────────────────────┐
│ ──────── 02.01.2022 ──────── │
│ ──────── 20.12.2021 ──────── │
│ │
│User ──────────────── │
│ (14:33) Check [this](https://example.com) and @username │

View File

@@ -1,12 +1,13 @@
---
source: tests/messages.rs
assertion_line: 264
expression: output
---
┌──────────────────────────────────────────────────────────────────────────────┐
│👤 Mom │
└──────────────────────────────────────────────────────────────────────────────┘
┌──────────────────────────────────────────────────────────────────────────────┐
│ ──────── 02.01.2022 ──────── │
│ ──────── 20.12.2021 ──────── │
│ │
│User ──────────────── │
│ (14:33) Spoiler: ||hidden text|| │

View File

@@ -1,12 +1,13 @@
---
source: tests/messages.rs
assertion_line: 283
expression: output
---
┌──────────────────────────────────────────────────────────────────────────────┐
│👤 Mom │
└──────────────────────────────────────────────────────────────────────────────┘
┌──────────────────────────────────────────────────────────────────────────────┐
│ ──────── 02.01.2022 ──────── │
│ ──────── 20.12.2021 ──────── │
│ │
│User ──────────────── │
│ (14:33) [Фото] │

View File

@@ -1,12 +1,13 @@
---
source: tests/messages.rs
assertion_line: 368
expression: output
---
┌──────────────────────────────────────────────────────────────────────────────┐
│👤 Mom │
└──────────────────────────────────────────────────────────────────────────────┘
┌──────────────────────────────────────────────────────────────────────────────┐
│ ──────── 02.01.2022 ──────── │
│ ──────── 20.12.2021 ──────── │
│ │
│User ──────────────── │
│ (14:33) Popular message │

View File

@@ -1,12 +1,13 @@
---
source: tests/messages.rs
assertion_line: 167
expression: output
---
┌──────────────────────────────────────────────────────────────────────────────┐
│👤 Mom │
└──────────────────────────────────────────────────────────────────────────────┘
┌──────────────────────────────────────────────────────────────────────────────┐
│ ──────── 02.01.2022 ──────── │
│ ──────── 20.12.2021 ──────── │
│ │
│ Вы ──────────────── │
│ Read message (14:33 ✓✓) │

View File

@@ -1,12 +1,13 @@
---
source: tests/messages.rs
assertion_line: 137
expression: output
---
┌──────────────────────────────────────────────────────────────────────────────┐
│👤 Mom │
└──────────────────────────────────────────────────────────────────────────────┘
┌──────────────────────────────────────────────────────────────────────────────┐
│ ──────── 02.01.2022 ──────── │
│ ──────── 20.12.2021 ──────── │
│ │
│ Вы ──────────────── │
│ Just sent (14:33 ✓✓) │

View File

@@ -1,12 +1,13 @@
---
source: tests/messages.rs
assertion_line: 304
expression: output
---
┌──────────────────────────────────────────────────────────────────────────────┐
│👤 Mom │
└──────────────────────────────────────────────────────────────────────────────┘
┌──────────────────────────────────────────────────────────────────────────────┐
│ ──────── 02.01.2022 ──────── │
│ ──────── 20.12.2021 ──────── │
│ │
│User ──────────────── │
│┌ Mom: Original message text │

View File

@@ -1,12 +1,13 @@
---
source: tests/messages.rs
assertion_line: 388
expression: output
---
┌──────────────────────────────────────────────────────────────────────────────┐
│👤 Mom │
└──────────────────────────────────────────────────────────────────────────────┘
┌──────────────────────────────────────────────────────────────────────────────┐
│ ──────── 02.01.2022 ──────── │
│ ──────── 20.12.2021 ──────── │
│ │
│User ──────────────── │
│ (14:33) Selected message │

View File

@@ -1,12 +1,13 @@
---
source: tests/messages.rs
assertion_line: 118
expression: output
---
┌──────────────────────────────────────────────────────────────────────────────┐
│👤 Group Chat │
└──────────────────────────────────────────────────────────────────────────────┘
┌──────────────────────────────────────────────────────────────────────────────┐
│ ──────── 02.01.2022 ──────── │
│ ──────── 20.12.2021 ──────── │
│ │
│Alice ──────────────── │
│ (14:33) First message │

View File

@@ -1,12 +1,13 @@
---
source: tests/messages.rs
assertion_line: 46
expression: output
---
┌──────────────────────────────────────────────────────────────────────────────┐
│👤 Mom │
└──────────────────────────────────────────────────────────────────────────────┘
┌──────────────────────────────────────────────────────────────────────────────┐
│ ──────── 02.01.2022 ──────── │
│ ──────── 20.12.2021 ──────── │
│ │
│Mom ──────────────── │
│ (14:33) Hello there! │

View File

@@ -1,12 +1,13 @@
---
source: tests/messages.rs
assertion_line: 65
expression: output
---
┌──────────────────────────────────────────────────────────────────────────────┐
│👤 Mom │
└──────────────────────────────────────────────────────────────────────────────┘
┌──────────────────────────────────────────────────────────────────────────────┐
│ ──────── 02.01.2022 ──────── │
│ ──────── 20.12.2021 ──────── │
│ │
│ Вы ──────────────── │
│ Hi mom! (14:33 ✓✓) │

View File

@@ -1,12 +1,13 @@
---
source: tests/messages.rs
assertion_line: 346
expression: output
---
┌──────────────────────────────────────────────────────────────────────────────┐
│👤 Mom │
└──────────────────────────────────────────────────────────────────────────────┘
┌──────────────────────────────────────────────────────────────────────────────┐
│ ──────── 02.01.2022 ──────── │
│ ──────── 20.12.2021 ──────── │
│ │
│User ──────────────── │
│ (14:33) Great! │

View File

@@ -1,12 +1,13 @@
---
source: tests/modals.rs
assertion_line: 30
expression: output
---
┌──────────────────────────────────────────────────────────────────────────────┐
│👤 Mom │
└──────────────────────────────────────────────────────────────────────────────┘
┌──────────────────────────────────────────────────────────────────────────────┐
│ ──────── 02.01.2022 ──────── │
│ ──────── 20.12.2021 ──────── │
│ │
│ Вы ──────────────── │
│ Delete me (14:33 ✓✓) │

View File

@@ -1,12 +1,13 @@
---
source: tests/modals.rs
assertion_line: 61
expression: output
---
┌──────────────────────────────────────────────────────────────────────────────┐
│👤 Mom │
└──────────────────────────────────────────────────────────────────────────────┘
┌──────────────────────────────────────────────────────────────────────────────┐
│ ──────── 02.01.2022 ──────── │
│ ──────── 20.12.2021 ──────── │
│ │
│User ──────────────── │
│ (14:33) React to this │

View File

@@ -1,12 +1,13 @@
---
source: tests/modals.rs
assertion_line: 97
expression: output
---
┌──────────────────────────────────────────────────────────────────────────────┐
│👤 Mom │
└──────────────────────────────────────────────────────────────────────────────┘
┌──────────────────────────────────────────────────────────────────────────────┐
│ ──────── 02.01.2022 ──────── │
│ ──────── 20.12.2021 ──────── │
│ │
│User ──────────────── │
│ (14:33) React to this │

View File

@@ -1,13 +1,14 @@
---
source: tests/modals.rs
assertion_line: 163
expression: output
---
┌──────────────────────────────────────────────────────────────────────────────┐
│👤 Mom │
└──────────────────────────────────────────────────────────────────────────────┘
📌 02.01.2022 14:33 Important pinned message! Ctrl+P
📌 20.12.2021 14:33 Important pinned message! Ctrl+P
┌──────────────────────────────────────────────────────────────────────────────┐
│ ──────── 02.01.2022 ──────── │
│ ──────── 20.12.2021 ──────── │
│ │
│User ──────────────── │
│ (14:33) Regular message │

View File

@@ -1,15 +1,16 @@
---
source: tests/modals.rs
assertion_line: 193
expression: output
---
┌ Поиск по сообщениям ─────────────────────────────────────────────────────────┐
│🔍 world█ (1/2) │
└──────────────────────────────────────────────────────────────────────────────┘
┌──────────────────────────────────────────────────────────────────────────────┐
│▶ User (02.01.2022 14:33) │
│▶ User (20.12.2021 14:33) │
│ Hello world │
│ │
│ User (02.01.2022 14:33) │
│ User (20.12.2021 14:33) │
│ World is beautiful │
│ │
│ │