From 6865ac0272ff0196a71057b9a23dbd4c3f0fe38a Mon Sep 17 00:00:00 2001 From: arsam Date: Fri, 18 Sep 2026 23:06:49 +0330 Subject: [PATCH] Import tests: tests/test_reranker Import the existing local project snapshot. This commit organizes existing files; it does not represent newly developed functionality. --- tests/test_reranker.py | 74 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 tests/test_reranker.py diff --git a/tests/test_reranker.py b/tests/test_reranker.py new file mode 100644 index 0000000..986474b --- /dev/null +++ b/tests/test_reranker.py @@ -0,0 +1,74 @@ +import pytest + + +def test_chunk_text_single_short_text(): + from axon.services.reranker import chunk_text + chunks = chunk_text("hello world", max_tokens=512) + assert chunks == ["hello world"] + + +def test_chunk_text_splits_long_text(): + from axon.services.reranker import chunk_text + # 384 words per chunk at max_tokens=512 (512 * 0.75) + text = " ".join([f"word{i}" for i in range(500)]) + chunks = chunk_text(text, max_tokens=512) + assert len(chunks) == 2 + assert chunks[0].startswith("word0") + + +def test_chunk_text_empty_string(): + from axon.services.reranker import chunk_text + chunks = chunk_text("", max_tokens=512) + assert chunks == [""] + + +def test_chunk_text_exact_boundary(): + from axon.services.reranker import chunk_text + # 384 words should be exactly 1 chunk + text = " ".join(["x"] * 384) + chunks = chunk_text(text, max_tokens=512) + assert len(chunks) == 1 + + +@pytest.mark.asyncio +async def test_rerank_results_sorts_by_score(monkeypatch): + import numpy as np + + call_count = [0] + + class FakeModel: + def predict(self, pairs): + # First pair gets low score, second gets high score + return np.array([0.1, 0.9]) + + monkeypatch.setattr("axon.services.reranker._model", FakeModel()) + + from axon.services.reranker import rerank_results + contents = [("https://low.com", "low relevance text"), ("https://high.com", "high relevance text")] + ranked = await rerank_results("query", contents) + assert ranked[0][0] == "https://high.com" + assert ranked[1][0] == "https://low.com" + assert ranked[0][2] > ranked[1][2] + + +@pytest.mark.asyncio +async def test_rerank_results_empty_input(): + from axon.services.reranker import rerank_results + result = await rerank_results("query", []) + assert result == [] + + +@pytest.mark.asyncio +async def test_rerank_results_handles_empty_content(monkeypatch): + import numpy as np + + class FakeModel: + def predict(self, pairs): + return np.array([0.5] * len(pairs)) + + monkeypatch.setattr("axon.services.reranker._model", FakeModel()) + + from axon.services.reranker import rerank_results + contents = [("https://a.com", ""), ("https://b.com", "some text")] + ranked = await rerank_results("query", contents) + assert len(ranked) == 2