- 新增 ai_consult_drug_names:中药饮片、方剂/中成药、西药显式词表,长词优先识别,西药按通用名去重 - AI 回复标题下新增“用药速览”卡片:中药绿、西药蓝色块 - 正文药名同色浅底加粗高亮,剂量数字不着色;Markdown 回退路径同样保留底色 - 新增药名识别测试,更新药物剂量着色测试 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
418 lines
17 KiB
Python
418 lines
17 KiB
Python
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}</span>' in rendered
|
||
assert 'background-color: #EAF7F0; font-weight: 700;">黄芪</span>' 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",
|
||
'<script>alert("test")</script>\n<a href="https://example.com">6-7 mmol/L</a>',
|
||
'<img src="x" onerror="alert(1)">HbA1c7.2% <7% & custom',
|
||
'<span style="color:red">正常</span> & \'" < >',
|
||
],
|
||
)
|
||
def test_html_is_literal_and_plain_text_and_whitespace_are_preserved(text: str) -> None:
|
||
document = _document(text)
|
||
html = metric_text_html(text)
|
||
assert "<script>" not in html
|
||
assert "<a " not in html
|
||
assert "<img " not in html
|
||
assert '<span style="color:red">' not in html
|
||
assert not document.find("https://example.com").charFormat().isAnchor()
|
||
assert "\n" not in html
|
||
|
||
|
||
@pytest.mark.parametrize(
|
||
"label,text,reading",
|
||
[
|
||
("空腹血糖", "FPG6.0mmol/L", "6.0mmol/L"),
|
||
("FPG", "空腹6.0 mmol/L", "6.0 mmol/L"),
|
||
("fasting_glucose", "空腹血糖6.0 mmol/L", "6.0 mmol/L"),
|
||
("HbA1c", "糖化血红蛋白7.2%", "7.2%"),
|
||
("糖化血红蛋白", "glycated hemoglobin 7.2%", "7.2%"),
|
||
("glycated haemoglobin", "HbA1c7.2%", "7.2%"),
|
||
("血压", "BP140/90mmHg", "140/90mmHg"),
|
||
("LDL-C", "低密度脂蛋白胆固醇3.2 mmol/L", "3.2 mmol/L"),
|
||
("尿素氮", "尿素氮6.0 mmol/L", "6.0 mmol/L"),
|
||
],
|
||
)
|
||
def test_recorded_states_match_field_aliases(label: str, text: str, reading: str) -> None:
|
||
with metric_text_context([MetricTextState(label, reading, "high")]):
|
||
assert _color(_document(text), reading) == "#BD4B58"
|
||
|
||
|
||
@pytest.mark.parametrize(
|
||
"status,color",
|
||
[
|
||
("high", "#BD4B58"),
|
||
("low", "#BD4B58"),
|
||
("warning", "#BD4B58"),
|
||
("未达标", "#BD4B58"),
|
||
("偏低", "#BD4B58"),
|
||
("normal", "#287B65"),
|
||
("达标", "#287B65"),
|
||
("unknown", "#697386"),
|
||
("", "#697386"),
|
||
("未记录", "#697386"),
|
||
("unexpected", "#697386"),
|
||
],
|
||
)
|
||
def test_value_only_widgets_use_the_supplied_field(status: str, color: str) -> None:
|
||
with metric_text_context([MetricTextState("FPG", "6.0 mmol/L", status)]):
|
||
document = _document("6.0 mmol/L", metric_label="空腹血糖")
|
||
assert _color(document, "6.0 mmol/L") == color
|
||
assert _color(_document("6.0 mmol/L"), "6.0 mmol/L") == "#697386"
|
||
|
||
|
||
def test_unitless_value_only_widgets_bind_without_classifying_other_numbers() -> None:
|
||
with metric_text_context([MetricTextState("BMI", "26.1", "normal")]):
|
||
assert _color(_document("26.1", metric_label="体质指数"), "26.1") == "#287B65"
|
||
assert "color:" not in metric_text_html("2026-08-18", metric_label="BMI")
|
||
|
||
|
||
def test_context_restores_outer_state_after_nested_scope_and_exception() -> None:
|
||
text = "空腹血糖6.0 mmol/L"
|
||
reading = "6.0 mmol/L"
|
||
with metric_text_context([MetricTextState("FPG", reading, "high")]):
|
||
assert _color(_document(text), reading) == "#BD4B58"
|
||
with (
|
||
pytest.raises(RuntimeError),
|
||
metric_text_context([MetricTextState("空腹", reading, "normal")]),
|
||
):
|
||
assert _color(_document(text), reading) == "#287B65"
|
||
raise RuntimeError("restore scope")
|
||
assert _color(_document(text), reading) == "#BD4B58"
|
||
assert _color(_document(text), reading) == "#697386"
|
||
|
||
|
||
def test_context_snapshots_input_sequence() -> None:
|
||
states = [MetricTextState("空腹", "6.0 mmol/L", "high")]
|
||
with metric_text_context(states):
|
||
states.clear()
|
||
assert _color(_document("空腹6.0 mmol/L"), "6.0 mmol/L") == "#BD4B58"
|
||
|
||
|
||
@pytest.mark.parametrize("statuses", [("high", "normal"), ("unknown", "normal"), ("high", "")])
|
||
def test_conflicting_same_field_and_reading_states_stay_gray(statuses: tuple[str, str]) -> None:
|
||
with metric_text_context(
|
||
[
|
||
MetricTextState("FPG", "6.0 mmol/L", statuses[0]),
|
||
MetricTextState("空腹血糖", "6.0mmol/L", statuses[1]),
|
||
]
|
||
):
|
||
assert _color(_document("空腹血糖6.0 mmol/L正常"), "6.0 mmol/L") == "#697386"
|
||
|
||
|
||
def test_equivalent_abnormal_source_flags_do_not_conflict() -> None:
|
||
with metric_text_context(
|
||
[
|
||
MetricTextState("FPG", "6.0 mmol/L", "high"),
|
||
MetricTextState("空腹血糖", "6.0mmol/L", "warning"),
|
||
]
|
||
):
|
||
assert _color(_document("空腹血糖6.0 mmol/L"), "6.0 mmol/L") == "#BD4B58"
|
||
|
||
|
||
@pytest.mark.parametrize(
|
||
"text",
|
||
[
|
||
"餐后血糖6.0 mmol/L",
|
||
"血糖6.0 mmol/L",
|
||
"空腹血糖6.1 mmol/L",
|
||
"空腹血糖6.0 mg/dL",
|
||
"空腹血糖<6.0 mmol/L",
|
||
"空腹血糖6.0-7.0 mmol/L",
|
||
],
|
||
)
|
||
def test_matching_value_cannot_leak_between_fields_units_or_readings(text: str) -> None:
|
||
with metric_text_context([MetricTextState("FPG", "6.0 mmol/L", "normal")]):
|
||
assert "#287B65" not in metric_text_html(text)
|
||
|
||
|
||
def test_range_source_requires_whole_range_and_comparator() -> None:
|
||
reading = "≤ 4.4至7.0 mmol/L"
|
||
with metric_text_context([MetricTextState("空腹", reading, "warning")]):
|
||
assert _color(_document(f"空腹血糖{reading}"), reading) == "#BD4B58"
|
||
assert _color(_document("空腹血糖4.4 mmol/L"), "4.4 mmol/L") == "#697386"
|
||
|
||
|
||
def test_known_source_state_takes_precedence_over_ai_assertion() -> None:
|
||
with metric_text_context([MetricTextState("空腹血糖", "6.0 mmol/L", "high")]):
|
||
document = _document("空腹血糖6.0 mmol/L正常")
|
||
assert _color(document, "6.0 mmol/L") == "#BD4B58"
|
||
assert _color(document, "正常") == "#287B65"
|
||
|
||
|
||
@pytest.mark.parametrize("hint", ["", "空腹血糖", "空腹血糖目标"])
|
||
def test_targets_never_inherit_source_state_even_when_value_matches(hint: str) -> None:
|
||
with metric_text_context([MetricTextState("空腹血糖", "6.0 mmol/L", "high")]):
|
||
document = _document("目标空腹血糖6.0 mmol/L", metric_label=hint)
|
||
assert _color(document, "6.0 mmol/L") == "#697386"
|
||
document = _document("6.0 mmol/L(参考值)", metric_label=hint)
|
||
assert _color(document, "6.0 mmol/L") == "#697386"
|
||
|
||
|
||
def test_percent_reference_and_same_value_source_are_separated() -> None:
|
||
with metric_text_context([MetricTextState("HbA1c", "7.2%", "high")]):
|
||
document = _document("糖化血红蛋白7.2%,目标7.2%")
|
||
assert _color(document, "7.2%") == "#BD4B58"
|
||
cursor = document.find("目标7.2%")
|
||
cursor.setPosition(cursor.selectionEnd() - 2)
|
||
cursor.movePosition(
|
||
QTextCursor.MoveOperation.NextCharacter, QTextCursor.MoveMode.KeepAnchor
|
||
)
|
||
assert cursor.charFormat().foreground().color().name().upper() == "#697386"
|
||
|
||
|
||
def test_percent_body_uses_its_separate_label_without_coloring_unrelated_percentages() -> None:
|
||
with metric_text_context([MetricTextState("HbA1c", "7.2%", "high")]):
|
||
document = _document("7.2%,目标7.2%;完成率80%,风险下降20%", metric_label="HbA1c")
|
||
assert _color(document, "7.2%") == "#BD4B58"
|
||
assert _color(document, "80%") != "#697386"
|
||
assert _color(document, "20%") != "#697386"
|
||
|
||
|
||
@pytest.mark.parametrize("cue", ["正常范围", "target", "reference range"])
|
||
def test_reference_phrases_always_stay_gray(cue: str) -> None:
|
||
with metric_text_context([MetricTextState("HbA1c", "7.2%", "high")]):
|
||
document = _document(f"HbA1c {cue}: 7.2%")
|
||
assert _color(document, "7.2%") == "#697386"
|
||
|
||
|
||
@pytest.mark.parametrize(
|
||
"text,reading,color",
|
||
[
|
||
("血糖6.0 mmol/L未达标", "6.0 mmol/L", "#BD4B58"),
|
||
("血压140/90mmHg正常", "140/90mmHg", "#287B65"),
|
||
("血糖偏高:6.0 mmol/L", "6.0 mmol/L", "#BD4B58"),
|
||
("空腹6.0 mmol/L偏低", "6.0 mmol/L", "#BD4B58"),
|
||
("BMI26.1达标", "26.1", "#287B65"),
|
||
("FPG6.0mmol/L high", "6.0mmol/L", "#BD4B58"),
|
||
("血糖6.0 mmol/L可能偏高", "6.0 mmol/L", "#697386"),
|
||
("若血糖6.0 mmol/L未达标", "6.0 mmol/L", "#697386"),
|
||
("血糖6.0 mmol/L无明显升高", "6.0 mmol/L", "#697386"),
|
||
("血糖6.0 mmol/L血压正常", "6.0 mmol/L", "#697386"),
|
||
("血糖6.0 mmol/L血压140/90mmHg正常", "6.0 mmol/L", "#697386"),
|
||
],
|
||
)
|
||
def test_local_assertion_colors_only_its_nearby_reading(
|
||
text: str, reading: str, color: str
|
||
) -> None:
|
||
assert _color(_document(text), reading) == color
|
||
|
||
|
||
@pytest.mark.parametrize(
|
||
"missing", ["未记录", "未检测", "未知", "暂无数据", "unknown", "N/A", "—", "--", "/"]
|
||
)
|
||
def test_missing_values_are_gray(missing: str) -> None:
|
||
with metric_text_context([MetricTextState("空腹血糖", missing, "normal")]):
|
||
assert _color(_document(missing, metric_label="空腹血糖"), missing) == "#697386"
|
||
|
||
|
||
@pytest.mark.parametrize(
|
||
"label,color", [("高风险", "#BD4B58"), ("中风险", "#BD4B58"), ("低风险", "#287B65")]
|
||
)
|
||
def test_asserted_risk_levels_use_the_chart_palette(label: str, color: str) -> None:
|
||
assert _color(_document(f"风险评估:{label}"), label) == color
|
||
|
||
|
||
@pytest.mark.parametrize("text", ["可能高风险", "排除高风险", "中风险参考标准", "若为低风险"])
|
||
def test_conditional_risk_or_reference_is_not_an_asserted_state(text: str) -> None:
|
||
assert "#BD4B58" not in metric_text_html(text)
|
||
assert "#287B65" not in metric_text_html(text)
|