use super::*;
pub(super) async fn get_search_audit(
registry: &SessionRegistry,
params: Value,
_ctx: ConnectionContext,
) -> Result<Value, RpcError> {
let p: SessionIdParams = parse(params, "session.get_search_audit")?;
Ok(json!({ "search_audit": registry.get_search_audit(&p.session_id)? }))
}
#[cfg(test)]
mod tests {
use super::*;
use tokio::sync::mpsc;
fn test_ctx() -> ConnectionContext {
let (tx, _rx) = mpsc::unbounded_channel();
ConnectionContext::new(tx)
}
#[tokio::test]
async fn get_search_audit_wraps_records_under_search_audit_key() {
let registry = SessionRegistry::new();
let session = registry.create("t".to_string(), None, crate::binding::ProjectBinding::None);
registry
.record_search_performed(
&session.id,
"pm",
"pm-1",
&crate::tools::SearchTelemetry {
lane: "grep".to_string(),
query: "where is auth".to_string(),
hit_count: Some(3),
hits: vec![],
latency_ms: 5,
},
)
.unwrap();
let result = get_search_audit(®istry, json!({"session_id": session.id}), test_ctx())
.await
.unwrap();
let records = result["search_audit"].as_array().unwrap();
assert_eq!(records.len(), 1);
assert_eq!(records[0]["kind"], "search");
assert_eq!(records[0]["query"], "where is auth");
assert_eq!(records[0]["hit_count"], 3);
}
#[tokio::test]
async fn get_search_audit_empty_session_returns_empty_array() {
let registry = SessionRegistry::new();
let session = registry.create("t".to_string(), None, crate::binding::ProjectBinding::None);
let result = get_search_audit(®istry, json!({"session_id": session.id}), test_ctx())
.await
.unwrap();
assert_eq!(result["search_audit"].as_array().unwrap().len(), 0);
}
#[tokio::test]
async fn get_search_audit_unknown_session_maps_to_session_not_found() {
let registry = SessionRegistry::new();
let err = get_search_audit(®istry, json!({"session_id": "nope"}), test_ctx())
.await
.unwrap_err();
assert_eq!(err.code, -32007);
}
}