From 605555607256d2e98a6b8c44243ec76863fc0596 Mon Sep 17 00:00:00 2001 From: Barafu Albino Cheetah Date: Mon, 15 Jul 2024 18:52:05 +0300 Subject: [PATCH] Create test for dowmloading baya character --- .gitignore | 1 + Cargo.lock | 61 ++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/baya_download.rs | 49 ++++++++++++++++++++++++++++++++++- src/main.rs | 23 ++++------------- 5 files changed, 116 insertions(+), 19 deletions(-) diff --git a/.gitignore b/.gitignore index 8085dda..f55aab8 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ /.vscode *.log src/example.rs +testing/* diff --git a/Cargo.lock b/Cargo.lock index 3ab8d60..b8c7396 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -516,6 +516,21 @@ dependencies = [ "new_debug_unreachable", ] +[[package]] +name = "futures" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "645c6916888f6cb6350d2550b80fb63e734897a8498abe35cfb732b6487804b0" +dependencies = [ + "futures-channel", + "futures-core", + "futures-executor", + "futures-io", + "futures-sink", + "futures-task", + "futures-util", +] + [[package]] name = "futures-channel" version = "0.3.30" @@ -532,12 +547,34 @@ version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dfc6580bb841c5a68e9ef15c77ccc837b40a7504914d52e47b8b0e9bbda25a1d" +[[package]] +name = "futures-executor" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a576fc72ae164fca6b9db127eaa9a9dda0d61316034f33a0a0d4eda41f02b01d" +dependencies = [ + "futures-core", + "futures-task", + "futures-util", +] + [[package]] name = "futures-io" version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a44623e20b9681a318efdd71c299b6b222ed6f231972bfe2f224ebad6311f0c1" +[[package]] +name = "futures-macro" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87750cf4b7a4c0625b1529e4c543c2182106e4dedc60a2a6455e00d212c489ac" +dependencies = [ + "proc-macro2 1.0.86", + "quote 1.0.36", + "syn 2.0.70", +] + [[package]] name = "futures-sink" version = "0.3.30" @@ -556,8 +593,10 @@ version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3d6401deb83407ab3da39eba7e33987a73c3df0c82b4bb5813ee871c19c41d48" dependencies = [ + "futures-channel", "futures-core", "futures-io", + "futures-macro", "futures-sink", "futures-task", "memchr", @@ -2043,6 +2082,7 @@ dependencies = [ "serde_json", "serde_path_to_error", "soup", + "test-context", ] [[package]] @@ -2068,6 +2108,27 @@ dependencies = [ "utf-8", ] +[[package]] +name = "test-context" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6676ab8513edfd2601a108621103fdb45cac9098305ca25ec93f7023b06b05d9" +dependencies = [ + "futures", + "test-context-macros", +] + +[[package]] +name = "test-context-macros" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ea17a2dc368aeca6f554343ced1b1e31f76d63683fa8016e5844bd7a5144a1" +dependencies = [ + "proc-macro2 1.0.86", + "quote 1.0.36", + "syn 2.0.70", +] + [[package]] name = "thiserror" version = "1.0.62" diff --git a/Cargo.toml b/Cargo.toml index fab2169..b7851d5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,3 +23,4 @@ serde = { version = "1.0.204", features = ["derive"] } serde_json = "1.0.120" serde_path_to_error = "0.1.16" soup = "0.5.1" +test-context = "0.3.0" diff --git a/src/baya_download.rs b/src/baya_download.rs index 19f05ff..92095bf 100644 --- a/src/baya_download.rs +++ b/src/baya_download.rs @@ -131,7 +131,7 @@ pub fn download_card_from_baya_url(url: &str) -> Result<()> { let tavern_card = TavernCardV2::from(&baya_character); let tavern_image = write_tavern_card(&tavern_card, &card_image).context("Could not write tavern card")?; - write_image_to_file(&tavern_image, &card_name)? ; + write_image_to_file(&tavern_image, &card_name)?; println!("Done!"); print!("Fap away!"); flush(); @@ -245,4 +245,51 @@ impl From<&Lorebook> for CharacterBook { } } +mod tests { + use super::*; + use std::collections::HashMap; + use test_context::{test_context, TestContext}; + use anyhow::Result; + const CACHE_PATH: &str = "testing/test_cache.txt"; // Cache for downloaded pages will be stored here. + + #[derive(serde::Serialize, serde::Deserialize)] + struct TestCache { + page_cache: HashMap, + } + + impl TestContext for TestCache { + fn setup() -> Self { + let file_content = + std::fs::read_to_string(CACHE_PATH).unwrap_or_else(|_| String::new()); + serde_json::from_str(&file_content).unwrap_or_else(|_| TestCache { + page_cache: HashMap::new(), + }) + } + + fn teardown(self) { + let serialized = serde_json::to_string(&self).expect("Failed to serialize cache"); + std::fs::write(CACHE_PATH, serialized).expect("Failed to write cache to file"); + } + } + + fn download_testing_webpage<'a>(url: &str, cache: &'a mut TestCache) -> Result<&'a str> { + if cache.page_cache.contains_key(url) { + return Ok(cache.page_cache.get(url).unwrap()); + } + let page_content = tools::download_page(url)?; + cache.page_cache.insert(url.to_string(), page_content); + return Ok(cache.page_cache.get(url).unwrap()); + } + + #[test_context(TestCache)] + #[test] + fn test_downloading_page(cache: &mut TestCache) -> Result<()> { + const TEST_URL: &str = "https://backyard.ai/hub/character/clmg7rj2e03j0mc0v69b1tai1"; + let page = download_testing_webpage(TEST_URL, cache)?; + let baya_char = parse_page(page)?; + let baya_char_name = baya_char.aiDisplayName.unwrap(); + assert_eq!(baya_char_name, "Character crafter Puppy"); + Ok(()) + } +} diff --git a/src/main.rs b/src/main.rs index 812d787..33f4839 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,24 +1,21 @@ #![allow(dead_code)] use env_logger::Builder; -use std::{ - env, - fs::File, -}; +use std::{env, fs::File}; -mod tavern_card_v2; mod baya_download; +mod tavern_card_v2; mod tools; //mod example; - fn main() { // Prepare debug logging. #[cfg(debug_assertions)] { - let target = Box::new(File::create("last_run.log").expect("Can't create file")); + let target = Box::new(File::create("testing/last_run.log") + .expect("Can't create file")); - Builder::new() + Builder::new() .target(env_logger::Target::Pipe(target)) .filter(None, log::LevelFilter::Info) .init(); @@ -57,13 +54,3 @@ fn wrong_usage() { fn print_usage() { println!("Usage: baya_get "); } - - - - - - - - - -