feat: Add light/dark mode support with backend persistence

This commit is contained in:
Yunxiao Xu
2026-02-17 00:32:15 -08:00
parent 3881ca6fd8
commit de25dc8a4d
17 changed files with 253 additions and 18 deletions

View File

@@ -0,0 +1,73 @@
import pytest
from fastapi.testclient import TestClient
from unittest.mock import patch
from ea_chatbot.api.main import app
from ea_chatbot.history.models import User
from ea_chatbot.api.utils import create_access_token
client = TestClient(app)
@pytest.fixture
def test_user():
return User(
id="user-123",
username="test@example.com",
display_name="Test User",
theme_preference="light"
)
@pytest.fixture
def auth_token():
return create_access_token(data={"sub": "user-123"})
def test_get_me_includes_theme(test_user, auth_token):
"""Test that /auth/me returns the theme_preference."""
with patch("ea_chatbot.api.dependencies.history_manager") as mock_hm:
mock_hm.get_user_by_id.return_value = test_user
response = client.get(
"/api/v1/auth/me",
cookies={"access_token": auth_token}
)
assert response.status_code == 200
data = response.json()
assert "theme_preference" in data
assert data["theme_preference"] == "light"
def test_update_theme_success(test_user, auth_token):
"""Test successful theme update via PATCH /auth/theme."""
updated_user = User(
id="user-123",
username="test@example.com",
display_name="Test User",
theme_preference="dark"
)
with patch("ea_chatbot.api.dependencies.history_manager") as mock_hm_dep, \
patch("ea_chatbot.api.routers.auth.history_manager") as mock_hm_router:
# Dependency injection uses the one from dependencies
mock_hm_dep.get_user_by_id.return_value = test_user
# The router uses its own reference to history_manager
mock_hm_router.update_user_theme.return_value = updated_user
response = client.patch(
"/api/v1/auth/theme",
json={"theme": "dark"},
cookies={"access_token": auth_token}
)
assert response.status_code == 200
data = response.json()
assert data["theme_preference"] == "dark"
mock_hm_router.update_user_theme.assert_called_once_with("user-123", "dark")
def test_update_theme_unauthorized():
"""Test that theme update requires authentication."""
response = client.patch(
"/api/v1/auth/theme",
json={"theme": "dark"}
)
assert response.status_code == 401