130 lines
3.5 KiB
Python
130 lines
3.5 KiB
Python
import json
|
|
|
|
import requests
|
|
|
|
from gfa_core.mas import dual_kb_qa
|
|
|
|
|
|
class FakeResponse:
|
|
status_code = 200
|
|
content = b"{}"
|
|
text = ""
|
|
|
|
def __init__(self, iter_lines_calls=None):
|
|
self.iter_lines_calls = iter_lines_calls
|
|
|
|
def json(self):
|
|
return {"success": True, "answer": "ok", "sources": []}
|
|
|
|
def iter_lines(self, chunk_size=None, decode_unicode=True):
|
|
if self.iter_lines_calls is not None:
|
|
self.iter_lines_calls.append((chunk_size, decode_unicode))
|
|
payload = json.dumps({"success": True, "answer": "ok", "sources": []})
|
|
yield "event: complete"
|
|
yield f"data: {payload}"
|
|
yield ""
|
|
|
|
def close(self):
|
|
return None
|
|
|
|
|
|
def _force_v1173(monkeypatch):
|
|
monkeypatch.setattr(dual_kb_qa, "DUAL_KB_QA_ROUTE", "qa-v1-1-73")
|
|
monkeypatch.delenv("QA_V1_1_73_TIMEOUT", raising=False)
|
|
monkeypatch.delenv("QA_V1_1_73_STREAM_IDLE_TIMEOUT", raising=False)
|
|
|
|
|
|
def test_non_stream_uses_10_second_connect_and_900_second_read_timeout(monkeypatch):
|
|
_force_v1173(monkeypatch)
|
|
captured = []
|
|
|
|
def fake_post(*args, **kwargs):
|
|
captured.append(kwargs["timeout"])
|
|
return FakeResponse()
|
|
|
|
monkeypatch.setattr(dual_kb_qa.requests, "post", fake_post)
|
|
|
|
dual_kb_qa.query_dual_kb_qa("question")
|
|
|
|
assert captured == [(10.0, 900.0)]
|
|
|
|
|
|
def test_non_stream_honors_environment_timeout_above_150_seconds(monkeypatch):
|
|
_force_v1173(monkeypatch)
|
|
monkeypatch.setenv("QA_V1_1_73_TIMEOUT", "300")
|
|
captured = []
|
|
|
|
def fake_post(*args, **kwargs):
|
|
captured.append(kwargs["timeout"])
|
|
return FakeResponse()
|
|
|
|
monkeypatch.setattr(dual_kb_qa.requests, "post", fake_post)
|
|
|
|
dual_kb_qa.query_dual_kb_qa("question")
|
|
|
|
assert captured == [(10.0, 300.0)]
|
|
|
|
|
|
def test_v1173_requests_answer_response_detail(monkeypatch):
|
|
_force_v1173(monkeypatch)
|
|
captured = []
|
|
|
|
def fake_post(*args, **kwargs):
|
|
captured.append(kwargs["json"])
|
|
return FakeResponse()
|
|
|
|
monkeypatch.setattr(dual_kb_qa.requests, "post", fake_post)
|
|
|
|
dual_kb_qa.query_dual_kb_qa("question")
|
|
|
|
assert captured[0]["response_detail"] == "answer"
|
|
|
|
|
|
def test_v1173_stream_reads_one_byte_chunks(monkeypatch):
|
|
_force_v1173(monkeypatch)
|
|
iter_lines_calls = []
|
|
|
|
def fake_post(*args, **kwargs):
|
|
return FakeResponse(iter_lines_calls)
|
|
|
|
monkeypatch.setattr(dual_kb_qa.requests, "post", fake_post)
|
|
|
|
list(dual_kb_qa.stream_dual_kb_qa("question"))
|
|
|
|
assert iter_lines_calls == [(1, True)]
|
|
|
|
|
|
def test_stream_uses_configurable_idle_timeout(monkeypatch):
|
|
_force_v1173(monkeypatch)
|
|
captured = []
|
|
|
|
def fake_post(*args, **kwargs):
|
|
captured.append(kwargs["timeout"])
|
|
return FakeResponse()
|
|
|
|
monkeypatch.setattr(dual_kb_qa.requests, "post", fake_post)
|
|
|
|
list(dual_kb_qa.stream_dual_kb_qa("question"))
|
|
assert captured == [(10.0, 45.0)]
|
|
|
|
monkeypatch.setenv("QA_V1_1_73_STREAM_IDLE_TIMEOUT", "70")
|
|
captured.clear()
|
|
list(dual_kb_qa.stream_dual_kb_qa("question"))
|
|
assert captured == [(10.0, 70.0)]
|
|
|
|
|
|
def test_stream_timeout_is_reported_as_request_timeout(monkeypatch):
|
|
_force_v1173(monkeypatch)
|
|
|
|
def fake_post(*args, **kwargs):
|
|
raise requests.ReadTimeout("read timed out")
|
|
|
|
monkeypatch.setattr(dual_kb_qa.requests, "post", fake_post)
|
|
|
|
frames = list(dual_kb_qa.stream_dual_kb_qa("question"))
|
|
|
|
assert len(frames) == 1
|
|
assert "event: error" in frames[0]
|
|
assert '"status": 504' in frames[0]
|
|
assert '"error_code": "request_timeout"' in frames[0]
|