Skip to content

Commit

Permalink
test(ctx): add test for cleaning streams bucket
Browse files Browse the repository at this point in the history
  • Loading branch information
tymmesyde committed Oct 12, 2023
1 parent 3f2f332 commit ea24e51
Showing 1 changed file with 118 additions and 1 deletion.
119 changes: 118 additions & 1 deletion src/unit_tests/ctx/uninstall_addon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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::<TestEnv, _>::new(
TestModel {
ctx: Ctx::new(
profile,
LibraryBucket::default(),
streams,
NotificationsBucket::new::<TestEnv>(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::<Profile>(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"
);
}

0 comments on commit ea24e51

Please sign in to comment.