from __future__ import annotations import base64 import hashlib from copy import deepcopy from threading import BoundedSemaphore, Event from types import SimpleNamespace from uuid import uuid4 import httpx import pytest from doctor_workstation.core.errors import ApiBusinessError, ApiTimeoutError from doctor_workstation.services.api_client import ApiClient from doctor_workstation.services.repository import RemoteDoctorRepository from doctor_workstation.video import tongue_screenshot as tongue from doctor_workstation.video import window JPEG = b"\xff\xd8synthetic-test-bytes\xff\xd9" URL = "data:image/jpeg;base64," + base64.b64encode(JPEG).decode() def response(request_id, content=JPEG, model="qwen"): return {"request_id": request_id, "diagnosis_id": 71, "image_sha256": hashlib.sha256(content).hexdigest(), "model": model, "model_label": "合成模型", "analysis": {"image_quality": "unusable", "quality_note": "未見清晰舌體。", "features": [], "limitations": ["請重新截圖。"], "tcm_diagnosis": { "status": "insufficient", "summary": "無法根據此圖辨證。", "candidates": [], "missing_information": ["清晰舌象、症狀與脈象"]}}} class Repo: def __init__(self): self.calls = [] self.started = Event() self.release = Event() self.release.set() self.change = lambda value: value self.error = None def analyze_tongue_screenshot(self, diagnosis_id, content, *, request_id, model="qwen"): self.calls.append((diagnosis_id, content, request_id)) self.started.set() assert self.release.wait(3) if self.error: raise self.error return self.change(response(request_id, content, model)) @pytest.fixture def service(monkeypatch): monkeypatch.setattr(tongue, "_SLOTS", BoundedSemaphore(2)) repo = Repo() controller = tongue.TongueScreenshotController(repo, diagnosis_id=71, enabled=True) controller.register_capture(URL) yield controller, repo controller.close() repo.release.set() assert controller.wait(3) def test_capture_is_local_and_analysis_is_bound_without_chart_write(service): controller, repo = service assert repo.calls == [] request_id = str(uuid4()) controller.analyze(request_id, URL) assert controller.wait(3) result = controller.take_result() assert result["ok"] and result["requestId"] == request_id assert result["data"]["analysis"] == { "image_quality": "unusable", "quality_note": "未见清晰舌体。", "features": [], "limitations": ["请重新截图。"], "tcm_diagnosis": {"status": "insufficient", "summary": "无法根据此图辨证。", "candidates": [], "missing_information": ["清晰舌象、症状与脉象"]}} assert repo.calls == [(71, JPEG, request_id)] controller.analyze(request_id, URL) assert len(repo.calls) == 1 and controller.take_result() is None @pytest.mark.parametrize("data_url", ["https://test.invalid/image.jpg", "data:image/png;base64,AAAA", "data:image/jpeg;base64,@@@@", "data:image/jpeg;base64,AAAA", URL + "AAAA"]) def test_arbitrary_or_changed_image_never_reaches_service(service, data_url): controller, repo = service controller.analyze(str(uuid4()), data_url) assert controller.take_result()["ok"] is False assert not repo.calls def test_permission_disabled_does_not_submit(): repo = Repo() controller = tongue.TongueScreenshotController(repo, diagnosis_id=71, enabled=False) controller.register_capture(URL) controller.analyze(str(uuid4()), URL) assert controller.take_result()["ok"] is False and not repo.calls controller.close() @pytest.mark.parametrize("finish", ["cancel", "new_capture", "clear", "close"]) def test_late_result_cannot_replace_current_preview(service, finish): controller, repo = service repo.release.clear() request_id = str(uuid4()) controller.analyze(request_id, URL) assert repo.started.wait(2) if finish == "cancel": controller.cancel(request_id) elif finish == "new_capture": controller.register_capture(URL) elif finish == "clear": controller.clear_capture() else: controller.close() repo.release.set() assert controller.wait(3) and controller.take_result() is None @pytest.mark.parametrize("change", [ lambda value: {**value, "diagnosis_id": 72}, lambda value: {**value, "request_id": str(uuid4())}, lambda value: {**value, "image_sha256": "a" * 64}, lambda value: {**value, "analysis": {**value["analysis"], "image_quality": "perfect"}}, lambda value: {**value, "analysis": {**value["analysis"], "features": [{"name": "舌苔", "observation": "白"}]}}, lambda value: {**value, "analysis": {**value["analysis"], "limitations": [123]}}, ]) def test_invalid_identity_or_contradictory_findings_rejected(service, change): controller, repo = service repo.change = change controller.analyze(str(uuid4()), URL) assert controller.wait(3) assert controller.take_result()["ok"] is False def test_timeout_and_raw_error_do_not_disclose_payload(service): controller, repo = service repo.error = ApiTimeoutError("private image/body/credential") controller.analyze(str(uuid4()), URL) assert controller.wait(3) result = controller.take_result() assert "超时" in result["message"] and "private" not in str(result) def test_global_concurrency_is_bounded(service, monkeypatch): controller, repo = service monkeypatch.setattr(tongue, "_SLOTS", BoundedSemaphore(0)) controller.analyze(str(uuid4()), URL) assert "稍后" in controller.take_result()["message"] and not repo.calls @pytest.mark.parametrize("model", ["qwen", "openai"]) def test_repository_uses_single_authenticated_temporary_multipart_request(model): requests = [] request_id = str(uuid4()) def handler(request): requests.append(request) return httpx.Response(200, json={"code": 1, "data": response(request_id, model=model)}) with ApiClient("https://synthetic.test", token="synthetic-token", transport=httpx.MockTransport(handler)) as client: result = RemoteDoctorRepository(client).analyze_tongue_screenshot(71, JPEG, request_id=request_id, model=model) assert result["request_id"] == request_id and len(requests) == 1 request = requests[0] assert request.url.path == "/adminapi/tcm.diagnosis/aiTongueScreenshot" assert request.headers["token"] == "synthetic-token" assert request.headers["content-type"].startswith("multipart/form-data;") assert JPEG in request.content and request_id.encode() in request.content assert model.encode() in request.content assert request.extensions["timeout"]["read"] == 60.0 @pytest.mark.parametrize("changes", [{"diagnosis_id": True}, {"diagnosis_id": 0}, {"request_id": "bad"}, {"content": b"not-jpeg"}, {"content": b"\xff\xd8" + b"a" * tongue.MAX_IMAGE_BYTES}, {"model": "unknown"}, {"model": ["qwen"]}]) def test_repository_invalid_input_never_sends(changes): args = {"diagnosis_id": 71, "content": JPEG, "request_id": str(uuid4()), **changes} repo = RemoteDoctorRepository(SimpleNamespace()) with pytest.raises(ValueError): repo.analyze_tongue_screenshot(**args) def test_bridge_has_dedicated_image_slot_and_trusted_document_gate(): messages = [] bridge = window._QtVideoBridge(messages.append, lambda _region: URL) request_id = str(uuid4()) bridge.analyzeTongueScreenshot(request_id, URL) assert messages == [{"source": "doctor-call", "event": "tongue-screenshot-analyze", "requestId": request_id, "dataUrl": URL}] calls = [] host = SimpleNamespace(_closing=False, _media_active=True, _start_requested=True, _call_cycle_closed=False, _tongue_ai=SimpleNamespace(analyze=lambda *args: calls.append(args)), _im_ai_context_is_trusted=lambda: False) window._EmbeddedVideoWindow._handle_bridge_message(host, messages[0]) assert not calls host._im_ai_context_is_trusted = lambda: True window._EmbeddedVideoWindow._handle_bridge_message(host, messages[0]) assert calls == [(request_id, URL, "qwen")] host._call_cycle_closed = True window._EmbeddedVideoWindow._handle_bridge_message(host, messages[0]) assert len(calls) == 1 def test_result_projection_discards_unexpected_fields(): request_id = str(uuid4()) value = response(request_id) value["raw_model"] = "private" value["analysis"]["diagnosis"] = "unsupported" clean = tongue.validated_result(deepcopy(value), 71, request_id, hashlib.sha256(JPEG).hexdigest()) assert "private" not in str(clean) and "unsupported" not in str(clean) class DualRepo: def __init__(self): self.started = {model: Event() for model in ("qwen", "openai")} self.release = {model: Event() for model in self.started} self.calls = [] self.errors = {} def analyze_tongue_screenshot(self, diagnosis_id, content, *, request_id, model="qwen"): self.calls.append((model, request_id, content)) self.started[model].set() assert self.release[model].wait(3) if model in self.errors: raise self.errors[model] return response(request_id, content, model) @pytest.fixture def dual_service(monkeypatch): monkeypatch.setattr(tongue, "_SLOTS", BoundedSemaphore(4)) repo = DualRepo() controller = tongue.TongueScreenshotController(repo, diagnosis_id=71, enabled=True) controller.register_capture(URL) yield controller, repo controller.close() for event in repo.release.values(): event.set() assert controller.wait(3) def test_both_models_run_concurrently_and_fast_result_arrives_while_other_waits(dual_service): controller, repo = dual_service ids = {model: str(uuid4()) for model in repo.started} for model, request_id in ids.items(): controller.analyze(request_id, URL, model) assert all(event.wait(1) for event in repo.started.values()) repo.release["openai"].set() assert not controller.wait(0.2), "qwen is still waiting, GPT must not wait for it" fast = controller.take_result() assert fast["ok"] and fast["data"]["model"] == "openai" assert fast["requestId"] == ids["openai"] and controller.take_result() is None repo.release["qwen"].set() assert controller.wait(2) assert controller.take_result()["data"]["model"] == "qwen" assert all(content == JPEG for _, _, content in repo.calls) def test_cancel_or_retry_one_model_retains_the_other_model_result(dual_service): controller, repo = dual_service qwen, gpt = str(uuid4()), str(uuid4()) controller.analyze(qwen, URL, "qwen") controller.analyze(gpt, URL, "openai") assert all(event.wait(1) for event in repo.started.values()) controller.cancel(qwen) for event in repo.release.values(): event.set() assert controller.wait(2) retry = str(uuid4()) controller.analyze(retry, URL, "qwen") assert controller.wait(2) results = [controller.take_result(), controller.take_result()] assert {item["requestId"] for item in results} == {gpt, retry} assert all(item["ok"] for item in results) assert controller.take_result() is None def test_one_model_failure_does_not_replace_other_success(dual_service): controller, repo = dual_service repo.errors["qwen"] = ApiTimeoutError("private failure body") for event in repo.release.values(): event.set() for model in repo.started: controller.analyze(str(uuid4()), URL, model) assert controller.wait(2) results = {item["model"]: item for item in (controller.take_result(), controller.take_result())} assert results["qwen"]["ok"] is False and "private" not in str(results["qwen"]) assert results["openai"]["ok"] is True @pytest.mark.parametrize("end", ["clear_capture", "close"]) def test_replacing_or_closing_capture_discards_both_late_model_results(dual_service, end): controller, repo = dual_service for model in repo.started: controller.analyze(str(uuid4()), URL, model) assert all(event.wait(1) for event in repo.started.values()) getattr(controller, end)() for event in repo.release.values(): event.set() assert controller.wait(2) and controller.take_result() is None def test_swapped_model_identity_is_rejected(service): controller, repo = service repo.change = lambda value: {**value, "model": "qwen"} controller.analyze(str(uuid4()), URL, "openai") assert controller.wait(2) assert controller.take_result()["ok"] is False def possible_pattern(request_id): value = response(request_id) value["analysis"].update({"image_quality": "limited", "features": [ {"name": "舌苔颜色", "observation": "合成白色示例"}], "tcm_diagnosis": { "status": "possible_patterns", "summary": "合成辨證參考,需四診核實。", "candidates": [{"name": "待核實的示例證候", "basis": ["舌苔颜色"], "verify": ["待詢問症狀與脈象"]}], "missing_information": ["主訴、症狀與脈象"]}}) return value def test_tcm_reference_preserves_visible_feature_links_and_converts_to_simplified(): request_id = str(uuid4()) clean = tongue.validated_result(possible_pattern(request_id), 71, request_id, hashlib.sha256(JPEG).hexdigest()) diagnosis = clean["analysis"]["tcm_diagnosis"] assert diagnosis["candidates"][0]["basis"] == ["舌苔颜色"] assert diagnosis["summary"] == "合成辨证参考,需四诊核实。" assert diagnosis["candidates"][0]["name"] == "待核实的示例证候" @pytest.mark.parametrize("change", [ lambda a: a.pop("tcm_diagnosis"), lambda a: a.update(image_quality="unusable"), lambda a: a["tcm_diagnosis"].update(status="confirmed"), lambda a: a["tcm_diagnosis"].update(status="insufficient"), lambda a: a["tcm_diagnosis"].update(candidates=[]), lambda a: a["tcm_diagnosis"].update(missing_information=[]), lambda a: a["tcm_diagnosis"]["candidates"][0].update(basis=["未观察到的舌体颜色"]), lambda a: a["tcm_diagnosis"]["candidates"][0].update(basis=[]), lambda a: a["tcm_diagnosis"]["candidates"][0].update(verify=[]), ]) def test_incomplete_unsupported_or_unobservable_tcm_references_fail_closed(change): request_id = str(uuid4()) value = possible_pattern(request_id) change(value["analysis"]) with pytest.raises(ValueError): tongue.validated_result(value, 71, request_id, hashlib.sha256(JPEG).hexdigest()) def test_model_bridge_accepts_only_configured_model_keys_and_forwards_identity(): messages = [] bridge = window._QtVideoBridge(messages.append, lambda _region: URL) request_id = str(uuid4()) for model in ("unknown", None, ["qwen"]): bridge.analyzeTongueScreenshotModel(request_id, URL, model) assert messages == [] bridge.analyzeTongueScreenshotModel(request_id, URL, "openai") assert messages == [{"source": "doctor-call", "event": "tongue-screenshot-analyze", "requestId": request_id, "dataUrl": URL, "model": "openai"}] @pytest.mark.parametrize("message,visible", [ ("GPT 模型接口配置异常,请管理员检查 Dify 中的模型 API 地址。", True), ("API upstream secret private patient detail", False), ]) def test_only_allowlisted_configuration_failure_is_exposed(service, message, visible): controller, repo = service repo.error = ApiBusinessError(message) controller.analyze(str(uuid4()), URL, "openai") assert controller.wait(2) result = controller.take_result() assert result["ok"] is False and (result["message"] == message) is visible @pytest.mark.parametrize("server_message,expected", [ ("截图分析超时,请稍后重试", "舌象分析超时,请重试。"), ("截图分析结果格式无效,请重新截图后重试", "模型返回的舌象结果格式不符合要求,请重试此模型。"), ("截图分析服务配置不完整", "舌象分析服务配置不完整,请管理员检查现有 AI 配置。"), ("权限不足,无法使用诊单 AI 助手", "当前账号没有舌象分析权限。"), ("upstream sensitive credential and patient text", "舌象分析未完成,请稍后重试。"), ]) def test_real_business_error_envelope_retains_safe_failure_category(monkeypatch, server_message, expected): monkeypatch.setattr(tongue, "_SLOTS", BoundedSemaphore(2)) client = ApiClient("https://fixture.test", token="synthetic-token", transport=httpx.MockTransport( lambda request: httpx.Response(200, json={"code": 0, "msg": server_message, "data": []}))) controller = tongue.TongueScreenshotController(RemoteDoctorRepository(client), diagnosis_id=71, enabled=True) try: controller.register_capture(URL) controller.analyze(str(uuid4()), URL, "qwen") assert controller.wait(2) result = controller.take_result() assert result["ok"] is False and result["message"] == expected finally: controller.close() client.close()