test(relevance): score past the embed batch cap against the real model

Forty texts of mixed lengths span three batches of 16 with the one
related item in the last, in both the Rust and the Python scorer: it
must still rank first, and every batched score must match the item
scored on its own. Run with RUN_FASTEMBED_TESTS=1 (Rust) or fastembed
installed (Python).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Garm
2026-10-27 16:38:07 +01:00
co-authored by Claude Opus 5.5
parent 7e789d89bf
commit 8ce2b26f5f
2 changed files with 49 additions and 0 deletions
@@ -511,4 +511,39 @@ mod tests {
assert!((0.0..=1.0).contains(&sc.score));
}
}
#[cfg(feature = "ml")]
#[test]
fn fastembed_batches_past_the_cap_keep_each_score_with_its_item() {
if !fastembed_enabled() {
return;
}
// Three EMBED_BATCH_SIZE batches of mixed lengths, the one related item
// in the last: a misaligned batch boundary would hand its score away.
let s = EmbeddingScorer::try_new().expect("model loads");
let mut items: Vec<String> = (0..40)
.map(|i| {
format!(
"inventory row {i}: {}",
"pallets of garden furniture ".repeat(i % 5 + 1)
)
})
.collect();
items[37] = "authentication failed for user admin: invalid password".to_string();
let refs: Vec<&str> = items.iter().map(String::as_str).collect();
let batch = s.score_batch(&refs, "login error");
assert_eq!(batch.len(), 40);
let top = (0..40)
.max_by(|&a, &b| batch[a].score.total_cmp(&batch[b].score))
.unwrap();
assert_eq!(top, 37);
for (item, sc) in refs.iter().zip(&batch) {
let single = s.score(item, "login error").score;
assert!(
(sc.score - single).abs() < 1e-3,
"{item}: batched {} vs single {single}",
sc.score
);
}
}
}
+14
View File
@@ -186,6 +186,20 @@ class TestEmbeddingScorer:
# "failed"/"error" semantically relates to "errors"
assert scores[0].score > scores[1].score
def test_batches_past_the_cap_keep_each_score_with_its_item(self, scorer):
"""Three EMBED_BATCH_SIZE batches of mixed lengths, the related item in
the last: a misaligned batch boundary would hand its score away."""
items = [
f"inventory row {i}: " + "pallets of garden furniture " * (i % 5 + 1) for i in range(40)
]
items[37] = "authentication failed for user admin: invalid password"
scores = [s.score for s in scorer.score_batch(items, "login error")]
assert len(scores) == 40
assert max(range(40), key=scores.__getitem__) == 37
for item, batched in zip(items, scores):
assert batched == pytest.approx(scorer.score(item, "login error").score, abs=1e-3)
def test_paraphrase_match(self, scorer):
"""Embeddings match paraphrases."""
items = [