Files
DeepTutor/tests/services/config/test_launch_settings.py
Jaeheon Kim 049a59123d fix(i18n): accept Korean as a backend interface language (#2785)
* fix(i18n): accept Korean as a backend interface language

The web picker offers Korean (added in #2359), but the backend UiLanguage
contract, its normalizers and the settings spec never learned "ko". Choosing
Korean and applying settings therefore fails with HTTP 422 on
PUT /api/settings/ui, and the saved language falls back to English on the
next load.

Register "ko" in the same seams Japanese used in #2213: UiLanguage and
_normalize_language() in interface_settings.py, the launch-settings
normalizer, and _LANGUAGE_CHOICES in settings_spec.py, then regenerate the
frontend API contracts. Add Korean to the existing router and launch-settings
tests, and add a registry test that fails whenever a web APP_LANGUAGES code
is missing from the backend contract.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* style: apply ruff format

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 20:00:02 +08:00

126 lines
3.7 KiB
Python

from __future__ import annotations
import json
from pathlib import Path
import pytest
from deeptutor.services.config.launch_settings import load_launch_settings
def _settings_dir(root: Path) -> Path:
path = root / "data" / "user" / "settings"
path.mkdir(parents=True, exist_ok=True)
return path
def test_launch_settings_reads_ports_from_system_json_and_ignores_env_json(
monkeypatch, tmp_path: Path
) -> None:
for key in ("BACKEND_PORT", "FRONTEND_PORT", "UI_LANGUAGE", "LANGUAGE"):
monkeypatch.delenv(key, raising=False)
settings_dir = _settings_dir(tmp_path)
(settings_dir / "system.json").write_text(
json.dumps({"backend_port": 9001, "frontend_port": 4000}),
encoding="utf-8",
)
(settings_dir / "env.json").write_text(
json.dumps({"ports": {"backend": 8101, "frontend": 4100}}),
encoding="utf-8",
)
(settings_dir / "interface.json").write_text(
json.dumps({"language": "zh"}),
encoding="utf-8",
)
settings = load_launch_settings(tmp_path)
assert settings.backend_port == 9001
assert settings.frontend_port == 4000
assert settings.language == "zh"
assert "system.json" in settings.source
assert "env.json" not in settings.source
assert "interface.json" in settings.source
@pytest.mark.parametrize(
("stored_language", "expected"),
[
("french", "fr"),
("fr-FR", "fr"),
("ukrainian", "uk"),
("uk_UA", "uk"),
("uk-UA", "uk"),
("polish", "pl"),
("polski", "pl"),
("pl-PL", "pl"),
("turkish", "tr"),
("tr-TR", "tr"),
("vietnamese", "vi"),
("tiếng việt", "vi"),
("vi-VN", "vi"),
("vi_VN", "vi"),
("japanese", "ja"),
("ja-JP", "ja"),
("korean", "ko"),
("한국어", "ko"),
("ko-KR", "ko"),
("ko_KR", "ko"),
],
)
def test_launch_settings_reads_supported_languages_from_interface_json(
monkeypatch, tmp_path: Path, stored_language: str, expected: str
) -> None:
for key in ("BACKEND_PORT", "FRONTEND_PORT", "UI_LANGUAGE", "LANGUAGE"):
monkeypatch.delenv(key, raising=False)
settings_dir = _settings_dir(tmp_path)
(settings_dir / "system.json").write_text(
json.dumps({"backend_port": 8001, "frontend_port": 3782}),
encoding="utf-8",
)
(settings_dir / "interface.json").write_text(
json.dumps({"language": stored_language}),
encoding="utf-8",
)
settings = load_launch_settings(tmp_path)
assert settings.language == expected
assert "interface.json" in settings.source
def test_launch_settings_creates_default_system_json_without_dotenv_migration(
monkeypatch, tmp_path: Path
) -> None:
for key in ("BACKEND_PORT", "FRONTEND_PORT", "UI_LANGUAGE", "LANGUAGE"):
monkeypatch.delenv(key, raising=False)
(tmp_path / ".env").write_text(
"BACKEND_PORT=9101\nFRONTEND_PORT=4200\nUI_LANGUAGE=zh\n",
encoding="utf-8",
)
settings = load_launch_settings(tmp_path)
assert settings.backend_port == 8001
assert settings.frontend_port == 3782
assert settings.system_json_path.exists()
assert settings.language == "en"
def test_launch_settings_allows_process_env_as_deployment_override(
monkeypatch, tmp_path: Path
) -> None:
for key in ("BACKEND_PORT", "FRONTEND_PORT", "UI_LANGUAGE", "LANGUAGE"):
monkeypatch.delenv(key, raising=False)
monkeypatch.setenv("BACKEND_PORT", "9201")
monkeypatch.setenv("FRONTEND_PORT", "4300")
settings = load_launch_settings(tmp_path)
assert settings.backend_port == 9201
assert settings.frontend_port == 4300
assert "environment" in settings.source