use super::tests::EnvVarGuard;
use super::*;
use tempfile::tempdir;
#[test]
#[serial_test::serial]
fn register_project_index_never_bypasses_sensitive_path_denylist() {
let data_dir = tempdir().unwrap();
let _env = EnvVarGuard::set(
trusty_common::data_dir::DATA_DIR_OVERRIDE_ENV,
data_dir.path(),
);
let _allow_production =
EnvVarGuard::set_str(trusty_common::test_harness::ALLOW_PRODUCTION_ENV, "1");
let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
let addr = listener.local_addr().unwrap();
let (tx, rx) = std::sync::mpsc::channel();
let server = std::thread::spawn(move || {
use std::io::{Read, Write};
let (mut stream, _) = listener.accept().unwrap();
drop(listener);
let mut buf = [0u8; 4096];
let n = stream.read(&mut buf).unwrap();
let request = String::from_utf8_lossy(&buf[..n]).to_string();
let _ =
stream.write_all(b"HTTP/1.1 200 OK\r\ncontent-length: 0\r\nconnection: close\r\n\r\n");
let _ = stream.flush();
let body = request.split("\r\n\r\n").nth(1).unwrap_or("").to_string();
let _ = tx.send(body);
});
let search_data_dir = data_dir.path().join("trusty-search");
std::fs::create_dir_all(&search_data_dir).unwrap();
std::fs::write(search_data_dir.join("http_addr"), addr.to_string()).unwrap();
let project = tempdir().unwrap();
std::fs::create_dir_all(project.path().join(".git")).unwrap();
let _ = register_project_index(project.path());
let body_json: serde_json::Value = serde_json::from_str(
&rx.recv_timeout(std::time::Duration::from_secs(5))
.expect("fake daemon must have received the create-index POST"),
)
.expect("captured body must be valid JSON");
let _ = server.join();
assert_eq!(
body_json.get("allow_sensitive_path"),
Some(&serde_json::Value::Bool(false)),
"register_project_index must never set allow_sensitive_path: true; got {body_json:?}"
);
}
#[test]
fn worktree_skip_vector_true_for_worktree_root() {
let tmp = tempdir().unwrap();
let wt = tmp.path().join("feat-branch");
std::fs::create_dir_all(&wt).unwrap();
std::fs::write(wt.join(".git"), "gitdir: /elsewhere/.git/worktrees/feat").unwrap();
assert!(
super::search_index::worktree_skip_vector(&wt),
"a worktree keeps its BM25+KG-only registration at launch"
);
}
#[test]
fn worktree_skip_vector_true_from_worktree_subdirectory() {
let tmp = tempdir().unwrap();
let wt = tmp.path().join("feat-branch");
let nested = wt.join("crates").join("inner");
std::fs::create_dir_all(&nested).unwrap();
std::fs::write(wt.join(".git"), "gitdir: /elsewhere/.git/worktrees/feat").unwrap();
assert!(
super::search_index::worktree_skip_vector(&nested),
"the decision must follow the git-root the index is keyed to, not the cwd"
);
}
#[test]
fn worktree_skip_vector_false_for_plain_clone() {
let tmp = tempdir().unwrap();
let clone = tmp.path().join("my-repo");
std::fs::create_dir_all(clone.join(".git")).unwrap();
assert!(
!super::search_index::worktree_skip_vector(&clone),
"a primary clone still embeds — skip_vector is a worktree-only decision"
);
}