Skip to content

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

test(content): add comprehensive content pipeline and metadata hydration test suite

Samir Hossains2ceec14639c9
diff --git a/CHANGELOG.md b/CHANGELOG.md
index dce604c..859c397 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -4,8 +4,8 @@
### Added
- **Comprehensive QA & Regression Test Architecture**:
- - Introduced a 71-test automated suite covering critical algorithmic boundaries, end-to-end user journeys, active player session lifecycle & duplicate launch protection, dynamic slash command autocomplete (`/download-dir reset`), search/command draft cancellation via `Esc`, 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/`).
+ - Introduced a 78-test automated suite covering critical algorithmic boundaries, end-to-end user journeys, content & metadata loading pipelines, stale request isolation, active player session lifecycle & duplicate launch protection, dynamic slash command autocomplete (`/download-dir reset`), search/command draft cancellation via `Esc`, 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/` (`content_pipeline.rs`, `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/known-issues.md b/docs/known-issues.md
index 670d342..56c7631 100644
--- a/docs/known-issues.md
+++ b/docs/known-issues.md
@@ -26,6 +26,6 @@ Tracked here so future work and issue reports reference the same facts.
## Verification
-- Automated testing is enforced via `cargo test --locked` covering 71 unit and integration tests across 10 test suites (see [`docs/testing.md`](testing.md)).
+- Automated testing is enforced via `cargo test --locked` covering 78 unit and integration tests across 11 test suites (see [`docs/testing.md`](testing.md)).
- Static correctness is enforced by strict compiler type checking, the lint gate (`cargo clippy --all-targets --all-features -- -D warnings`), formatting (`cargo fmt --check`), dependency vulnerability scanning (`cargo audit`), and packaging verification (`cargo package --locked`).
- Runtime and platform-specific behavior (terminal resize, focus handling, external player launch, and Termux chooser) are verified through the release checklist in [`release-checklist.md`](release-checklist.md).
diff --git a/docs/testing.md b/docs/testing.md
index addc4dd..9db5378 100644
--- a/docs/testing.md
+++ b/docs/testing.md
@@ -25,6 +25,7 @@ MovieBox-TUI/
├── 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
+ ├── content_pipeline.rs # Content/metadata pipeline, stale request isolation, & cache keys
├── tui_acceptance.rs # Headless TUI rendering, theme rendering, and resize matrix
└── addons_manifest.rs # Addon manifest deserialization & catalog checks
```
@@ -38,6 +39,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:
+- **`content_pipeline.rs`**: Validates search result identity, ambiguous title isolation, stale metadata response protection (`request_id` validation), cache key dimensional isolation, addon metadata mapping & partial degradation, search failure vs empty result status distinction, and mode-switch stale response isolation.
- **`error_handling.rs`**: Validates active player session lifecycle, playback debounce guards, 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, end-to-end user journeys (search, details, navigation, mode switching), 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).
diff --git a/tests/content_pipeline.rs b/tests/content_pipeline.rs
new file mode 100644
index 0000000..21a5cf5
--- /dev/null
+++ b/tests/content_pipeline.rs
@@ -0,0 +1,456 @@
+use moviebox_tui::{
+ providers::{
+ addons::{
+ adapter::{meta_detail_to_moviebox_json, metas_to_moviebox_search_json},
+ models::{MetaDetail, MetaItem, MetaVideo},
+ },
+ models::{ProviderKind, RequestContext},
+ },
+ tui::{
+ action::Action,
+ app::App,
+ state::{AppMode, InputMode, Screen},
+ },
+};
+
+#[test]
+fn test_search_result_identity_and_similar_title_isolation() {
+ let metas = vec![
+ MetaItem {
+ id: "tt0096895".to_string(),
+ r#type: "movie".to_string(),
+ name: "Batman".to_string(),
+ title: None,
+ poster: Some("https://example.com/batman1989.jpg".to_string()),
+ cover: None,
+ description: Some(
+ "The Dark Knight of Gotham City begins his war on crime.".to_string(),
+ ),
+ overview: None,
+ synopsis: None,
+ release_info: Some("1989".to_string()),
+ year: Some("1989".to_string()),
+ released: None,
+ imdb_rating: Some("7.5".to_string()),
+ rating: None,
+ genres: vec!["Action".to_string(), "Adventure".to_string()],
+ genre: vec![],
+ },
+ MetaItem {
+ id: "tt1877830".to_string(),
+ r#type: "movie".to_string(),
+ name: "The Batman".to_string(),
+ title: None,
+ poster: Some("https://example.com/thebatman2022.jpg".to_string()),
+ cover: None,
+ description: Some("In his second year of fighting crime...".to_string()),
+ overview: None,
+ synopsis: None,
+ release_info: Some("2022".to_string()),
+ year: Some("2022".to_string()),
+ released: None,
+ imdb_rating: Some("7.8".to_string()),
+ rating: None,
+ genres: vec![
+ "Action".to_string(),
+ "Crime".to_string(),
+ "Drama".to_string(),
+ ],
+ genre: vec![],
+ },
+ MetaItem {
+ id: "tt0103359".to_string(),
+ r#type: "series".to_string(),
+ name: "Batman: The Animated Series".to_string(),
+ title: None,
+ poster: Some("https://example.com/batman_tas.jpg".to_string()),
+ cover: None,
+ description: Some("The Caped Crusader battles Gotham City criminals.".to_string()),
+ overview: None,
+ synopsis: None,
+ release_info: Some("1992-1995".to_string()),
+ year: Some("1992".to_string()),
+ released: None,
+ imdb_rating: Some("9.0".to_string()),
+ rating: None,
+ genres: vec!["Animation".to_string(), "Action".to_string()],
+ genre: vec![],
+ },
+ ];
+
+ let search_json = metas_to_moviebox_search_json(metas);
+ let subjects = search_json
+ .get("results")
+ .and_then(|r| r.as_array())
+ .and_then(|arr| arr.first())
+ .and_then(|first| first.get("subjects"))
+ .and_then(|s| s.as_array())
+ .expect("subjects array must exist");
+
+ assert_eq!(subjects.len(), 3);
+
+ // Verify item 1 (Batman 1989)
+ assert_eq!(subjects[0]["subjectId"], "tt0096895");
+ assert_eq!(subjects[0]["title"], "Batman");
+ assert_eq!(subjects[0]["subjectType"], 1);
+ assert_eq!(subjects[0]["releaseDate"], "1989");
+
+ // Verify item 2 (The Batman 2022)
+ assert_eq!(subjects[1]["subjectId"], "tt1877830");
+ assert_eq!(subjects[1]["title"], "The Batman");
+ assert_eq!(subjects[1]["subjectType"], 1);
+ assert_eq!(subjects[1]["releaseDate"], "2022");
+
+ // Verify item 3 (Batman: The Animated Series - Series stype=2)
+ assert_eq!(subjects[2]["subjectId"], "tt0103359");
+ assert_eq!(subjects[2]["title"], "Batman: The Animated Series");
+ assert_eq!(subjects[2]["subjectType"], 2);
+ assert_eq!(subjects[2]["releaseDate"], "1992");
+}
+
+#[tokio::test]
+async fn test_stale_details_response_protection() {
+ let mut app = App::new();
+ app.state_mut().update_available = None;
+ app.state_mut().active_provider = ProviderKind::MovieBox;
+ app.state_mut().active_screen = Screen::Details;
+
+ // Simulate opening Movie A (id = "movie_a", request_id = 1)
+ let context_a = RequestContext {
+ provider: ProviderKind::MovieBox,
+ generation: app.state().provider_generation,
+ };
+ app.state_mut().active_details_request = 1;
+ app.state_mut().active_subject_id = Some("movie_a".to_string());
+ app.state_mut().selected_details = Some(serde_json::json!({
+ "id": "movie_a",
+ "title": "Movie A",
+ }));
+
+ // Immediately user opens Movie B (id = "movie_b", request_id = 2)
+ let context_b = RequestContext {
+ provider: ProviderKind::MovieBox,
+ generation: app.state().provider_generation,
+ };
+ app.state_mut().active_details_request = 2;
+ app.state_mut().active_subject_id = Some("movie_b".to_string());
+ app.state_mut().selected_details = Some(serde_json::json!({
+ "id": "movie_b",
+ "title": "Movie B Draft",
+ }));
+
+ // Now Movie A's stale response arrives with request_id = 1
+ let stale_payload = serde_json::json!({
+ "id": "movie_a",
+ "title": "Movie A Full Metadata",
+ "synopsis": "Stale synopsis from Movie A",
+ });
+ app.handle_action(Action::DetailsSuccess(
+ context_a,
+ 1, // Stale request ID
+ "movie_a".to_string(),
+ stale_payload,
+ ))
+ .await;
+
+ // Verify Movie A was REJECTED and did NOT overwrite Movie B
+ assert_eq!(app.state().active_subject_id.as_deref(), Some("movie_b"));
+ assert_eq!(
+ app.state().selected_details.as_ref().unwrap()["title"],
+ "Movie B Draft"
+ );
+
+ // Now Movie B's legitimate response arrives with request_id = 2
+ let valid_payload = serde_json::json!({
+ "id": "movie_b",
+ "title": "Movie B Full Metadata",
+ "synopsis": "Correct synopsis for Movie B",
+ });
+ app.handle_action(Action::DetailsSuccess(
+ context_b,
+ 2, // Current request ID
+ "movie_b".to_string(),
+ valid_payload,
+ ))
+ .await;
+
+ // Verify Movie B was ACCEPTED
+ assert_eq!(app.state().active_subject_id.as_deref(), Some("movie_b"));
+ assert_eq!(
+ app.state().selected_details.as_ref().unwrap()["title"],
+ "Movie B Full Metadata"
+ );
+ assert_eq!(
+ app.state().selected_details.as_ref().unwrap()["synopsis"],
+ "Correct synopsis for Movie B"
+ );
+}
+
+#[test]
+fn test_cache_key_isolation_across_providers_queries_and_dimensions() {
+ use moviebox_tui::cache::{
+ get_provider_details_path, get_provider_search_path, get_provider_stream_path,
+ };
+
+ // 1. Search paths isolation across providers, queries, and pages
+ let search_mb_batman_p1 = get_provider_search_path(ProviderKind::MovieBox, "batman", 1);
+ let search_mb_batman_p2 = get_provider_search_path(ProviderKind::MovieBox, "batman", 2);
+ let search_mb_superman_p1 = get_provider_search_path(ProviderKind::MovieBox, "superman", 1);
+ let search_addon_batman_p1 = get_provider_search_path(ProviderKind::Addons, "batman", 1);
+
+ assert_ne!(search_mb_batman_p1, search_mb_batman_p2);
+ assert_ne!(search_mb_batman_p1, search_mb_superman_p1);
+ assert_ne!(search_mb_batman_p1, search_addon_batman_p1);
+
+ // 2. Details paths isolation across providers and IDs
+ let details_mb_1 = get_provider_details_path(ProviderKind::MovieBox, "1001");
+ let details_mb_2 = get_provider_details_path(ProviderKind::MovieBox, "1002");
+ let details_4k_1 = get_provider_details_path(ProviderKind::FourKHdHub, "1001");
+
+ assert_ne!(details_mb_1, details_mb_2);
+ assert_ne!(details_mb_1, details_4k_1);
+
+ // 3. Stream paths isolation across seasons and episodes
+ let stream_s1_e1 = get_provider_stream_path(ProviderKind::MovieBox, "series_1", 1, 1);
+ let stream_s1_e2 = get_provider_stream_path(ProviderKind::MovieBox, "series_1", 1, 2);
+ let stream_s2_e1 = get_provider_stream_path(ProviderKind::MovieBox, "series_1", 2, 1);
+
+ assert_ne!(stream_s1_e1, stream_s1_e2);
+ assert_ne!(stream_s1_e1, stream_s2_e1);
+}
+
+#[test]
+fn test_addon_metadata_mapping_and_partial_data_degradation() {
+ // Minimal MetaDetail with missing optional fields
+ let minimal = MetaDetail {
+ id: "tt9999999".to_string(),
+ r#type: "movie".to_string(),
+ name: "Minimal Indie Film".to_string(),
+ title: None,
+ poster: None,
+ cover: None,
+ background: None,
+ logo: None,
+ description: None,
+ overview: None,
+ synopsis: None,
+ release_info: None,
+ year: None,
+ released: None,
+ imdb_rating: None,
+ rating: None,
+ genres: vec![],
+ genre: vec![],
+ runtime: None,
+ cast: vec![],
+ director: vec![],
+ videos: vec![],
+ };
+
+ let json_output = meta_detail_to_moviebox_json(&minimal);
+
+ assert_eq!(json_output["id"], "tt9999999");
+ assert_eq!(json_output["title"], "Minimal Indie Film");
+ assert_eq!(json_output["subjectType"], 1);
+ assert_eq!(json_output["releaseDate"], "");
+ assert!(json_output["description"].is_null());
+ assert!(json_output["director"].is_null());
+ assert!(json_output["stars"].is_null());
+
+ // Series with decomposed multi-season video records
+ let series_detail = MetaDetail {
+ id: "tt8888888".to_string(),
+ r#type: "series".to_string(),
+ name: "Test Drama".to_string(),
+ title: None,
+ poster: Some("https://example.com/poster.jpg".to_string()),
+ cover: None,
+ background: None,
+ logo: None,
+ description: Some("A gripping story.".to_string()),
+ overview: None,
+ synopsis: None,
+ release_info: Some("2021".to_string()),
+ year: Some("2021".to_string()),
+ released: None,
+ imdb_rating: Some("8.4".to_string()),
+ rating: None,
+ genres: vec!["Drama".to_string()],
+ genre: vec![],
+ runtime: Some("45 min".to_string()),
+ cast: vec!["Actor One".to_string(), "Actor Two".to_string()],
+ director: vec!["Director Name".to_string()],
+ videos: vec![
+ MetaVideo {
+ id: Some("ep1".to_string()),
+ title: Some("Pilot".to_string()),
+ name: None,
+ season: Some(1),
+ episode: Some(1),
+ number: Some(1),
+ released: None,
+ thumbnail: None,
+ },
+ MetaVideo {
+ id: Some("ep2".to_string()),
+ title: Some("Chapter 2".to_string()),
+ name: None,
+ season: Some(1),
+ episode: Some(2),
+ number: Some(2),
+ released: None,
+ thumbnail: None,
+ },
+ MetaVideo {
+ id: Some("ep3".to_string()),
+ title: Some("Season 2 Premiere".to_string()),
+ name: None,
+ season: Some(2),
+ episode: Some(1),
+ number: Some(1),
+ released: None,
+ thumbnail: None,
+ },
+ ],
+ };
+
+ let series_json = meta_detail_to_moviebox_json(&series_detail);
+
+ assert_eq!(series_json["subjectType"], 2);
+ assert_eq!(series_json["releaseDate"], "2021");
+ assert_eq!(series_json["director"], "Director Name");
+ assert_eq!(series_json["stars"], "Actor One, Actor Two");
+
+ let seasons = series_json["seasons"]["seasons"]
+ .as_array()
+ .expect("seasons array");
+ assert_eq!(seasons.len(), 2);
+ assert_eq!(seasons[0]["se"], 1);
+ assert_eq!(seasons[0]["maxEp"], 2);
+ assert_eq!(seasons[1]["se"], 2);
+ assert_eq!(seasons[1]["maxEp"], 1);
+}
+
+#[tokio::test]
+async fn test_search_failure_vs_empty_result_distinction() {
+ let mut app = App::new();
+ app.state_mut().update_available = None;
+ app.state_mut().active_provider = ProviderKind::MovieBox;
+ app.state_mut().input_mode = InputMode::Normal;
+ app.state_mut().search_query = "xyznonexistent".to_string();
+
+ let context = RequestContext {
+ provider: ProviderKind::MovieBox,
+ generation: app.state().provider_generation,
+ };
+
+ // Case 1: Search succeeds with 0 results
+ app.handle_action(Action::SearchSuccess {
+ context,
+ request_id: app.state().active_search_request,
+ query: "xyznonexistent".to_string(),
+ page: 1,
+ payload: serde_json::json!({
+ "results": [{
+ "subjects": []
+ }]
+ }),
+ })
+ .await;
+
+ assert!(app.state().search_results.is_empty());
+ assert!(app.state().search_error.is_none());
+ assert!(!app.state().is_loading);
+
+ // Case 2: Search fails due to network outage
+ app.handle_action(Action::SearchFailure(
+ context,
+ app.state().active_search_request,
+ 1,
+ "Network connection reset by peer".to_string(),
+ ))
+ .await;
+
+ assert!(app.state().search_results.is_empty());
+ assert_eq!(
+ app.state().search_error.as_deref(),
+ Some("Network connection reset by peer")
+ );
+ assert!(!app.state().is_loading);
+}
+
+#[tokio::test]
+async fn test_mode_switch_stale_response_protection() {
+ let mut app = App::new();
+ app.state_mut().update_available = None;
+ app.state_mut().set_mode(AppMode::Streaming);
+
+ let streaming_generation = app.state().provider_generation;
+ let streaming_context = RequestContext {
+ provider: ProviderKind::MovieBox,
+ generation: streaming_generation,
+ };
+ app.state_mut().active_search_request = 1;
+ app.state_mut().search_query = "avatar".to_string();
+
+ // User switches mode to Addon Mode while MovieBox search is in-flight
+ app.handle_action(Action::ToggleAddonMode).await;
+ assert_eq!(app.state().mode(), AppMode::Addon);
+ assert_ne!(app.state().provider_generation, streaming_generation);
+
+ // Now MovieBox search response arrives with previous generation context
+ let moviebox_payload = serde_json::json!({
+ "results": [{
+ "subjects": [{
+ "subjectId": "mb_12345",
+ "title": "Avatar",
+ "subjectType": 1,
+ "releaseDate": "2009",
+ "cover": { "url": "https://example.com/avatar.jpg" }
+ }]
+ }]
+ });
+
+ app.handle_action(Action::SearchSuccess {
+ context: streaming_context,
+ request_id: 1,
+ query: "avatar".to_string(),
+ page: 1,
+ payload: moviebox_payload,
+ })
+ .await;
+
+ // Stale streaming search MUST NOT leak into Addon Mode
+ assert!(app.state().search_results.is_empty());
+}
+
+#[tokio::test]
+async fn test_poster_identity_isolation() {
+ let mut app = App::new();
+ app.state_mut().update_available = None;
+ app.state_mut().active_screen = Screen::Details;
+ app.state_mut().active_subject_id = Some("target_movie_id".to_string());
+ app.state_mut().poster_image = None;
+
+ let dynamic_img = std::sync::Arc::new(image::DynamicImage::new_rgb8(10, 10));
+
+ // Stale poster for a different movie arrives
+ app.handle_action(Action::PosterSuccess(
+ "other_movie_id".to_string(),
+ dynamic_img.clone(),
+ ))
+ .await;
+
+ // Target movie's poster remains untouched (None)
+ assert!(app.state().poster_image.is_none());
+
+ // Legitimate poster for target movie arrives
+ app.handle_action(Action::PosterSuccess(
+ "target_movie_id".to_string(),
+ dynamic_img.clone(),
+ ))
+ .await;
+
+ // Poster is now populated
+ assert!(app.state().poster_image.is_some());
+}