bcc-ui-theme / tests /test_theme.py
Annie Voigt
Claude Opus 5
feat: bcc-ui-theme 0.1.0 — shared Gradio theme + stylesheet for the fleet
89e8390
Raw History Blame Contribute Delete
5.26 kB
"""The theme object, and the cross-version compatibility it promises.
The binding constraint is gradio 5.50: its ``Base.set()`` accepts 262 keywords,
a strict subset of 6.18's 267, and the two ``__init__`` signatures are
identical. So a theme that constructs on 5.50 constructs on 6.18, and
``test_uses_only_portable_set_keywords`` is what keeps that true — it checks the
keywords against the *installed* gradio, so running the suite in each venv is
the whole compatibility proof.
"""
import inspect
import gradio as gr
import pytest
from bcc_ui_theme import CSS, FOOTER_CSS, BCCTheme, __version__, theme
def test_theme_is_a_base_subclass():
assert issubclass(BCCTheme, gr.themes.Base)
assert isinstance(theme(), gr.themes.Base)
def test_theme_returns_a_fresh_instance_each_call():
"""gradio mutates a theme while rendering; two apps must not share one."""
assert theme() is not theme()
def test_theme_constructs_and_serialises():
"""to_dict() is what gradio does with a theme internally — if a token were
malformed, this is where it surfaces rather than at render time."""
payload = theme().to_dict()
assert isinstance(payload, dict)
assert payload
def test_theme_applies_our_palette_not_the_default():
from bcc_ui_theme import DARK, LIGHT
t = theme()
assert t.body_text_color == LIGHT["text"]
assert t.body_text_color_dark == DARK["text"]
assert t.button_primary_background_fill == LIGHT["accent"]
assert t.button_primary_text_color == LIGHT["on_accent"]
def test_uses_only_portable_set_keywords():
"""Every keyword the theme passes to set() must exist in the installed
gradio. Run under both 5.50 and 6.18, this IS the compatibility guarantee."""
source = inspect.getsource(BCCTheme.__init__)
accepted = set(inspect.signature(gr.themes.Base.set).parameters)
passed = {
line.split("=", 1)[0].strip()
for line in source.splitlines()
if "=" in line and line.strip().endswith(",") and not line.strip().startswith("#")
}
# Keep only names that look like set() keywords, then require each is real.
candidates = {p for p in passed if p.replace("_", "").isalnum() and not p.startswith("_")}
unknown = {
p
for p in candidates
if p not in accepted
and p
not in {
"primary_hue",
"secondary_hue",
"neutral_hue",
"spacing_size",
"radius_size",
"text_size",
"font",
"font_mono",
}
}
assert not unknown, f"not accepted by gradio {gr.__version__}'s Base.set(): {sorted(unknown)}"
def test_no_web_font_is_requested():
"""A Space must render an answer without an outbound font request."""
t = theme()
rendered = str(t.to_dict())
assert "fonts.googleapis.com" not in rendered
assert "googlefont" not in rendered.lower()
def test_no_brand_assets():
"""OHSU's marks are not ours to use, and the palette must not imply one."""
from bcc_ui_theme import _palette
blob = (CSS + FOOTER_CSS + inspect.getsource(_palette)).lower()
for token in ("ohsu", "brenden", "colson", "knight", "logo", ".png", ".svg", "base64"):
assert token not in blob, f"brand/asset reference in the package: {token}"
class TestCSS:
def test_is_a_non_empty_string(self):
assert isinstance(CSS, str) and CSS.strip()
def test_is_scoped_to_the_gradio_container(self):
"""An embedded app must not style its host page. Every selector block
is scoped; the only unscoped rules are the :root-style variable
declarations, which are themselves scoped to .gradio-container/.dark."""
selectors = [
line.strip()
for line in CSS.splitlines()
if line.strip().endswith("{") and not line.strip().startswith(("/*", "*", "@"))
]
assert selectors
for sel in selectors:
assert ".gradio-container" in sel or ".dark" in sel, f"unscoped selector: {sel}"
def test_braces_balance(self):
assert CSS.count("{") == CSS.count("}")
def test_carries_the_provenance_classes(self):
"""docs/design/provenance-panel.md depends on these existing."""
for cls in (
".bcc-provenance",
".bcc-provenance-intro",
".bcc-panel-note",
".bcc-caveat",
".bcc-caveat-action",
):
assert cls in CSS, f"missing provenance class: {cls}"
def test_does_not_hide_the_footer(self):
"""Hiding attribution is opt-in, via FOOTER_CSS — not inherited."""
assert "footer" not in CSS
assert "footer" in FOOTER_CSS
def test_does_not_force_a_colour_scheme(self):
"""DecoupleRpy's injected JS overrides the user's own theme choice.
Nothing here may do the equivalent."""
assert "__theme" not in CSS
assert "!important" not in CSS
def test_version_is_declared():
assert __version__.count(".") == 2
@pytest.mark.parametrize("attr", ["theme", "CSS", "FOOTER_CSS", "LIGHT", "DARK", "CONTRACT"])
def test_public_api(attr):
import bcc_ui_theme
assert hasattr(bcc_ui_theme, attr)
assert attr in bcc_ui_theme.__all__