use async_trait::async_trait;
use lunaris_core::LunarisError;
use crate::{RerankCandidate, Reranker};
#[derive(Debug, Clone, Copy, Default)]
pub struct NoopReranker;
#[async_trait]
impl Reranker for NoopReranker {
async fn rerank(
&self,
_query: &str,
docs: Vec<RerankCandidate>,
) -> Result<Vec<RerankCandidate>, LunarisError> {
Ok(docs)
}
fn applies(&self) -> bool {
false
}
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
fn cand(id: &[u8], score: f32) -> RerankCandidate {
RerankCandidate {
id: id.to_vec(),
text: format!("doc-{}", String::from_utf8_lossy(id)),
score,
metadata: json!({}),
}
}
#[tokio::test]
async fn noop_returns_input_unchanged() {
let docs = vec![
cand(b"a", 0.1),
cand(b"b", 0.5),
cand(b"c", 0.3),
cand(b"d", 0.9),
cand(b"e", 0.2),
];
let out = NoopReranker.rerank("any query", docs).await.unwrap();
assert_eq!(out.len(), 5);
assert_eq!(out[0].id, b"a".to_vec());
assert_eq!(out[1].id, b"b".to_vec());
assert_eq!(out[2].id, b"c".to_vec());
assert_eq!(out[3].id, b"d".to_vec());
assert_eq!(out[4].id, b"e".to_vec());
assert!((out[0].score - 0.1).abs() < 1e-6);
assert!((out[3].score - 0.9).abs() < 1e-6);
}
#[test]
fn noop_applies_is_false() {
assert!(!NoopReranker.applies());
}
}