|
| 1 | +"""Tests for extractive text summarization with MMR.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from app.algorithms.summarizer import extractive_summarize, _split_sentences |
| 6 | + |
| 7 | + |
| 8 | +# --------------------------------------------------------------------------- |
| 9 | +# _split_sentences |
| 10 | +# --------------------------------------------------------------------------- |
| 11 | + |
| 12 | +def test_split_sentences_basic(): |
| 13 | + text = "First sentence. Second sentence. Third sentence." |
| 14 | + parts = _split_sentences(text) |
| 15 | + assert len(parts) == 3 |
| 16 | + assert parts[0] == "First sentence." |
| 17 | + |
| 18 | + |
| 19 | +def test_split_sentences_newlines(): |
| 20 | + text = "Line one\nLine two\nLine three" |
| 21 | + parts = _split_sentences(text) |
| 22 | + assert len(parts) == 3 |
| 23 | + |
| 24 | + |
| 25 | +def test_split_sentences_empty_string(): |
| 26 | + assert _split_sentences("") == [] |
| 27 | + |
| 28 | + |
| 29 | +def test_split_sentences_ignores_blank_parts(): |
| 30 | + text = "Hello.\n\n\nWorld." |
| 31 | + parts = _split_sentences(text) |
| 32 | + assert "" not in parts |
| 33 | + |
| 34 | + |
| 35 | +# --------------------------------------------------------------------------- |
| 36 | +# extractive_summarize — edge cases |
| 37 | +# --------------------------------------------------------------------------- |
| 38 | + |
| 39 | +def test_empty_snippets_returns_empty(): |
| 40 | + assert extractive_summarize([]) == [] |
| 41 | + |
| 42 | + |
| 43 | +def test_single_snippet_returns_one_point(): |
| 44 | + snippets = [{"id": "a1", "description": "React hooks simplify state. Use useState for local state management."}] |
| 45 | + result = extractive_summarize(snippets, max_points=5) |
| 46 | + assert len(result) == 1 |
| 47 | + assert result[0]["source_snippet_id"] == "a1" |
| 48 | + assert isinstance(result[0]["point"], str) |
| 49 | + assert len(result[0]["point"]) > 0 |
| 50 | + |
| 51 | + |
| 52 | +def test_snippet_with_no_description_is_skipped(): |
| 53 | + snippets = [ |
| 54 | + {"id": "x1", "description": ""}, |
| 55 | + {"id": "x2", "description": "Python list comprehensions are concise and fast."}, |
| 56 | + ] |
| 57 | + result = extractive_summarize(snippets, max_points=5) |
| 58 | + # Only the non-empty snippet should produce a point |
| 59 | + ids = [r["source_snippet_id"] for r in result] |
| 60 | + assert "x2" in ids |
| 61 | + |
| 62 | + |
| 63 | +def test_max_points_is_respected(): |
| 64 | + snippets = [ |
| 65 | + {"id": str(i), "description": f"Snippet {i} talks about topic {i} in detail."} |
| 66 | + for i in range(10) |
| 67 | + ] |
| 68 | + result = extractive_summarize(snippets, max_points=3) |
| 69 | + assert len(result) <= 3 |
| 70 | + |
| 71 | + |
| 72 | +def test_result_structure_has_required_keys(): |
| 73 | + snippets = [{"id": "s1", "description": "TypeScript generics enable reusable typed functions."}] |
| 74 | + result = extractive_summarize(snippets) |
| 75 | + assert "point" in result[0] |
| 76 | + assert "source_snippet_id" in result[0] |
| 77 | + |
| 78 | + |
| 79 | +def test_notes_field_used_when_description_missing(): |
| 80 | + snippets = [{"id": "n1", "notes": "Tailwind utility classes reduce custom CSS overhead."}] |
| 81 | + result = extractive_summarize(snippets, max_points=5) |
| 82 | + assert len(result) == 1 |
| 83 | + assert result[0]["source_snippet_id"] == "n1" |
| 84 | + |
| 85 | + |
| 86 | +def test_both_description_and_notes_combined(): |
| 87 | + snippets = [{ |
| 88 | + "id": "b1", |
| 89 | + "description": "useEffect runs after render.", |
| 90 | + "notes": "Cleanup function prevents memory leaks.", |
| 91 | + }] |
| 92 | + result = extractive_summarize(snippets, max_points=5) |
| 93 | + assert len(result) == 1 |
| 94 | + |
| 95 | + |
| 96 | +# --------------------------------------------------------------------------- |
| 97 | +# MMR diversity — selected points should not be identical |
| 98 | +# --------------------------------------------------------------------------- |
| 99 | + |
| 100 | +def test_selected_points_are_diverse(): |
| 101 | + """Three snippets with different topics should yield 3 different sentences.""" |
| 102 | + snippets = [ |
| 103 | + {"id": "r1", "description": "React useState hook manages component state. Call it at top level only."}, |
| 104 | + {"id": "p1", "description": "Python generators use yield keyword. They are memory efficient for large data."}, |
| 105 | + {"id": "s1", "description": "SQL JOIN merges rows from two tables. Use ON clause to specify condition."}, |
| 106 | + ] |
| 107 | + result = extractive_summarize(snippets, max_points=5) |
| 108 | + points = [r["point"] for r in result] |
| 109 | + # All three selected sentences should be distinct |
| 110 | + assert len(set(points)) == len(points) |
| 111 | + |
| 112 | + |
| 113 | +def test_source_ids_map_correctly(): |
| 114 | + """Each result point must trace back to the snippet it came from.""" |
| 115 | + snippets = [ |
| 116 | + {"id": "alpha", "description": "Git rebase rewrites commit history cleanly."}, |
| 117 | + {"id": "beta", "description": "Docker containers isolate application environments."}, |
| 118 | + ] |
| 119 | + result = extractive_summarize(snippets, max_points=5) |
| 120 | + ids = {r["source_snippet_id"] for r in result} |
| 121 | + assert ids == {"alpha", "beta"} |
| 122 | + |
| 123 | + |
| 124 | +# --------------------------------------------------------------------------- |
| 125 | +# MMR lambda boundary values |
| 126 | +# --------------------------------------------------------------------------- |
| 127 | + |
| 128 | +def test_mmr_lambda_zero_still_runs(): |
| 129 | + """lambda=0.0 is purely diversity-based — should not crash.""" |
| 130 | + snippets = [ |
| 131 | + {"id": "1", "description": "TypeScript interfaces define object shapes."}, |
| 132 | + {"id": "2", "description": "TypeScript generics allow reusable typed code."}, |
| 133 | + ] |
| 134 | + result = extractive_summarize(snippets, max_points=5, mmr_lambda=0.0) |
| 135 | + assert len(result) >= 1 |
| 136 | + |
| 137 | + |
| 138 | +def test_mmr_lambda_one_still_runs(): |
| 139 | + """lambda=1.0 is purely relevance-based — should not crash.""" |
| 140 | + snippets = [ |
| 141 | + {"id": "1", "description": "Next.js app router uses file-based routing."}, |
| 142 | + {"id": "2", "description": "Next.js server components reduce client bundle size."}, |
| 143 | + ] |
| 144 | + result = extractive_summarize(snippets, max_points=5, mmr_lambda=1.0) |
| 145 | + assert len(result) >= 1 |
| 146 | + |
| 147 | + |
| 148 | +# --------------------------------------------------------------------------- |
| 149 | +# Real-world-like cluster: 5 related React snippets |
| 150 | +# --------------------------------------------------------------------------- |
| 151 | + |
| 152 | +REACT_SNIPPETS = [ |
| 153 | + { |
| 154 | + "id": "rc1", |
| 155 | + "description": ( |
| 156 | + "useState returns a state value and a setter. " |
| 157 | + "Calling the setter triggers a re-render with the new value." |
| 158 | + ), |
| 159 | + }, |
| 160 | + { |
| 161 | + "id": "rc2", |
| 162 | + "description": ( |
| 163 | + "useEffect runs after every render by default. " |
| 164 | + "Pass a dependency array to control when it fires." |
| 165 | + ), |
| 166 | + }, |
| 167 | + { |
| 168 | + "id": "rc3", |
| 169 | + "description": ( |
| 170 | + "useContext provides global state without prop drilling. " |
| 171 | + "Wrap your tree in a Provider to supply the value." |
| 172 | + ), |
| 173 | + }, |
| 174 | + { |
| 175 | + "id": "rc4", |
| 176 | + "description": ( |
| 177 | + "useReducer is better than useState for complex state logic. " |
| 178 | + "Dispatch actions to a reducer function instead of calling setters." |
| 179 | + ), |
| 180 | + }, |
| 181 | + { |
| 182 | + "id": "rc5", |
| 183 | + "description": ( |
| 184 | + "useMemo caches expensive computed values between renders. " |
| 185 | + "Only recomputes when its dependency array changes." |
| 186 | + ), |
| 187 | + }, |
| 188 | +] |
| 189 | + |
| 190 | + |
| 191 | +def test_react_cluster_returns_one_point_per_snippet(): |
| 192 | + result = extractive_summarize(REACT_SNIPPETS, max_points=5) |
| 193 | + assert len(result) == 5 |
| 194 | + |
| 195 | + |
| 196 | +def test_react_cluster_all_source_ids_present(): |
| 197 | + result = extractive_summarize(REACT_SNIPPETS, max_points=5) |
| 198 | + ids = {r["source_snippet_id"] for r in result} |
| 199 | + expected = {"rc1", "rc2", "rc3", "rc4", "rc5"} |
| 200 | + assert ids == expected |
| 201 | + |
| 202 | + |
| 203 | +def test_react_cluster_points_are_non_empty_strings(): |
| 204 | + result = extractive_summarize(REACT_SNIPPETS, max_points=5) |
| 205 | + for item in result: |
| 206 | + assert isinstance(item["point"], str) |
| 207 | + assert len(item["point"]) > 0 |
0 commit comments