|
2 | 2 |
|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
| 5 | +import logging |
5 | 6 | from unittest.mock import MagicMock, patch |
6 | 7 |
|
7 | 8 | import pytest |
8 | | -from pageindex import IndexConfig |
9 | 9 |
|
10 | 10 | from openkb.indexer import ( |
11 | 11 | IndexResult, |
|
15 | 15 | ) |
16 | 16 |
|
17 | 17 |
|
| 18 | +class _FakeIndexConfigWithConcurrency: |
| 19 | + """Stand-in for a PageIndex ``IndexConfig`` that declares ``max_concurrency``. |
| 20 | +
|
| 21 | + Used instead of relying on whatever ``pageindex`` happens to be installed in |
| 22 | + this environment, so the forwarding tests are deterministic regardless of |
| 23 | + the currently-pinned PageIndex version (see ``test_forwards_...`` below). |
| 24 | + """ |
| 25 | + |
| 26 | + model_fields = { |
| 27 | + "if_add_node_text": None, |
| 28 | + "if_add_node_summary": None, |
| 29 | + "if_add_doc_description": None, |
| 30 | + "max_concurrency": None, |
| 31 | + } |
| 32 | + |
| 33 | + def __init__(self, **kwargs): |
| 34 | + self.__dict__.update(kwargs) |
| 35 | + |
| 36 | + |
| 37 | +class _FakeIndexConfigWithoutConcurrency: |
| 38 | + """Stand-in for a PageIndex ``IndexConfig`` predating ``max_concurrency``.""" |
| 39 | + |
| 40 | + model_fields = { |
| 41 | + "if_add_node_text": None, |
| 42 | + "if_add_node_summary": None, |
| 43 | + "if_add_doc_description": None, |
| 44 | + } |
| 45 | + |
| 46 | + def __init__(self, **kwargs): |
| 47 | + self.__dict__.update(kwargs) |
| 48 | + |
| 49 | + |
18 | 50 | class TestBuildIndexConfig: |
19 | 51 | def test_sets_base_flags(self): |
20 | 52 | cfg = _build_index_config({}) |
21 | 53 | assert cfg.if_add_node_text is True |
22 | 54 | assert cfg.if_add_node_summary is True |
23 | 55 | assert cfg.if_add_doc_description is True |
24 | 56 |
|
25 | | - def test_forwards_max_concurrency_when_supported(self): |
| 57 | + def test_forwards_max_concurrency_when_supported(self, monkeypatch): |
| 58 | + monkeypatch.setattr("openkb.indexer.IndexConfig", _FakeIndexConfigWithConcurrency) |
26 | 59 | cfg = _build_index_config({"pageindex_max_concurrency": 8}) |
27 | | - if "max_concurrency" in IndexConfig.model_fields: |
28 | | - assert cfg.max_concurrency == 8 |
29 | | - else: |
30 | | - # A PageIndex predating the field: forwarded as a no-op, never raised. |
31 | | - assert not hasattr(cfg, "max_concurrency") |
| 60 | + assert cfg.max_concurrency == 8 |
32 | 61 |
|
33 | | - def test_none_value_is_left_to_pageindex_default(self): |
| 62 | + def test_does_not_forward_when_unsupported(self, monkeypatch): |
| 63 | + monkeypatch.setattr("openkb.indexer.IndexConfig", _FakeIndexConfigWithoutConcurrency) |
| 64 | + cfg = _build_index_config({"pageindex_max_concurrency": 8}) |
| 65 | + assert not hasattr(cfg, "max_concurrency") |
| 66 | + |
| 67 | + def test_none_value_is_left_to_pageindex_default(self, monkeypatch): |
| 68 | + monkeypatch.setattr("openkb.indexer.IndexConfig", _FakeIndexConfigWithConcurrency) |
34 | 69 | cfg = _build_index_config({"pageindex_max_concurrency": None}) |
35 | 70 | assert getattr(cfg, "max_concurrency", None) is None |
36 | 71 |
|
| 72 | + def test_warns_when_configured_but_unsupported(self, monkeypatch, caplog): |
| 73 | + monkeypatch.setattr("openkb.indexer.IndexConfig", _FakeIndexConfigWithoutConcurrency) |
| 74 | + with caplog.at_level(logging.WARNING, logger="openkb.indexer"): |
| 75 | + _build_index_config({"pageindex_max_concurrency": 8}) |
| 76 | + assert "pageindex_max_concurrency" in caplog.text |
| 77 | + |
| 78 | + def test_no_warning_when_unset(self, monkeypatch, caplog): |
| 79 | + monkeypatch.setattr("openkb.indexer.IndexConfig", _FakeIndexConfigWithoutConcurrency) |
| 80 | + with caplog.at_level(logging.WARNING, logger="openkb.indexer"): |
| 81 | + _build_index_config({}) |
| 82 | + assert caplog.text == "" |
| 83 | + |
| 84 | + def test_no_warning_when_supported(self, monkeypatch, caplog): |
| 85 | + monkeypatch.setattr("openkb.indexer.IndexConfig", _FakeIndexConfigWithConcurrency) |
| 86 | + with caplog.at_level(logging.WARNING, logger="openkb.indexer"): |
| 87 | + _build_index_config({"pageindex_max_concurrency": 8}) |
| 88 | + assert caplog.text == "" |
| 89 | + |
37 | 90 |
|
38 | 91 | class TestNormalizePageContent: |
39 | 92 | def test_normalizes_pageindex_dicts(self): |
@@ -204,6 +257,32 @@ def test_localclient_called_with_index_config(self, kb_dir, sample_tree, tmp_pat |
204 | 257 | assert ic.if_add_node_summary is True |
205 | 258 | assert ic.if_add_doc_description is True |
206 | 259 |
|
| 260 | + def test_pageindex_max_concurrency_flows_from_kb_config(self, kb_dir, sample_tree, tmp_path): |
| 261 | + """The KB's real config.yaml, loaded by index_long_document itself, must |
| 262 | + reach the IndexConfig passed to PageIndexClient — not just the isolated |
| 263 | + _build_index_config unit tested directly with a hand-built dict.""" |
| 264 | + (kb_dir / ".openkb" / "config.yaml").write_text( |
| 265 | + "model: gpt-4o-mini\npageindex_max_concurrency: 7\n", encoding="utf-8" |
| 266 | + ) |
| 267 | + |
| 268 | + doc_id = "conc-789" |
| 269 | + fake_col = self._make_fake_collection(doc_id, sample_tree) |
| 270 | + fake_client = MagicMock() |
| 271 | + fake_client.collection.return_value = fake_col |
| 272 | + |
| 273 | + pdf_path = tmp_path / "report.pdf" |
| 274 | + pdf_path.write_bytes(b"%PDF-1.4 fake") |
| 275 | + |
| 276 | + with ( |
| 277 | + patch("openkb.indexer.IndexConfig", _FakeIndexConfigWithConcurrency), |
| 278 | + patch("openkb.indexer.PageIndexClient", return_value=fake_client) as mock_cls, |
| 279 | + patch("openkb.images.convert_pdf_to_pages", return_value=self._fake_pages()), |
| 280 | + ): |
| 281 | + index_long_document(pdf_path, kb_dir) |
| 282 | + |
| 283 | + _, kwargs = mock_cls.call_args |
| 284 | + assert kwargs["index_config"].max_concurrency == 7 |
| 285 | + |
207 | 286 | def test_cloud_page_content_is_normalized(self, kb_dir, sample_tree, tmp_path, monkeypatch): |
208 | 287 | doc_id = "cloud-123" |
209 | 288 | fake_col = self._make_fake_collection(doc_id, sample_tree) |
|
0 commit comments