bmc_hub/tests/test_dark_theme.py

37 lines
1.8 KiB
Python
Raw Normal View History

"""Contrast checks for the shared dark palette and its integration."""
from pathlib import Path
import re
import pytest
CSS=Path('static/css/theme-dark.css').read_text()
COLORS=dict(re.findall(r'(--theme-[\w-]+):\s*(#[\da-fA-F]{6})',CSS))
def luminance(color):
rgb=[int(color[index:index+2],16)/255 for index in (1,3,5)]
rgb=[channel/12.92 if channel<=.04045 else ((channel+.055)/1.055)**2.4 for channel in rgb]
return sum(channel*weight for channel,weight in zip(rgb,(.2126,.7152,.0722)))
@pytest.mark.parametrize('foreground,background',[
('text','surface'),('text','surface-soft'),('muted','surface'),('muted','surface-soft'),
('success-text','success-bg'),('warning-text','warning-bg'),('danger-text','danger-bg'),
('info-text','info-bg'),('primary-text','primary-bg'),('primary-text','surface'),
('success-text','surface'),('danger-text','surface'),
])
def test_dark_text_has_normal_text_contrast(foreground,background):
values=sorted([luminance(COLORS['--theme-'+foreground]),luminance(COLORS['--theme-'+background])])
assert (values[1]+.05)/(values[0]+.05)>=4.5
def test_shared_theme_loaded_after_module_css_and_before_first_paint():
html=Path('app/shared/frontend/base.html').read_text()
assert html.index('theme-dark.css')>html.index('{% block extra_css %}')
assert html.index("localStorage.getItem('theme')")<html.index('<style>')
for alias in ['--bg-surface','--bg-element','--text-color','--border-color','--bg-hover']:
assert alias+':' in CSS
def test_light_fallbacks_and_filled_badge_foregrounds_are_preserved():
html=Path('app/billing/frontend/supplier_invoices.html').read_text()
assert 'background-color: #ffc107; color: #000;' in html
assert 'var(--theme-surface, #fff)' in html
assert ':root' not in CSS
assert '.bg-warning' in CSS and 'color: #17212b !important' in CSS