Files
DeepTutor/tests/scripts/test_start_tour.py
Bingxi Zhao (Frank) 4b7d24d33e release: v1.2.3 — document attachments, thinking blocks, provider core refactor
- Chat document attachments (PDF/DOCX/XLSX/PPTX) with text extraction
- Reasoning model <think> block display as collapsible cards
- Tri-state send_dimensions toggle for embeddings (#368)
- LLM provider_core module refactor + context_window detection
- Soul template editor (PR #373), Co-Writer save-to-notebook
- Knowledge Base drag-and-drop upload, delete resilience (#370),
  progress persistence, supported-file-types endpoint
- Question generation language fidelity via shared prompt directives
- Settings tour redesign, CLI notebook add-md/replace-md
- Bug fixes: CJK bot name slug, I18nProvider init, Skills default off,
  Moonshot base URL, version badge
- Provider registry: custom_anthropic, thinking_style, alias expansion
- +1900 test lines across 15 new/expanded test files

Made-with: Cursor
2026-04-24 22:36:32 +08:00

168 lines
5.9 KiB
Python

from __future__ import annotations
import importlib.util
import locale
from pathlib import Path
import shutil
import sys
import types
from unittest import mock
def _load_start_tour_module():
module_path = Path(__file__).resolve().parents[2] / "scripts" / "start_tour.py"
cli_kit = types.ModuleType("_cli_kit")
for name in (
"accent",
"banner",
"bold",
"confirm",
"countdown",
"dim",
"log_error",
"log_info",
"log_success",
"log_warn",
"select",
"step",
"text_input",
):
setattr(cli_kit, name, lambda *args, **kwargs: None)
deeptutor_pkg = types.ModuleType("deeptutor")
services_pkg = types.ModuleType("deeptutor.services")
config_module = types.ModuleType("deeptutor.services.config")
config_module.get_config_test_runner = lambda: None
config_module.get_env_store = lambda: None
config_module.get_model_catalog_service = lambda: None
original_modules = {
"_cli_kit": sys.modules.get("_cli_kit"),
"deeptutor": sys.modules.get("deeptutor"),
"deeptutor.services": sys.modules.get("deeptutor.services"),
"deeptutor.services.config": sys.modules.get("deeptutor.services.config"),
}
services_pkg.config = config_module
deeptutor_pkg.services = services_pkg
sys.modules["_cli_kit"] = cli_kit
sys.modules["deeptutor"] = deeptutor_pkg
sys.modules["deeptutor.services"] = services_pkg
sys.modules["deeptutor.services.config"] = config_module
try:
spec = importlib.util.spec_from_file_location("start_tour_under_test", module_path)
assert spec and spec.loader
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module
finally:
for module_name, original_module in original_modules.items():
if original_module is None:
sys.modules.pop(module_name, None)
else:
sys.modules[module_name] = original_module
def test_stream_text_kwargs_use_best_effort_decoding() -> None:
start_tour = _load_start_tour_module()
kwargs = start_tour._stream_text_kwargs()
assert kwargs["text"] is True
assert kwargs["errors"] == "replace"
assert kwargs["encoding"] == locale.getpreferredencoding(False)
# ---------------------------------------------------------------------------
# _resolve_python tests (issue #289 — Python 3.14+ compatibility)
# ---------------------------------------------------------------------------
def _get_resolve_python():
"""Import _resolve_python without triggering full module load."""
start_tour = _load_start_tour_module()
return start_tour._resolve_python
class TestResolvePython:
"""Verify _resolve_python handles broken sys.executable gracefully."""
def test_returns_resolved_path_when_executable_exists(self) -> None:
resolve = _get_resolve_python()
real_python = sys.executable
with mock.patch("sys.executable", real_python):
result = resolve()
assert Path(result).exists()
def test_falls_back_to_shutil_which_when_executable_missing(self) -> None:
"""Simulates Python 3.14+ on Windows where sys.executable is bogus."""
resolve = _get_resolve_python()
fake_path = "/nonexistent/python3.14"
expected = shutil.which("python3") or shutil.which("python") or "python3"
with mock.patch("sys.executable", fake_path):
result = resolve()
assert result == expected
def test_falls_back_when_executable_is_empty(self) -> None:
"""sys.executable can be '' in embedded or frozen environments."""
resolve = _get_resolve_python()
expected = shutil.which("python3") or shutil.which("python") or "python3"
with mock.patch("sys.executable", ""):
result = resolve()
assert result == expected
def test_ultimate_fallback_when_nothing_found(self) -> None:
"""If sys.executable is empty and PATH has no python, return 'python3'."""
resolve = _get_resolve_python()
with (
mock.patch("sys.executable", ""),
mock.patch("shutil.which", return_value=None),
):
result = resolve()
assert result == "python3"
def test_preserves_valid_sys_executable(self) -> None:
"""Normal case: sys.executable points to a real file."""
resolve = _get_resolve_python()
result = resolve()
assert Path(result).exists()
assert "python" in Path(result).name.lower()
def test_install_commands_use_resolved_python(self) -> None:
"""_install_commands should embed _PYTHON, not raw sys.executable."""
start_tour = _load_start_tour_module()
python_used = start_tour._PYTHON
catalog: dict = {"services": {}}
cmds = start_tour._install_commands("cli-core", catalog)
for cmd, _cwd in cmds:
if cmd[0] != "npm":
assert cmd[0] == python_used, (
f"Expected resolved python {python_used!r}, got {cmd[0]!r}"
)
def test_ensure_env_file_copies_template_when_missing(tmp_path: Path) -> None:
start_tour = _load_start_tour_module()
env_path = tmp_path / ".env"
template_path = tmp_path / ".env.example"
template_path.write_text("BACKEND_PORT=8001\n", encoding="utf-8")
created = start_tour._ensure_env_file(env_path=env_path, template_path=template_path)
assert created is True
assert env_path.read_text(encoding="utf-8") == "BACKEND_PORT=8001\n"
def test_save_ui_language_preserves_existing_theme(tmp_path: Path) -> None:
start_tour = _load_start_tour_module()
settings_path = tmp_path / "interface.json"
settings_path.write_text('{"theme":"glass","language":"en"}', encoding="utf-8")
start_tour._save_ui_language("zh", path=settings_path)
saved = settings_path.read_text(encoding="utf-8")
assert '"theme": "glass"' in saved
assert '"language": "zh"' in saved