#![cfg(unix)]
use crate::daemon::server::resolve_scan_target;
use std::path::PathBuf;
fn absolute_path(path: &str) -> String {
format!("/{}", path.replace('\\', "/"))
}
#[test]
fn absolute_path_passes_through_unchanged() {
let hosts = absolute_path(r"etc\hosts");
let app_log = absolute_path(r"var\log\app.log");
let home = absolute_path(r"home\u");
assert_eq!(
resolve_scan_target(&hosts, None).expect("absolute path resolves"),
PathBuf::from(&hosts)
);
assert_eq!(
resolve_scan_target(&app_log, Some(&home)).expect("absolute path resolves"),
PathBuf::from(&app_log)
);
}
#[test]
fn relative_path_is_anchored_to_the_client_working_dir() {
let project = absolute_path(r"home\u\proj");
assert_eq!(
resolve_scan_target("sub/file.txt", Some(&project))
.expect("relative path resolves under the client working_dir"),
PathBuf::from(project).join("sub/file.txt")
);
}
#[test]
fn relative_working_dir_fails_closed_instead_of_using_daemon_cwd() {
let err = resolve_scan_target("sub/file.txt", Some("relative-client-cwd"))
.expect_err("relative working_dir would resolve against daemon cwd");
assert!(
err.contains("working_dir")
&& err.contains("not absolute")
&& err.contains("absolute working_dir"),
"error must reject the relative working_dir explicitly, got: {err}"
);
}
#[test]
fn unanchored_relative_path_fails_closed_instead_of_using_daemon_cwd() {
let err = resolve_scan_target("rel.txt", None).expect_err(
"a relative path with no working_dir must fail closed, not fall back to daemon cwd",
);
assert!(
err.contains("cannot resolve relative path") && err.contains("absolute path"),
"error must be actionable and tell the client to resend an absolute path, got: {err}"
);
}