generated from john/python-template
77 lines
2.7 KiB
Python
77 lines
2.7 KiB
Python
"""Tests for safe runtime settings .env editing helpers."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from transcription.config import Settings
|
|
from transcription.errors import AppError
|
|
from transcription.ui.runtime_settings_store import RUNTIME_SETTINGS_CATALOG
|
|
from transcription.ui.runtime_settings_store import SETTINGS_UI_EXCLUDED_FIELDS
|
|
from transcription.ui.runtime_settings_store import read_runtime_settings_snapshot
|
|
from transcription.ui.runtime_settings_store import save_runtime_settings
|
|
from transcription.ui.runtime_settings_store import settings_catalog_field_names
|
|
|
|
|
|
def _settings_for_runtime_editing(tmp_path: Path) -> Settings:
|
|
env_path = tmp_path / ".env"
|
|
env_path.write_text("OPENROUTER_API_KEY=test-key\n", encoding="utf-8")
|
|
return Settings(_env_file=env_path, _cli_parse_args=False)
|
|
|
|
|
|
def test_runtime_settings_catalog_classifies_all_non_secret_settings_fields():
|
|
available_fields = set(Settings.model_fields)
|
|
expected = available_fields - set(SETTINGS_UI_EXCLUDED_FIELDS)
|
|
assert settings_catalog_field_names() == expected
|
|
assert "openrouter_api_key" not in settings_catalog_field_names()
|
|
|
|
|
|
def test_runtime_settings_snapshot_includes_catalog_fields(tmp_path: Path):
|
|
settings = _settings_for_runtime_editing(tmp_path)
|
|
|
|
snapshot = read_runtime_settings_snapshot(settings=settings, env_file_path=tmp_path / ".env")
|
|
|
|
assert snapshot.env_file_path == tmp_path / ".env"
|
|
assert len(snapshot.fields) == len(RUNTIME_SETTINGS_CATALOG)
|
|
assert any(field.field_name == "port" for field in snapshot.fields)
|
|
assert any(field.field_name == "provider" for field in snapshot.fields)
|
|
|
|
|
|
def test_save_runtime_settings_writes_allowed_updates(tmp_path: Path):
|
|
settings = _settings_for_runtime_editing(tmp_path)
|
|
env_path = tmp_path / ".env"
|
|
env_path.write_text(
|
|
"# sample env\nOPENROUTER_API_KEY=test-key\nPORT=8000\nLOG_LEVEL=info\n",
|
|
encoding="utf-8",
|
|
)
|
|
|
|
snapshot = save_runtime_settings(
|
|
settings=settings,
|
|
updates={
|
|
"port": "9001",
|
|
"log_level": "debug",
|
|
"reload": True,
|
|
},
|
|
env_file_path=env_path,
|
|
)
|
|
|
|
updated = env_path.read_text(encoding="utf-8")
|
|
assert "PORT=9001" in updated
|
|
assert "LOG_LEVEL=debug" in updated
|
|
assert "RELOAD=true" in updated
|
|
assert any(field.field_name == "port" and str(field.value) == "9001" for field in snapshot.fields)
|
|
|
|
|
|
def test_save_runtime_settings_rejects_invalid_values(tmp_path: Path):
|
|
settings = _settings_for_runtime_editing(tmp_path)
|
|
env_path = tmp_path / ".env"
|
|
|
|
with pytest.raises(AppError):
|
|
save_runtime_settings(
|
|
settings=settings,
|
|
updates={"port": "not-a-number"},
|
|
env_file_path=env_path,
|
|
)
|