447 lines
21 KiB
Python
447 lines
21 KiB
Python
"""Approved SSE bursts paint progressively without extending request lifetime.
|
|
|
|
Real offscreen Qt, finite workers and MockTransport only; all data is synthetic.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import os
|
|
from time import monotonic, perf_counter
|
|
from types import SimpleNamespace
|
|
|
|
os.environ.setdefault("QT_QPA_PLATFORM", "offscreen")
|
|
|
|
import httpx
|
|
import pytest
|
|
from PySide6.QtCore import QEvent, QObject, Qt, QThreadPool, QTimer
|
|
from PySide6.QtTest import QTest
|
|
from PySide6.QtWidgets import QApplication, QLabel
|
|
from shiboken6 import isValid
|
|
|
|
from doctor_workstation.core import PermissionSet
|
|
from doctor_workstation.services import ApiClient, RemoteDoctorRepository
|
|
from doctor_workstation.ui.dialogs import ai_consult as module
|
|
from doctor_workstation.ui.dialogs.ai_consult_charts import GlucoseReportCard
|
|
|
|
SOURCE_KEYS = (
|
|
"basic_info", "vital_signs", "chief_complaint", "present_illness", "past_history",
|
|
"other_history", "diagnosis_info", "doctor_image_notes", "doctor_file_notes",
|
|
"daily_tracking", "prescriptions", "video_consultation_text", "chat",
|
|
)
|
|
TOOLS = (
|
|
("分析病历", "case_analysis", "病历分析", "口渴记录提示需结合现有血糖资料分析,缺少病程不能确立证型。"),
|
|
("血糖趋势", "glucose_trend", "血糖趋势", "一条血糖只能说明当次情况,不能据此认定连续升降趋势。"),
|
|
("用药评估", "medication_review", "用药评估", "二甲双胍的既有记录可用于核对治疗,剂量未记不能据此加量。"),
|
|
("并发症风险", "complication_risk", "并发症风险", "糖尿病史提示筛查价值;未记录筛查不能直接认定已患或排除并发症。"),
|
|
("生成问诊小结", "summary", "问诊小结", "本次口渴,既往有糖尿病及二甲双胍记录,病程与剂量仍需确认。"),
|
|
("健康建议", "lifestyle_advice", "健康建议", "可规律分配饮食并记录血糖,活动量需结合现有体能逐步安排。"),
|
|
)
|
|
|
|
|
|
def receipt():
|
|
return {"transfer_manifest_final": True, "attachment_count": 0, "items": [],
|
|
"required_sections": [{"key": key, "availability": "absent", "status": "submitted"}
|
|
for key in SOURCE_KEYS]}
|
|
|
|
|
|
def report():
|
|
original = [{"record_date": "2026-01-01", "role": "患者", "content": f"第 {i} 类合成来源原文 🩺"}
|
|
for i in range(13)]
|
|
text = "## 合成判断\n\n" + "已有信息可支持有限分析;缺少日期不能认定控制正常。" * 320
|
|
text += "\n\n## 原始记录\n\n```json\n" + json.dumps(original, ensure_ascii=False)
|
|
return text + "\n```\n\n**唯一完整结尾** 🩺"
|
|
|
|
|
|
def wait_until(predicate, seconds=5):
|
|
deadline = monotonic() + seconds
|
|
while not predicate() and monotonic() < deadline:
|
|
QTest.qWait(5)
|
|
assert predicate(), "finite presentation/transport wait expired"
|
|
|
|
|
|
def event(dialog, kind, **values):
|
|
dialog._stream_event(dialog._generation, dialog._stream_generation, {"event": kind, **values})
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
def application():
|
|
return QApplication.instance() or QApplication([])
|
|
|
|
|
|
@pytest.fixture
|
|
def dialogs(application, monkeypatch):
|
|
resources = []
|
|
|
|
def create(handler=None):
|
|
if handler is None:
|
|
monkeypatch.setattr(module, "QThreadPool", SimpleNamespace(
|
|
globalInstance=lambda: SimpleNamespace(start=lambda _: None),
|
|
))
|
|
repository, client = object(), None
|
|
else:
|
|
client = ApiClient("https://example.test", transport=httpx.MockTransport(handler))
|
|
repository = RemoteDoctorRepository(client)
|
|
dialog = module.AiConsultDialog(repository, PermissionSet(["tcm.diagnosis/aiAssistant"]))
|
|
monkeypatch.setattr(dialog, "_load_workspace", lambda _: None)
|
|
dialog.open_for(diagnosis_id=501, patient_id=1501)
|
|
dialog.show()
|
|
application.processEvents()
|
|
resources.append((dialog, client))
|
|
return dialog
|
|
|
|
yield create
|
|
for dialog, client in resources:
|
|
dialog.close()
|
|
for timer in dialog.findChildren(QTimer):
|
|
timer.stop()
|
|
if client is not None:
|
|
client.close()
|
|
dialog.deleteLater()
|
|
assert QThreadPool.globalInstance().waitForDone(3000)
|
|
application.sendPostedEvents(None, QEvent.Type.DeferredDelete)
|
|
application.processEvents()
|
|
|
|
|
|
def test_validated_burst_done_and_transport_finished_leave_many_real_intermediate_frames(dialogs):
|
|
answer = report()
|
|
calls = []
|
|
|
|
def handler(request):
|
|
assert request.url.host == "example.test"
|
|
calls.append(request)
|
|
body = "".join(f"event: {kind}\ndata: {json.dumps(value, ensure_ascii=False)}\n\n"
|
|
for kind, value in [
|
|
("start", {"model_label": "原配置千问", "model_key": "qwen"}),
|
|
("progress", {"phase": "attachments", "status": "submitted", **receipt()}),
|
|
("delta", {"text": answer}), ("done", receipt()),
|
|
])
|
|
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
|
|
|
|
dialog = dialogs(handler)
|
|
dialog._ask("生成合成病历分析", task="case_analysis")
|
|
bubble = dialog._stream_bubble
|
|
|
|
class PaintProbe(QObject):
|
|
def __init__(self):
|
|
super().__init__(bubble.body)
|
|
self.sizes = []
|
|
|
|
def eventFilter(self, watched, paint_event): # noqa: N802 - Qt API
|
|
if paint_event.type() == QEvent.Type.Paint:
|
|
size = len(bubble.body.toPlainText())
|
|
if not self.sizes or self.sizes[-1] != size:
|
|
self.sizes.append(size)
|
|
return False
|
|
|
|
paint_probe = PaintProbe()
|
|
bubble.body.viewport().installEventFilter(paint_probe)
|
|
wait_until(lambda: not dialog._asking and dialog._stream_worker is None)
|
|
assert dialog.send_button.isEnabled() and dialog._stream_completed
|
|
assert bubble._presentation_timer.isActive()
|
|
assert 0 < len(bubble.body.toPlainText()) < len(answer) // 3
|
|
assert bubble._raw_payload == answer == dialog._stream_text
|
|
assert bubble._clinical_panel is None
|
|
assert "本次回复已完成" in bubble.transfer_status.text()
|
|
frames, gaps = [], []
|
|
previous = perf_counter()
|
|
|
|
def sample():
|
|
nonlocal previous
|
|
now = perf_counter()
|
|
gaps.append((now - previous) * 1000)
|
|
previous = now
|
|
size = len(bubble.body.toPlainText())
|
|
if not frames or size != frames[-1]:
|
|
frames.append(size)
|
|
|
|
heartbeat = QTimer()
|
|
heartbeat.setInterval(5)
|
|
heartbeat.timeout.connect(sample)
|
|
heartbeat.start()
|
|
try:
|
|
wait_until(lambda: not bubble._presentation_timer.isActive() and not bubble._report_render_pending)
|
|
finally:
|
|
heartbeat.stop()
|
|
assert len(frames) >= 15
|
|
assert len(paint_probe.sizes) >= 10
|
|
assert max(gaps) < 200 # Host-local event-loop guard, not a native-display/model SLA.
|
|
assert len(calls) == 1
|
|
assert json.loads(calls[0].content)["task"] == "case_analysis"
|
|
assert "model" not in json.loads(calls[0].content)
|
|
assert bubble._raw_payload == answer
|
|
oracle = module._RichMessage("ai")
|
|
oracle.document().setDefaultFont(bubble.body.document().defaultFont())
|
|
oracle.set_payload(answer)
|
|
module._highlight_document_metrics(oracle.document())
|
|
assert bubble.body.toPlainText() == oracle.toPlainText()
|
|
actual_html, expected_html = bubble.body.toHtml(), oracle.toHtml()
|
|
mismatch = next((i for i, (a, b) in enumerate(zip(actual_html, expected_html, strict=False)) if a != b), 0)
|
|
assert actual_html == expected_html, (mismatch, actual_html[mismatch:mismatch + 120], expected_html[mismatch:mismatch + 120])
|
|
oracle.deleteLater()
|
|
assert [row["key"] for row in bubble._provenance["required_sections"]] == list(SOURCE_KEYS)
|
|
assert "原配置千问" in bubble.stamp.text()
|
|
# Fenced original-source JSON keeps the existing full Markdown renderer.
|
|
assert bubble._clinical_panel is None
|
|
rendered = bubble.body.toPlainText()
|
|
for i in range(13):
|
|
assert f"第 {i} 类合成来源原文" in rendered
|
|
|
|
|
|
@pytest.mark.parametrize("tool", TOOLS, ids=[tool[1] for tool in TOOLS])
|
|
@pytest.mark.parametrize("legacy", [False, True])
|
|
def test_six_actual_tool_answers_keep_source_fields_after_presentation(dialogs, tool, legacy):
|
|
label, task, title, analysis = tool
|
|
sources = "\n".join(
|
|
f"- 合成来源 {i} 原文:" + json.dumps({"record_date": "2026-01-01", "note": f"原文唯一 {i}"}, ensure_ascii=False)
|
|
for i in range(13)
|
|
)
|
|
answer = f"## {title}判断\n- {analysis}\n- {task}_ACTUAL_MODEL_ANSWER\n\n## 全部原文\n{sources}"
|
|
requests = []
|
|
|
|
def handler(request):
|
|
assert request.url.host == "example.test"
|
|
requests.append(request)
|
|
if request.url.path.endswith("/aiAssistantStream"):
|
|
if legacy:
|
|
return httpx.Response(404, json={"msg": "合成旧服务"})
|
|
body = "".join(f"event: {kind}\ndata: {json.dumps(value, ensure_ascii=False)}\n\n"
|
|
for kind, value in [("delta", {"text": answer}), ("done", receipt())])
|
|
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
|
|
assert legacy and request.url.path.endswith("/aiAssistant")
|
|
return httpx.Response(200, json={"code": 1, "data": {"answer": answer, **receipt()}})
|
|
|
|
dialog = dialogs(handler)
|
|
card = next(card for card in dialog.findChildren(module._ClickCard, "AiConsultToolCard")
|
|
if any(child.text() == label for child in card.findChildren(QLabel)))
|
|
target = next(child for child in card.findChildren(QLabel) if child.text() == label)
|
|
QTest.mouseClick(target, Qt.MouseButton.LeftButton)
|
|
bubble = dialog._stream_bubble
|
|
wait_until(lambda: dialog._stream_worker is None and not dialog._asking)
|
|
assert bubble._raw_payload == answer and dialog.send_button.isEnabled()
|
|
wait_until(lambda: not bubble._presentation_timer.isActive() and not bubble._report_render_pending)
|
|
assert bubble._raw_payload == answer
|
|
assert len(requests) == (2 if legacy else 1)
|
|
for request in requests:
|
|
body = json.loads(request.content)
|
|
assert body["task"] == task and "model_key" not in body and "model" not in body
|
|
assert isinstance(bubble._clinical_panel, module._StructuredReportPanel)
|
|
panel = bubble._clinical_panel
|
|
assert panel.findChild(QLabel, "AiConsultClinicalTitle").text() == f"AI · {title}"
|
|
text = "\n".join(view.toPlainText() for view in panel.findChildren(module.QTextBrowser))
|
|
assert analysis in text and f"{task}_ACTUAL_MODEL_ANSWER" in text
|
|
assert "记录日期:2026-01-01" in text and """ not in text
|
|
for i in range(13):
|
|
assert f"原文唯一 {i}" in text
|
|
assert [row["key"] for row in bubble._provenance["required_sections"]] == list(SOURCE_KEYS)
|
|
|
|
|
|
@pytest.mark.parametrize("length", [1, 8, 24, 72, 513])
|
|
@pytest.mark.parametrize("answer_only", [False, True])
|
|
def test_tiny_and_done_answer_races_finish_once_with_exact_text(dialogs, length, answer_only):
|
|
dialog = dialogs()
|
|
dialog._ask("合成问诊", task="summary")
|
|
bubble, worker = dialog._stream_bubble, dialog._stream_worker
|
|
answer = ("合成文字🩺\n" * 100)[:length]
|
|
finished = []
|
|
bubble.stream_presentation_finished.connect(lambda: finished.append(True))
|
|
if not answer_only:
|
|
event(dialog, "delta", text=answer)
|
|
event(dialog, "done", answer=answer, **receipt())
|
|
assert not dialog._asking and dialog.send_button.isEnabled()
|
|
dialog._stream_finished(dialog._generation, dialog._stream_generation, worker)
|
|
assert dialog._stream_worker is None
|
|
event(dialog, "done", answer="伪造迟到成功")
|
|
wait_until(lambda: not bubble._presentation_timer.isActive())
|
|
assert finished == [True]
|
|
assert bubble._raw_payload == answer
|
|
oracle = module._RichMessage("ai")
|
|
oracle.document().setDefaultFont(bubble.body.document().defaultFont())
|
|
oracle.set_payload(answer)
|
|
assert bubble.body.toHtml() == oracle.toHtml()
|
|
oracle.deleteLater()
|
|
|
|
|
|
def test_completed_old_bubble_keeps_own_animation_after_new_request(dialogs):
|
|
dialog = dialogs()
|
|
dialog._ask("旧问题", task="case_analysis")
|
|
old, worker = dialog._stream_bubble, dialog._stream_worker
|
|
answer = report()
|
|
event(dialog, "delta", text=answer)
|
|
event(dialog, "done")
|
|
dialog._stream_finished(dialog._generation, dialog._stream_generation, worker)
|
|
assert dialog._stream_worker is None and old._presentation_timer.isActive()
|
|
old_frames = []
|
|
old.stream_frame_presented.connect(lambda: old_frames.append(True))
|
|
dialog._ask("新问题", task="summary")
|
|
new = dialog._stream_bubble
|
|
event(dialog, "delta", text="新请求的唯一答案")
|
|
event(dialog, "done")
|
|
wait_until(lambda: not old._presentation_timer.isActive() and not old._report_render_pending)
|
|
assert old_frames and old._raw_payload == answer
|
|
assert new._raw_payload == "新请求的唯一答案"
|
|
assert new.body.toPlainText() == "新请求的唯一答案"
|
|
assert "旧问题" not in new.body.toPlainText()
|
|
assert dialog._stream_text == "新请求的唯一答案"
|
|
assert new._task_title == "问诊小结" and old._task_title == "病历分析"
|
|
|
|
|
|
@pytest.mark.parametrize("exit_path", ["close", "patient", "replace", "cancel"])
|
|
def test_exit_stops_correct_bubble_and_no_late_timer_can_change_next_request(dialogs, exit_path):
|
|
dialog = dialogs()
|
|
dialog._ask("旧问题", task="case_analysis")
|
|
old, worker = dialog._stream_bubble, dialog._stream_worker
|
|
old_generation = (dialog._generation, dialog._stream_generation)
|
|
answer = report()
|
|
event(dialog, "delta", text=answer)
|
|
assert old._presentation_timer.isActive()
|
|
if exit_path == "close":
|
|
dialog.close()
|
|
elif exit_path == "patient":
|
|
dialog.open_for(diagnosis_id=502, patient_id=1502)
|
|
dialog._ask("新患者问题")
|
|
elif exit_path == "replace":
|
|
dialog._ask("替代问题", replace_active=True)
|
|
else:
|
|
dialog._cancel_stream(reason="合成超时")
|
|
assert not old._presentation_timer.isActive()
|
|
assert old._clinical_panel is None
|
|
assert old._raw_payload == answer
|
|
current = dialog._stream_bubble
|
|
expected = current._raw_payload if current is not None else None
|
|
dialog._stream_event(*old_generation, {"event": "done", "answer": "迟到伪造成功"})
|
|
dialog._stream_finished(*old_generation, worker)
|
|
QTest.qWait(80)
|
|
assert old._raw_payload == answer
|
|
assert not isValid(old._presentation_timer) or not old._presentation_timer.isActive()
|
|
if current is not None:
|
|
assert current._raw_payload == expected
|
|
if exit_path in {"replace", "cancel"}:
|
|
assert "唯一完整结尾" in old.body.toPlainText()
|
|
assert "取消" in old.transfer_status.text()
|
|
|
|
|
|
def test_error_preserves_all_approved_partial_and_never_formats_success(dialogs):
|
|
dialog = dialogs()
|
|
dialog._ask("合成判断", task="case_analysis")
|
|
bubble, worker = dialog._stream_bubble, dialog._stream_worker
|
|
answer = report()
|
|
event(dialog, "delta", text=answer)
|
|
assert len(bubble.body.toPlainText()) < len(answer)
|
|
failure = RuntimeError("AI正文与病历的已知、阴性或未记录状态不一致")
|
|
dialog._stream_failed(dialog._generation, dialog._stream_generation, failure)
|
|
assert not bubble._presentation_timer.isActive()
|
|
assert answer in bubble._raw_payload
|
|
assert "生成中断:AI正文与病历" in bubble._raw_payload
|
|
assert "唯一完整结尾" in bubble.body.toPlainText()
|
|
assert not dialog._stream_completed and bubble._clinical_panel is None
|
|
event(dialog, "done", answer="迟到伪造成功")
|
|
dialog._stream_finished(dialog._generation, dialog._stream_generation, worker)
|
|
QTest.qWait(80)
|
|
assert bubble._clinical_panel is None and not bubble._presentation_timer.isActive()
|
|
assert "迟到伪造成功" not in bubble._raw_payload
|
|
assert dialog._stream_worker is None and dialog.send_button.isEnabled()
|
|
|
|
|
|
def test_cancelled_progress_is_terminal_even_if_a_late_done_follows(dialogs):
|
|
dialog = dialogs()
|
|
dialog._ask("合成判断", task="case_analysis")
|
|
bubble, worker = dialog._stream_bubble, dialog._stream_worker
|
|
answer = report()
|
|
event(dialog, "delta", text=answer)
|
|
event(dialog, "progress", phase="cancelled")
|
|
assert not dialog._asking and not bubble._presentation_timer.isActive()
|
|
assert "取消" in bubble.transfer_status.text()
|
|
event(dialog, "delta", text="迟到伪造正文")
|
|
event(dialog, "done", answer="迟到伪造成功")
|
|
dialog._stream_finished(dialog._generation, dialog._stream_generation, worker)
|
|
QTest.qWait(80)
|
|
assert bubble._raw_payload == answer and bubble._clinical_panel is None
|
|
assert dialog._stream_worker is None and not dialog._stream_completed
|
|
|
|
|
|
def test_eof_releases_transport_and_keeps_approved_partial_without_success_panel(dialogs):
|
|
dialog = dialogs()
|
|
dialog._ask("合成问诊", task="case_analysis")
|
|
bubble, worker = dialog._stream_bubble, dialog._stream_worker
|
|
answer = report()
|
|
event(dialog, "delta", text=answer)
|
|
dialog._stream_finished(dialog._generation, dialog._stream_generation, worker)
|
|
assert dialog._stream_worker is None and not dialog._asking and dialog.send_button.isEnabled()
|
|
assert bubble._presentation_timer.isActive()
|
|
assert "未收到回复完成确认" in bubble.transfer_status.text()
|
|
assert not dialog._stream_completed and bubble._clinical_panel is None
|
|
wait_until(lambda: not bubble._presentation_timer.isActive())
|
|
assert bubble._raw_payload == answer and "唯一完整结尾" in bubble.body.toPlainText()
|
|
assert bubble._clinical_panel is None and not bubble._report_render_pending
|
|
|
|
|
|
@pytest.mark.parametrize("owner_change", ["none", "patient_id", "diagnosis_id", "_chart_patient_id"])
|
|
def test_done_freezes_record_snapshot_before_playback_and_rechecks_owner(dialogs, owner_change):
|
|
dialog = dialogs()
|
|
dialog._workspace_sections = {"tracking": {"value": {"diagnosis_id": 501, "blood_records": [
|
|
{"diagnosis_id": 501, "patient_id": 1501, "record_date": "2026-01-01", "fasting_blood_sugar": 6.2},
|
|
]}}}
|
|
dialog._ask("血糖资料", task="glucose_trend")
|
|
bubble, worker = dialog._stream_bubble, dialog._stream_worker
|
|
answer = ("## 血糖记录\n- 已有空腹血糖 6.2 mmol/L,单条记录不能确定趋势。\n\n## 来源复核\n- "
|
|
+ "合成来源日期与数值逐项保留。" * 500)
|
|
event(dialog, "delta", text=answer)
|
|
event(dialog, "done")
|
|
dialog._stream_finished(dialog._generation, dialog._stream_generation, worker)
|
|
assert bubble._presentation_timer.isActive() and bubble._report_render_pending
|
|
dialog._workspace_sections["tracking"]["value"]["blood_records"][0]["fasting_blood_sugar"] = 99
|
|
if owner_change == "none":
|
|
dialog._ask("新的问题")
|
|
else:
|
|
setattr(dialog, owner_change, 9999)
|
|
wait_until(lambda: not bubble._presentation_timer.isActive() and not bubble._report_render_pending)
|
|
assert bubble._raw_payload == answer
|
|
if owner_change == "none":
|
|
assert bubble._clinical_panel is not None
|
|
chart = bubble.findChild(GlucoseReportCard)
|
|
assert chart is not None and chart.chart.data.readings[0].fasting == 6.2
|
|
else:
|
|
assert bubble._clinical_panel is None and bubble.findChild(GlucoseReportCard) is None
|
|
|
|
|
|
@pytest.mark.parametrize("terminal", ["close", "patient"])
|
|
def test_session_exit_also_stops_previously_completed_bubble_animation(dialogs, terminal):
|
|
dialog = dialogs()
|
|
dialog._ask("已完成旧问题", task="case_analysis")
|
|
old, worker = dialog._stream_bubble, dialog._stream_worker
|
|
event(dialog, "delta", text=report())
|
|
event(dialog, "done")
|
|
dialog._stream_finished(dialog._generation, dialog._stream_generation, worker)
|
|
dialog._ask("当前问题")
|
|
assert old._presentation_timer.isActive()
|
|
if terminal == "close":
|
|
dialog.close()
|
|
else:
|
|
dialog.open_for(diagnosis_id=502, patient_id=1502)
|
|
assert not old._presentation_timer.isActive()
|
|
QTest.qWait(80)
|
|
assert old._clinical_panel is None
|
|
|
|
|
|
def test_first_frame_and_every_playback_slice_are_bounded(dialogs, monkeypatch):
|
|
dialog = dialogs()
|
|
dialog._ask("合成大段正文")
|
|
bubble = dialog._stream_bubble
|
|
chunks = []
|
|
original = bubble.body.append_stream_text
|
|
|
|
def append(text):
|
|
chunks.append(len(text))
|
|
original(text)
|
|
|
|
monkeypatch.setattr(bubble.body, "append_stream_text", append)
|
|
started = perf_counter()
|
|
event(dialog, "delta", text=report())
|
|
assert (perf_counter() - started) * 1000 < 80
|
|
assert len(chunks) == 1 and chunks[0] <= 24
|
|
event(dialog, "done")
|
|
assert len(chunks) == 1 and not dialog._asking
|
|
QTest.qWait(130)
|
|
assert len(chunks) >= 3 and max(chunks) <= 256
|
|
assert bubble._presentation_timer.isActive()
|