use crate::_test_support::{remove_test_dir, save_file_content};
use crate::exec::packer::unpack_pack;
use crate::runtime::Runtime;
use simple_fs::SPath;
pub type Result<T> = core::result::Result<T, Box<dyn std::error::Error>>;
#[tokio::test]
async fn test_unpacker_impl_unpack_dest_exists_no_force() -> Result<()> {
let runtime = Runtime::new_test_runtime_sandbox_01().await?;
let dir_context = runtime.dir_context();
let aipack_wks_dir = dir_context
.aipack_paths()
.aipack_wks_dir()
.ok_or("Should have aipack_wks_dir")?;
let wks_custom_dir = aipack_wks_dir.get_pack_custom_dir()?;
let dest_dir = wks_custom_dir.join("test_ns").join("test_pack");
std::fs::create_dir_all(dest_dir.path())?;
save_file_content(&dest_dir.join("marker.txt"), "existing")?;
let result = unpack_pack(dir_context, "test_ns@test_pack", false).await;
assert!(result.is_err(), "Should fail when destination exists without --force");
let err_msg = format!("{}", result.unwrap_err());
assert!(
err_msg.contains("already exists"),
"Error should mention destination already exists, got: {err_msg}"
);
if dest_dir.exists() {
std::fs::remove_dir_all(dest_dir.path())?;
}
Ok(())
}
#[tokio::test]
async fn test_unpacker_impl_unpack_dest_exists_with_force() -> Result<()> {
let runtime = Runtime::new_test_runtime_sandbox_01().await?;
let dir_context = runtime.dir_context();
let aipack_wks_dir = dir_context
.aipack_paths()
.aipack_wks_dir()
.ok_or("Should have aipack_wks_dir")?;
let wks_custom_dir = aipack_wks_dir.get_pack_custom_dir()?;
let dest_dir = wks_custom_dir.join("test_ns_force").join("test_pack_force");
std::fs::create_dir_all(dest_dir.path())?;
save_file_content(&dest_dir.join("old_marker.txt"), "old content")?;
let result = unpack_pack(dir_context, "test_ns_force@test_pack_force", true).await;
let old_marker = dest_dir.join("old_marker.txt");
assert!(
!old_marker.exists(),
"Old marker file should have been removed by --force trash"
);
if result.is_err() {
}
if dest_dir.exists() {
std::fs::remove_dir_all(dest_dir.path())?;
}
Ok(())
}