peregrine/tests/test_ui_switcher.py
pyr0ball dc508d7197
Some checks failed
CI / test (push) Failing after 28s
fix: update tests to match refactored scheduler and free-tier Vue SPA
- task_scheduler: extend LocalScheduler (concrete class), not TaskScheduler
  (Protocol); remove unsupported VRAM kwargs from super().__init__()
- dev-api: lazy import db_migrate inside _startup() to avoid worktree
  scripts cache issue in test_dev_api_settings.py
- test_task_scheduler: update VRAM-attribute tests to match LocalScheduler
  (no _available_vram/_reserved_vram); drop deepest-queue VRAM-gating
  ordering assertion (LocalScheduler is FIFO, not priority-gated);
  suppress PytestUnhandledThreadExceptionWarning on crash test; fix
  budget assertion to not depend on shared pytest tmp dir state
- test_dev_api_settings: patch path functions (_resume_path, _search_prefs_path,
  _license_path, _tokens_path, _config_dir) instead of removed module-level
  constants; mock _TRAINING_JSONL for finetune status idle test
- test_wizard_tiers: Vue SPA is free tier (issue #20), assert True
- test_wizard_api: patch _search_prefs_path() function, not SEARCH_PREFS_PATH
- test_ui_switcher: free-tier vue preference no longer downgrades to streamlit
2026-04-05 07:35:45 -07:00

105 lines
4 KiB
Python

"""Tests for app/components/ui_switcher.py.
Streamlit is not running during tests — mock all st.* calls.
"""
import sys
from pathlib import Path
from unittest.mock import patch
import pytest
import yaml
sys.path.insert(0, str(Path(__file__).parent.parent))
@pytest.fixture
def profile_yaml(tmp_path):
data = {"name": "Test", "ui_preference": "streamlit", "wizard_complete": True}
p = tmp_path / "user.yaml"
p.write_text(yaml.dump(data))
return p
def test_sync_cookie_injects_vue_js(profile_yaml, monkeypatch):
"""When ui_preference is vue, JS sets prgn_ui=vue."""
import yaml as _yaml
profile_yaml.write_text(_yaml.dump({"name": "T", "ui_preference": "vue"}))
injected = []
monkeypatch.setattr("streamlit.components.v1.html", lambda html, height=0: injected.append(html))
monkeypatch.setattr("streamlit.query_params", {}, raising=False)
from app.components.ui_switcher import sync_ui_cookie
sync_ui_cookie(profile_yaml, tier="paid")
assert any("prgn_ui=vue" in s for s in injected)
def test_sync_cookie_injects_streamlit_js(profile_yaml, monkeypatch):
"""When ui_preference is streamlit, JS sets prgn_ui=streamlit."""
injected = []
monkeypatch.setattr("streamlit.components.v1.html", lambda html, height=0: injected.append(html))
monkeypatch.setattr("streamlit.query_params", {}, raising=False)
from app.components.ui_switcher import sync_ui_cookie
sync_ui_cookie(profile_yaml, tier="paid")
assert any("prgn_ui=streamlit" in s for s in injected)
def test_sync_cookie_prgn_switch_param_overrides_yaml(profile_yaml, monkeypatch):
"""?prgn_switch=streamlit in query params resets ui_preference to streamlit."""
import yaml as _yaml
profile_yaml.write_text(_yaml.dump({"name": "T", "ui_preference": "vue"}))
injected = []
monkeypatch.setattr("streamlit.components.v1.html", lambda html, height=0: injected.append(html))
monkeypatch.setattr("streamlit.query_params", {"prgn_switch": "streamlit"}, raising=False)
with patch('app.components.ui_switcher._DEMO_MODE', False):
from app.components.ui_switcher import sync_ui_cookie
sync_ui_cookie(profile_yaml, tier="paid")
# user.yaml should now say streamlit
saved = _yaml.safe_load(profile_yaml.read_text())
assert saved["ui_preference"] == "streamlit"
# JS should set cookie to streamlit
assert any("prgn_ui=streamlit" in s for s in injected)
def test_sync_cookie_free_tier_keeps_vue(profile_yaml, monkeypatch):
"""Free-tier user with vue preference keeps vue (vue_ui_beta is free tier).
Previously this test verified a downgrade to streamlit. Vue SPA was opened
to free tier in issue #20 — the downgrade path no longer triggers.
"""
import yaml as _yaml
profile_yaml.write_text(_yaml.dump({"name": "T", "ui_preference": "vue"}))
injected = []
monkeypatch.setattr("streamlit.components.v1.html", lambda html, height=0: injected.append(html))
monkeypatch.setattr("streamlit.query_params", {}, raising=False)
with patch('app.components.ui_switcher._DEMO_MODE', False):
from app.components.ui_switcher import sync_ui_cookie
sync_ui_cookie(profile_yaml, tier="free")
saved = _yaml.safe_load(profile_yaml.read_text())
assert saved["ui_preference"] == "vue"
assert any("prgn_ui=vue" in s for s in injected)
def test_switch_ui_writes_yaml_and_calls_sync(profile_yaml, monkeypatch):
"""switch_ui(to='vue') writes user.yaml and calls sync."""
import yaml as _yaml
synced = []
monkeypatch.setattr("streamlit.components.v1.html", lambda html, height=0: synced.append(html))
monkeypatch.setattr("streamlit.query_params", {}, raising=False)
monkeypatch.setattr("streamlit.rerun", lambda: None)
with patch('app.components.ui_switcher._DEMO_MODE', False):
from app.components.ui_switcher import switch_ui
switch_ui(profile_yaml, to="vue", tier="paid")
saved = _yaml.safe_load(profile_yaml.read_text())
assert saved["ui_preference"] == "vue"
assert any("prgn_ui=vue" in s for s in synced)