From 996b0785f5c75fac64ea5d96b720f98b1c2ed3df Mon Sep 17 00:00:00 2001 From: ionfwsrijan Date: Tue, 11 Aug 2026 10:08:20 +0530 Subject: [PATCH] fix: honor MAX_RESULTS default cap in recommendations (issue #1881) --- src/utils/recommender.py | 2 +- tests/test_basic.py | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/utils/recommender.py b/src/utils/recommender.py index c481bd30..a679c57a 100644 --- a/src/utils/recommender.py +++ b/src/utils/recommender.py @@ -725,7 +725,7 @@ def get_recommendations( scored_projects.sort(key=lambda item: (-item["score"], int(item["project"].get("id", 0)))) selected_projects = ( - scored_projects + scored_projects[:MAX_RESULTS] if max_results is None else scored_projects[:max_results]) diff --git a/tests/test_basic.py b/tests/test_basic.py index fe1431ae..53c05fa5 100644 --- a/tests/test_basic.py +++ b/tests/test_basic.py @@ -304,6 +304,21 @@ def test_get_recommendations_max_three(): assert len(results.get("recommendations", [])) <= 3, f"Expected at most 3 results, got {len(results.get('recommendations', []))}" +def test_get_recommendations_default_cap_applies(): + """MAX_RESULTS must be honored as the default cap when max_results is None.""" + capped = get_recommendations("python", "Beginner", "Web", "Low") + assert len(capped.get("recommendations", [])) <= 3, ( + f"Expected default cap of 3, got {len(capped.get('recommendations', []))}" + ) + + uncapped = get_recommendations( + "python", "Beginner", "Web", "Low", max_results=100 + ) + assert len(uncapped.get("recommendations", [])) > 3, ( + "Explicit max_results should override the default cap" + ) + + def test_get_recommendations_result_format(): """Each returned project must be a dict with at least a title and id.""" results = get_recommendations("Python", "Beginner", "Data", "Low")