Files
zyt/app/tests/test_ai_large_attachment_receipt_ui.py
T
2026-10-23 13:57:11 +08:00

96 lines
4.8 KiB
Python

"""Large synthetic manifests stay complete and collapsed across terminal states."""
from __future__ import annotations
import os
from types import SimpleNamespace
os.environ.setdefault("QT_QPA_PLATFORM", "offscreen")
import pytest
from PySide6.QtCore import QEvent, Qt, QTimer
from PySide6.QtTest import QTest
from PySide6.QtWidgets import QApplication, QLabel
from doctor_workstation.core import PermissionSet
from doctor_workstation.core.errors import ApiBusinessError
from doctor_workstation.ui.dialogs import ai_consult as module
@pytest.fixture(scope="module")
def application():
return QApplication.instance() or QApplication([])
@pytest.fixture
def dialog(application, monkeypatch):
monkeypatch.setattr(module, "QThreadPool", SimpleNamespace(
globalInstance=lambda: SimpleNamespace(start=lambda _: None),
))
instance = module.AiConsultDialog(object(), PermissionSet(["tcm.diagnosis/aiAssistant"]))
monkeypatch.setattr(instance, "_load_workspace", lambda _: None)
instance.open_for(diagnosis_id=501, patient_id=1501)
instance.resize(1280, 820)
instance.show()
yield instance
for timer in instance.findChildren(QTimer):
timer.stop()
instance.close()
instance.deleteLater()
application.sendPostedEvents(None, QEvent.Type.DeferredDelete)
application.processEvents()
def sections(status):
return [{"key": key, "status": status, "availability": "present"}
for key, _ in module._REQUIRED_TRANSFER_SECTIONS]
@pytest.mark.parametrize("count", [10, 11, 20, 40])
@pytest.mark.parametrize("terminal", ["done", "error"])
def test_large_batch_receipt_keeps_every_source_separate_and_collapsed(dialog, count, terminal):
dialog._ask("合成大量附件核对")
generation, stream = dialog._generation, dialog._stream_generation
bubble = dialog._stream_bubble
items = [{"kind": "image", "label": f"图片附件{index}", "status": "prepared"}
for index in range(1, count + 1)]
prepared = {"event": "progress", "phase": "attachments", "status": "uploading",
"attachment_mode": "batch_evidence", "attachment_batch_count": (count + 9) // 10,
"attachment_count": count, "submitted_attachment_count": 0,
"attachment_transfer": "uploaded_files", "required_sections": sections("prepared"),
"items": items}
dialog._stream_event(generation, stream, prepared)
assert bubble.transfer_status.isHidden() and not bubble.transfer_toggle.isChecked()
assert "完整病历尚未提交综合分析" in bubble.transfer_status.text()
if terminal == "done":
final_items = [{**item, "status": "submitted"} for item in items]
final_items[-1].update({"status": "rejected", "reason": "file_size"})
manifest = {**prepared, "items": final_items, "submitted_attachment_count": count - 1,
"rejected_attachment_count": 1, "required_sections": sections("submitted"),
"transfer_manifest_final": True}
dialog._stream_event(generation, stream, {**manifest, "event": "done", "answer": "合成最终正文"})
assert bubble._raw_payload == "合成最终正文" and dialog._stream_completed
assert f"图片附件{count}(图片):上游拒收,未读取(大小限制)" in bubble.transfer_status.text()
else:
final_items = [{**item, "status": "submitted" if index < count // 2 else "prepared"}
for index, item in enumerate(items)]
manifest = {**prepared, "items": final_items, "submitted_attachment_count": count // 2,
"transfer_manifest_final": True, "code": "UPSTREAM_UNAVAILABLE"}
manifest.pop("event")
dialog._stream_failed(generation, stream, ApiBusinessError("合成连接故障", data=manifest))
assert not dialog._stream_completed and "合成连接故障" in bubble._raw_payload
assert f"图片附件{count}(图片):等待上传" in bubble.transfer_status.text()
assert "基本信息:已整理,待提交" in bubble.transfer_status.text()
assert bubble.transfer_status.isHidden() and not bubble.transfer_toggle.isChecked()
assert bubble._inner.itemAt(0).widget() is bubble.transfer_receipt
assert len(bubble.findChildren(QLabel, "AiConsultTransferStatus")) == 1
assert len(dialog._stream_meta["items"]) == count
for index in range(1, count + 1):
assert f"图片附件{index}(图片):" in bubble.transfer_status.text()
assert "资料栏目提交清单" not in bubble._raw_payload
assert not dialog._asking and dialog.send_button.isEnabled()
QTest.mouseClick(bubble.transfer_toggle, Qt.MouseButton.LeftButton)
assert bubble.transfer_status.isVisible()
QTest.mouseClick(bubble.transfer_toggle, Qt.MouseButton.LeftButton)
assert bubble.transfer_status.isHidden()