from __future__ import annotations import os os.environ.setdefault("QT_QPA_PLATFORM", "offscreen") import pytest from PySide6.QtGui import QTextCursor, QTextDocument from PySide6.QtWidgets import QApplication from doctor_workstation.ui.dialogs.ai_consult_metric_text import ( MetricTextState, metric_text_context, metric_text_html, ) @pytest.fixture(scope="module", autouse=True) def application() -> QApplication: return QApplication.instance() or QApplication([]) def _document(text: str, *, metric_label: str = "") -> QTextDocument: document = QTextDocument() document.setHtml(metric_text_html(text, metric_label=metric_label)) assert document.toPlainText() == text.replace("\r\n", "\n").replace("\r", "\n") return document def _color(document: QTextDocument, phrase: str, offset: int = 0) -> str: position = document.toPlainText().index(phrase) + offset cursor = QTextCursor(document) cursor.setPosition(position) cursor.movePosition(QTextCursor.MoveOperation.NextCharacter, QTextCursor.MoveMode.KeepAnchor) return cursor.charFormat().foreground().color().name().upper() @pytest.mark.parametrize( "text,measurement", [ ("空腹血糖6-7 mmol/L,餐后8-9mmol/L", "6-7 mmol/L"), ("空腹血糖6-7 mmol/L,餐后8-9mmol/L", "8-9mmol/L"), ("血糖<7.0 mmol/L", "<7.0 mmol/L"), ("血糖≤ 7.0 mmol/L", "≤ 7.0 mmol/L"), ("血糖<= 7.0 mmol/L", "<= 7.0 mmol/L"), ("血糖<7.0 mmol/L", "<7.0 mmol/L"), ("血糖3.9~6.1 mmol/L", "3.9~6.1 mmol/L"), ("血糖4.4至7.0 mmol/L", "4.4至7.0 mmol/L"), ("BP140/90mmHg", "140/90mmHg"), ("血压140 / 90 mmHg", "140 / 90 mmHg"), ("HbA1c7.2%", "7.2%"), ("糖化血红蛋白(HbA1c)7.2%", "7.2%"), ("BMI26.1", "26.1"), ("BMI26.1kg/m²", "26.1kg/m²"), ("体质量指数:26.1", "26.1"), ("身高175cm,体重80 kg", "175cm"), ("身高175cm,体重80 kg", "80 kg"), ("身高1.75m", "1.75m"), ("height175cm,weight80kg", "175cm"), ("height175cm,weight80kg", "80kg"), ("LDL-C130mg/dL", "130mg/dL"), ("TC:5.8mmol/L", "5.8mmol/L"), ("eGFR 85 ml/min/1.73m²", "85 ml/min/1.73m²"), ("eGFR:85", "85"), ("eGFR 85 mL/min/1.73 m2", "85 mL/min/1.73 m2"), ("ACR30mg/g", "30mg/g"), ("尿白蛋白肌酐比值30mg/gCr", "30mg/gCr"), ("肌酐88 μmol/L", "88 μmol/L"), ("尿酸300µmol/L", "300µmol/L"), ("heart_rate:72", "72"), ("heart_rate72bpm", "72bpm"), ("心率72次/分", "72次/分"), ("HR72bpm", "72bpm"), ("血氧饱和度98%", "98%"), ("体温36.8℃", "36.8℃"), ("ALT35U/L", "35U/L"), ("血红蛋白130g/L", "130g/L"), ], ) def test_clinical_values_are_semibold_gray_and_preserve_all_characters( text: str, measurement: str ) -> None: document = _document(text) for offset in range(len(measurement)): assert _color(document, measurement, offset) == "#697386" cursor = QTextCursor(document) cursor.setPosition(document.toPlainText().index(measurement)) cursor.movePosition(QTextCursor.MoveOperation.NextCharacter, QTextCursor.MoveMode.KeepAnchor) assert cursor.charFormat().fontWeight() >= 600 @pytest.mark.parametrize("cue", ["目标", "参考", "理想", "阈值"]) def test_targets_and_reference_ranges_stay_gray(cue: str) -> None: document = _document(f"当前血糖8.2 mmol/L,{cue}:<7.0 mmol/L") assert _color(document, "8.2 mmol/L") == "#697386" for offset in range(len("<7.0 mmol/L")): assert _color(document, "<7.0 mmol/L", offset) == "#697386" def test_percent_targets_inherit_only_a_clinical_percent_label() -> None: document = _document("HbA1c7.2%,目标<7.0%;总体完成率80%,风险降低20%") assert _color(document, "7.2%") == "#697386" assert _color(document, "<7.0%") == "#697386" assert _color(document, "80%") != "#697386" assert _color(document, "20%") != "#697386" def test_current_readings_after_targets_do_not_keep_reference_color() -> None: document = _document("目标<7.0 mmol/L;当前8.2 mmol/L,6.8 mmol/L(参考值)") assert _color(document, "<7.0 mmol/L") == "#697386" assert _color(document, "8.2 mmol/L") == "#697386" assert _color(document, "6.8 mmol/L") == "#697386" @pytest.mark.parametrize( "text", [ "诊单ID:123456;patient_id=1501;流水ABC123456;电话13800138000", "2026-08-18,2026/08/19,08:30,第1章,1. 检查,2. 处理", "病程3年,持续2周,随访7天,每次30分钟", "剂量0.2mg/kg、浓度5mg/mL,外用2%利多卡因", "统计显示80%的患者改善20%,目标完成率90%,报销比例50%", "BMI2026-08-18;eGFR2026/08/19;heart_rate2026-08-18", "not_BMI26.1;abceGFR85;ID7mmol/L;abc72bpm", ], ) def test_unrelated_numbers_and_drug_doses_remain_unemphasized(text: str) -> None: rendered = metric_text_html(text) _document(text) assert "color:" not in rendered def test_drug_names_are_highlighted_but_their_doses_stay_plain() -> None: text = "二甲双胍500mg,每日2次;胰岛素10U;阿司匹林100 mg;黄芪30g" rendered = metric_text_html(text) for name in ("二甲双胍", "胰岛素", "阿司匹林"): assert f'background-color: #EEF1FA; font-weight: 700;">{name}' in rendered assert 'background-color: #EAF7F0; font-weight: 700;">黄芪' in rendered for dose in ("500mg", "10U", "100 mg", "30g"): assert f'font-weight: 600;">{dose}' not in rendered assert f'font-weight: 700;">{dose}' not in rendered def test_explicit_statuses_are_emphasized_without_classifying_the_numbers() -> None: document = _document("血糖6.0 mmol/L未达标;血压140/90mmHg正常;血糖偏高;检查异常") assert _color(document, "6.0 mmol/L") == "#BD4B58" assert _color(document, "140/90mmHg") == "#287B65" assert _color(document, "未达标") == "#BD4B58" assert _color(document, "正常") == "#287B65" assert _color(document, "偏高") == "#BD4B58" assert _color(document, "异常") == "#BD4B58" @pytest.mark.parametrize( "status,color", [ ("未达标", "#BD4B58"), ("不达标", "#BD4B58"), ("尚未完全达标", "#BD4B58"), ("未见异常", "#287B65"), ("无异常", "#287B65"), ("未见明显异常", "#287B65"), ("没有显著异常", "#287B65"), ("不正常", "#BD4B58"), ("非正常", "#BD4B58"), ("未恢复正常", "#BD4B58"), ("已达标", "#287B65"), ], ) def test_negated_statuses_do_not_color_a_misleading_substring(status: str, color: str) -> None: document = _document(status) for offset in range(len(status)): assert _color(document, status, offset) == color @pytest.mark.parametrize( "text", [ "建议复查以排除异常", "警惕血糖偏低风险", "可能偏高,需结合进一步检查", "若仍未达标,应复查", "不能排除血糖异常", "是否正常有待检查", "未见升高;无明显升高;不偏高", "无法达标;未达到正常;不能完全达标", "正常值与正常范围;达标率80%", "如血糖升高或不达标,应随访", "需使血糖达标", "建议控制至正常,争取早日达标", ], ) def test_uncertain_conditional_or_recommended_statuses_stay_neutral(text: str) -> None: _document(text) assert "color:" not in metric_text_html(text) @pytest.mark.parametrize( "text", [ "", " 原文 保留\t制表符\n\n最后 ", "\n首行前换行\r\n血糖<7.0 mmol/L\r末行\n", '\n6-7 mmol/L', 'HbA1c7.2% <7%   & custom', '正常 & \'" < >', ], ) def test_html_is_literal_and_plain_text_and_whitespace_are_preserved(text: str) -> None: document = _document(text) html = metric_text_html(text) assert "