|
5 | 5 |
|
6 | 6 | import pytest |
7 | 7 |
|
8 | | -from openkb.agent.tools import list_wiki_files, read_wiki_file, write_wiki_file |
| 8 | +from openkb.agent.tools import get_page_content, list_wiki_files, parse_pages, read_wiki_file, write_wiki_file |
9 | 9 |
|
10 | 10 |
|
11 | 11 | # --------------------------------------------------------------------------- |
@@ -128,3 +128,88 @@ def test_returns_written_path(self, tmp_path): |
128 | 128 | result = write_wiki_file("reports/health.md", "All good.", wiki_root) |
129 | 129 |
|
130 | 130 | assert result == "Written: reports/health.md" |
| 131 | + |
| 132 | + |
| 133 | +# --------------------------------------------------------------------------- |
| 134 | +# parse_pages |
| 135 | +# --------------------------------------------------------------------------- |
| 136 | + |
| 137 | + |
| 138 | +class TestParsePages: |
| 139 | + def test_single_page(self): |
| 140 | + assert parse_pages("3") == [3] |
| 141 | + |
| 142 | + def test_range(self): |
| 143 | + assert parse_pages("3-5") == [3, 4, 5] |
| 144 | + |
| 145 | + def test_comma_separated(self): |
| 146 | + assert parse_pages("1,3,5") == [1, 3, 5] |
| 147 | + |
| 148 | + def test_mixed(self): |
| 149 | + assert parse_pages("1-3,7,10-12") == [1, 2, 3, 7, 10, 11, 12] |
| 150 | + |
| 151 | + def test_deduplication(self): |
| 152 | + assert parse_pages("3,3,3") == [3] |
| 153 | + |
| 154 | + def test_sorted(self): |
| 155 | + assert parse_pages("5,1,3") == [1, 3, 5] |
| 156 | + |
| 157 | + def test_ignores_zero_and_negative(self): |
| 158 | + assert parse_pages("0,-1,3") == [3] |
| 159 | + |
| 160 | + |
| 161 | +# --------------------------------------------------------------------------- |
| 162 | +# get_page_content |
| 163 | +# --------------------------------------------------------------------------- |
| 164 | + |
| 165 | + |
| 166 | +class TestGetPageContent: |
| 167 | + def test_reads_pages_from_json(self, tmp_path): |
| 168 | + import json |
| 169 | + wiki_root = str(tmp_path) |
| 170 | + sources = tmp_path / "sources" |
| 171 | + sources.mkdir() |
| 172 | + pages = [ |
| 173 | + {"page": 1, "content": "Page one text."}, |
| 174 | + {"page": 2, "content": "Page two text."}, |
| 175 | + {"page": 3, "content": "Page three text."}, |
| 176 | + ] |
| 177 | + (sources / "paper.json").write_text(json.dumps(pages), encoding="utf-8") |
| 178 | + result = get_page_content("paper", "1,3", wiki_root) |
| 179 | + assert "[Page 1]" in result |
| 180 | + assert "Page one text." in result |
| 181 | + assert "[Page 3]" in result |
| 182 | + assert "Page three text." in result |
| 183 | + assert "Page two" not in result |
| 184 | + |
| 185 | + def test_returns_error_for_missing_file(self, tmp_path): |
| 186 | + wiki_root = str(tmp_path) |
| 187 | + (tmp_path / "sources").mkdir() |
| 188 | + result = get_page_content("nonexistent", "1", wiki_root) |
| 189 | + assert "not found" in result.lower() |
| 190 | + |
| 191 | + def test_returns_error_for_no_matching_pages(self, tmp_path): |
| 192 | + import json |
| 193 | + wiki_root = str(tmp_path) |
| 194 | + sources = tmp_path / "sources" |
| 195 | + sources.mkdir() |
| 196 | + pages = [{"page": 1, "content": "Only page."}] |
| 197 | + (sources / "paper.json").write_text(json.dumps(pages), encoding="utf-8") |
| 198 | + result = get_page_content("paper", "99", wiki_root) |
| 199 | + assert "no content" in result.lower() |
| 200 | + |
| 201 | + def test_includes_images_info(self, tmp_path): |
| 202 | + import json |
| 203 | + wiki_root = str(tmp_path) |
| 204 | + sources = tmp_path / "sources" |
| 205 | + sources.mkdir() |
| 206 | + pages = [{"page": 1, "content": "Text.", "images": [{"path": "images/p/img.png", "width": 100, "height": 80}]}] |
| 207 | + (sources / "doc.json").write_text(json.dumps(pages), encoding="utf-8") |
| 208 | + result = get_page_content("doc", "1", wiki_root) |
| 209 | + assert "img.png" in result |
| 210 | + |
| 211 | + def test_path_escape_denied(self, tmp_path): |
| 212 | + wiki_root = str(tmp_path) |
| 213 | + (tmp_path / "sources").mkdir() |
| 214 | + result = get_page_content("../../etc/passwd", "1", wiki_root) |
| 215 | + assert "denied" in result.lower() or "not found" in result.lower() |
0 commit comments