227 lines
8.2 KiB
Python
227 lines
8.2 KiB
Python
from __future__ import annotations
|
|
|
|
from concurrent.futures import Future
|
|
from pathlib import Path
|
|
from types import SimpleNamespace
|
|
from typing import Any
|
|
|
|
import pytest
|
|
from PySide6.QtCore import Qt
|
|
from PySide6.QtTest import QTest
|
|
from PySide6.QtWidgets import QApplication, QLabel, QPushButton
|
|
|
|
from doctor_workstation.ui.dialogs.local_audio_queue import LocalAudioQueueDialog
|
|
|
|
|
|
@pytest.fixture
|
|
def application() -> QApplication:
|
|
return QApplication.instance() or QApplication([])
|
|
|
|
|
|
def _record(
|
|
tmp_path: Path,
|
|
*,
|
|
status: str = "uploaded",
|
|
transcription_status: str = "pending",
|
|
transcription_error: str = "",
|
|
) -> SimpleNamespace:
|
|
return SimpleNamespace(
|
|
id=1,
|
|
diagnosis_id=8169,
|
|
call_record_id=901,
|
|
room_id="12345678",
|
|
file_path=tmp_path / "synthetic-recording.webm",
|
|
size_bytes=2048,
|
|
status=status,
|
|
error_text="",
|
|
uploaded_url="cos://recordings/synthetic.webm" if status == "uploaded" else "",
|
|
attempts=1,
|
|
created_at="2026-11-06T01:00:00+00:00",
|
|
exists=False,
|
|
transcription_status=transcription_status,
|
|
transcription_error=transcription_error,
|
|
)
|
|
|
|
|
|
class _Store:
|
|
def __init__(self, record: SimpleNamespace) -> None:
|
|
self.record = record
|
|
self.upload_retries: list[int] = []
|
|
|
|
def list_records(self, *, diagnosis_id: int | None) -> list[Any]:
|
|
if diagnosis_id is None or diagnosis_id == self.record.diagnosis_id:
|
|
return [self.record]
|
|
return []
|
|
|
|
def retry(self, record_id: int) -> None:
|
|
self.upload_retries.append(record_id)
|
|
|
|
|
|
class _LegacyManager:
|
|
def __init__(self, store: _Store) -> None:
|
|
self.store = store
|
|
self.uploads: list[int] = []
|
|
self.pending_upload_scopes: list[int | None] = []
|
|
|
|
def submit_pending(self, *, diagnosis_id: int | None) -> list[Future[bool]]:
|
|
self.pending_upload_scopes.append(diagnosis_id)
|
|
return []
|
|
|
|
def submit(self, record_id: int) -> Future[bool]:
|
|
self.uploads.append(record_id)
|
|
return Future()
|
|
|
|
|
|
class _Manager(_LegacyManager):
|
|
def __init__(self, store: _Store) -> None:
|
|
super().__init__(store)
|
|
self.transcriptions: list[int] = []
|
|
self.pending_transcription_scopes: list[int | None] = []
|
|
|
|
def submit_pending_transcriptions(
|
|
self, *, diagnosis_id: int | None
|
|
) -> list[Future[bool]]:
|
|
self.pending_transcription_scopes.append(diagnosis_id)
|
|
return []
|
|
|
|
def submit_transcription(self, record_id: int) -> Future[bool]:
|
|
self.transcriptions.append(record_id)
|
|
self.store.record.transcription_status = "transcribing"
|
|
self.store.record.transcription_error = ""
|
|
return Future()
|
|
|
|
|
|
def _buttons(dialog: LocalAudioQueueDialog) -> dict[str, QPushButton]:
|
|
host = dialog.table.cellWidget(0, dialog._action_column)
|
|
assert host is not None
|
|
return {button.text(): button for button in host.findChildren(QPushButton)}
|
|
|
|
|
|
def test_uploaded_record_with_failed_text_can_retry_without_uploading_again(
|
|
application: QApplication, tmp_path: Path
|
|
) -> None:
|
|
record = _record(
|
|
tmp_path,
|
|
transcription_status="failed",
|
|
transcription_error="文字保存失败,请稍后重试",
|
|
)
|
|
store = _Store(record)
|
|
manager = _Manager(store)
|
|
dialog = LocalAudioQueueDialog(object(), 8169, store=store, manager=manager)
|
|
dialog.show()
|
|
application.processEvents()
|
|
try:
|
|
assert dialog.table.item(0, dialog._status_column).text() == "已上传"
|
|
assert dialog.table.item(0, dialog._transcription_column).text() == "生成失败"
|
|
assert dialog.table.item(0, dialog._error_column).text() == (
|
|
"文字:文字保存失败,请稍后重试"
|
|
)
|
|
assert dialog.summary_uploaded.text() == "已上传 1"
|
|
assert dialog.summary_failed.text() == "失败 0"
|
|
assert dialog.summary_transcription.text() == "文字未完成 1"
|
|
assert not dialog.retry_failed_button.isEnabled()
|
|
|
|
QTest.mouseClick(_buttons(dialog)["重试文字"], Qt.MouseButton.LeftButton)
|
|
application.processEvents()
|
|
|
|
assert manager.transcriptions == [record.id]
|
|
assert manager.uploads == []
|
|
assert store.upload_retries == []
|
|
assert record.status == "uploaded"
|
|
assert dialog.table.item(0, dialog._status_column).text() == "已上传"
|
|
assert dialog.table.item(0, dialog._transcription_column).text() == "生成中"
|
|
assert not _buttons(dialog)["文字生成中"].isEnabled()
|
|
finally:
|
|
dialog.close()
|
|
|
|
|
|
def test_transcribing_controls_are_disabled_and_polling_detects_completion(
|
|
application: QApplication, tmp_path: Path
|
|
) -> None:
|
|
record = _record(tmp_path, transcription_status="transcribing")
|
|
store = _Store(record)
|
|
manager = _Manager(store)
|
|
dialog = LocalAudioQueueDialog(object(), 8169, store=store, manager=manager)
|
|
try:
|
|
assert not _buttons(dialog)["文字生成中"].isEnabled()
|
|
QTest.mouseClick(_buttons(dialog)["文字生成中"], Qt.MouseButton.LeftButton)
|
|
assert manager.transcriptions == []
|
|
|
|
record.transcription_status = "completed"
|
|
dialog.refresh_records()
|
|
|
|
assert dialog.table.item(0, dialog._transcription_column).text() == "已保存"
|
|
assert dialog.summary_transcription.text() == "文字未完成 0"
|
|
assert _buttons(dialog) == {}
|
|
hint = dialog.findChild(QLabel, "LocalAudioQueueHint")
|
|
assert hint is not None
|
|
assert "视频录制回放 → 查看文字" in hint.text()
|
|
assert "未分离说话人" in hint.text()
|
|
finally:
|
|
dialog.close()
|
|
|
|
|
|
@pytest.mark.parametrize("diagnosis_id", [8169, None])
|
|
def test_pending_text_resumes_for_dialog_scope_and_has_separate_action(
|
|
application: QApplication, tmp_path: Path, diagnosis_id: int | None
|
|
) -> None:
|
|
record = _record(tmp_path)
|
|
store = _Store(record)
|
|
manager = _Manager(store)
|
|
dialog = LocalAudioQueueDialog(object(), diagnosis_id, store=store, manager=manager)
|
|
try:
|
|
assert manager.pending_transcription_scopes == [diagnosis_id]
|
|
assert dialog.table.horizontalHeaderItem(dialog._transcription_column).text() == (
|
|
"文字状态"
|
|
)
|
|
assert dialog.table.item(0, dialog._transcription_column).text() == "待生成"
|
|
assert _buttons(dialog)["补全文字"].isEnabled()
|
|
finally:
|
|
dialog.close()
|
|
|
|
|
|
def test_unuploaded_record_waits_for_upload_and_keeps_upload_retry(
|
|
application: QApplication, tmp_path: Path
|
|
) -> None:
|
|
record = _record(tmp_path, status="failed")
|
|
record.error_text = "上传连接中断"
|
|
store = _Store(record)
|
|
dialog = LocalAudioQueueDialog(object(), 8169, store=store, manager=_Manager(store))
|
|
try:
|
|
assert dialog.table.item(0, dialog._transcription_column).text() == "等待上传"
|
|
assert dialog.table.item(0, dialog._error_column).text() == "上传:上传连接中断"
|
|
assert set(_buttons(dialog)) == {"重试"}
|
|
assert dialog.summary_transcription.text() == "文字未完成 0"
|
|
finally:
|
|
dialog.close()
|
|
|
|
|
|
def test_polling_detects_text_error_changes_without_upload_changes(
|
|
application: QApplication, tmp_path: Path
|
|
) -> None:
|
|
record = _record(tmp_path, transcription_status="failed", transcription_error="请求失败")
|
|
store = _Store(record)
|
|
dialog = LocalAudioQueueDialog(object(), 8169, store=store, manager=_Manager(store))
|
|
try:
|
|
record.transcription_error = "转写服务暂不可用"
|
|
dialog.refresh_records()
|
|
assert dialog.table.item(0, dialog._error_column).text() == "文字:转写服务暂不可用"
|
|
assert dialog.table.item(0, dialog._transcription_column).toolTip() == (
|
|
"转写服务暂不可用"
|
|
)
|
|
finally:
|
|
dialog.close()
|
|
|
|
|
|
def test_dialog_accepts_legacy_manager_without_transcription_methods(
|
|
application: QApplication, tmp_path: Path
|
|
) -> None:
|
|
store = _Store(_record(tmp_path))
|
|
manager = _LegacyManager(store)
|
|
dialog = LocalAudioQueueDialog(object(), 8169, store=store, manager=manager)
|
|
try:
|
|
assert manager.pending_upload_scopes == [8169]
|
|
assert not _buttons(dialog)["补全文字"].isEnabled()
|
|
finally:
|
|
dialog.close()
|