use std::path::PathBuf;
use crate::find_build_assets_dir_logic;
use std::env;
use std::fs;
// --- Test 1: Debug Environment Logic (Mocked) ---
#[test]
fn test_find_build_assets_dir_logic_debug() {
// Simulate: ResDir is None, ExeDir is deep in target/debug
// target/debug/deps/app-123.exe -> we need to go up to project root
// Structure simulation:
// /mock_root/build_assets
// /mock_root/src/backend/target/debug/deps/app.exe
let root = env::temp_dir().join("sqlitevector_integrity_test_debug");
if root.exists() { fs::remove_dir_all(&root).unwrap(); }
fs::create_dir_all(&root).unwrap();
let build_assets = root.join("build_assets");
fs::create_dir_all(&build_assets).unwrap();
let exe_dir = root.join("src/backend/target/debug/deps");
fs::create_dir_all(&exe_dir).unwrap();
// Execute Logic
let result = find_build_assets_dir_logic(None, Some(exe_dir));
// Assert
assert!(result.is_some(), "Should find build_assets from debug exe location");
assert_eq!(result.unwrap(), build_assets, "Path should resolve to project root build_assets");
// Cleanup
let _ = fs::remove_dir_all(root);
}
// --- Test 2: Release Environment Logic (Mocked) ---
#[test]
fn test_find_build_assets_dir_logic_release() {
// Simulate: ResDir is set (Tauri resource dir), ExeDir is irrelevant
// Structure simulation:
// /mock_app/resources/build_assets (Nested as per new plan)
let root = env::temp_dir().join("sqlitevector_integrity_test_release");
if root.exists() { fs::remove_dir_all(&root).unwrap(); }
fs::create_dir_all(&root).unwrap();
let res_dir = root.join("resources");
fs::create_dir_all(&res_dir).unwrap();
let build_assets = res_dir.join("build_assets");
fs::create_dir_all(&build_assets).unwrap();
// Execute Logic
let result = find_build_assets_dir_logic(Some(res_dir.clone()), Some(PathBuf::from("/any/place")));
// Assert
assert!(result.is_some(), "Should find build_assets from resource dir");
assert_eq!(result.unwrap(), build_assets, "Path should resolve to nested build_assets");
// Cleanup
let _ = fs::remove_dir_all(root);
}
// --- Test 3: Configuration Consistency (Source Check) ---
#[test]
fn test_source_files_existence() {
// Validates that files referenced in tauri.conf.json actually exist in the checkout
// This runs in the actual build environment (cargo test)
let manifest_dir = env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR not set");
let manifest_path = PathBuf::from(manifest_dir).join("tauri.conf.json");
assert!(manifest_path.exists(), "tauri.conf.json must exist at {}", manifest_path.display());
let content = fs::read_to_string(&manifest_path).expect("Failed to read tauri.conf.json");
let json: serde_json::Value = serde_json::from_str(&content).expect("Failed to parse tauri.conf.json");
// Check Resources
if let Some(resources) = json.get("bundle").and_then(|b| b.get("resources")).and_then(|r| r.as_array()) {
for res in resources {
let res_str = res.as_str().expect("Resource path must be string");
// Resolve relative to tauri.conf.json (src/backend)
// Note: paths in tauri.conf.json are relative to it
// e.g. "../../build_assets_tmp"
// Remove glob if present for check (Logic in lib.rs handles '/**/*' but we removed it in plan)
let clean_path = res_str.replace("/**/*", "");
let full_path = manifest_path.parent().unwrap().join(&clean_path);
// Allow for some flexibility if it's a glob we didn't fully handle, but for now we expect exact directory
assert!(full_path.exists(),
"Resource path defined in tauri.conf.json does not exist: {} (Resolved: {})",
res_str, full_path.display()
);
println!("Verified resource exists: {}", full_path.display());
}
}
// Check External Bin (llama-server)
if let Some(bins) = json.get("bundle").and_then(|b| b.get("externalBin")).and_then(|r| r.as_array()) {
for bin in bins {
let bin_str = bin.as_str().expect("Bin path must be string");
// Tauri appends target triple, but the SOURCE file should be at the path or path.exe?
// Actually Tauri defines input paths.
// Wait, tauri.conf.json: "externalBin": ["../../bin/llama-server"]
// On Windows, the source file expected is likely `../../bin/llama-server.exe`
let mut full_path = manifest_path.parent().unwrap().join(bin_str);
if cfg!(windows) {
full_path.set_extension("exe");
}
// Fallback: Check for target triple suffix (Tauri Sidecar requirement)
// If bin/llama-server.exe doesn't exist, check bin/llama-server-x86_64-pc-windows-msvc.exe
if !full_path.exists() {
let parent = full_path.parent().unwrap();
if let Some(stem) = full_path.file_stem().and_then(|s| s.to_str()) {
let target_triple_suffix = "-x86_64-pc-windows-msvc";
let new_name = format!("{}{}.exe", stem, target_triple_suffix);
let triple_path = parent.join(new_name);
if triple_path.exists() {
println!("Found binary with target triple: {:?}", triple_path);
full_path = triple_path;
}
}
}
assert!(full_path.exists(),
"External Binary defined in tauri.conf.json does not exist: {} (Resolved: {})",
bin_str, full_path.display()
);
println!("Verified binary exists: {}", full_path.display());
}
}
}