#[cfg(test)]
mod tests {
use g_tools::bin_xournalpp;
use g_tools::config::*;
use g_tools::*;
#[test]
fn test_config() {
let path = shellexpand::tilde("~/pdf_images/index.txt").to_string();
initialize_mutable_config("~/pdf_images/".to_string());
let index_txt_path = &MUTABLE_CONFIG
.get()
.expect("Error in config")
.lock()
.unwrap()
.index_txt;
assert_eq!(index_txt_path, &path);
}
#[test]
fn test_bin_xournalpp() {
use std::env::consts::OS;
match OS {
"linux" => {
let path = bin_xournalpp();
assert_eq!(path, "/usr/bin/xournalpp");
}
"macos" => {
let path = bin_xournalpp();
assert_eq!(path, "/Applications/Xournal++.app/Contents/MacOS/xournalpp");
}
_ => panic!("Unsupported OS: {}", OS),
}
}
#[test]
#[ignore]
fn test_show_bookmark() {
let tmp_dir = std::env::temp_dir().join("g_tools_test_bm_");
std::fs::create_dir_all(&tmp_dir).unwrap();
let bookmarks_path = tmp_dir.join("bookmarks.txt");
std::fs::write(&bookmarks_path, "abc12345 Chapter 1\nabc456 Section A").unwrap();
if let Some(config_lock) = MUTABLE_CONFIG.get() {
let mut config = config_lock.lock().unwrap();
config.bookmarks_txt = bookmarks_path.clone();
}
let result = show_bookmark(&"abc123".to_string());
assert!(result.is_some());
std::fs::remove_dir_all(&tmp_dir).ok();
}
#[test]
#[ignore]
fn test_locate_related_file() {
let tmp_dir = std::env::temp_dir().join("g_tools_test_loc_");
std::fs::create_dir_all(&tmp_dir).unwrap();
let index_path = tmp_dir.join("index.txt");
std::fs::write(&index_path, "abc123 doc1.pdf abc456 doc2.pdf").unwrap();
if let Some(config_lock) = MUTABLE_CONFIG.get() {
let mut config = config_lock.lock().unwrap();
config.index_txt = index_path.clone();
}
let result = locate_related_file("abc123");
assert!(result.is_some());
std::fs::remove_dir_all(&tmp_dir).ok();
}
#[test]
fn test_locate_related_file_not_found() {
let tmp_dir = std::env::temp_dir().join("g_tools_test_loc2_");
std::fs::create_dir_all(&tmp_dir).unwrap();
let index_path = tmp_dir.join("index.txt");
std::fs::write(&index_path, "abc123 doc1.pdf abc456 doc2.pdf").unwrap();
if let Some(config_lock) = MUTABLE_CONFIG.get() {
let mut config = config_lock.lock().unwrap();
config.index_txt = index_path.clone();
}
let result = locate_related_file("xyz999");
assert!(result.is_none());
std::fs::remove_dir_all(&tmp_dir).ok();
}
#[test]
fn test_copy_text_to_clipboard() {
let text1 = "Ipsum lorem".to_string();
let _ = copy_text_to_clipboard(text1);
let text2 = copy_text_from_clipboard();
assert_eq!(text2.unwrap(), "Ipsum lorem".to_string());
}
#[test]
#[ignore]
fn test_cmd_xournal_search() {
let tmp_dir = std::env::temp_dir().join("g_tools_test_search_");
std::fs::create_dir_all(&tmp_dir).unwrap();
let index_path = tmp_dir.join("index.txt");
std::fs::write(&index_path, "abc123 doc1.pdf abc456 doc2.pdf").unwrap();
if let Some(config_lock) = MUTABLE_CONFIG.get() {
let mut config = config_lock.lock().unwrap();
config.index_txt = index_path.clone();
}
let result = cmd_xournal(
XournalAction::Search {
text: "doc".to_string(),
},
false,
);
assert!(result.is_ok());
std::fs::remove_dir_all(&tmp_dir).ok();
}
#[test]
fn test_cmd_xournal() {
let result = cmd_xournal(
XournalAction::Open {
hash: "12345678".to_string(),
},
false,
);
assert!(result.is_err());
let error = result.unwrap_err();
assert_eq!(error, "Hash not found at index.txt");
}
#[test]
fn test_cmd_xournal_open_not_found() {
let result = cmd_xournal(
XournalAction::Open {
hash: "12345678".to_string(),
},
false,
);
assert!(result.is_err());
let error = result.unwrap_err();
assert_eq!(error, "Hash not found at index.txt");
}
#[test]
fn test_cmd_microci_install() {
let result = cmd_microci(MicroCIAction::Install);
assert!(result.is_ok());
}
}