feat: surgery pipeline API, video inference, voice confirm, and tests
- Add FastAPI routes for surgery start/end, results, pending confirmation (WAV upload), and health checks.
- Implement RTSP/Hikvision capture, consumable classification, session manager, MinIO/Baidu voice resolution, and DB persistence.
- Add documentation (client API, video backends, staging checklist) and sample camera/RTSP config.
- Add pytest suite (API contract, session manager, voice, repositories, pipeline persistence) and httpx dev dependency.
- Replace deprecated HTTP_422_UNPROCESSABLE_ENTITY with HTTP_422_UNPROCESSABLE_CONTENT.
- Fix SurgeryPipeline DB reads to use an explicit transaction with autobegin disabled.
Made-with: Cursor
2026-04-21 18:33:54 +08:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
2026-04-23 14:24:20 +08:00
|
|
|
import asyncio
|
feat: surgery pipeline API, video inference, voice confirm, and tests
- Add FastAPI routes for surgery start/end, results, pending confirmation (WAV upload), and health checks.
- Implement RTSP/Hikvision capture, consumable classification, session manager, MinIO/Baidu voice resolution, and DB persistence.
- Add documentation (client API, video backends, staging checklist) and sample camera/RTSP config.
- Add pytest suite (API contract, session manager, voice, repositories, pipeline persistence) and httpx dev dependency.
- Replace deprecated HTTP_422_UNPROCESSABLE_ENTITY with HTTP_422_UNPROCESSABLE_CONTENT.
- Fix SurgeryPipeline DB reads to use an explicit transaction with autobegin disabled.
Made-with: Cursor
2026-04-21 18:33:54 +08:00
|
|
|
import json
|
|
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
from sqlalchemy import select
|
|
|
|
|
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
|
|
|
|
|
|
|
|
|
|
import app.db.models # noqa: F401
|
|
|
|
|
from app.db.base import Base
|
|
|
|
|
from app.db.models import VoiceConfirmationAudit
|
|
|
|
|
from app.repositories.voice_audits import VoiceAuditRepository
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
|
async def db_session() -> AsyncSession:
|
|
|
|
|
engine = create_async_engine("sqlite+aiosqlite:///:memory:")
|
|
|
|
|
async with engine.begin() as conn:
|
|
|
|
|
await conn.run_sync(Base.metadata.create_all)
|
|
|
|
|
factory = async_sessionmaker(engine, class_=AsyncSession, expire_on_commit=False)
|
|
|
|
|
session = factory()
|
|
|
|
|
yield session
|
|
|
|
|
await session.close()
|
|
|
|
|
await engine.dispose()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
async def test_save_audit_persists_fields(db_session: AsyncSession) -> None:
|
|
|
|
|
repo = VoiceAuditRepository()
|
|
|
|
|
opts = json.dumps([{"label": "纱布", "confidence": 0.4}], ensure_ascii=False)
|
|
|
|
|
async with db_session.begin():
|
|
|
|
|
await repo.save_audit(
|
|
|
|
|
db_session,
|
|
|
|
|
surgery_id="123456",
|
|
|
|
|
confirmation_id="cid-1",
|
|
|
|
|
status="recognized",
|
|
|
|
|
audio_object_key="surgeries/123456/x.wav",
|
|
|
|
|
audio_content_type="audio/wav",
|
|
|
|
|
audio_size_bytes=100,
|
|
|
|
|
audio_sha256="a" * 64,
|
|
|
|
|
asr_text="纱布",
|
|
|
|
|
resolved_label="纱布",
|
|
|
|
|
options_snapshot_json=opts,
|
|
|
|
|
error_message=None,
|
|
|
|
|
)
|
|
|
|
|
async with db_session.begin():
|
|
|
|
|
res = await db_session.execute(select(VoiceConfirmationAudit))
|
|
|
|
|
rows = res.scalars().all()
|
|
|
|
|
assert len(rows) == 1
|
|
|
|
|
r = rows[0]
|
|
|
|
|
assert r.surgery_id == "123456"
|
|
|
|
|
assert r.confirmation_id == "cid-1"
|
|
|
|
|
assert r.status == "recognized"
|
|
|
|
|
assert r.asr_text == "纱布"
|
|
|
|
|
assert r.resolved_label == "纱布"
|
|
|
|
|
assert r.options_snapshot_json == opts
|
|
|
|
|
assert r.error_message is None
|
2026-04-23 14:24:20 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
async def test_list_by_surgery_order_and_total(db_session: AsyncSession) -> None:
|
|
|
|
|
repo = VoiceAuditRepository()
|
|
|
|
|
async with db_session.begin():
|
|
|
|
|
await repo.save_audit(
|
|
|
|
|
db_session,
|
|
|
|
|
surgery_id="111111",
|
|
|
|
|
confirmation_id="a",
|
|
|
|
|
status="parse_failed",
|
|
|
|
|
audio_object_key=None,
|
|
|
|
|
audio_content_type=None,
|
|
|
|
|
audio_size_bytes=None,
|
|
|
|
|
audio_sha256=None,
|
|
|
|
|
asr_text="糊",
|
|
|
|
|
resolved_label=None,
|
|
|
|
|
options_snapshot_json="[]",
|
|
|
|
|
error_message="x",
|
|
|
|
|
)
|
|
|
|
|
await asyncio.sleep(0.02)
|
|
|
|
|
async with db_session.begin():
|
|
|
|
|
await repo.save_audit(
|
|
|
|
|
db_session,
|
|
|
|
|
surgery_id="111111",
|
|
|
|
|
confirmation_id="b",
|
|
|
|
|
status="recognized",
|
|
|
|
|
audio_object_key="k.wav",
|
|
|
|
|
audio_content_type="audio/wav",
|
|
|
|
|
audio_size_bytes=10,
|
|
|
|
|
audio_sha256="b" * 64,
|
|
|
|
|
asr_text="纱布",
|
|
|
|
|
resolved_label="纱布",
|
|
|
|
|
options_snapshot_json="[]",
|
|
|
|
|
error_message=None,
|
|
|
|
|
)
|
|
|
|
|
async with db_session.begin():
|
|
|
|
|
rows, total = await repo.list_by_surgery(db_session, "111111", limit=10, offset=0)
|
|
|
|
|
assert total == 2
|
|
|
|
|
assert [r.confirmation_id for r in rows] == ["b", "a"]
|
|
|
|
|
async with db_session.begin():
|
|
|
|
|
page2, total2 = await repo.list_by_surgery(
|
|
|
|
|
db_session, "111111", limit=1, offset=1
|
|
|
|
|
)
|
|
|
|
|
assert total2 == 2
|
|
|
|
|
assert len(page2) == 1
|
|
|
|
|
assert page2[0].confirmation_id == "a"
|