|
2 | 2 |
|
3 | 3 | # pylint: disable=protected-access |
4 | 4 | # pylint: disable=unused-argument |
| 5 | +# pylint: disable=too-many-lines |
5 | 6 |
|
6 | 7 | import re |
7 | 8 | from collections.abc import Callable |
@@ -752,6 +753,77 @@ async def test_infer_include_metadata_ignored_when_verbose_infer_disabled( |
752 | 753 | assert response.data.output_tokens is None |
753 | 754 |
|
754 | 755 |
|
| 756 | +def _setup_config_mock( |
| 757 | + mocker: MockerFixture, |
| 758 | + mock_configuration: AppConfig, |
| 759 | + verbose_enabled: bool, |
| 760 | +) -> None: |
| 761 | + """Helper to set up configuration mock with verbose setting.""" |
| 762 | + custom_mock = mocker.Mock() |
| 763 | + custom_mock.allow_verbose_infer = verbose_enabled |
| 764 | + custom_mock.system_prompt = "You are a helpful assistant." |
| 765 | + config_mock = mocker.Mock() |
| 766 | + config_mock.inference = mock_configuration.inference |
| 767 | + config_mock.customization = custom_mock |
| 768 | + mocker.patch("app.endpoints.rlsapi_v1.configuration", config_mock) |
| 769 | + |
| 770 | + |
| 771 | +async def test_infer_verbose_extract_token_usage_on_text_extraction_failure( |
| 772 | + mocker: MockerFixture, |
| 773 | + mock_configuration: AppConfig, |
| 774 | + mock_auth_resolvers: None, |
| 775 | +) -> None: |
| 776 | + """Verify extract_token_usage called in except block when text extraction fails.""" |
| 777 | + _setup_config_mock(mocker, mock_configuration, verbose_enabled=True) |
| 778 | + mock_response = mocker.Mock() |
| 779 | + mock_response.output = [_create_mock_response_output(mocker, "Response")] |
| 780 | + mock_usage = mocker.Mock() |
| 781 | + mock_usage.input_tokens = 50 |
| 782 | + mock_usage.output_tokens = 25 |
| 783 | + mock_response.usage = mock_usage |
| 784 | + _setup_responses_mock(mocker, mocker.AsyncMock(return_value=mock_response)) |
| 785 | + mocker.patch( |
| 786 | + "app.endpoints.rlsapi_v1.extract_text_from_response_items", |
| 787 | + side_effect=RuntimeError("text extraction failed"), |
| 788 | + ) |
| 789 | + mock_extract = mocker.patch("app.endpoints.rlsapi_v1.extract_token_usage") |
| 790 | + with pytest.raises(RuntimeError): |
| 791 | + await infer_endpoint( |
| 792 | + infer_request=RlsapiV1InferRequest( |
| 793 | + question="How do I list files?", include_metadata=True |
| 794 | + ), |
| 795 | + request=_create_mock_request(mocker), |
| 796 | + background_tasks=_create_mock_background_tasks(mocker), |
| 797 | + auth=MOCK_AUTH, |
| 798 | + ) |
| 799 | + mock_extract.assert_called_once() |
| 800 | + call_args = mock_extract.call_args |
| 801 | + assert call_args[0][0] == mock_usage |
| 802 | + assert call_args[0][1] == "openai/gpt-4-turbo" |
| 803 | + |
| 804 | + |
| 805 | +async def test_infer_non_verbose_no_extract_token_usage_on_failure( |
| 806 | + mocker: MockerFixture, |
| 807 | + mock_configuration: AppConfig, |
| 808 | + mock_auth_resolvers: None, |
| 809 | +) -> None: |
| 810 | + """Verify extract_token_usage NOT called in except block for non-verbose.""" |
| 811 | + _setup_config_mock(mocker, mock_configuration, verbose_enabled=False) |
| 812 | + mocker.patch( |
| 813 | + "app.endpoints.rlsapi_v1.retrieve_simple_response", |
| 814 | + side_effect=RuntimeError("retrieval failed"), |
| 815 | + ) |
| 816 | + mock_extract = mocker.patch("app.endpoints.rlsapi_v1.extract_token_usage") |
| 817 | + with pytest.raises(RuntimeError): |
| 818 | + await infer_endpoint( |
| 819 | + infer_request=RlsapiV1InferRequest(question="How do I list files?"), |
| 820 | + request=_create_mock_request(mocker), |
| 821 | + background_tasks=_create_mock_background_tasks(mocker), |
| 822 | + auth=MOCK_AUTH, |
| 823 | + ) |
| 824 | + mock_extract.assert_not_called() |
| 825 | + |
| 826 | + |
755 | 827 | # --- Test Splunk integration --- |
756 | 828 |
|
757 | 829 |
|
|
0 commit comments