# SPDX-License-Identifier: AGPL-3.0-or-later """Gestaltung ohne Datenbank: Eingaben prüfen, Kontrast, theme.css, Export und Import.""" from __future__ import annotations import json from dataclasses import replace from pathlib import Path import pytest from surveybox import appearance as app def test_parse_accepts_and_rejects() -> None: look, errors = app.parse({ "name": " Feuerwehr\nTitisee ", "mode": "dark", "primary": "#C00000", "imprint_url": "https://fw.example/impressum", "welcome": "Zeile 1\r\nZeile 2\x07", "css": ".x { color: red }", }) assert errors == [] assert look.name == "Feuerwehr Titisee" and look.mode == "dark" assert look.primary == "#c00000" and look.welcome == "Zeile 1\nZeile 2" bad, errors = app.parse({"name": "", "primary": "red", "privacy_url": "javascript:x", "imprint_url": "https://x\" onmouseover=1", "mode": "neon", "footer_text": "x" * 400}) assert errors == ["color", "name", "too_long", "url"] assert bad.primary == "" and bad.privacy_url == "" and bad.imprint_url == "" assert bad.mode == "auto" and len(bad.footer_text) == app.MAX_FOOTER def test_contrast_and_palette() -> None: assert round(app.contrast("#000000", "#ffffff"), 1) == 21.0 assert app.warnings(app.DEFAULT) == [] keys = [key for key, _ in app.warnings(app.Appearance(primary="#ffee00"))] assert "button" in keys and "link" in keys keys = [key for key, _ in app.warnings(app.Appearance(text="#cccccc"))] assert keys == ["text"] # Die dunkle Darstellung hellt eine dunkle Primärfarbe auf, bis sie lesbar ist colors = app.palette(app.Appearance(primary="#202060")) assert app.contrast(colors.dark["primary"], app.DARK["surface"]) >= 4.5 # Dunkler Hintergrund: Karten nicht weiß, Hinweise lesbar colors = app.palette(app.Appearance(background="#101820", text="#f0f0f0")) assert colors.light["surface"] != "#ffffff" assert app.contrast(colors.light["muted"], colors.light["surface"]) >= 4.5 def test_theme_css_by_mode() -> None: auto = app.theme_css(app.Appearance(primary="#c00000", css=".x{}"), None) assert "--sb-primary:#c00000" in auto and "@media (prefers-color-scheme: dark)" in auto assert auto.rstrip().endswith(".x{}") light = app.theme_css(app.Appearance(mode="light"), None) assert "color-scheme:light;" in light and "@media" not in light dark = app.theme_css(app.Appearance(mode="dark", font_format="woff2"), "/branding/font?v=1") assert "--sb-bg:#121417" in dark and 'format("woff2")' in dark and "SurveyboxCustom" in dark def test_save_current_and_font(tmp_path: Path) -> None: assert app.current(tmp_path) == app.DEFAULT saved = app.save(tmp_path, app.Appearance(name="Test", primary="#123456")) assert saved.name == "Test" and saved.version and saved.themed assert app.font_format(b"wOF2" + b"\0" * 20) == "woff2" assert app.font_format(b" None: look = replace(app.Appearance(name="X", footer_text="F"), version="v9") data = app.export(look, {"logo": b"\x89PNG", "font": b"wOF2"}) assert "version" not in data["appearance"] values, files = app.read_export(json.dumps(data).encode()) assert app.parse(values)[0].name == "X" and files == {"logo": b"\x89PNG", "font": b"wOF2"} for raw in (b"kein json", b"{}", json.dumps({"format": "x"}).encode(), json.dumps({**data, "files": {"logo": "%%%"}}).encode()): with pytest.raises(ValueError): app.read_export(raw)