use asar_rust::asar::*;
use std::fs;
fn setup_test_dir(name: &str) -> std::path::PathBuf {
let dir = std::env::temp_dir().join(format!("asar-api-{}", name));
let _ = fs::remove_dir_all(&dir);
fs::create_dir_all(&dir).unwrap();
dir
}
#[test]
fn test_create_and_extract_roundtrip() {
let dir = setup_test_dir("roundtrip");
let src = dir.join("src");
fs::create_dir_all(&src).unwrap();
fs::write(src.join("hello.txt"), b"Hello, World!").unwrap();
fs::write(src.join("config.json"), b"{\"key\": \"value\"}").unwrap();
let asar_path = dir.join("test.asar");
create_package(&src, &asar_path).unwrap();
assert!(asar_path.exists());
let files = list_package(&asar_path, None).unwrap();
assert_eq!(files.len(), 2);
let content = extract_file(&asar_path, "/hello.txt", true).unwrap();
assert_eq!(content, b"Hello, World!");
let out_dir = dir.join("out");
extract_all(&asar_path, &out_dir).unwrap();
assert!(out_dir.join("hello.txt").exists());
assert_eq!(
fs::read_to_string(out_dir.join("hello.txt")).unwrap(),
"Hello, World!"
);
let _ = fs::remove_dir_all(&dir);
}
#[test]
fn test_get_raw_header() {
let dir = setup_test_dir("header");
let src = dir.join("src");
fs::create_dir_all(&src).unwrap();
fs::write(src.join("test.txt"), b"test").unwrap();
let asar_path = dir.join("test.asar");
create_package(&src, &asar_path).unwrap();
let header = get_raw_header(&asar_path).unwrap();
assert!(header.header_size > 0);
assert!(!header.header_string.is_empty());
let _ = fs::remove_dir_all(&dir);
}
#[test]
fn test_stat_file() {
let dir = setup_test_dir("stat");
let src = dir.join("src");
fs::create_dir_all(&src).unwrap();
fs::write(src.join("data.bin"), b"binary data here").unwrap();
let asar_path = dir.join("test.asar");
create_package(&src, &asar_path).unwrap();
let entry = stat_file(&asar_path, "/data.bin", true).unwrap();
assert!(entry.is_file());
let _ = fs::remove_dir_all(&dir);
}