use std::{collections::BTreeMap, fs::File, ops::Deref}; use hex_literal::hex; use path_macro::path; use rstest::fixture; use temp_testdir::TempDir; use time::macros::datetime; use talon::{ db::{ model::{Version, Website}, Db, }, Talon, }; pub const SUBDOMAIN_1: &str = "-"; pub const SUBDOMAIN_2: &str = "spotify-gender-ex"; pub const SUBDOMAIN_3: &str = "rustypipe"; pub const SUBDOMAIN_4: &str = "spa"; pub const HASH_1_1_INDEX: [u8; 32] = hex!("3b5f6bad5376897435def176d0fe77e5b9b4f0deafc7491fc27262650744ad68"); pub const HASH_1_1_STYLE: [u8; 32] = hex!("356f131c825fbf604797c7e9c85352549d81db8af91fee834016d075110af026"); pub const HASH_1_2_INDEX: [u8; 32] = hex!("a44816e6c3b650bdf88e6532659ba07ef187c2113ae311da9709e056aec8eadb"); pub const HASH_1_2_STYLE: [u8; 32] = hex!("356f131c825fbf604797c7e9c85352549d81db8af91fee834016d075110af026"); pub const HASH_2_1_INDEX: [u8; 32] = hex!("6c5d37546616519e8973be51515b8a90898b4675f7b6d01f2d891edb686408a2"); pub const HASH_2_1_STYLE: [u8; 32] = hex!("fc825b409a49724af8f5b3c4ad15e175e68095ea746237a7b46152d3f383f541"); pub const HASH_3_1_INDEX: [u8; 32] = hex!("cc31423924cf1f124750825861ab1ccc675e755921fc2fa111c0a98e8c346a5e"); pub const HASH_3_1_STYLE: [u8; 32] = hex!("ee4fc4911a56e627c047a29ba3085131939d8d487759b9149d42aaab89ce8993"); pub const HASH_3_1_PAGE2: [u8; 32] = hex!("be4f409ca0adcb21cdc7130cde63031718406726f889ef97ac8870c90b330a75"); pub const HASH_SPA_INDEX: [u8; 32] = hex!("90d454dd999b52486902e845f748ce7510a1fc8404421c8f44dadccc5b4b8e1d"); pub const HASH_SPA_FALLBACK: [u8; 32] = hex!("4ee0d3f7522f620a2a69b39b7443f8fe65029e1324cefaf797b8cad2b223cf7b"); pub const API_KEY_ROOT: &str = "c32ff286c8ac1c3102625badf38ffd251ae0c4a56079d8ba490f320af63f1f47"; pub const API_KEY_2: &str = "21bdac19ffd22870d561b1d55b35eddd9029497107edb7b926aa3e7856bb409b"; // pub const API_KEY_3: &str = "04e99561e3824f387a217d141d2a3b46375de6864afbedf9c9a2cc102bc946a4"; pub const API_KEY_RO: &str = "48691ad9f42bb12e61e259b5e90dc941a293cfae11af18c9e6557f92557f0086"; pub struct DbTest { db: Db, _temp: TempDir, } impl Deref for DbTest { type Target = Db; fn deref(&self) -> &Self::Target { &self.db } } #[fixture] pub fn db_empty() -> DbTest { let temp = temp_testdir::TempDir::default(); let db = Db::new(&temp).unwrap(); DbTest { db, _temp: temp } } fn insert_websites(db: &Db) { db.insert_website( SUBDOMAIN_1, &Website { name: "ThetaDev".to_owned(), created_at: datetime!(2023-02-18 16:30 +0), updated_at: datetime!(2023-02-18 16:30 +0), latest_version: Some(2), color: Some(2068974), visibility: talon::model::Visibility::Featured, ..Default::default() }, ) .unwrap(); db.insert_website( SUBDOMAIN_2, &Website { name: "Spotify-Gender-Ex".to_owned(), created_at: datetime!(2023-02-18 16:30 +0), updated_at: datetime!(2023-02-18 16:30 +0), latest_version: Some(1), color: Some(1947988), visibility: talon::model::Visibility::Featured, source_url: Some("https://github.com/Theta-Dev/Spotify-Gender-Ex".to_owned()), source_icon: Some(talon::model::SourceIcon::Github), ..Default::default() }, ) .unwrap(); db.insert_website( SUBDOMAIN_3, &Website { name: "RustyPipe".to_owned(), created_at: datetime!(2023-02-20 18:30 +0), updated_at: datetime!(2023-02-20 18:30 +0), latest_version: Some(1), color: Some(7943647), visibility: talon::model::Visibility::Featured, source_url: Some("https://code.thetadev.de/ThetaDev/rustypipe".to_owned()), source_icon: Some(talon::model::SourceIcon::Gitea), ..Default::default() }, ) .unwrap(); db.insert_website( SUBDOMAIN_4, &Website { name: "SvelteKit SPA".to_owned(), created_at: datetime!(2023-03-03 22:00 +0), updated_at: datetime!(2023-03-03 22:00 +0), latest_version: Some(1), color: Some(16727552), visibility: talon::model::Visibility::Hidden, ..Default::default() }, ) .unwrap(); let mut v1_data = BTreeMap::new(); v1_data.insert("Version".to_owned(), "v0.1.0".to_owned()); v1_data.insert( "Deployed by".to_owned(), "https://github.com/Theta-Dev/Talon/actions/runs/1352014628".to_owned(), ); assert_eq!( db.insert_version( SUBDOMAIN_1, &Version { created_at: datetime!(2023-02-18 16:30 +0), data: v1_data, ..Default::default() }, ) .unwrap(), 1 ); let mut v2_data = BTreeMap::new(); v2_data.insert("Version".to_owned(), "v0.1.1".to_owned()); v2_data.insert( "Deployed by".to_owned(), "https://github.com/Theta-Dev/Talon/actions/runs/1354755231".to_owned(), ); assert_eq!( db.insert_version( SUBDOMAIN_1, &Version { created_at: datetime!(2023-02-18 16:52 +0), data: v2_data, ..Default::default() }, ) .unwrap(), 2 ); assert_eq!( db.insert_version( SUBDOMAIN_2, &Version { created_at: datetime!(2023-02-18 16:30 +0), ..Default::default() }, ) .unwrap(), 1 ); assert_eq!( db.insert_version( SUBDOMAIN_3, &Version { created_at: datetime!(2023-02-20 18:30 +0), ..Default::default() }, ) .unwrap(), 1 ); assert_eq!( db.insert_version( SUBDOMAIN_4, &Version { created_at: datetime!(2023-03-03 22:00 +0), fallback: Some("200.html".to_owned()), spa: true, ..Default::default() }, ) .unwrap(), 1 ); } #[fixture] pub fn db() -> DbTest { let temp = temp_testdir::TempDir::default(); let db = Db::new(&temp).unwrap(); insert_websites(&db); db.insert_file(SUBDOMAIN_1, 1, "index.html", &HASH_1_1_INDEX) .unwrap(); db.insert_file(SUBDOMAIN_1, 1, "style.css", &HASH_1_1_STYLE) .unwrap(); db.insert_file(SUBDOMAIN_1, 2, "index.html", &HASH_1_2_INDEX) .unwrap(); db.insert_file(SUBDOMAIN_1, 2, "assets/style.css", &HASH_1_2_STYLE) .unwrap(); db.insert_file( SUBDOMAIN_1, 2, "assets/image.jpg", &hex!("901d291a47a8a9b55c06f84e5e5f82fd2dcee65cac1406d6e878b805d45c1e93"), ) .unwrap(); db.insert_file( SUBDOMAIN_1, 2, "assets/test.js", &hex!("b6ed35f5ae339a35a8babb11a91ff90c1a62ef250d30fa98e59500e8dbb896fa"), ) .unwrap(); db.insert_file( SUBDOMAIN_1, 2, "data/example.txt", &hex!("bae6bdae8097c24f9a99028e04bfc8d5e0a0c318955316db0e7b955def9c1dbb"), ) .unwrap(); db.insert_file(SUBDOMAIN_2, 1, "index.html", &HASH_2_1_INDEX) .unwrap(); db.insert_file(SUBDOMAIN_2, 1, "gex_style.css", &HASH_2_1_STYLE) .unwrap(); db.insert_file(SUBDOMAIN_3, 1, "index.html", &HASH_3_1_INDEX) .unwrap(); db.insert_file(SUBDOMAIN_3, 1, "rp_style.css", &HASH_3_1_STYLE) .unwrap(); db.insert_file(SUBDOMAIN_3, 1, "page2/index.html", &HASH_3_1_PAGE2) .unwrap(); DbTest { db, _temp: temp } } pub struct TalonTest { talon: Talon, pub temp: TempDir, } impl Deref for TalonTest { type Target = Talon; fn deref(&self) -> &Self::Target { &self.talon } } #[fixture] pub fn tln() -> TalonTest { let temp = temp_testdir::TempDir::default(); std::fs::copy( path!("tests" / "testfiles" / "config" / "config_test.toml"), path!(temp / "config.toml"), ) .unwrap(); let talon = Talon::new(&temp).unwrap(); insert_websites(&talon.db); talon .storage .insert_dir(path!("tests" / "testfiles" / "ThetaDev0"), SUBDOMAIN_1, 1) .unwrap(); talon .storage .insert_dir(path!("tests" / "testfiles" / "ThetaDev1"), SUBDOMAIN_1, 2) .unwrap(); talon .storage .insert_dir(path!("tests" / "testfiles" / "GenderEx"), SUBDOMAIN_2, 1) .unwrap(); talon .storage .insert_dir(path!("tests" / "testfiles" / "RustyPipe"), SUBDOMAIN_3, 1) .unwrap(); talon .storage .insert_tgz_archive( File::open(path!("tests" / "testfiles" / "archive" / "spa.tar.gz")).unwrap(), SUBDOMAIN_4, 1, ) .unwrap(); TalonTest { talon, temp } }