Skip to content

Terminal interface to find, download, and stream movies, TV shows, and live TV using local media players.

test(errors): add error_handling integration test suite covering failure state recovery

Samir Hossainsb4aeaf8b28e9
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 461561c..dfcf84c 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -4,8 +4,8 @@
### Added
- **Comprehensive QA & Regression Test Architecture**:
- - Introduced a 53-test automated suite covering critical algorithmic boundaries, addon manifest validation, mouse interactions, modal dismissals, TUI rendering across terminal size matrices, state reconciliation, crypto HMAC signing, download chunk arithmetic, and URL/stem security.
- - Added structured integration tests in `tests/` (`tui_acceptance.rs`, `history_reconciliation.rs`, `cache_lifecycle.rs`, `player_integration.rs`, `m3u_integration.rs`, `addons_manifest.rs`, `download_integration.rs`, `url_security.rs`) and test fixtures (`tests/fixtures/`).
+ - Introduced a 59-test automated suite covering critical algorithmic boundaries, error handling, addon manifest validation, mouse interactions, modal dismissals, TUI rendering across terminal size matrices, state reconciliation, crypto HMAC signing, download chunk arithmetic, and URL/stem security.
+ - Added structured integration tests in `tests/` (`error_handling.rs`, `tui_acceptance.rs`, `history_reconciliation.rs`, `cache_lifecycle.rs`, `player_integration.rs`, `m3u_integration.rs`, `addons_manifest.rs`, `download_integration.rs`, `url_security.rs`) and test fixtures (`tests/fixtures/`).
- Added [`docs/testing.md`](docs/testing.md) detailing test architecture, command references, and manual QA procedures.
- **Playback Tracking & Watch History Progress**:
- Added real-time playback position tracking for `mpv` with injected tracker script (`moviebox_tracker.lua`) and 5-second periodic state auto-save to disk.
diff --git a/docs/testing.md b/docs/testing.md
index fa53f96..cd4b9f3 100644
--- a/docs/testing.md
+++ b/docs/testing.md
@@ -24,6 +24,7 @@ MovieBox-TUI/
├── m3u_integration.rs # Offline M3U playlist parsing
├── download_integration.rs # File stem sanitization & folder structure
├── url_security.rs # URL validation & device stem protection
+ ├── error_handling.rs # Failure state cleanup, error toasts, and recovery
├── tui_acceptance.rs # Headless TUI rendering, theme rendering, and resize matrix
└── addons_manifest.rs # Addon manifest deserialization & catalog checks
```
@@ -37,6 +38,7 @@ Inline unit tests live inside `#[cfg(test)] mod tests` blocks within their respe
### B. Subsystem Integration Tests (`tests/*.rs`)
Integration tests live in the `tests/` directory and test externally observable behaviors without mocking internal types:
+- **`error_handling.rs`**: Validates search failure cleanup, addon manifest error toasts, stream and download resolution failure notifications, malformed M3U recovery, and URL scheme rejection.
- **`tui_acceptance.rs`**: Validates headless TUI rendering, all theme palettes, mouse click and scroll interactions, modal dismissals, and terminal resize matrices across 8 standard and boundary dimensions without panics.
- **`history_reconciliation.rs`**: Validates `HistoryManager::is_same_show`, media type separation (Movies vs TV Series), remake year distinction, and state file reconciliation (MISS-01).
- **`cache_lifecycle.rs`**: Validates atomic file writing (both sync and async) and deterministic MD5 cache key generation.
diff --git a/tests/error_handling.rs b/tests/error_handling.rs
new file mode 100644
index 0000000..29652d1
--- /dev/null
+++ b/tests/error_handling.rs
@@ -0,0 +1,119 @@
+use moviebox_tui::models::NotificationKind;
+use moviebox_tui::providers::addons::client::AddonClient;
+use moviebox_tui::providers::models::RequestContext;
+use moviebox_tui::providers::tv::parser::M3UParser;
+use moviebox_tui::tui::action::Action;
+use moviebox_tui::tui::app::App;
+use moviebox_tui::tui::text::is_http_url;
+
+#[tokio::test]
+async fn test_search_failure_clears_loading_and_sets_error_state() {
+ let mut app = App::new();
+ app.state_mut().is_loading = true;
+ app.state_mut().active_search_request = 42;
+ app.state_mut().search_query = "Inception".to_string();
+
+ let context = RequestContext {
+ provider: app.state().active_provider,
+ generation: app.state().provider_generation,
+ };
+
+ app.handle_action(Action::SearchFailure(
+ context,
+ 42,
+ 1,
+ "Network connection timed out after 10s".to_string(),
+ ))
+ .await;
+
+ assert!(!app.state().is_loading);
+ assert_eq!(
+ app.state().search_error.as_deref(),
+ Some("Network connection timed out after 10s")
+ );
+ assert!(app.state().search_results.is_empty());
+}
+
+#[tokio::test]
+async fn test_addon_manifest_invalid_url_triggers_error_notification_and_preserves_state() {
+ let client = AddonClient::new();
+ let err = client
+ .fetch_manifest("https://invalid-nonexistent-domain.test/manifest.json")
+ .await
+ .unwrap_err();
+ assert!(err.contains("Failed to reach manifest") || err.contains("Manifest returned HTTP"));
+
+ let mut app = App::new();
+ let initial_addons = app.state().installed_addons.len();
+
+ app.handle_action(Action::SetStatus(format!(
+ "Error: Addon install failed: {err}"
+ )))
+ .await;
+
+ assert!(!app.state().notifications.is_empty());
+ let notif = app.state().notifications.back().unwrap();
+ assert_eq!(notif.kind, NotificationKind::Error);
+ assert!(notif.message.contains("Addon install failed"));
+ assert_eq!(app.state().installed_addons.len(), initial_addons);
+}
+
+#[tokio::test]
+async fn test_stream_resolve_failure_resets_resolving_flag_and_notifies_user() {
+ let mut app = App::new();
+ app.state_mut().is_resolving_playback = true;
+
+ app.handle_action(Action::SetStatus(
+ "Error: 4KHDHub source failed: Stream extraction returned HTTP 403 Forbidden".to_string(),
+ ))
+ .await;
+
+ assert!(!app.state().is_resolving_playback);
+ assert!(!app.state().notifications.is_empty());
+ let notif = app.state().notifications.back().unwrap();
+ assert_eq!(notif.kind, NotificationKind::Error);
+ assert!(
+ notif
+ .message
+ .contains("Stream extraction returned HTTP 403")
+ );
+}
+
+#[tokio::test]
+async fn test_download_resolve_failure_sets_error_status_and_notifies() {
+ let mut app = App::new();
+
+ app.handle_action(Action::SetStatus(
+ "Error: Resolve failed: Stream mirror link expired".to_string(),
+ ))
+ .await;
+
+ assert!(!app.state().notifications.is_empty());
+ let notif = app.state().notifications.back().unwrap();
+ assert_eq!(notif.kind, NotificationKind::Error);
+ assert!(notif.message.contains("Stream mirror link expired"));
+}
+
+#[tokio::test]
+async fn test_malformed_m3u_playlist_recovers_without_panic() {
+ let parser = M3UParser::new();
+ let malformed_m3u = "#EXTM3U\n#EXTINF:-1 tvg-id=\"\"\n\n#EXTINF:broken attributes without url";
+ let channels = parser.parse_m3u(malformed_m3u);
+ assert!(channels.is_empty());
+
+ let partial_valid_m3u = "#EXTM3U\n#EXTINF:-1 tvg-name=\"Channel 1\",Valid Channel\nhttps://stream.example.com/live.m3u8\n#BROKEN_LINE";
+ let channels2 = parser.parse_m3u(partial_valid_m3u);
+ assert_eq!(channels2.len(), 1);
+ assert_eq!(channels2[0].name, "Valid Channel");
+}
+
+#[tokio::test]
+async fn test_invalid_url_schemes_rejected_by_security_filter() {
+ assert!(!is_http_url("file:///etc/passwd"));
+ assert!(!is_http_url("ftp://server.local/file"));
+ assert!(!is_http_url("javascript:alert(1)"));
+ assert!(!is_http_url("data:text/html;base64,PHNjcmlwdD4="));
+ assert!(!is_http_url(""));
+ assert!(is_http_url("http://example.com"));
+ assert!(is_http_url("https://example.com/manifest.json"));
+}