| 1 | """Regression tests for entity grounding on generic, entity-less topics.""" |
| 2 | |
| 3 | import pytest |
| 4 | |
| 5 | from lib import rerank |
| 6 | |
| 7 | GENERIC_TOPIC = "AI code review bottleneck: can reviewers still judge AI-produced work" |
| 8 | |
| 9 | |
| 10 | def test_primary_entity_keeps_review_when_review_is_the_subject(): |
| 11 | entity = rerank._primary_entity(GENERIC_TOPIC) |
| 12 | assert "code review" in entity.lower(), ( |
| 13 | f"expected the subject phrase to survive intent-modifier stripping, got {entity!r}" |
| 14 | ) |
| 15 | |
| 16 | |
| 17 | def test_ubiquitous_head_token_does_not_ground_off_topic_items(): |
| 18 | entity = rerank._primary_entity(GENERIC_TOPIC) |
| 19 | off_topic = ( |
| 20 | "INSTEAD OF WATCHING NETFLIX TONIGHT Spend 1 hour with this Claude AI FULL " |
| 21 | "COURSE that teaches you how to BUILD and AUTOMATE anything" |
| 22 | ) |
| 23 | assert not rerank._entity_grounded(off_topic, entity), ( |
| 24 | "a course advertisement sharing only the token 'AI' should not count as grounded" |
| 25 | ) |
| 26 | |
| 27 | |
| 28 | def test_broad_token_buried_inside_another_word_does_not_ground(): |
| 29 | entity = rerank._primary_entity(GENERIC_TOPIC) |
| 30 | unrelated = "my weekend project: a barcode scanner written in rust" |
| 31 | assert not rerank._entity_grounded(unrelated, entity), ( |
| 32 | "'code' inside 'barcode' should not ground an unrelated candidate" |
| 33 | ) |
| 34 | |
| 35 | |
| 36 | @pytest.mark.parametrize( |
| 37 | "unrelated", |
| 38 | [ |
| 39 | "This code works", |
| 40 | "The review was helpful", |
| 41 | "Work is changing", |
| 42 | ], |
| 43 | ) |
| 44 | def test_broad_trailing_token_alone_does_not_ground(unrelated): |
| 45 | entity = rerank._primary_entity(GENERIC_TOPIC) |
| 46 | assert not rerank._entity_grounded(unrelated, entity) |
| 47 | |
| 48 | |
| 49 | def test_inflected_trailing_token_still_grounds(): |
| 50 | entity = rerank._primary_entity(GENERIC_TOPIC) |
| 51 | on_topic = "our reviewers cannot keep up with generated diffs" |
| 52 | assert rerank._entity_grounded(on_topic, entity) |
| 53 | |
| 54 | |
| 55 | def test_on_topic_item_is_grounded_control(): |
| 56 | entity = rerank._primary_entity(GENERIC_TOPIC) |
| 57 | on_topic = "How to keep QA from being a giant bottleneck with AI coding" |
| 58 | assert rerank._entity_grounded(on_topic, entity) |
| 59 | |
| 60 | |
| 61 | def test_short_generic_head_keeps_safe_no_op(): |
| 62 | assert rerank._entity_grounded("Golang concurrency patterns", "Go programming") |
| 63 |