|
6 | 6 | from unittest.mock import MagicMock, patch |
7 | 7 |
|
8 | 8 | import pytest |
| 9 | +from pageindex import PageIndexClient |
9 | 10 |
|
| 11 | +from openkb.config import LlmCredentialBundle |
10 | 12 | from openkb.indexer import ( |
11 | 13 | IndexResult, |
12 | 14 | _build_index_config, |
@@ -94,6 +96,23 @@ def test_no_warning_when_supported(self, monkeypatch, caplog): |
94 | 96 | _build_index_config({"concurrency": 8}) |
95 | 97 | assert caplog.text == "" |
96 | 98 |
|
| 99 | + def test_forwards_rest_credentials_as_pageindex_per_call_params(self): |
| 100 | + bundle = LlmCredentialBundle( |
| 101 | + api_key="ui-key", |
| 102 | + base_url="https://gateway.example/v1", |
| 103 | + extra_headers={"X-Tenant": "alpha"}, |
| 104 | + timeout=45, |
| 105 | + ) |
| 106 | + |
| 107 | + cfg = _build_index_config({}, bundle=bundle) |
| 108 | + |
| 109 | + assert cfg.llm_params == { |
| 110 | + "api_key": "ui-key", |
| 111 | + "base_url": "https://gateway.example/v1", |
| 112 | + "extra_headers": {"X-Tenant": "alpha"}, |
| 113 | + "timeout": 45, |
| 114 | + } |
| 115 | + |
97 | 116 |
|
98 | 117 | class TestNormalizePageContent: |
99 | 118 | def test_normalizes_pageindex_dicts(self): |
@@ -313,6 +332,57 @@ def test_concurrency_flows_from_kb_config(self, kb_dir, sample_tree, tmp_path): |
313 | 332 | _, kwargs = mock_cls.call_args |
314 | 333 | assert kwargs["index_config"].max_concurrency == 7 |
315 | 334 |
|
| 335 | + def test_rest_bundle_reaches_local_pageindex_client(self, kb_dir, sample_tree, tmp_path): |
| 336 | + doc_id = "bundle-123" |
| 337 | + fake_col = self._make_fake_collection(doc_id, sample_tree) |
| 338 | + fake_client = MagicMock() |
| 339 | + fake_client.collection.return_value = fake_col |
| 340 | + bundle = LlmCredentialBundle( |
| 341 | + api_key="ui-key", |
| 342 | + base_url="https://gateway.example/v1", |
| 343 | + ) |
| 344 | + pdf_path = tmp_path / "report.pdf" |
| 345 | + pdf_path.write_bytes(b"%PDF-1.4 fake") |
| 346 | + |
| 347 | + with ( |
| 348 | + patch( |
| 349 | + "openkb.indexer._PerRequestPageIndexClient", |
| 350 | + return_value=fake_client, |
| 351 | + ) as mock_cls, |
| 352 | + patch("openkb.images.convert_pdf_to_pages", return_value=self._fake_pages()), |
| 353 | + ): |
| 354 | + index_long_document(pdf_path, kb_dir, bundle=bundle) |
| 355 | + |
| 356 | + _, kwargs = mock_cls.call_args |
| 357 | + assert kwargs["index_config"].llm_params == { |
| 358 | + "api_key": "ui-key", |
| 359 | + "base_url": "https://gateway.example/v1", |
| 360 | + } |
| 361 | + |
| 362 | + def test_rest_bundle_does_not_require_provider_key_in_process_env( |
| 363 | + self, kb_dir, sample_tree, tmp_path, monkeypatch |
| 364 | + ): |
| 365 | + monkeypatch.delenv("OPENAI_API_KEY", raising=False) |
| 366 | + monkeypatch.setattr("litellm.api_key", None) |
| 367 | + (kb_dir / ".openkb" / "config.yaml").write_text( |
| 368 | + "model: openai/cs/abc_ex\n", encoding="utf-8" |
| 369 | + ) |
| 370 | + fake_col = self._make_fake_collection("bundle-456", sample_tree) |
| 371 | + bundle = LlmCredentialBundle( |
| 372 | + api_key="ui-key", |
| 373 | + base_url="https://gateway.example/v1", |
| 374 | + ) |
| 375 | + pdf_path = tmp_path / "report.pdf" |
| 376 | + pdf_path.write_bytes(b"%PDF-1.4 fake") |
| 377 | + |
| 378 | + with ( |
| 379 | + patch.object(PageIndexClient, "collection", return_value=fake_col), |
| 380 | + patch("openkb.images.convert_pdf_to_pages", return_value=self._fake_pages()), |
| 381 | + ): |
| 382 | + result = index_long_document(pdf_path, kb_dir, bundle=bundle) |
| 383 | + |
| 384 | + assert result.doc_id == "bundle-456" |
| 385 | + |
316 | 386 | def test_cloud_page_content_is_normalized(self, kb_dir, sample_tree, tmp_path, monkeypatch): |
317 | 387 | doc_id = "cloud-123" |
318 | 388 | fake_col = self._make_fake_collection(doc_id, sample_tree) |
|
0 commit comments