From ea24e511eec292c31866570b9c98af957fffaa7c Mon Sep 17 00:00:00 2001 From: Tim Date: Thu, 12 Oct 2023 13:03:54 +0200 Subject: [PATCH] test(ctx): add test for cleaning streams bucket --- src/unit_tests/ctx/uninstall_addon.rs | 119 +++++++++++++++++++++++++- 1 file changed, 118 insertions(+), 1 deletion(-) diff --git a/src/unit_tests/ctx/uninstall_addon.rs b/src/unit_tests/ctx/uninstall_addon.rs index 80a0adaee..0810c7331 100644 --- a/src/unit_tests/ctx/uninstall_addon.rs +++ b/src/unit_tests/ctx/uninstall_addon.rs @@ -7,7 +7,8 @@ use crate::types::api::{APIResult, SuccessResponse}; use crate::types::library::LibraryBucket; use crate::types::notifications::NotificationsBucket; use crate::types::profile::{Auth, AuthKey, GDPRConsent, Profile, User}; -use crate::types::streams::StreamsBucket; +use crate::types::resource::{Stream, StreamBehaviorHints, StreamSource}; +use crate::types::streams::{StreamsBucket, StreamsItem, StreamsItemKey}; use crate::types::True; use crate::unit_tests::{ default_fetch_handler, Request, TestEnv, FETCH_HANDLER, REQUESTS, STORAGE, @@ -370,3 +371,119 @@ fn actionctx_uninstalladdon_not_installed() { "No requests have been sent" ); } + +#[test] +fn actionctx_uninstalladdon_streams_bucket() { + #[derive(Model, Clone, Default)] + #[model(TestEnv)] + struct TestModel { + ctx: Ctx, + } + let addon = Descriptor { + manifest: Manifest { + id: "id".to_owned(), + version: Version::new(0, 0, 1), + name: "name".to_owned(), + contact_email: None, + description: None, + logo: None, + background: None, + types: vec![], + resources: vec![], + id_prefixes: None, + catalogs: vec![], + addon_catalogs: vec![], + behavior_hints: Default::default(), + }, + transport_url: Url::parse("https://transport_url").unwrap(), + flags: Default::default(), + }; + let profile = Profile { + addons: vec![addon.to_owned()], + ..Default::default() + }; + let _env_mutex = TestEnv::reset().expect("Should have exclusive lock to TestEnv"); + STORAGE.write().unwrap().insert( + PROFILE_STORAGE_KEY.to_owned(), + serde_json::to_string(&profile).unwrap(), + ); + + let stream = Stream { + source: StreamSource::Url { + url: "https://source_url".parse().unwrap(), + }, + name: None, + description: None, + thumbnail: None, + subtitles: vec![], + behavior_hints: StreamBehaviorHints::default(), + }; + + let streams_item_key = StreamsItemKey { + meta_id: "tt123456".to_owned(), + video_id: "tt123456:1:0".to_owned(), + }; + + let streams_item = StreamsItem { + stream, + r#type: "movie".to_owned(), + meta_id: "tt123456".to_owned(), + video_id: "tt123456:1:0".to_owned(), + meta_transport_url: "https://transport_url".parse().unwrap(), + stream_transport_url: "https://transport_url".parse().unwrap(), + mtime: TestEnv::now(), + }; + + let mut streams = StreamsBucket::default(); + streams.items.insert(streams_item_key.clone(), streams_item); + + let (runtime, _rx) = Runtime::::new( + TestModel { + ctx: Ctx::new( + profile, + LibraryBucket::default(), + streams, + NotificationsBucket::new::(None, vec![]), + ), + }, + vec![], + 1000, + ); + TestEnv::run(|| { + runtime.dispatch(RuntimeAction { + field: None, + action: Action::Ctx(ActionCtx::UninstallAddon(addon)), + }) + }); + assert!( + runtime.model().unwrap().ctx.profile.addons.is_empty(), + "addons updated successfully in memory" + ); + assert!( + STORAGE + .read() + .unwrap() + .get(PROFILE_STORAGE_KEY) + .map_or(false, |data| { + serde_json::from_str::(data) + .unwrap() + .addons + .is_empty() + }), + "addons updated successfully in storage" + ); + assert!( + REQUESTS.read().unwrap().is_empty(), + "No requests have been sent" + ); + assert!( + !runtime + .model() + .unwrap() + .ctx + .streams + .items + .contains_key(&streams_item_key), + "streams have been removed from bucket" + ); +}