feat: Integrate HTML templates with FastAPI

- Created template_helpers.py for centralized template rendering
- Added ux_features.css for enhanced UX styling
- Implemented JavaScript modules for:
  - Keyboard shortcuts (Ctrl+K, Ctrl+R navigation)
  - User preferences persistence
  - Undo/redo functionality (Ctrl+Z/Ctrl+Y)
  - Mobile responsive features
  - Touch gesture support
  - Accessibility features (ARIA, focus management)
  - Screen reader support
  - Color contrast compliance (WCAG)
  - Multi-screen support
- Updated page_controller.py and error_controller.py to use template helpers
- Created comprehensive template integration tests
- All templates verified: index.html, login.html, setup.html, queue.html, error.html
- Maintained responsive layout and theme switching
- Updated instructions.md (removed completed task)
- Updated infrastructure.md with template integration details
This commit is contained in:
2025-10-17 12:01:22 +02:00
parent 043d8a2877
commit 99e24a2fc3
20 changed files with 1497 additions and 38 deletions

View File

@@ -0,0 +1,86 @@
"""
Tests for template helper utilities.
This module tests the template helper functions.
"""
from unittest.mock import Mock
import pytest
from src.server.utils.template_helpers import (
get_base_context,
list_available_templates,
validate_template_exists,
)
class TestTemplateHelpers:
"""Test template helper utilities."""
def test_get_base_context(self):
"""Test that base context is created correctly."""
request = Mock()
context = get_base_context(request, "Test Title")
assert "request" in context
assert context["request"] == request
assert context["title"] == "Test Title"
assert context["app_name"] == "Aniworld Download Manager"
assert context["version"] == "1.0.0"
def test_get_base_context_default_title(self):
"""Test that default title is used."""
request = Mock()
context = get_base_context(request)
assert context["title"] == "Aniworld"
def test_validate_template_exists_true(self):
"""Test template validation for existing template."""
# index.html should exist
exists = validate_template_exists("index.html")
assert exists is True
def test_validate_template_exists_false(self):
"""Test template validation for non-existing template."""
exists = validate_template_exists("nonexistent.html")
assert exists is False
def test_list_available_templates(self):
"""Test listing available templates."""
templates = list_available_templates()
# Should be a list
assert isinstance(templates, list)
# Should contain at least the main templates
expected_templates = [
"index.html",
"login.html",
"setup.html",
"queue.html",
"error.html"
]
for expected in expected_templates:
assert expected in templates, (
f"{expected} not found in templates list"
)
def test_list_available_templates_only_html(self):
"""Test that only HTML files are listed."""
templates = list_available_templates()
for template in templates:
assert template.endswith(".html")
@pytest.mark.parametrize("template_name", [
"index.html",
"login.html",
"setup.html",
"queue.html",
"error.html"
])
def test_all_required_templates_exist(self, template_name):
"""Test that all required templates exist."""
assert validate_template_exists(template_name), \
f"Required template {template_name} does not exist"

View File

@@ -0,0 +1,153 @@
"""
Tests for template integration and rendering.
This module tests that all HTML templates are properly integrated with FastAPI
and can be rendered correctly.
"""
import pytest
from fastapi.testclient import TestClient
from src.server.fastapi_app import app
class TestTemplateIntegration:
"""Test template integration with FastAPI."""
@pytest.fixture
def client(self):
"""Create test client."""
return TestClient(app)
def test_index_template_renders(self, client):
"""Test that index.html renders successfully."""
response = client.get("/")
assert response.status_code == 200
assert response.headers["content-type"].startswith("text/html")
assert b"AniWorld Manager" in response.content
assert b"/static/css/styles.css" in response.content
def test_login_template_renders(self, client):
"""Test that login.html renders successfully."""
response = client.get("/login")
assert response.status_code == 200
assert response.headers["content-type"].startswith("text/html")
assert b"Login" in response.content
assert b"/static/css/styles.css" in response.content
def test_setup_template_renders(self, client):
"""Test that setup.html renders successfully."""
response = client.get("/setup")
assert response.status_code == 200
assert response.headers["content-type"].startswith("text/html")
assert b"Setup" in response.content
assert b"/static/css/styles.css" in response.content
def test_queue_template_renders(self, client):
"""Test that queue.html renders successfully."""
response = client.get("/queue")
assert response.status_code == 200
assert response.headers["content-type"].startswith("text/html")
assert b"Download Queue" in response.content
assert b"/static/css/styles.css" in response.content
def test_error_template_404(self, client):
"""Test that 404 error page renders correctly."""
response = client.get("/nonexistent-page")
assert response.status_code == 404
assert response.headers["content-type"].startswith("text/html")
assert b"Error 404" in response.content or b"404" in response.content
def test_static_css_accessible(self, client):
"""Test that static CSS files are accessible."""
response = client.get("/static/css/styles.css")
assert response.status_code == 200
assert "text/css" in response.headers.get("content-type", "")
def test_static_js_accessible(self, client):
"""Test that static JavaScript files are accessible."""
response = client.get("/static/js/app.js")
assert response.status_code == 200
def test_templates_include_theme_switching(self, client):
"""Test that templates include theme switching functionality."""
response = client.get("/")
assert response.status_code == 200
# Check for theme toggle button
assert b"theme-toggle" in response.content
# Check for data-theme attribute
assert b'data-theme="light"' in response.content
def test_templates_include_responsive_meta(self, client):
"""Test that templates include responsive viewport meta tag."""
response = client.get("/")
assert response.status_code == 200
assert b'name="viewport"' in response.content
assert b"width=device-width" in response.content
def test_templates_include_font_awesome(self, client):
"""Test that templates include Font Awesome icons."""
response = client.get("/")
assert response.status_code == 200
assert b"font-awesome" in response.content.lower()
def test_all_templates_have_correct_structure(self, client):
"""Test that all templates have correct HTML structure."""
pages = ["/", "/login", "/setup", "/queue"]
for page in pages:
response = client.get(page)
assert response.status_code == 200
content = response.content
# Check for essential HTML elements
assert b"<!DOCTYPE html>" in content
assert b"<html" in content
assert b"<head>" in content
assert b"<body>" in content
assert b"</html>" in content
def test_templates_load_required_javascript(self, client):
"""Test that index template loads all required JavaScript files."""
response = client.get("/")
assert response.status_code == 200
content = response.content
# Check for main app.js
assert b"/static/js/app.js" in content
# Check for localization.js
assert b"/static/js/localization.js" in content
def test_templates_load_ux_features_css(self, client):
"""Test that templates load UX features CSS."""
response = client.get("/")
assert response.status_code == 200
assert b"/static/css/ux_features.css" in response.content
def test_queue_template_has_websocket_script(self, client):
"""Test that queue template includes WebSocket support."""
response = client.get("/queue")
assert response.status_code == 200
# Check for socket.io or WebSocket implementation
assert (
b"socket.io" in response.content or
b"WebSocket" in response.content
)
def test_index_includes_search_functionality(self, client):
"""Test that index page includes search functionality."""
response = client.get("/")
assert response.status_code == 200
content = response.content
assert b"search-input" in content
assert b"search-btn" in content
def test_templates_accessibility_features(self, client):
"""Test that templates include accessibility features."""
response = client.get("/")
assert response.status_code == 200
content = response.content
# Check for ARIA labels or roles
assert b"aria-" in content or b"role=" in content