Settings stays a config reader; stages now take a per-stage value (SttOptions, DiarizeOptions, ShotOptions, DescribeOptions, ReportOptions) instead of the whole settings bag. options_for(stage) is the single place that fills defaults, so the 0.7 diarize threshold, minimum segment durations and model resolution rules exist once. The STT backend rule no longer inspects model_fields_set: an explicit stt_backend wins, else a non-default whisper_model selects whisper, else the profile default. Whisper with the default checkpoint is selected by stt_backend=whisper via env/TOML. Pipeline runners, cache-key builders and the injectable stages= seam consume the option values. describe_shots and synthesise_report no longer take Settings. DiarizeOptions has no independent threshold default, and diarize_audio's sherpa happy path is covered with a fake sherpa module.
286 lines
10 KiB
Python
286 lines
10 KiB
Python
"""Settings: env overrides and per-profile resolution rules."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
from pydantic import ValidationError
|
|
|
|
from video_analyse.config import (
|
|
HOSTED_VLM_MODEL,
|
|
LOCAL_VLM_MODEL,
|
|
Settings,
|
|
)
|
|
from video_analyse.diarize import DiarizeOptions
|
|
from video_analyse.media import ShotOptions
|
|
from video_analyse.synthesis import ReportOptions
|
|
from video_analyse.transcribe import SttOptions
|
|
from video_analyse.vision import DescribeOptions
|
|
|
|
VA_ENV_VARS = (
|
|
"VA_PROFILE",
|
|
"VA_CACHE_DIR",
|
|
"VA_LANGUAGE",
|
|
"VA_STT_BACKEND",
|
|
"VA_STT_API_BASE_URL",
|
|
"VA_STT_API_MODEL",
|
|
"VA_STT_MAX_UPLOAD_BYTES",
|
|
"VA_WHISPER_MODEL",
|
|
"VA_DIARIZE",
|
|
"VA_DIARIZE_BACKEND",
|
|
"VA_DIARIZE_CLUSTER_THRESHOLD",
|
|
"VA_DIARIZE_MIN_DURATION_ON",
|
|
"VA_DIARIZE_MIN_DURATION_OFF",
|
|
"VA_VLM_MODEL",
|
|
"VA_LLAMA_SERVER_URL",
|
|
"VA_SYNTHESIS_MODEL",
|
|
)
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def clean_va_env(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
"""Make every test independent of the developer's environment."""
|
|
for name in VA_ENV_VARS:
|
|
monkeypatch.delenv(name, raising=False)
|
|
monkeypatch.setenv("HOME", "/home/tester")
|
|
|
|
|
|
def test_defaults() -> None:
|
|
s = Settings()
|
|
assert s.profile == "local"
|
|
assert s.cache_dir == Path("/home/tester/.cache/video-analyse")
|
|
assert s.language is None
|
|
assert s.stt_backend is None
|
|
assert s.stt_api_base_url == "https://api.groq.com/openai/v1"
|
|
assert s.stt_api_model == "whisper-large-v3-turbo"
|
|
assert s.whisper_model == "large-v3-turbo"
|
|
assert s.diarize is True
|
|
assert s.diarize_backend == "sherpa"
|
|
assert s.vlm_model is None
|
|
assert s.synthesis_model is None
|
|
assert s.llama_server_url == "http://127.0.0.1:8080/v1"
|
|
|
|
|
|
def test_cache_dir_tilde_is_expanded() -> None:
|
|
assert Settings().cache_dir.is_absolute()
|
|
assert "~" not in str(Settings().cache_dir)
|
|
|
|
|
|
def test_env_overrides(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
monkeypatch.setenv("VA_PROFILE", "hosted")
|
|
monkeypatch.setenv("VA_CACHE_DIR", "~/va-cache")
|
|
monkeypatch.setenv("VA_LANGUAGE", "en")
|
|
monkeypatch.setenv("VA_STT_BACKEND", "whisper")
|
|
monkeypatch.setenv("VA_STT_API_BASE_URL", "https://example.invalid/v1")
|
|
monkeypatch.setenv("VA_STT_API_MODEL", "whisper-1")
|
|
monkeypatch.setenv("VA_WHISPER_MODEL", "small.en")
|
|
monkeypatch.setenv("VA_DIARIZE", "false")
|
|
monkeypatch.setenv("VA_DIARIZE_BACKEND", "pyannote")
|
|
monkeypatch.setenv("VA_DIARIZE_CLUSTER_THRESHOLD", "0.42")
|
|
monkeypatch.setenv("VA_DIARIZE_MIN_DURATION_ON", "0.25")
|
|
monkeypatch.setenv("VA_DIARIZE_MIN_DURATION_OFF", "0.6")
|
|
monkeypatch.setenv("VA_VLM_MODEL", "openrouter:acme/vlm")
|
|
monkeypatch.setenv("VA_LLAMA_SERVER_URL", "http://127.0.0.1:9999/v1")
|
|
monkeypatch.setenv("VA_SYNTHESIS_MODEL", "deepseek:deepseek-flash")
|
|
|
|
s = Settings()
|
|
assert s.profile == "hosted"
|
|
assert str(s.cache_dir).endswith("va-cache") and s.cache_dir.is_absolute()
|
|
assert s.language == "en"
|
|
assert s.stt_backend == "whisper"
|
|
assert s.stt_api_base_url == "https://example.invalid/v1"
|
|
assert s.stt_api_model == "whisper-1"
|
|
assert s.whisper_model == "small.en"
|
|
assert s.diarize is False
|
|
assert s.diarize_backend == "pyannote"
|
|
assert s.diarize_cluster_threshold == pytest.approx(0.42)
|
|
assert s.diarize_min_duration_on == pytest.approx(0.25)
|
|
assert s.diarize_min_duration_off == pytest.approx(0.6)
|
|
assert s.vlm_model == "openrouter:acme/vlm"
|
|
assert s.llama_server_url == "http://127.0.0.1:9999/v1"
|
|
assert s.synthesis_model == "deepseek:deepseek-flash"
|
|
|
|
|
|
def test_invalid_profile_is_rejected(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
monkeypatch.setenv("VA_PROFILE", "cloud")
|
|
with pytest.raises(ValueError):
|
|
Settings()
|
|
|
|
|
|
def test_local_profile_defaults() -> None:
|
|
s = Settings(profile="local")
|
|
assert s.resolved_stt() == "parakeet"
|
|
assert s.resolved_vlm() == LOCAL_VLM_MODEL == "local:Qwen3-VL-4B-Instruct"
|
|
assert s.resolved_synthesis() == LOCAL_VLM_MODEL
|
|
|
|
|
|
def test_hosted_profile_defaults() -> None:
|
|
s = Settings(profile="hosted")
|
|
assert s.resolved_stt() == "openai-transcriptions"
|
|
assert s.resolved_vlm() == HOSTED_VLM_MODEL == "deepseek:deepseek-flash"
|
|
assert s.resolved_synthesis() == HOSTED_VLM_MODEL
|
|
|
|
|
|
def test_explicit_overrides_beat_profile_defaults() -> None:
|
|
s = Settings(
|
|
profile="local",
|
|
stt_backend="openai-transcriptions",
|
|
vlm_model="openrouter:acme/vlm",
|
|
synthesis_model="ollama:qwen3-vl:4b",
|
|
)
|
|
assert s.resolved_stt() == "openai-transcriptions"
|
|
assert s.resolved_vlm() == "openrouter:acme/vlm"
|
|
assert s.resolved_synthesis() == "ollama:qwen3-vl:4b"
|
|
|
|
hosted = Settings(
|
|
profile="hosted",
|
|
stt_backend="parakeet",
|
|
vlm_model="local:Qwen3-VL-2B-Instruct",
|
|
synthesis_model="local:Qwen3-VL-2B-Instruct",
|
|
)
|
|
assert hosted.resolved_stt() == "parakeet"
|
|
assert hosted.resolved_vlm() == "local:Qwen3-VL-2B-Instruct"
|
|
assert hosted.resolved_synthesis() == "local:Qwen3-VL-2B-Instruct"
|
|
|
|
|
|
def test_explicit_whisper_model_selects_whisper_backend(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
"""Naming a Whisper checkpoint implies the faster-whisper backend."""
|
|
monkeypatch.setenv("VA_WHISPER_MODEL", "tiny")
|
|
s = Settings()
|
|
assert s.whisper_model == "tiny"
|
|
assert s.resolved_stt() == "whisper"
|
|
|
|
assert Settings(stt_backend="parakeet", whisper_model="tiny").resolved_stt() == "parakeet"
|
|
|
|
monkeypatch.delenv("VA_WHISPER_MODEL")
|
|
assert Settings(profile="hosted").resolved_stt() == "openai-transcriptions"
|
|
|
|
|
|
def test_language_is_independent_of_profile() -> None:
|
|
assert Settings(profile="hosted", language="en").language == "en"
|
|
assert Settings(language=None).language is None
|
|
|
|
|
|
def test_options_for_stt_honours_the_whisper_checkpoint_rule(
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
) -> None:
|
|
"""A non-default checkpoint selects whisper without consulting construction history."""
|
|
monkeypatch.setenv("VA_WHISPER_MODEL", "tiny")
|
|
|
|
options = Settings().options_for("stt")
|
|
|
|
assert isinstance(options, SttOptions)
|
|
assert options.backend == "whisper"
|
|
assert options.whisper_model == "tiny"
|
|
assert options.language is None
|
|
assert options == Settings().options_for("transcribe")
|
|
|
|
|
|
def test_whisper_with_the_default_checkpoint_is_env_and_toml_expressible(
|
|
monkeypatch: pytest.MonkeyPatch, tmp_path: Path
|
|
) -> None:
|
|
monkeypatch.setenv("VA_STT_BACKEND", "whisper")
|
|
from_env = Settings()
|
|
assert from_env.options_for("stt").backend == "whisper"
|
|
assert from_env.options_for("stt").whisper_model == "large-v3-turbo"
|
|
assert from_env.resolved_stt() == "whisper"
|
|
|
|
config_home = tmp_path / "config"
|
|
config_dir = config_home / "video-analyse"
|
|
config_dir.mkdir(parents=True)
|
|
(config_dir / "config.toml").write_text(
|
|
'profile = "local"\nstt_backend = "whisper"\n', encoding="utf-8"
|
|
)
|
|
monkeypatch.delenv("VA_STT_BACKEND")
|
|
monkeypatch.setenv("XDG_CONFIG_HOME", str(config_home))
|
|
|
|
from_file = Settings()
|
|
assert from_file.options_for("stt").backend == "whisper"
|
|
assert from_file.options_for("stt").whisper_model == "large-v3-turbo"
|
|
|
|
|
|
def test_options_for_diarize_uses_the_configured_threshold(tmp_path: Path) -> None:
|
|
settings = Settings(
|
|
cache_dir=tmp_path,
|
|
diarize_backend="pyannote",
|
|
diarize_cluster_threshold=0.42,
|
|
)
|
|
|
|
options = settings.options_for("diarize")
|
|
|
|
assert isinstance(options, DiarizeOptions)
|
|
assert options.backend == "pyannote"
|
|
assert options.cluster_threshold == pytest.approx(0.42)
|
|
assert options.min_duration_on == pytest.approx(0.3)
|
|
assert options.min_duration_off == pytest.approx(0.5)
|
|
assert options.cache_dir == tmp_path
|
|
with pytest.raises(ValidationError):
|
|
DiarizeOptions(
|
|
backend="sherpa",
|
|
min_duration_on=0.3,
|
|
min_duration_off=0.5,
|
|
cache_dir=tmp_path,
|
|
)
|
|
with pytest.raises(ValidationError):
|
|
options.cluster_threshold = 0.5
|
|
|
|
|
|
def test_options_for_shots_composes_the_existing_shot_options() -> None:
|
|
settings = Settings(
|
|
shot_sampling="uniform",
|
|
sampling_interval_seconds=7.5,
|
|
subdivide_after_seconds=30.0,
|
|
)
|
|
|
|
options = settings.options_for("shots")
|
|
|
|
assert isinstance(options, ShotOptions)
|
|
assert options.sampling == "uniform"
|
|
assert options.sampling_interval_seconds == pytest.approx(7.5)
|
|
assert options.subdivide_after_seconds == pytest.approx(30.0)
|
|
# The plan_shots defaults stay authoritative for the fields Settings does not carry.
|
|
assert options.max_shots == 64
|
|
assert options.min_scene_len == pytest.approx(0.6)
|
|
|
|
|
|
def test_options_for_describe_and_report_resolve_the_profile_models() -> None:
|
|
settings = Settings(profile="hosted", llama_server_url="http://127.0.0.1:9999/v1")
|
|
|
|
describe = settings.options_for("describe")
|
|
report = settings.options_for("report")
|
|
|
|
assert isinstance(describe, DescribeOptions)
|
|
assert isinstance(report, ReportOptions)
|
|
assert describe.vlm_model == HOSTED_VLM_MODEL
|
|
assert report.synthesis_model == HOSTED_VLM_MODEL
|
|
assert describe.llama_server_url == "http://127.0.0.1:9999/v1"
|
|
assert report.llama_server_url == "http://127.0.0.1:9999/v1"
|
|
|
|
|
|
def test_options_for_rejects_an_unknown_stage() -> None:
|
|
with pytest.raises(ValueError, match="unknown stage"):
|
|
Settings().options_for("nope")
|
|
|
|
|
|
def test_optional_toml_config_is_read_with_env_precedence(
|
|
monkeypatch: pytest.MonkeyPatch, tmp_path: Path
|
|
) -> None:
|
|
"""PLAN §5: ~/.config/video-analyse/config.toml works, but VA_* still wins."""
|
|
home = tmp_path / "home"
|
|
config_dir = home / ".config" / "video-analyse"
|
|
config_dir.mkdir(parents=True)
|
|
(config_dir / "config.toml").write_text(
|
|
'profile = "hosted"\nlanguage = "de"\nwhisper_model = "small"\n', encoding="utf-8"
|
|
)
|
|
monkeypatch.setenv("HOME", str(home))
|
|
monkeypatch.delenv("XDG_CONFIG_HOME", raising=False)
|
|
|
|
from_file = Settings()
|
|
assert from_file.profile == "hosted"
|
|
assert from_file.language == "de"
|
|
assert from_file.resolved_stt() == "whisper"
|
|
|
|
monkeypatch.setenv("VA_LANGUAGE", "en")
|
|
assert Settings().language == "en"
|