Skip to content

Commit

Permalink
feat: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kKaskak committed Oct 7, 2024
1 parent 4fa6ffd commit 1cf708f
Show file tree
Hide file tree
Showing 2 changed files with 186 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/unit_tests/ctx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ mod sync_library_with_api;
mod uninstall_addon;
mod update_search_history;
mod update_settings;
mod update_streaming_server_urls;
mod upgrade_addon;
185 changes: 185 additions & 0 deletions src/unit_tests/ctx/update_streaming_server_urls.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
use crate::constants::STREAMING_SERVER_URLS_STORAGE_KEY;
use crate::models::ctx::Ctx;
use crate::runtime::msg::{Action, ActionServerUrlsBucket, ActionStreamingServer};
use crate::runtime::{Env, Runtime, RuntimeAction};
use crate::types::events::DismissedEventsBucket;
use crate::types::library::LibraryBucket;
use crate::types::notifications::NotificationsBucket;
use crate::types::profile::Profile;
use crate::types::search_history::SearchHistoryBucket;
use crate::types::server_urls::{ServerUrlItem, ServerUrlsBucket};
use crate::types::streams::StreamsBucket;
use crate::unit_tests::{TestEnv, STORAGE};
use stremio_derive::Model;
use url::Url;

#[test]
fn test_add_server_url() {
#[derive(Model, Clone, Default)]
#[model(TestEnv)]
struct TestModel {
ctx: Ctx,
}
let _env_mutex = TestEnv::reset().expect("Should have exclusive lock to TestEnv");
let ctx = Ctx::new(
Profile::default(),
LibraryBucket::default(),
StreamsBucket::default(),
ServerUrlsBucket::new(None),
NotificationsBucket::new::<TestEnv>(None, vec![]),
SearchHistoryBucket::default(),
DismissedEventsBucket::default(),
);
let (runtime, _rx) = Runtime::<TestEnv, _>::new(TestModel { ctx }, vec![], 1000);
let new_url = Url::parse("http://localhost:11470").unwrap();
TestEnv::run(|| {
runtime.dispatch(RuntimeAction {
field: None,
action: Action::StreamingServer(ActionStreamingServer::ServerUrlsBucket(
ActionServerUrlsBucket::AddServerUrl(new_url.clone()),
)),
})
});
let server_urls = &runtime.model().unwrap().ctx.streaming_server_urls;
assert!(
server_urls.items.values().any(|item| item.url == new_url),
"New server URL should be added"
);
assert!(
STORAGE
.read()
.unwrap()
.get(STREAMING_SERVER_URLS_STORAGE_KEY)
.map_or(false, |data| {
let stored_bucket: ServerUrlsBucket = serde_json::from_str(data).unwrap();
stored_bucket.items.values().any(|item| item.url == new_url)
}),
"New server URL should be stored"
);
}

#[test]
fn test_edit_server_url() {
#[derive(Model, Clone, Default)]
#[model(TestEnv)]
struct TestModel {
ctx: Ctx,
}
let _env_mutex = TestEnv::reset().expect("Should have exclusive lock to TestEnv");

// Initialize with a server URL
let initial_url = Url::parse("http://localhost:11470").unwrap();
let mut server_urls = ServerUrlsBucket::new(None);
let item_id = 1;
let server_url_item =
ServerUrlItem::new(item_id, initial_url.clone(), TestEnv::now().timestamp());
server_urls.items.insert(item_id, server_url_item);

STORAGE.write().unwrap().insert(
STREAMING_SERVER_URLS_STORAGE_KEY.to_owned(),
serde_json::to_string(&server_urls).unwrap(),
);

let ctx = Ctx::new(
Profile::default(),
LibraryBucket::default(),
StreamsBucket::default(),
server_urls,
NotificationsBucket::new::<TestEnv>(None, vec![]),
SearchHistoryBucket::default(),
DismissedEventsBucket::default(),
);
let (runtime, _rx) = Runtime::<TestEnv, _>::new(TestModel { ctx }, vec![], 1000);
let new_url = Url::parse("http://localhost:11471").unwrap();
TestEnv::run(|| {
runtime.dispatch(RuntimeAction {
field: None,
action: Action::StreamingServer(ActionStreamingServer::ServerUrlsBucket(
ActionServerUrlsBucket::EditServerUrl {
id: item_id,
new_url: new_url.clone(),
},
)),
})
});
let server_urls = &runtime.model().unwrap().ctx.streaming_server_urls;
assert!(
server_urls
.items
.get(&item_id)
.map_or(false, |item| item.url == new_url),
"Server URL should be updated"
);
assert!(
STORAGE
.read()
.unwrap()
.get(STREAMING_SERVER_URLS_STORAGE_KEY)
.map_or(false, |data| {
let stored_bucket: ServerUrlsBucket = serde_json::from_str(data).unwrap();
stored_bucket
.items
.get(&item_id)
.map_or(false, |item| item.url == new_url)
}),
"Updated server URL should be stored"
);
}

#[test]
fn test_delete_server_url() {
#[derive(Model, Clone, Default)]
#[model(TestEnv)]
struct TestModel {
ctx: Ctx,
}
let _env_mutex = TestEnv::reset().expect("Should have exclusive lock to TestEnv");

// Initialize with a server URL
let initial_url = Url::parse("http://localhost:11470").unwrap();
let mut server_urls = ServerUrlsBucket::new(None);
let item_id = 1;
let server_url_item =
ServerUrlItem::new(item_id, initial_url.clone(), TestEnv::now().timestamp());
server_urls.items.insert(item_id, server_url_item);

STORAGE.write().unwrap().insert(
STREAMING_SERVER_URLS_STORAGE_KEY.to_owned(),
serde_json::to_string(&server_urls).unwrap(),
);

let ctx = Ctx::new(
Profile::default(),
LibraryBucket::default(),
StreamsBucket::default(),
server_urls,
NotificationsBucket::new::<TestEnv>(None, vec![]),
SearchHistoryBucket::default(),
DismissedEventsBucket::default(),
);
let (runtime, _rx) = Runtime::<TestEnv, _>::new(TestModel { ctx }, vec![], 1000);
TestEnv::run(|| {
runtime.dispatch(RuntimeAction {
field: None,
action: Action::StreamingServer(ActionStreamingServer::ServerUrlsBucket(
ActionServerUrlsBucket::DeleteServerUrl(item_id),
)),
})
});
let server_urls = &runtime.model().unwrap().ctx.streaming_server_urls;
assert!(
!server_urls.items.contains_key(&item_id),
"Server URL should be deleted"
);
assert!(
STORAGE
.read()
.unwrap()
.get(STREAMING_SERVER_URLS_STORAGE_KEY)
.map_or(true, |data| {
let stored_bucket: ServerUrlsBucket = serde_json::from_str(data).unwrap();
!stored_bucket.items.contains_key(&item_id)
}),
"Deleted server URL should not be stored"
);
}

0 comments on commit 1cf708f

Please sign in to comment.