Files
video-analyse/tests/test_doctor.py
T

169 lines
5.7 KiB
Python

"""Unit tests for the doctor's checks (no CLI, no network, no model downloads).
The pre-#6 doctor only had CLI smoke coverage. These tests pin the two facts the
issue moved into the adapters: the STT verdict is whatever ``readiness()`` says, and
the diarization verdict uses the same size+SHA predicate as ``ensure_model``.
"""
from __future__ import annotations
import hashlib
from pathlib import Path
from typing import Any
import pytest
from video_analyse import doctor, llm
from video_analyse.config import Settings
from video_analyse.doctor import DoctorCheck
from video_analyse.model_assets import (
EMBEDDING_MODEL,
MODEL_ASSETS,
SEGMENTATION_MODEL,
ModelAsset,
model_path,
)
from video_analyse.transcribe.base import ModelIdentity, Readiness, Requirement
class FakeTranscriber:
"""Minimal Transcriber whose readiness is supplied by the test."""
def __init__(self, backend: str, readiness: Readiness) -> None:
self._backend = backend
self._readiness = readiness
async def transcribe(self, wav: Path, *, language: str | None = None) -> Any:
raise AssertionError("doctor must never transcribe")
def identity(self) -> ModelIdentity:
return ModelIdentity(backend=self._backend, model=f"{self._backend}-model")
def readiness(self) -> Readiness:
return self._readiness
def test_stt_checks_render_readiness_for_every_registered_backend(
monkeypatch: pytest.MonkeyPatch,
) -> None:
readiness = {
"alpha": Readiness(
weights_present=True,
weights_location="/models/alpha",
requirements=[Requirement(name="alpha extra", ok=True, hint="installed")],
),
"beta": Readiness(
weights_present=False,
weights_location="/models/beta",
requirements=[
Requirement(name="beta extra", ok=False, hint="install beta with uv sync")
],
),
}
monkeypatch.setattr(doctor, "STT_BACKENDS", ("alpha", "beta"))
monkeypatch.setattr(
doctor,
"get_transcriber",
lambda options: FakeTranscriber(str(options.backend), readiness[str(options.backend)]),
)
checks = doctor._stt_checks(Settings())
assert [check.name for check in checks] == ["stt model (alpha)", "stt model (beta)"]
assert checks[0].status == "ok"
assert "alpha-model" in checks[0].detail
assert checks[1].status == "warn"
assert "not cached" in checks[1].detail
assert "install beta with uv sync" in checks[1].detail
def test_stt_checks_surface_the_hosted_credential_from_readiness(
monkeypatch: pytest.MonkeyPatch,
) -> None:
from video_analyse.transcribe import openai_transcriptions
for name in openai_transcriptions.API_KEY_ENV_VARS:
monkeypatch.delenv(name, raising=False)
monkeypatch.setattr(doctor, "STT_BACKENDS", ("openai-transcriptions",))
(check,) = doctor._stt_checks(Settings(profile="hosted"))
assert check.status == "warn"
assert "GROQ_API_KEY" in check.detail
assert "API key" in check.detail
assert "VA_STT_API_KEY" in check.detail
def _dummy_asset(name: str, payload: bytes) -> ModelAsset:
return ModelAsset(
name=name,
url=f"https://example.invalid/{name}.bin",
filename=f"{name}.onnx",
size_bytes=len(payload),
sha256=hashlib.sha256(payload).hexdigest(),
)
def test_diarization_checks_use_the_model_assets_verification(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
payload = b"validated-diarization-model"
for name in (SEGMENTATION_MODEL, EMBEDDING_MODEL):
monkeypatch.setitem(MODEL_ASSETS, name, _dummy_asset(name, payload))
good = model_path(SEGMENTATION_MODEL, cache_dir=tmp_path)
good.parent.mkdir(parents=True)
good.write_bytes(payload)
bad = model_path(EMBEDDING_MODEL, cache_dir=tmp_path)
bad.parent.mkdir(parents=True)
bad.write_bytes(b"truncated")
checks = doctor._diarization_checks(Settings(cache_dir=tmp_path))
by_name = {check.name: check for check in checks}
assert by_name[SEGMENTATION_MODEL].status == "ok"
assert by_name[EMBEDDING_MODEL].status == "warn"
assert "expected" in by_name[EMBEDDING_MODEL].detail
async def test_llama_check_uses_the_llm_routing_record(
monkeypatch: pytest.MonkeyPatch,
) -> None:
async def forbidden(settings: Settings) -> bool:
raise AssertionError("hosted specs must not probe llama-server")
monkeypatch.setattr(llm, "llama_server_up", forbidden)
settings = Settings(
vlm_model="deepseek:deepseek-flash",
synthesis_model="openrouter:deepseek/deepseek-v4.1-flash-1017",
)
check, command = await doctor._llama_check(settings)
assert check.status == "info"
assert "hosted" in check.detail
assert command is None
async def test_run_doctor_includes_stt_and_diarization_units(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
def tool(name: str, *, timeout: float) -> DoctorCheck:
return DoctorCheck(name=name, status="ok", detail="test")
async def llama(settings: Settings) -> tuple[DoctorCheck, str | None]:
return DoctorCheck(name="llama-server", status="info", detail="test"), None
monkeypatch.setattr(doctor, "_tool_check", tool)
monkeypatch.setattr(
doctor, "_ytdlp_check", lambda: DoctorCheck(name="yt-dlp", status="ok", detail="test")
)
monkeypatch.setattr(doctor, "_llama_check", llama)
report = await doctor.run_doctor(Settings(cache_dir=tmp_path))
names = {check.name for check in report.checks}
assert {f"stt model ({backend})" for backend in doctor.STT_BACKENDS} <= names
assert SEGMENTATION_MODEL in names
assert EMBEDDING_MODEL in names
assert report.ok is True