Refactor UI assets and harden quality checks

This commit is contained in:
StasonJatham committed 2026-06-17 23:18:00 +02:00
1 parent 23ab466c52
commit 4ecc8eb874
62 files changed
+3314 -3206

No files matched your search

+82 -2
View File
@@ -1,9 +1,12 @@
import re
import types
from pathlib import Path
from fastapi.testclient import TestClient
from karldns import DdnsSettings, DdnsStore, make_app
from karldns.routers import assets
from karldns.web import scripts, styles
def _csrf(html: str) -> str:
@@ -226,6 +229,32 @@ def test_dashboard_link_can_delete_hostname(tmp_path: Path) -> None:
assert update.status_code == 404
def test_ui_uses_bearnie_inspired_structural_patterns(tmp_path: Path) -> None:
database_path = tmp_path / "ddns.sqlite3"
store = DdnsStore(database_path)
account = store.create_account("home.ddns.example.net", "home")
app = make_app(DdnsSettings(database_path=database_path, admin_password="admin"))
client = TestClient(app)
home = client.get("/")
dashboard = client.get(f"/m/{account['management_slug']}")
app_css = client.get("/app.css")
assert home.status_code == 200
assert dashboard.status_code == 200
assert app_css.status_code == 200
assert 'data-ui="hero-command"' in home.text
assert 'data-slot="card"' in dashboard.text
assert 'data-slot="card-header"' in dashboard.text
assert 'data-slot="card-content"' in dashboard.text
assert 'class="ui-badge ui-badge-secret"' in dashboard.text
assert 'class="ui-badge ui-badge-public"' in dashboard.text
assert 'class="empty-state"' in dashboard.text
assert "--ui-background:" in app_css.text
assert "--ui-card:" in app_css.text
assert "--ui-muted-foreground:" in app_css.text
def test_management_dashboard_password_locks_dashboard_only(tmp_path: Path) -> None:
database_path = tmp_path / "ddns.sqlite3"
store = DdnsStore(database_path)
@@ -595,9 +624,9 @@ def test_application_pages_use_stricter_script_csp(tmp_path: Path) -> None:
assert '<script src="/theme.js?v=2"></script>' in response.text
assert '<script src="/clipboard.js?v=3" defer></script>' in response.text
assert '<script src="/visitor-ip.js?v=1" defer></script>' in response.text
assert '<script src="/admin-tables.js?v=2" defer></script>' not in response.text
assert '<script src="/admin-tables.js?v=3" defer></script>' not in response.text
assert '<link href="/bootstrap-subset.css?v=3" rel="stylesheet">' in response.text
assert '<link href="/app.css?v=13" rel="stylesheet">' in response.text
assert '<link href="/app.css?v=14" rel="stylesheet">' in response.text
assert '<link href="/admin.css" rel="stylesheet">' not in response.text
assert "cdn.jsdelivr.net/npm/bootstrap" not in response.text
assert "<style>" not in response.text
@@ -629,6 +658,57 @@ def test_application_pages_use_stricter_script_csp(tmp_path: Path) -> None:
assert admin_css.headers["cache-control"] == "public, max-age=31536000, immutable"
def test_css_and_javascript_assets_are_served_from_files(tmp_path: Path) -> None:
app = make_app(DdnsSettings(database_path=tmp_path / "ddns.sqlite3", admin_password="admin"))
client = TestClient(app)
expected_assets = {
"/app.css": "app.css",
"/admin.css": "admin.css",
"/theme.js": "theme.js",
"/clipboard.js": "clipboard.js",
"/admin-tables.js": "admin-tables.js",
"/visitor-ip.js": "visitor-ip.js",
}
for route, filename in expected_assets.items():
asset_file = assets.ASSET_DIR / filename
response = client.get(route)
assert asset_file.exists()
assert response.status_code == 200
assert response.text == asset_file.read_text(encoding="utf-8")
def test_clipboard_asset_has_fallback_and_visible_failure_state(tmp_path: Path) -> None:
app = make_app(DdnsSettings(database_path=tmp_path / "ddns.sqlite3", admin_password="admin"))
client = TestClient(app)
response = client.get("/clipboard.js")
assert response.status_code == 200
assert 'document.execCommand("copy")' in response.text
assert "Copy failed" in response.text
def test_visitor_ip_asset_fetches_ipv6_only_when_ipv6_placeholder_is_present(tmp_path: Path) -> None:
app = make_app(DdnsSettings(database_path=tmp_path / "ddns.sqlite3", admin_password="admin"))
client = TestClient(app)
response = client.get("/visitor-ip.js")
assert response.status_code == 200
assert "api6.ipify.org" in response.text
assert 'needsIpv6() || document.querySelector(".terminal-card.manual-code")' not in response.text
def test_web_style_and_script_modules_do_not_embed_assets() -> None:
assert [name for name, value in vars(styles).items() if name.endswith("_CSS") and isinstance(value, str)] == []
assert [name for name, value in vars(scripts).items() if name.endswith("_JS") and isinstance(value, str)] == []
assert isinstance(styles, types.ModuleType)
assert isinstance(scripts, types.ModuleType)
def test_footer_links_and_privacy_page_are_available(tmp_path: Path) -> None:
app = make_app(
DdnsSettings(