| 123 | |
| 124 | |
| 125 | class TestChunkText(unittest.TestCase): |
| 126 | def test_small_text_no_split(self): |
| 127 | text = "hello world\n\nfoo bar" |
| 128 | chunks = chunk_text(text) |
| 129 | self.assertEqual(len(chunks), 1) |
| 130 | # Should contain numbered lines |
| 131 | self.assertIn("1| hello world", chunks[0]) |
| 132 | self.assertIn("3| foo bar", chunks[0]) |
| 133 | |
| 134 | def test_large_text_splits_on_sections(self): |
| 135 | # Build text with multiple sections, large enough to exceed CHUNK_SIZE_CHARS |
| 136 | sections = [] |
| 137 | for i in range(10): |
| 138 | body = ("x" * 80 + "\n") * 100 # ~8100 chars per section |
| 139 | sections.append(f"## Section {i}\n\n{body}") |
| 140 | text = "# NAME\n\ntest\n\n" + "\n".join(sections) |
| 141 | chunks = chunk_text(text) |
| 142 | self.assertGreater(len(chunks), 1) |
| 143 | |
| 144 | def test_oversized_section_with_paragraphs_splits(self): |
| 145 | # Build one big section with paragraph breaks |
| 146 | paras = ["z" * 5000 for _ in range(20)] |
| 147 | text = "# BIG\n\n" + "\n\n".join(paras) |
| 148 | chunks = chunk_text(text) |
| 149 | self.assertGreater(len(chunks), 1) |
| 150 | for chunk in chunks: |
| 151 | self.assertLessEqual(len(chunk), CHUNK_SIZE_CHARS) |
| 152 | |
| 153 | def test_single_unsplittable_line(self): |
| 154 | # A single line with no breaks can't be split further — it stays as one oversized chunk |
| 155 | text = "z" * (CHUNK_SIZE_CHARS + 1) |
| 156 | chunks = chunk_text(text) |
| 157 | self.assertEqual(len(chunks), 1) |
| 158 | self.assertGreater(len(chunks[0]), CHUNK_SIZE_CHARS) |
| 159 | |
| 160 | def test_line_numbers_are_globally_correct(self): |
| 161 | # Build text that will split into 2 chunks |
| 162 | section_body = ("line\n") * 800 # ~4000 chars per section |
| 163 | sections = [] |
| 164 | for i in range(20): |
| 165 | sections.append(f"## Section {i}\n\n{section_body}") |
| 166 | text = "# NAME\n\ntest\n\n" + "\n".join(sections) |
| 167 | chunks = chunk_text(text) |
| 168 | if len(chunks) >= 2: |
| 169 | # Last numbered line in chunk 0 + 1 == first numbered line in chunk 1 |
| 170 | import re |
| 171 | |
| 172 | last_match = list(re.finditer(r"^\s*(\d+)\|", chunks[0], re.MULTILINE)) |
| 173 | first_match = list(re.finditer(r"^\s*(\d+)\|", chunks[1], re.MULTILINE)) |
| 174 | if last_match and first_match: |
| 175 | last_line = int(last_match[-1].group(1)) |
| 176 | first_line = int(first_match[0].group(1)) |
| 177 | self.assertEqual(first_line, last_line + 1) |
| 178 | |
| 179 | def test_preamble_on_later_chunks(self): |
| 180 | section_body = ("line\n") * 800 |
| 181 | sections = [] |
| 182 | for i in range(20): |
nothing calls this directly
no outgoing calls
no test coverage detected