use serde_json::json;
use super::*;
fn index_entry(id: &str, root_path: &str) -> Value {
json!({ "id": id, "root_path": root_path, "size_bytes": 0 })
}
#[test]
fn pick_index_prefers_exact_match() {
let tmp = std::env::temp_dir();
let root = tmp.to_str().expect("utf8 temp dir");
let indexes = vec![
index_entry("other", "/nonexistent/elsewhere"),
index_entry("mine", root),
];
assert_eq!(pick_index(&indexes, &tmp).as_deref(), Some("mine"));
}
#[test]
fn pick_index_falls_back_to_longest_ancestor() {
let base = std::env::temp_dir();
let nested = base.join("tcode-pick-index-nested");
std::fs::create_dir_all(&nested).expect("mkdir nested");
let shallow = base.to_str().expect("utf8");
let deep = nested.to_str().expect("utf8");
let with_exact = vec![index_entry("ancestor", shallow), index_entry("exact", deep)];
assert_eq!(pick_index(&with_exact, &nested).as_deref(), Some("exact"));
let ancestor_only = vec![index_entry("ancestor", shallow)];
assert_eq!(
pick_index(&ancestor_only, &nested).as_deref(),
Some("ancestor")
);
let _ = std::fs::remove_dir_all(&nested);
}
#[test]
fn pick_index_returns_none_when_uncovered() {
let indexes = vec![index_entry("elsewhere", "/nonexistent/elsewhere")];
assert_eq!(
pick_index(&indexes, Path::new("/nonexistent/project")),
None
);
}
#[test]
fn mcp_text_extracts_content() {
let result = json!({ "content": [{ "type": "text", "text": "{\"indexes\":[]}" }] });
assert_eq!(mcp_text(&result).as_deref(), Some("{\"indexes\":[]}"));
let empty = json!({ "isError": false });
assert_eq!(mcp_text(&empty), None);
}
#[test]
fn is_mcp_error_detects_error_flag() {
assert!(is_mcp_error(&json!({ "isError": true, "content": [] })));
assert!(!is_mcp_error(&json!({ "isError": false })));
assert!(!is_mcp_error(&json!({ "content": [] })));
}
#[test]
fn is_stage_not_ready_detects_meta_code() {
let meta = json!({
"isError": true,
"content": [{ "type": "text", "text": "requires Stage 2 (embeddings), not yet ready" }],
"_meta": { "error_code": "STAGE_NOT_READY" }
});
assert!(is_stage_not_ready(&meta));
let other_code = json!({
"isError": true,
"_meta": { "error_code": "INDEX_NOT_FOUND" }
});
assert!(!is_stage_not_ready(&other_code));
let no_meta = json!({
"isError": true,
"content": [{ "type": "text", "text": "no index covers this project" }]
});
assert!(!is_stage_not_ready(&no_meta));
}
#[test]
fn should_lexical_fallback_only_for_stage_not_ready() {
let not_ready = json!({ "isError": true, "_meta": { "error_code": "STAGE_NOT_READY" } });
assert!(should_lexical_fallback("semantic", ¬_ready));
assert!(should_lexical_fallback("symbol", ¬_ready));
assert!(!should_lexical_fallback("grep", ¬_ready));
let other = json!({ "isError": true, "content": [{ "type": "text", "text": "boom" }] });
assert!(!should_lexical_fallback("semantic", &other));
}
#[test]
fn lexical_params_matches_search_lexical_schema() {
let params = lexical_params("trusty-tools", "apply_archive_downrank", 8);
assert_eq!(params["index_id"], "trusty-tools");
assert_eq!(params["query"], "apply_archive_downrank");
assert_eq!(params["top_k"], 8);
}
#[test]
fn build_call_routes_each_mode() {
let (tool, params) = build_call("semantic", "how does auth work", 5, Some("idx")).expect("ok");
assert_eq!(tool, "search_semantic");
assert_eq!(params["index_id"], "idx");
assert_eq!(params["query"], "how does auth work");
assert_eq!(params["top_k"], 5);
let (tool, params) = build_call("symbol", "validate_token", 5, Some("idx")).expect("ok");
assert_eq!(tool, "search_kg");
assert_eq!(params["query"], "validate_token");
let (tool, params) = build_call("grep", "TODO", 7, None).expect("ok");
assert_eq!(tool, "grep");
assert_eq!(params["pattern"], "TODO");
assert_eq!(params["max_results"], 7);
assert!(params.get("index_id").is_none());
}
#[test]
fn build_call_semantic_without_index_is_fallback() {
let err = build_call("semantic", "q", 5, None).expect_err("needs index");
assert!(err.contains("grep"), "must steer to local tools: {err}");
}
#[test]
fn schema_advertises_mode_and_query() {
let tool = TrustySearchTool::new(std::env::temp_dir());
let schema = tool.schema();
assert_eq!(schema["function"]["name"], SEARCH_CODE_TOOL_NAME);
let params = &schema["function"]["parameters"];
let required: Vec<&str> = params["required"]
.as_array()
.expect("required array")
.iter()
.filter_map(Value::as_str)
.collect();
assert_eq!(required, vec!["query"]);
assert_eq!(params["properties"]["mode"]["default"], "semantic");
assert_eq!(params["additionalProperties"], json!(false));
}
#[tokio::test]
async fn execute_fail_open_when_binary_absent() {
let tool = TrustySearchTool::new(std::env::temp_dir())
.with_binary("trusty-search-definitely-not-installed-xyzzy");
let result = tool.execute(json!({ "query": "how does auth work" })).await;
assert!(!result.is_error(), "must not surface as a tool error");
assert!(
result.content().contains("grep"),
"must steer to local tools: {}",
result.content()
);
}
#[tokio::test]
async fn execute_rejects_malformed_args_recoverably() {
let tool = TrustySearchTool::new(std::env::temp_dir())
.with_binary("trusty-search-definitely-not-installed-xyzzy");
let result = tool.execute(json!({ "mode": "semantic" })).await; assert!(result.is_error());
assert!(!result.is_fatal());
}
#[test]
fn lane_from_mode_matches_build_call_routing() {
for (mode, expect_lane, expect_tool) in [
("grep", SearchLane::Grep, "grep"),
("symbol", SearchLane::Symbol, "search_kg"),
("semantic", SearchLane::Semantic, "search_semantic"),
("nonsense", SearchLane::Semantic, "search_semantic"),
] {
assert_eq!(SearchLane::from_mode(mode), expect_lane, "lane for {mode}");
let (tool, _params) = build_call(mode, "q", 5, Some("idx")).expect("routes");
assert_eq!(tool, expect_tool, "build_call tool for {mode}");
}
}
#[test]
fn lane_labels_are_stable() {
assert_eq!(SearchLane::Semantic.label(), "semantic");
assert_eq!(SearchLane::Symbol.label(), "symbol");
assert_eq!(SearchLane::Grep.label(), "grep");
assert_eq!(SearchLane::Lexical.label(), "lexical");
}
#[test]
fn resolved_lane_reports_lexical_when_the_retry_served_it() {
assert_eq!(
SearchLane::resolved("semantic", true),
SearchLane::Lexical,
"a semantic query served by the lexical retry is a LEXICAL search"
);
assert_eq!(SearchLane::resolved("symbol", true), SearchLane::Lexical);
assert_eq!(
SearchLane::resolved("semantic", false),
SearchLane::Semantic
);
assert_eq!(SearchLane::resolved("symbol", false), SearchLane::Symbol);
assert_eq!(SearchLane::resolved("grep", false), SearchLane::Grep);
}
#[test]
fn parse_search_hits_reads_each_lane_shape() {
assert_eq!(
parse_search_hits(
r#"{"results":[{"path":"a.rs","score":0.9},{"path":"b.rs","score":0.4}]}"#
),
(
Some(2),
vec![
SearchHit {
path: "a.rs".into(),
score: 0.9
},
SearchHit {
path: "b.rs".into(),
score: 0.4
},
]
)
);
assert_eq!(
parse_search_hits(r#"{"hits":[{"path":"c.rs","score":1.0}]}"#),
(
Some(1),
vec![SearchHit {
path: "c.rs".into(),
score: 1.0
}]
)
);
assert_eq!(parse_search_hits(r#"{"matches":[]}"#), (Some(0), vec![]));
}
#[test]
fn parse_search_hits_is_none_for_unrecognised_payloads() {
assert_eq!(parse_search_hits("not json at all"), (None, vec![]));
assert_eq!(
parse_search_hits(r#"{"unexpected":"shape"}"#),
(None, vec![])
);
assert_eq!(
parse_search_hits(r#"{"results":"not-an-array"}"#),
(None, vec![]),
"a non-array under a known key is uncountable, not empty"
);
}
#[test]
fn parse_search_hits_falls_back_to_file_and_defaults_missing_score() {
let (count, hits) = parse_search_hits(
r#"{"matches":[{"file":"src/main.rs","line":10},{"file":"src/lib.rs","line":2}]}"#,
);
assert_eq!(count, Some(2));
assert_eq!(
hits,
vec![
SearchHit {
path: "src/main.rs".into(),
score: 0.0
},
SearchHit {
path: "src/lib.rs".into(),
score: 0.0
},
]
);
}
#[test]
fn parse_search_hits_skips_items_with_no_path_or_file() {
let (count, hits) =
parse_search_hits(r#"{"results":[{"score":0.5},{"path":"src/ok.rs","score":0.1}]}"#);
assert_eq!(count, Some(2));
assert_eq!(
hits,
vec![SearchHit {
path: "src/ok.rs".into(),
score: 0.1
}]
);
}
#[tokio::test]
async fn fail_open_paths_report_no_telemetry() {
let tool = TrustySearchTool::new(std::env::temp_dir())
.with_binary("trusty-search-definitely-not-installed-xyzzy");
let result = tool.execute(json!({ "query": "how does auth work" })).await;
assert!(
result.telemetry().is_none(),
"an absent daemon is not a search with zero hits"
);
}
#[cfg(unix)]
fn write_fake_mcp_server(responses: &[Value; 3]) -> std::path::PathBuf {
use std::os::unix::fs::PermissionsExt;
let script = format!(
"#!/bin/sh\ni=0\nwhile IFS= read -r line; do\n case \"$line\" in\n *'\"id\":'*)\n i=$((i + 1))\n case \"$i\" in\n 1) printf '%s\\n' '{}' ;;\n 2) printf '%s\\n' '{}' ;;\n 3) printf '%s\\n' '{}' ;;\n esac\n ;;\n esac\ndone\n",
responses[0], responses[1], responses[2],
);
let path = std::env::temp_dir().join(format!(
"tcode-fake-mcp-{}-{}.sh",
std::process::id(),
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.expect("clock")
.as_nanos()
));
std::fs::write(&path, script).expect("write fake mcp script");
let mut perms = std::fs::metadata(&path)
.expect("stat fake mcp script")
.permissions();
perms.set_mode(0o755);
std::fs::set_permissions(&path, perms).expect("chmod fake mcp script");
path
}
#[tokio::test]
#[cfg(unix)]
async fn execute_attaches_telemetry_when_lane_reply_has_no_text() {
let init_resp = json!({
"jsonrpc": "2.0",
"id": 1,
"result": {
"serverInfo": { "name": "fake-trusty-search", "version": "0.0.0" },
"protocolVersion": "2024-11-05"
}
});
let list_indexes_resp = json!({
"jsonrpc": "2.0",
"id": 2,
"result": { "content": [{ "type": "text", "text": "{\"indexes\":[]}" }] }
});
let grep_resp = json!({
"jsonrpc": "2.0",
"id": 3,
"result": { "content": [{ "type": "text" }] }
});
let script_path = write_fake_mcp_server(&[init_resp, list_indexes_resp, grep_resp]);
let tool = TrustySearchTool::new(std::env::temp_dir()).with_binary(
script_path
.to_str()
.expect("utf8 fake mcp script path")
.to_string(),
);
let result = tool
.execute(json!({ "query": "TODO", "mode": "grep" }))
.await;
let _ = std::fs::remove_file(&script_path);
assert!(
!result.is_error(),
"a malformed-but-successful lane reply is still fail-open success: {}",
result.content()
);
assert!(
result.telemetry().is_some(),
"execute()'s doc comment promises every lane-reached path attaches \
telemetry, but the no-extractable-text arm attached none"
);
}